/// <summary> /// Return winding order /// </summary> /// <param name="Polygon"></param> /// <returns></returns> public static WindingOrder GetWindingOrder(FigPolyline Polygon) { double Area = 0; for (int j = 0; j < Polygon.Points.Count - 1; j++) { PointF Start = Polygon.Points[j]; PointF End = Polygon.Points[j + 1]; Area = Area + (Start.X * End.Y) - (End.X * Start.Y); } if (!IsClosed(Polygon)) { PointF Start = Polygon.Points[Polygon.Points.Count - 1]; PointF End = Polygon.Points[0]; Area = Area + (Start.X * End.Y) - (End.X * Start.Y); } if (Area < 0) { return(WindingOrder.Clockwise); } else { return(WindingOrder.AntiClockwise); } }
public static bool ClipPolyToLine(GraphicsSurface surface, Line ClipLine) { bool result = false; FigPolyline new_fig = new FigPolyline(); if (surface.selected.Count == 0 || surface.selected.Count > 1) { return(result); // nothing selected } if (surface.selected[0] is FigPolyline) { PointF s, p, i; int j; FigPolyline inPoly = surface.selected[0] as FigPolyline; s = inPoly.Points[inPoly.Points.Count - 1]; for (j = 0; j < inPoly.Points.Count; j++) { p = inPoly.Points[j]; if (Inside(p, ClipLine)) // {Cases 1 and 4} { if (Inside(s, ClipLine)) { new_fig.Points.Add(p); // {Case 1} } else { Intersect(s, p, ClipLine, out i); new_fig.Points.Add(i); new_fig.Points.Add(p); } } else // {Cases 2 and 3} { if (Inside(s, ClipLine)) // {Cases 2} { Intersect(s, p, ClipLine, out i); new_fig.Points.Add(i); } // {No action for case 3} } s = p; // {Advance to next pair of vertices} } // replace selected surface.Drawing.RemoveFigures(surface.selected); surface.Drawing.AddFigure(new_fig); // set new selection surface.selected.Clear(); //surface.AddToSelected(new_fig); surface.selected.Add(new_fig); return(result); } else { return(result); } }
public static bool LineIntersectsPolyLines(FigPolyline Line1, List <FigPolyline> Lines) { foreach (FigPolyline line in Lines) { if (LineIntersectsPolyLine(Line1, line)) { return(true); } } return(false); }
/// <summary> /// Test if a polygon is closed (last point = first point) /// </summary> /// <param name="Polygon"></param> /// <returns></returns> public static bool IsClosed(FigPolyline Polygon) { if (Polygon.Points[0] == Polygon.Points[Polygon.Points.Count - 1]) { return(true); } else { return(false); } }
public static bool LineIntersectsPolyLine(FigPolyline Line1, FigPolyline Line2) { PointF iPt; for (int j = 0; j < Line1.Points.Count - 1; j++) { for (int k = 0; k < Line1.Points.Count - 1; k++) { if (Intersect(Line1.Points[j], Line1.Points[j + 1], Line2.Points[k], Line2.Points[k + 1], out iPt)) { return(true); } } } return(false); }
/// <summary> /// Calculate the area of a polygon /// </summary> /// <param name="Polygon"></param> /// <returns></returns> public static double CalcPolygonArea(FigPolyline Polygon) { double Area = 0; for (int j = 0; j < Polygon.Points.Count - 1; j++) { PointF Start = Polygon.Points[j]; PointF End = Polygon.Points[j + 1]; Area = Area + (Start.X * End.Y) - (End.X * Start.Y); } if (!IsClosed(Polygon)) { PointF Start = Polygon.Points[Polygon.Points.Count - 1]; PointF End = Polygon.Points[0]; Area = Area + (Start.X * End.Y) - (End.X * Start.Y); } return(Math.Abs(Area) / 2.0); }
/// <summary> /// Replace the selected lines or polylines with a single polyline. /// </summary> /// <param name="surface"></param> /// <returns></returns> public static bool MakePath(GraphicsSurface surface) { bool result = false; FigPolyline new_fig = new FigPolyline(); if (surface.selected.Count == 0) { return(result); // nothing selected } foreach (Figure fig in surface.selected) { if (!((fig is Line) || (fig is FigPolyline))) { return(false); } } foreach (Figure fig in surface.selected) { if (new_fig.Points.Count == 0) { if (fig is Line) { Line line = fig as Line; new_fig.Points.Add(new PointF(line.p1.X, line.p1.Y)); new_fig.Points.Add(new PointF(line.p2.X, line.p2.Y)); new_fig.LineProperties = line.LineProperties; } else { FigPolyline poly = fig as FigPolyline; foreach (PointF p in poly.Points) { new_fig.Points.Add(new PointF(p.X, p.Y)); } new_fig.LineProperties = poly.LineProperties; } } else { // check contiguous if (fig is Line) { Line line = fig as Line; if (new_fig.Points[new_fig.Points.Count - 1] == line.p1) { new_fig.Points.Add(new PointF(line.p2.X, line.p2.Y)); } else if (new_fig.Points[new_fig.Points.Count - 1] == line.p2) { new_fig.Points.Add(new PointF(line.p1.X, line.p1.Y)); } else if (new_fig.Points[0] == line.p1) { new_fig.Points.Insert(0, new PointF(line.p2.X, line.p2.Y)); } else if (new_fig.Points[0] == line.p2) { new_fig.Points.Insert(0, new PointF(line.p1.X, line.p1.Y)); } else { return(false); } } else { //polyline FigPolyline poly = fig as FigPolyline; if (new_fig.Points[new_fig.Points.Count - 1] == poly.Points[0]) { AddPoints(ref new_fig.Points, poly.Points, 1); } else if (new_fig.Points[new_fig.Points.Count - 1] == poly.Points[poly.Points.Count - 1]) { AddPointsReverse(ref new_fig.Points, poly.Points, 1); } } } } surface.Drawing.RemoveFigures(surface.selected); surface.Drawing.AddFigure(new_fig); // set new selection //surface.ClearSelected(); surface.selected.Clear(); //surface.AddToSelected(new_fig); surface.selected.Add(new_fig); return(result); }