Board index » delphi » Line
Alexander Adam
Delphi Developer |
Alexander Adam
Delphi Developer |
Line2003-10-04 01:28:56 AM delphi260 hi, I have a line that is created by X1, Y1, X2 and Y2. I want now this line as a polygon, so that I get a polygon that behaves like the line would have etc. a thick of 5 pixels. I can easily make a polyline with the Start and End Point, but how to make a polygon like I want ? thanks a lot, alex |
Ananth B.
Delphi Developer |
2003-10-04 01:49:13 AM
Re:Line
Hi Alexander,
Given a general formula ax+by+c=0, if b<>0, the point-slope form is y=-ax/b-c/b, so the vector is proportional to (-a/b,-1) which, multiplying by -b, is proportional to (a,b). Given a line of the form ax+by+c, b=0, we have the vector (1,0) which is proportional to the vector (a,0)=(a,b). So, given ax+by+c=0, ax+by-c=0, or ax+by=c style equations the vector is proportional to (a,b) regardless of a and b. Simply offset each of the end points of your original line by a sufficient quantity of the normal. You should now have a set of parallel lines ... connect their edges to get a polygon. Regards, Ananth B. "Alexander Adam" <XXXX@XXXXX.COM>writes Quotehi, |
Francesco Savastano
Delphi Developer |
2003-10-04 06:15:27 AM
Re:Line
I'm glad that you got the sense of his question..i must be very tired cause
i couldnt understand it at all QuoteRegards, |
Alexander Adam
Delphi Developer |
2003-10-04 08:14:12 AM
Re:Line
hi,
QuoteI'm glad that you got the sense of his question..i must be very tired ask that, please don't mind me ;-) Alex |
Alexander Adam
Delphi Developer |
2003-10-04 09:55:47 AM
Re:Line
hi,
Offseting doesn't help, e.g. when I have a horizontal or vertical line... What to do ? alex |
Ananth B.
Delphi Developer |
2003-10-04 10:17:44 AM
Re:Line
Hi Alexander,
Offset the endpoints of the line along the normal's direction. You will get two parallel lines. Lets take the vertical line case ... with line equation x - 1 = 0; a = 1 and b = 0, so the normals here would be (1, 0) or (-1, 0). Lets choose (1, 0) newX1 = oldX1 + 1; newY1 = oldY1 + 0; the x coordinate just got shifted to the left now. Do the same for X2, Y2 ... we have 2 parallel lines (new line lies to the left of the old line). now connecting oldX1 to newX1 etc etc ... we get a rectangle of a certain thickness. The thickness can be adjusted by scaling the normal (ex: for half the width you scale it by 0.5 so the new line gets offseted only by 0.5 units) Regards, Ananth B. "Alexander Adam" <XXXX@XXXXX.COM>writes Quotehi, |
Francesco Savastano
Delphi Developer |
2003-10-04 05:48:41 PM
Re:Line
hi Alex,
anyway trying to explain better what you want to ask is convenient for 2 good reasons: 1) you will get more and much useful answers 2) if you know how to explain well your problem then you are very near to solve it. Dont mind me too, i just try to give you some advice for the future, ok? |
Alexander Adam
Delphi Developer |
2003-10-04 07:16:45 PM
Re:Line
hi Ananth,
QuoteOffset the endpoints of the line along the normal's direction. You How to get the line equation ? I mean, if I always use x - 1 = 0; I always get a = 1 and b = 0, the same for x2 ... I thought that the 1 here must be in any relation to X1,Y1,X2 or Y2 ? thanks a lot! Alex |
Ananth B.
Delphi Developer |
2003-10-04 07:40:09 PM
Re:Line
Ok, Alexander ...
Here goes again. Given 2 pts (X1, Y1) and (X2, Y2), the line equation is (Y - Y1) = ((Y2 - Y1)/(X2 - X1))(X - X1) (but X and Y are just that ... X and Y. Dont substiture values for them.) simplify this to get an equation of the form ax + by + c = 0 Now, any multiples of a and b are your normal. (X1 + scale * a, Y1 + scale * b) and (X2 + scale * a, Y2 + scale * b) are the new line coordinates. Adjust scale to get an appropriate thickness for your polygon. Regards, Ananth B. "Alexander Adam" <XXXX@XXXXX.COM>writes Quotehi Ananth, |
Alexander Adam
Delphi Developer |
2003-10-05 10:54:57 PM
Re:Line
Thanks Ananth,
I tried to get the correct equation and then to get a and b, but it seems I am too stupid for that kind of stuff... What am I missing ? thanks a lot, Alexander "Ananth B." <XXXX@XXXXX.COM>schrieb im Newsbeitrag QuoteOk, Alexander ... |
Ananth B.
Delphi Developer |
2003-10-06 12:27:18 AM
Re:Line
Hi Alexander,
Ok ... heres some Delphi code const DISTANCE_SCALE = 2.0; procedure ParallelLine(aX1, aY1, aX2, aY2: integer; var aX3, aY3, aX4, aY4: integer); var slope: single; begin slope := (aY2 - aY1) / (aX2 - aX1); aX3 := Round(aX1 + DISTANCE_SCALE * Slope); aY3 := Round(aY1 + DISTANCE_SCALE * -1); aX4 := Round(aX2 + DISTANCE_SCALE * Slope); aY4 := Round(aY2 + DISTANCE_SCALE * -1); end; procedure TForm1.Timer1Timer(Sender: TObject); var nx1, ny1, nx2, ny2: integer; begin Canvas.MoveTo(10, 10); Canvas.LineTo(340, 100); ParallelLine(10, 10, 340, 100, nx1, ny1, nx2, ny2); Canvas.MoveTo(nx1, ny1); Canvas.LineTo(nx2, ny2); end; Regards, Ananth B. "Alexander Adam" <XXXX@XXXXX.COM>writes QuoteThanks Ananth, |