[net.micro.mac] How to draw a Region around a random Line?

harrow@exodus.dec.com (Jeff Harrow, NCSE LKG1-3/F16 DTN=226-7445) (08/18/86)

Having received  such  good  advice  from  my last question about 
rubber-band lines, I now have another:

Given that I  have a random line going from StartPt to EndPt (any 
orientation), what is a  good  way to create a rectangular Region 
around it, perhaps 2 pixels  out from the line (ie a Region which 
encloses the line but is slightly larger than the line itself, to 
make for  easier  mouse capture).  (My problem is not in actually 
creating the Region,  but  making it come out in the shape that I 
want around the line...)

My initial simplistic attempt at this (drawing a rectangle around 
it expanded by the magic "2") works fine for  vertical lines, but 
the region is less and less rectangular, and less and  less  over 
the  ends of the line (ok in the center) as the  line  approaches 
horizontal.

(Something  tells  me  that  I  wish  I'd  paid more attention to 
Trig...)

Anyway, any ideas on a generalized algorithm?

(This  doesn't   belong  here,  but  boy,  is  Lightspeed  PASCAL 
**GREAT**!)

Jeff

sdh@joevax.UUCP (The Doctor) (08/19/86)

> Given that I  have a random line going from StartPt to EndPt (any 
> orientation), what is a  good  way to create a rectangular Region 
> around it, perhaps 2 pixels  out from the line (ie a Region which 
> encloses the line but is slightly larger than the line itself, to 
> make for  easier  mouse capture).  (My problem is not in actually 
> creating the Region,  but  making it come out in the shape that I 
> want around the line...)
>

I haven't actually tried this, but you might want to try this routine

/* this is for a c system */

surround_line(x1, y1, x2, y2, region)
int x1, y1, x2, y3;
RgnHandle region;

/* this should do it.  Note that this routine doesn't define the region
 * itself.  You must do that with a call to NewRgn() before trying to
 * manipulate the contents.
 */

{
	Rect temprect;
/* You may wish to set the rectangle differently than I do.
 * This case allows for what may be "improper" rectangles, that is , those
 * whose bottom is above their top, and left before there right.
 * it may be neccessary to sort the points accordingly, ie, if x1 is greater
 * than x2, swap them, and if y1 is greater than y2, swap them. This will
 * generate an equivalent rectangle, but not the specific one.
 * If a slash is a diagonal line,   \  and  /  could be fit in the same
 * rectangle, but the routine generates spceific ones, not generalizations.
 */
	SetRect(&temprect, x1, y1, x2, y2);
	InsetRect(&temprect, -2, -2); /* don't like the magic numbers */
	
	RectRgn(region, &temprect);
}
 

Hope this helps.

Steve Hawley

seth@hera.cs.ucla.edu (Seth Goldman) (08/20/86)

From reading IM, I would expect the proper way to create a
rectangular region given 2 endpoints would be to use
Pt2Rect (I-175) followed by a call to RectRgn (I-183).
You probably want to call InsetRect inbetween with an
offset of -2 in both directions.

                                                Seth Goldman

ARPA:   seth@LOCUS.UCLA.EDU
UUCP:   ...!{ihnp4,cepu,trwspp,sdcrdcf,ucbvax}!ucla-cs!seth
USMail: A.I. Lab, 3531 Boelter Hall, UCLA, Los Angeles, CA  90024
MaBell: (213) 825-5199