ArunKishESSDI
asked on
Create Offset or Outlines for Polygon.
Hello Experts,
I'm working on an application that uses VECAD activeX for creating shapes using lines / splines etc. I do have a problem in calculating the match for creating offset lines ( just like autocad offset command). The offset command is not directly available in Vecad. So... say for eg, I have drawn a shape using some lines (values of Left,Right,Top,Bottom will not be equal - in other words the lines will not be straight). I need to create a line parallel to the line that I clicked. But outside the shape. Please advice me on this. I have attached a Doc file that gives so more additional details.
Dear-Experts.doc
I'm working on an application that uses VECAD activeX for creating shapes using lines / splines etc. I do have a problem in calculating the match for creating offset lines ( just like autocad offset command). The offset command is not directly available in Vecad. So... say for eg, I have drawn a shape using some lines (values of Left,Right,Top,Bottom will not be equal - in other words the lines will not be straight). I need to create a line parallel to the line that I clicked. But outside the shape. Please advice me on this. I have attached a Doc file that gives so more additional details.
Dear-Experts.doc
To guarantee that your new line is outside the polygon, the offset D1 can be arbitrary, but has some constraints. One approach is to consider the polygon's sides as an ordered sequence of vectors, such that the interior points are always at the left (or right, depending on the initial direction) side of each vector.
A method is to consider an arbitrary offset D=(dx,dy) and apply it to a line, say, to both ends of the line, and test if both points are outside the polygon.
To test if a point is inside a polygon, there is a number of methods, for example the ones described at
http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/
which has the theoretical explanation and code samples.
Jose
A method is to consider an arbitrary offset D=(dx,dy) and apply it to a line, say, to both ends of the line, and test if both points are outside the polygon.
To test if a point is inside a polygon, there is a number of methods, for example the ones described at
http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/
which has the theoretical explanation and code samples.
Jose
ASKER
Dear JoseParrot
I tested with what you said, additionally instead of testing two points of the line I tested with three points including the mid point, but the problem is in some shapes ( polygon ) some lines have two possiblities like Top, Right ... etc. But for one line Top will be the right position to be placed and in some cases Right will be the position that has to be placed. What to do in such sitiuations??? I hope u can figure out what I'm telling. Please let me know experts.
I tested with what you said, additionally instead of testing two points of the line I tested with three points including the mid point, but the problem is in some shapes ( polygon ) some lines have two possiblities like Top, Right ... etc. But for one line Top will be the right position to be placed and in some cases Right will be the position that has to be placed. What to do in such sitiuations??? I hope u can figure out what I'm telling. Please let me know experts.
Hello ArunKishESSDI,
You don't need three points to define a line, just two.
Let's assume you are working only with convex polygons. See Fig 1.
If the polygon is convex (all its internal angles are smaller than 180º) then any displaced line that passes by a point outside it is out of the polygon. In this case it is very easy to determine if the displaced line C'D' is outside the polygon: just calculate the point M in the middle of the polygon, by making
xM = (xA+xB+xC+...+xN)/N and xY=(yA+yB+...+yN)/N
and mesure which distance is smaller: MC or MC'. If MC < MC' then the point C' is outside the polygon, then the line C'D' is outside too.
Now you can create offsets dx and dy, such that one of the points is outside the polygon.
Which criteria do you want? If you want each displaced line to be distant by distance = d from the original line, say A'B' to be distant by d from AB (Fig. 2), then you need to make
d*d = dx*dx + dy*dy
and determine the direction of AB
tg = tangent(a) = (yA-yB) / (xA-xB)
then determine the relationship between dx and dy
dx = dy * tg
As you have determined the distsnce d, then
dx = squareroot( d*d / (1+tg*tg)
dy = dx / tg
Jose
You don't need three points to define a line, just two.
Let's assume you are working only with convex polygons. See Fig 1.
If the polygon is convex (all its internal angles are smaller than 180º) then any displaced line that passes by a point outside it is out of the polygon. In this case it is very easy to determine if the displaced line C'D' is outside the polygon: just calculate the point M in the middle of the polygon, by making
xM = (xA+xB+xC+...+xN)/N and xY=(yA+yB+...+yN)/N
and mesure which distance is smaller: MC or MC'. If MC < MC' then the point C' is outside the polygon, then the line C'D' is outside too.
Now you can create offsets dx and dy, such that one of the points is outside the polygon.
Which criteria do you want? If you want each displaced line to be distant by distance = d from the original line, say A'B' to be distant by d from AB (Fig. 2), then you need to make
d*d = dx*dx + dy*dy
and determine the direction of AB
tg = tangent(a) = (yA-yB) / (xA-xB)
then determine the relationship between dx and dy
dx = dy * tg
As you have determined the distsnce d, then
dx = squareroot( d*d / (1+tg*tg)
dy = dx / tg
Jose
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Sounds clear your objective.
Let us concentrate first on Line 1.
L1 = (P1,P2), being P1=(36,73) and P2=(56,66).
To make any arbitrary line parallel to Line, it is enough to create an arbitrary offset
D = (dx,dy) and add to the line's coordinates. As an example, if (dx=15 and dy = -10) we can create the Line 11 parallel to Line 1. We simple add these values to the line coordinates:
D1 = (15,-10)
P11 = P1 + D1 --> P11 = (36+15, 73-10) --> P11 = (51,63)
P12 = P2 + D1 --> P12 = (56+15, 66-10) --> P12 = (71,56)
offset1.GIF