Reputation: 5444
I have defined a Polygon
. I want to intersect and trim a list of other Lines
with the Edges
of this polygon (white rectangle here), so that endpoints of lines are limited to the inner part of the polygon.
currently, I'm intersecting each cyan line with the edges of the polygon which gives me the intersection points. But the problems is I don't know how to trim them. I know that I need to change the X1
, Y1
, X2
and Y2
of each intersecting line (cyan line) to the intersection point. But I don't know how do do it.
Let me explain it this way. A cyan line intersects one of the edges of the polygon, now I need to move the endpoint of the cyan line to the intersection point to simulate a trim right? Which endpoint I need to move? I'm a bit lost here.
public class Polygon
{
public List<Line>() Edges;
}
public class Line
{
public double X1;
public double X2;
public double Y1;
public double Y2;
}
var listOfIntersectingLines = new List<Line>() {L1, L2, ... };
var ListOfLinesLimitedToPolygon = ?
Upvotes: 0
Views: 693
Reputation: 1133
When you intersect two sections, that dont have an intersection point, your function should return null. For example: in your first picture, the 4th line from the bottom does not intersect the left or the top edge of the rectangle, it only intersects the bottom and the right edges. If you pick a cyan line, and intersect it with all edges of the polygon, you will always get either 2 or 0 intersection points. If you got 2, then those 2 points are the endpoints of your trimmed cyan line. If you got 0, that means the cyan line is outside of the polygon.
Possible problems: 1. An edge overlaps with a cyan line. You must decide whether you want to keep this line or not, and adjust you intersection function accordingly. 2. An edge goes through a corner. The easiest way to handle it is to return an intersect point if the line goes to the "first" endpoint of your edge, but return null if it crosses the 2nd endpoint. As your polygon is probably defined from point to point, it ensure that at every corner, there is only 1 edge that can be intersected.
Upvotes: 1