public Polygon ToPolygon(int offset = 0) { offset += HitBox; var result = new Polygon(); var innerRadius = -0.1562f * Distance + 687.31f; var outerRadius = 0.35256f * Distance + 133f; outerRadius = outerRadius / (float) Math.Cos(2 * Math.PI / CircleLineSegmentN); var innerCenters = LeagueSharp.Common.Geometry.CircleCircleIntersection( Start, End, innerRadius, innerRadius); var outerCenters = LeagueSharp.Common.Geometry.CircleCircleIntersection( Start, End, outerRadius, outerRadius); var innerCenter = innerCenters[0]; var outerCenter = outerCenters[0]; var direction = (End - outerCenter).Normalized(); var end = (Start - outerCenter).Normalized(); var maxAngle = (float) (direction.AngleBetween(end) * Math.PI / 180); var step = -maxAngle / CircleLineSegmentN; for (var i = 0; i < CircleLineSegmentN; i++) { var angle = step * i; var point = outerCenter + (outerRadius + 15 + offset) * direction.Rotated(angle); result.Add(point); } direction = (Start - innerCenter).Normalized(); end = (End - innerCenter).Normalized(); maxAngle = (float) (direction.AngleBetween(end) * Math.PI / 180); step = maxAngle / CircleLineSegmentN; for (var i = 0; i < CircleLineSegmentN; i++) { var angle = step * i; var point = innerCenter + Math.Max(0, innerRadius - offset - 100) * direction.Rotated(angle); result.Add(point); } return result; }
public static Polygons GetCorrectedWinding(this Polygons polygonsToFix) { polygonsToFix = Clipper.CleanPolygons(polygonsToFix); Polygon boundsPolygon = new Polygon(); IntRect bounds = Clipper.GetBounds(polygonsToFix); bounds.left -= 10; bounds.bottom += 10; bounds.right += 10; bounds.top -= 10; boundsPolygon.Add(new IntPoint(bounds.left, bounds.top)); boundsPolygon.Add(new IntPoint(bounds.right, bounds.top)); boundsPolygon.Add(new IntPoint(bounds.right, bounds.bottom)); boundsPolygon.Add(new IntPoint(bounds.left, bounds.bottom)); Clipper clipper = new Clipper(); clipper.AddPaths(polygonsToFix, PolyType.ptSubject, true); clipper.AddPath(boundsPolygon, PolyType.ptClip, true); PolyTree intersectionResult = new PolyTree(); clipper.Execute(ClipType.ctIntersection, intersectionResult); Polygons outputPolygons = Clipper.ClosedPathsFromPolyTree(intersectionResult); return outputPolygons; }
public PDFReferencePointRenderer() { // Create the polygon that represents the body bodyPlygn = new Polygon(); bodyPlygn.Add(new Vector2(this.X + .25, this.Y + .25)); bodyPlygn.Add(new Vector2(this.X + .25, this.Y - .25)); bodyPlygn.Add(new Vector2(this.X - .25, this.Y + .25)); bodyPlygn.Add(new Vector2(this.X - .25, this.Y - .25)); }
public WaypointRenderer() { // Create the polygon that represents the body bodyPlygn = new Polygon(); bodyPlygn.Add(new Vector2(this.X+.2, this.Y+.2)); bodyPlygn.Add(new Vector2(this.X+.2, this.Y-.2)); bodyPlygn.Add(new Vector2(this.X-.2, this.Y+.2)); bodyPlygn.Add(new Vector2(this.X-.2, this.Y-.2)); }
public static void TestSquare() { Polygon poly = new Polygon(); poly.Add(new Position("36.60987854N,88.30194711W")); poly.Add(new Position("36.60989999N,88.30430746W")); poly.Add(new Position("36.60797953N,88.30428600W")); poly.Add(new Position("36.60796880N,88.30190420W")); // Built-in formula Console.WriteLine("GeoFrameworks new calc: " + poly.Perimeter.ToString()); // Custom formula Console.WriteLine("GeoFrameworks old calc: " + Perimeter(poly)); }
/// <summary> /// Create and return octagon model of vehicle from given radius /// </summary> /// <param name="r"></param> /// <returns></returns> public static Polygon VehiclePolygonWithRadius(double r) { Polygon poly = new Polygon(); poly.Add(new Vector2(r, r)); poly.Add(new Vector2(-r, r)); poly.Add(new Vector2(-r, -r)); poly.Add(new Vector2(r, -r)); /* for (int i = -5; i < 7; i += 2) { poly.Add(new Vector2(r * Math.Cos(Math.PI * i / 8), r * Math.Sin(Math.PI * i / 8))); } * */ return poly; }
public static Polygon ToPolygon(this Path v) { var polygon = new Polygon(); foreach (var point in v) { polygon.Add(new Vector2(point.X, point.Y)); } return polygon; }
public static void Build(RooFile Room) { if (Room == null) return; room = Room; if (BuildStarted != null) BuildStarted(null, new EventArgs()); /////////////////////////////////////////////////////////////// BoundingBox2D box = Room.GetBoundingBox2D(true); Polygon poly = new Polygon(); poly.Add(box.Min); poly.Add(box.Min + new V2(box.Max.X - box.Min.X, 0f)); poly.Add(box.Max); poly.Add(box.Max - new V2(box.Max.X - box.Min.X, 0f)); /////////////////////////////////////////////////////////////// // clean up old data from room Room.Walls.Clear(); Room.BSPTree.Clear(); foreach (RooSector sector in Room.Sectors) { sector.Walls.Clear(); sector.Sides.Clear(); } // convert roomeditor walls to roowall for (int i = 0; i < Room.WallsEditor.Count; i++) { RooWall wall = Room.WallsEditor[i].ToRooWall(RooFile.VERSIONHIGHRESGRID, Room); Room.Walls.Add(wall); } /////////////////////////////////////////////////////////////// RooBSPItem tree = BuildNode(Room.Walls, poly, 0); /////////////////////////////////////////////////////////////// FillNode(tree, Room.BSPTree); SetNums(Room.BSPTree); }
PathToRobotRenderer runningPath; // the path that the robot is currently running #endregion Fields #region Constructors public RobotKeypoint(int id) { color = Color.Black; this.id = id; name = "Robot " + id; runningPath = new PathToRobotRenderer(); commandedPath = new PathToRobotRenderer(); pose = new RobotPose(); origPlygn = new Polygon(); origPlygn.Add(new Vector2(.25, .36)); origPlygn.Add(new Vector2(.25, -.36)); origPlygn.Add(new Vector2(-.25, -.36)); origPlygn.Add(new Vector2(-.25, .36)); bodyPlygn = new Polygon(origPlygn.points); }
public static void Triangulate(IList <Vector2> vertices, IList <Edge> edges, IList <int> indices) { indices.Clear(); if (vertices.Count < 3) { return; } var polygon = new Polygon(vertices.Count); for (int i = 0; i < vertices.Count; ++i) { Vector2 position = vertices[i]; polygon.Add(new Vertex(position.x, position.y, 1)); } for (int i = 0; i < edges.Count; ++i) { Edge edge = edges[i]; polygon.Add(new Segment(polygon.Points[edge.index1], polygon.Points[edge.index2])); } var mesh = polygon.Triangulate(); foreach (ITriangle triangle in mesh.Triangles) { int id0 = triangle.GetVertexID(0); int id1 = triangle.GetVertexID(1); int id2 = triangle.GetVertexID(2); if (id0 < 0 || id1 < 0 || id2 < 0 || id0 >= vertices.Count || id1 >= vertices.Count || id2 >= vertices.Count) { continue; } indices.Add(id0); indices.Add(id2); indices.Add(id1); } }
void lazyFill(Polygons polysToWriteTo, IntPoint startPoint, int z, CheckForSupport checkIfSupportNeeded) { Polygon poly = new Polygon(); polysToWriteTo.Add(poly); Polygon tmpPoly = new Polygon(); while (true) { IntPoint endPoint = startPoint; done[endPoint.X + endPoint.Y * supportStorage.gridWidth] = true; while (checkIfSupportNeeded(endPoint + new IntPoint(1, 0), z)) { endPoint.X++; done[endPoint.X + endPoint.Y * supportStorage.gridWidth] = true; } tmpPoly.Add(startPoint * supportStorage.gridScale + supportStorage.gridOffset - new IntPoint(supportStorage.gridScale / 2, 0)); poly.Add(endPoint * supportStorage.gridScale + supportStorage.gridOffset); startPoint.Y++; while (!checkIfSupportNeeded(startPoint, z) && startPoint.X <= endPoint.X) { startPoint.X++; } if (startPoint.X > endPoint.X) { for (int n = 0; n < tmpPoly.Count; n++) { poly.Add(tmpPoly[tmpPoly.Count - n - 1]); } polysToWriteTo.Add(poly); return; } while (checkIfSupportNeeded(startPoint - new IntPoint(1, 0), z) && startPoint.X > 1) { startPoint.X--; } } }
public Shape(GeoPoint center, double radius, int polygons = 60) { var angle = (360.0 / polygons) * (Math.PI / 180.0); Center = center; for (int i = 0; i < polygons; i++) { var point = new GeoPoint(center.Latitude + radius * Math.Cos(angle * i), center.Longitude + (radius / 1.5) * Math.Sin(angle * i)); Polygon.Add(point); } }
void Start() { Triangulator tri = new Triangulator(); // Vector2 p0 = new Vector2( 0.0f, 0.0f); // Vector2 p1 = new Vector2( 0.0f, 1.0f); // Vector2 p2 = new Vector2(-0.5f, 0.0f); // Debug.Log("Orientation of [" + p0 + p1 + "] [" + p2 + "] : " + Orientation(p0, p1, p2)); // p0 = new Vector2( 0.0f, 0.0f); // p1 = new Vector2( 0.0f, 1.0f); // p2 = new Vector2(+4.5f, -1.0f); // Debug.Log("Orientation of [" + p0 + p1 + "] [" + p2 + "] : " + Orientation(p0, p1, p2)); MartinezClipping clippingAlgo = new MartinezClipping(); Polygon subject = new Polygon(); Polygon clipper = new Polygon(); SimpleClosedPath subjectContour = new SimpleClosedPath(subject); SimpleClosedPath clipperContour = new SimpleClosedPath(clipper); foreach (Vector2 vertex in subjectVerts) { subjectContour.Add(vertex); } foreach (Vector2 vertex in clipperVerts) { clipperContour.Add(vertex); } subject.Add(subjectContour); clipper.Add(clipperContour); subject.ComputeHoles(); clipper.ComputeHoles(); clippingAlgo.subject = subject; clippingAlgo.clipper = clipper; Polygon result = clippingAlgo.Compute(operationType); result.ComputeHoles(); //result = tri.Simplify(result); polyRenderer.AddPolygon(subject, Color.red); polyRenderer.AddPolygon(clipper, Color.blue); polyRenderer.AddPolygon(result, Color.green); polyRenderer.AddEdges(clippingAlgo.DebugLines); }
private static Polygon CreateRandom() { Polygon polygon = new Polygon(); Random.InitState(seed); for (int i = 0; i < territoryCount; i++) { polygon.Add(new Vertex(Random.Range(0, values.x), Random.Range(0, values.y))); } return(polygon); }
public PolygonSurface Triangulate(ModularMesh mesh, Material material) { Polygon polygon = new Polygon(); if (Type == StreetPolygonType.LeftHand) { for (int i = 0; i < streetmember.Count; i++) { polygon.Add(streetmember[i].LeftOutline); } } else { for (int i = 0; i < streetmember.Count; i++) { polygon.Add(streetmember[i].RightOutline); } } polygon.MakeClockwise(); return(polygon.Triangulate(mesh, material)); }
//--------------------------------------------------------------------- private void GenerateTestPolygon(int count) { subjects.Clear(); clips.Clear(); int SCALE = 1000; Polygon subj = new Polygon(5); subj.Add(new IntPoint(SCALE * 05, SCALE * 25)); subj.Add(new IntPoint(SCALE * 25, SCALE * 70)); subj.Add(new IntPoint(SCALE * 40, SCALE * 70)); subj.Add(new IntPoint(SCALE * 40, SCALE * 25)); subj.Add(new IntPoint(SCALE * 60, SCALE * 10)); subjects.Add(subj); Polygon clip = new Polygon(4); clip.Add(new IntPoint(SCALE * 90, SCALE * 90)); clip.Add(new IntPoint(SCALE * 130, SCALE * 90)); clip.Add(new IntPoint(SCALE * 130, SCALE * 130)); clip.Add(new IntPoint(SCALE * 90, SCALE * 130)); clips.Add(clip); }
public Polygon ToPolygon(int offset = 0) { var result = new Polygon(); var outRadius = (offset + Radius + RingRadius) / (float)Math.Cos(2 * Math.PI / CircleLineSegmentN); var innerRadius = Radius - RingRadius - offset; for (var i = 0; i <= CircleLineSegmentN; i++) { var angle = i * 2 * Math.PI / CircleLineSegmentN; var point = new Vector2(Center.X - outRadius * (float)Math.Cos(angle), Center.Y - outRadius * (float)Math.Sin(angle)); result.Add(point); } for (var i = 0; i <= CircleLineSegmentN; i++) { var angle = i * 2 * Math.PI / CircleLineSegmentN; var point = new Vector2(Center.X + innerRadius * (float)Math.Cos(angle), Center.Y - innerRadius * (float)Math.Sin(angle)); result.Add(point); } return(result); }
/// <summary> /// Creates a collider from the StarMapColliderInfo /// </summary> /// <param name="starMapColliderInfo"></param> public void CreateFromCollider(StarMapColliderInfo starMapColliderInfo) { polygon = starMapColliderInfo.Polygon; Origin = -(starMapColliderInfo.Size.ToFloat() / 2); var first = polygon.FirstOrDefault(); if (first != null) { polygon.Add(first); } }
public static IPolygon CreatePolygon() { // Generate three concentric circles. var poly = new Polygon(); // Center point. var center = new Point(0, 0); // Inner contour (hole). poly.Add(Generate.Circle(1.0, center, 0.1, 1), center); // Internal contour. poly.Add(Generate.Circle(2.0, center, 0.1, 2)); // Outer contour. poly.Add(Generate.Circle(3.0, center, 0.3, 3)); // Note that the outer contour has a larger segment size! return(poly); }
private void ReadPoints(BinaryReader f, ref Polygon Shape, int UpTo) { Shape.Points.BeginUpdate(); for (int t = 0; t < UpTo - po; ++t) { Shape.Add(f.ReadDouble(), f.ReadDouble()); } po = UpTo; Shape.Points.EndUpdate(); }
public virtual void Generate(int worldSeed) { int chunkSeed = Generator.GetPerIslandSeed(this); creatingChunkStopwatch = System.Diagnostics.Stopwatch.StartNew(); random = new Random(chunkSeed); elevations = new List <float>(); islandColors = new List <Color>(); CreatingPoolTask = delegate { PoissonDiscSampler sampler = new PoissonDiscSampler(Size, Size, minPointRadius); Polygon polygon = new Polygon(); //Add uniformly-spaced points foreach (Vector2 sample in sampler.Samples(chunkSeed)) { polygon.Add(new Vertex((double)sample.x, (double)sample.y)); } //add points at corners so chunk will be always square shaped polygon.Add(new Vertex(0, 0)); polygon.Add(new Vertex(0, Size)); polygon.Add(new Vertex(Size, 0)); polygon.Add(new Vertex(Size, Size)); //Add some randomly sampled points for (int i = 0; i < randomPoints - 4; i++) { polygon.Add(new Vertex(random.Range(0.0f, Size), random.Range(0.0f, Size))); } TriangleNet.Meshing.ConstraintOptions options = new TriangleNet.Meshing.ConstraintOptions() { ConformingDelaunay = true }; mesh = (TriangleNet.Mesh)polygon.Triangulate(options); // Sample perlin noise to get elevations foreach (Vertex vert in mesh.Vertices) { float height = Generator.GetTerrainHeight((float)vert.x, (float)vert.y, this); Color color = Generator.GetTerrainColor((float)vert.x, (float)vert.y, height, this); elevations.Add(height); islandColors.Add(color); } CreateMeshRequest = true; //let this be always the last piece of code here try { bin = new TriangleBin(mesh, Size, Size, minPointRadius * 2.0f); } catch (Exception e) { Debug.Log("triangulation failed!"); } }; CustomThreadPool.AddTask(CreatingPoolTask); }
private Obstacle GetSideObstacle(SideObstacles sideObstacles) { if (sideObstacles == null) { return(null); } double minDist = 100; Obstacle minObstacle = null; foreach (SideObstacle obs in sideObstacles.obstacles) { if (obs.distance > 0.5 && obs.distance < minDist) { minDist = obs.distance; // create a polygon the specified distance away and 1 m in front of the rear axle and 0.5 m behind the front axle Polygon poly = new Polygon(); if (sideObstacles.side == SideObstacleSide.Driver) { poly.Add(new Coordinates(1, obs.distance + TahoeParams.T / 2.0)); poly.Add(new Coordinates(TahoeParams.FL - 0.5, obs.distance + TahoeParams.T / 2.0)); poly.Add(new Coordinates(TahoeParams.FL - 0.5, obs.distance + TahoeParams.T / 2.0 + 1)); poly.Add(new Coordinates(1, obs.distance + TahoeParams.T / 2.0 + 1)); } else { poly.Add(new Coordinates(1, -(obs.distance + TahoeParams.T / 2.0))); poly.Add(new Coordinates(TahoeParams.FL - 0.5, -(obs.distance + TahoeParams.T / 2.0))); poly.Add(new Coordinates(TahoeParams.FL - 0.5, -(obs.distance + TahoeParams.T / 2.0 + 1))); poly.Add(new Coordinates(1, -(obs.distance + TahoeParams.T / 2.0 + 1))); } Obstacle finalObs = new Obstacle(); finalObs.age = 1; finalObs.obstacleClass = ObstacleClass.StaticLarge; finalObs.obstaclePolygon = poly; finalObs.minSpacing = min_spacing[(int)finalObs.obstacleClass]; finalObs.desSpacing = des_spacing[(int)finalObs.obstacleClass]; finalObs.cspacePolygon = Polygon.ConvexMinkowskiConvolution(conv_polygon[(int)finalObs.obstacleClass], finalObs.AvoidancePolygon); minObstacle = finalObs; } } return(minObstacle); }
public static void GenerateLinePaths(Polygons polygonToInfill, ref Polygons infillLinesToPrint, int lineSpacing, int infillExtendIntoPerimeter_um, double rotation, long rotationOffset = 0) { if (polygonToInfill.Count > 0) { Polygons outlines = polygonToInfill.Offset(infillExtendIntoPerimeter_um); if (outlines.Count > 0) { PointMatrix matrix = new PointMatrix(-(rotation + 90)); // we are rotating the part so we rotate by the negative so the lines go the way we expect outlines.ApplyMatrix(matrix); Aabb boundary = new Aabb(outlines); boundary.min.X = ((boundary.min.X / lineSpacing) - 1) * lineSpacing - rotationOffset; int xLineCount = (int)((boundary.max.X - boundary.min.X + (lineSpacing - 1)) / lineSpacing); Polygons unclipedPatern = new Polygons(); long firstX = boundary.min.X / lineSpacing * lineSpacing; for (int lineIndex = 0; lineIndex < xLineCount; lineIndex++) { Polygon line = new Polygon(); line.Add(new IntPoint(firstX + lineIndex * lineSpacing, boundary.min.Y)); line.Add(new IntPoint(firstX + lineIndex * lineSpacing, boundary.max.Y)); unclipedPatern.Add(line); } PolyTree ret = new PolyTree(); Clipper clipper = new Clipper(); clipper.AddPaths(unclipedPatern, PolyType.ptSubject, false); clipper.AddPaths(outlines, PolyType.ptClip, true); clipper.Execute(ClipType.ctIntersection, ret, PolyFillType.pftPositive, PolyFillType.pftEvenOdd); Polygons newSegments = Clipper.OpenPathsFromPolyTree(ret); PointMatrix inversematrix = new PointMatrix((rotation + 90)); newSegments.ApplyMatrix(inversematrix); infillLinesToPrint.AddRange(newSegments); } } }
private Polygon GetPolygon() { var poly = new Polygon(7); var p = new List <Vertex>() { new Vertex(0.0, 0.0, 1), new Vertex(2.0, 0.0, 1), new Vertex(2.0, 1.0, 1), new Vertex(2.0, 2.0, 1), new Vertex(0.0, 2.0, 1), new Vertex(1.5, 1.6, 1), new Vertex(2.0, 0.0, 1) // duplicate }; poly.Points.AddRange(p); poly.Add(new Segment(p[0], p[1], 1)); poly.Add(new Segment(p[1], p[2], 1)); poly.Add(new Segment(p[2], p[3], 1)); poly.Add(new Segment(p[3], p[4], 1)); poly.Add(new Segment(p[4], p[0], 1)); poly.Add(new Segment(p[2], p[5], 2)); return(poly); }
public override IPolygon Generate(double param0, double param1, double param2) { // Number of points on the outer circle int n = GetParamValueInt(0, param0); double radius = GetParamValueInt(1, param1); // Current radius and step size double r, h = radius / n; var polygon = new Polygon(n + 1); // Inner cirlce (radius = 1) (hole) r = 1; var innerCircleMarker = 1; polygon.Add(CreateCircleContour(r, (int)(r / h), innerCircleMarker), new Point(0, 0)); // Center cirlce r = (radius + 1.0) / 2.0; var centerCircleMarker = 2; polygon.Add(CreateCircleContour(r, (int)(r / h), centerCircleMarker)); //count = input.Count; // Outer cirlce r = radius; var outerCircleMarker = 3; polygon.Add(CreateCircleContour(r, (int)(r / h), outerCircleMarker)); // Regions: |++++++|++++++|---| // r 1 0 polygon.Regions.Add(new RegionPointer((r + 3.0) / 4.0, 0, 1)); polygon.Regions.Add(new RegionPointer((3 * r + 1.0) / 4.0, 0, 2)); return(polygon); }
public DTConvexPolygroup PolygonToTriangleList(DTPolygon subject) { // Mark any unmarked holes in the contour, otherwise Triangle.NET won't handle them properly subject = DTUtility.IdentifyHoles(subject); if (subject.Contour.Count < 3) { return(new DTConvexPolygroup()); } // Don't triangulate if this is already a triangle else if (subject.Contour.Count == 3 && subject.Holes.Count == 0) { return(new DTConvexPolygroup(new List <DTPolygon>() { subject })); } // Format polygon input and execute Polygon polygon = new Polygon(); polygon.Add(new Contour(subject.Contour.ToVertexList()), false); foreach (var hole in subject.Holes) { if (hole.Count >= 3) { try { polygon.Add(new Contour(hole.ToVertexList()), true); } catch (Exception) {} } } DTProfilerMarkers.TriangleNet.Begin(); IMesh triangleNetOutput = polygon.Triangulate(); ++callCount; DTProfilerMarkers.TriangleNet.End(); // Convert Triangle.NET output into poly group return(new DTConvexPolygroup(triangleNetOutput.Triangles.Select(t => t.ToVertexList()).ToList())); }
public override unsafe void ReadFrom(ref byte *Buffer) { base.ReadFrom(ref Buffer); SectorNum = *((ushort *)Buffer); Buffer += TypeSizes.SHORT; ushort len = *((ushort *)Buffer); Buffer += TypeSizes.SHORT; Vertices = new Polygon(); if (RooVersion < RooFile.VERSIONFLOATCOORDS) { for (int i = 0; i < len; i++) { int x = *((int *)Buffer); Buffer += TypeSizes.INT; int y = *((int *)Buffer); Buffer += TypeSizes.INT; Vertices.Add(new V2(x, y)); } } else { for (int i = 0; i < len; i++) { float x = *((float *)Buffer); Buffer += TypeSizes.FLOAT; float y = *((float *)Buffer); Buffer += TypeSizes.FLOAT; Vertices.Add(new V2(x, y)); } } }
public override IPolygon Generate(double param0, double param1, double param2) { int numPoints = GetParamValueInt(0, param0); numPoints = (numPoints / 10) * 10; if (numPoints < 5) { numPoints = 5; } double exp = (param1 + 10) / 100; var input = new Polygon(numPoints); int i = 0, cNum = 2 * (int)Math.Floor(Math.Sqrt(numPoints)); double r, phi, radius = 100, step = 2 * Math.PI / cNum; // Distrubute points equally on circle border for (; i < cNum; i++) { // Add a little error r = Util.Random.NextDouble(); input.Add(new Vertex((radius + r) * Math.Cos(i * step), (radius + r) * Math.Sin(i * step))); } for (; i < numPoints; i++) { // Use sqrt(rand) to get normal distribution right. r = Math.Pow(Util.Random.NextDouble(), exp) * radius; phi = Util.Random.NextDouble() * Math.PI * 2; input.Add(new Vertex(r * Math.Cos(phi), r * Math.Sin(phi))); } return(input); }
public static void GenerateLinePaths(Polygons in_outline, ref Polygons result, int lineSpacing, int infillExtendIntoPerimeter_um, double rotation, long rotationOffset = 0) { if (in_outline.Count > 0) { Polygons outlines = in_outline.Offset(infillExtendIntoPerimeter_um); if (outlines.Count > 0) { PointMatrix matrix = new PointMatrix(-(rotation + 90)); // we are rotating the part so we rotate by the negative so the lines go the way we expect outlines.ApplyMatrix(matrix); Aabb boundary = new Aabb(outlines); boundary.min.X = ((boundary.min.X / lineSpacing) - 1) * lineSpacing - rotationOffset; int xLineCount = (int)((boundary.max.X - boundary.min.X + (lineSpacing - 1)) / lineSpacing); Polygons unclipedPatern = new Polygons(); long firstX = boundary.min.X / lineSpacing * lineSpacing; for (int lineIndex = 0; lineIndex < xLineCount; lineIndex++) { Polygon line = new Polygon(); line.Add(new IntPoint(firstX + lineIndex * lineSpacing, boundary.min.Y)); line.Add(new IntPoint(firstX + lineIndex * lineSpacing, boundary.max.Y)); unclipedPatern.Add(line); } PolyTree ret = new PolyTree(); Clipper clipper = new Clipper(); clipper.AddPaths(unclipedPatern, PolyType.ptSubject, false); clipper.AddPaths(outlines, PolyType.ptClip, true); clipper.Execute(ClipType.ctIntersection, ret, PolyFillType.pftPositive, PolyFillType.pftEvenOdd); Polygons newSegments = Clipper.OpenPathsFromPolyTree(ret); PointMatrix inversematrix = new PointMatrix((rotation + 90)); newSegments.ApplyMatrix(inversematrix); result.AddRange(newSegments); } } }
//--------------------------------------------------------------------- private void GenerateAustPlusRandomEllipses(int count) { subjects.Clear(); //load map of Australia from resource ... _assembly = Assembly.GetExecutingAssembly(); polyStream = _assembly.GetManifestResourceStream("ClipperCSharpDemo1.australia.bin"); int len = (int)polyStream.Length; byte[] b = new byte[len]; polyStream.Read(b, 0, len); int polyCnt = BitConverter.ToInt32(b, 0); int k = 4; for (int i = 0; i < polyCnt; ++i) { int vertCnt = BitConverter.ToInt32(b, k); k += 4; Polygon pg = new Polygon(vertCnt); for (int j = 0; j < vertCnt; ++j) { float x = BitConverter.ToSingle(b, k) * scale; float y = BitConverter.ToSingle(b, k + 4) * scale; k += 8; pg.Add(new IntPoint((int)x, (int)y)); } subjects.Add(pg); } clips.Clear(); Random rand = new Random(); GraphicsPath path = new GraphicsPath(); Point pt = new Point(); for (int i = 0; i < count; ++i) { int w = pictureBox1.ClientRectangle.Width - 220; int h = pictureBox1.ClientRectangle.Height - 140 - statusStrip1.Height; pt.X = rand.Next(w) + panel1.Width + 10; pt.Y = rand.Next(h) + 30; int size = rand.Next(90) + 10; path.Reset(); path.AddEllipse(pt.X, pt.Y, size, size); path.Flatten(); Polygon clip = new Polygon(path.PathPoints.Count()); foreach (PointF p in path.PathPoints) { clip.Add(new IntPoint((int)(p.X * scale), (int)(p.Y * scale))); } clips.Add(clip); } }
public void EnsureWipeTowerIsSolid(int layerIndex, LayerGCodePlanner gcodeLayer, GCodePathConfig fillConfig, ConfigSettings config) { if (layerIndex >= LastLayerWithChange(config) || extrudersThatHaveBeenPrimed == null) { return; } // TODO: if layer index == 0 do all the loops from the outside in in order (no lines should be in the wipe tower) if (layerIndex == 0) { CheckNoExtruderPrimed(config); long insetPerLoop = fillConfig.lineWidth_um; int extruderCount = config.GenerateSupport ? config.ExtruderCount * 2 : config.ExtruderCount; Polygons outlineForExtruder = this.wipeTower.Offset(-insetPerLoop); Polygons fillPolygons = new Polygons(); while (outlineForExtruder.Count > 0) { for (int polygonIndex = 0; polygonIndex < outlineForExtruder.Count; polygonIndex++) { Polygon newInset = outlineForExtruder[polygonIndex]; newInset.Add(newInset[0]); // add in the last move so it is a solid polygon fillPolygons.Add(newInset); } outlineForExtruder = outlineForExtruder.Offset(-insetPerLoop); } var oldPathFinder = gcodeLayer.PathFinder; gcodeLayer.PathFinder = null; gcodeLayer.QueuePolygons(fillPolygons, fillConfig); gcodeLayer.PathFinder = oldPathFinder; } else { // print all of the extruder loops that have not already been printed int extruderCount = config.GenerateSupport ? config.ExtruderCount * 2 : config.ExtruderCount; for (int extruderIndex = 0; extruderIndex < extruderCount; extruderIndex++) { if (!extrudersThatHaveBeenPrimed[extruderIndex]) { // write the loops for this extruder, but don't change to it. We are just filling the prime tower. PrimeOnWipeTower(extruderIndex, layerIndex, gcodeLayer, fillConfig, config, false); } // clear the history of printer extruders for the next layer extrudersThatHaveBeenPrimed[extruderIndex] = false; } } }
private static Polygon CreatePoisonDisc() { Polygon polygon = new Polygon(); Random.InitState(seed); DiscPoisonShape poissonDiscSampler = new DiscPoisonShape(values.x, values.y, orbit); foreach (var sample in poissonDiscSampler.Samples()) { polygon.Add(sample.ToVertex()); } return(polygon); }
/// <summary> /// Creates the collider from a polygon /// </summary> /// <param name="polygon">The polygon</param> public void CreateFromPolygon(Polygon polygon) { this.polygon = polygon; Origin = -(new Vector2f(polygon.Width, polygon.Height) / 2); var first = polygon.FirstOrDefault(); if (first != null) { polygon.Add(first); } }
public RobotRenderer(string name, Color color) { this.name = name; this.color = color; // Create the polygon that represents the body origPlygn = new Polygon(); origPlygn.Add(new Vector2(.25, .36)); origPlygn.Add(new Vector2(.25, -.36)); origPlygn.Add(new Vector2(-.25, -.36)); origPlygn.Add(new Vector2(-.25, .36)); bodyPlygn = new Polygon(); bodyPlygn.Add(new Vector2(.25, .36)); bodyPlygn.Add(new Vector2(.25, -.36)); bodyPlygn.Add(new Vector2(-.25, -.36)); bodyPlygn.Add(new Vector2(-.25, .36)); robotContextMenu = new ContextMenu(); this.MythName = establishMythName(name); }
public static Polygon PathsToPolygon(this Paths paths) { var result = new Polygon(); foreach (var path in paths) { foreach (var point in path) { result.Add(new Vector2(point.X, point.Y)); } } return(result); }
private Polygon ParsePolygon(string value) { var pointStrings = value.Split(' '); Polygon result = new Polygon(); foreach (var pt in pointStrings) { result.Add(Vector2X.Parse(pt)); } return(result); }
public static Polygon GenerateRandomDistribution(int size, int density) { Polygon polygon = new Polygon(); for (int i = 0; i < density; i++) { Vector2 p = new Vector2(Random.Range(.0f, size), Random.Range(.0f, size)); polygon.Add(new Vertex(p.x, p.y)); } return(polygon); }
private static Polygon GeneratePoisonDisc() { Polygon polygon = new Polygon(); Random.InitState(seed); PoissonDiscSampler poissonDiscSampler = new PoissonDiscSampler(dimensions.x, dimensions.y, radius); foreach (var sample in poissonDiscSampler.Samples()) { polygon.Add(sample.ToVertex()); } return(polygon); }
public void CreateWipeTower(int totalLayers, ConfigSettings config) { if (config.WipeTowerSize_um < 1 || LastLayerWithChange(config) == -1) { return; } Polygon wipeTowerShape = new Polygon(); wipeTowerShape.Add(new IntPoint(this.modelMin.X - 3000, this.modelMax.Y + 3000)); wipeTowerShape.Add(new IntPoint(this.modelMin.X - 3000, this.modelMax.Y + 3000 + config.WipeTowerSize_um)); wipeTowerShape.Add(new IntPoint(this.modelMin.X - 3000 - config.WipeTowerSize_um, this.modelMax.Y + 3000 + config.WipeTowerSize_um)); wipeTowerShape.Add(new IntPoint(this.modelMin.X - 3000 - config.WipeTowerSize_um, this.modelMax.Y + 3000)); this.WipeTower.Add(wipeTowerShape); var wipeTowerBounds = wipeTowerShape.GetBounds(); config.WipeCenter_um = new IntPoint( wipeTowerBounds.minX + (wipeTowerBounds.maxX - wipeTowerBounds.minX) / 2, wipeTowerBounds.minY + (wipeTowerBounds.maxY - wipeTowerBounds.minY) / 2); }
private void Triangulate(List <Vector2> points) { // Vertex is TriangleNet.Geometry.Vertex Polygon polygon = new Polygon(); maxX = -1000; maxY = -1000; minY = 1000; minX = 1000; float x, y; foreach (Vector2 point in points) { x = point.x; y = point.y; polygon.Add(new Vertex(x, y)); if (x > maxX) { maxX = x; } if (y > maxY) { maxY = y; } if (x < minX) { minX = x; } if (y < minY) { minY = y; } } if (polygon.Count > 2) { // ConformingDelaunay is false by default; this leads to ugly long polygons at the edges // because the algorithm will try to keep the mesh convex // TriangleNet.Meshing.ConstraintOptions options = // new TriangleNet.Meshing.ConstraintOptions() { ConformingDelaunay = true }; triangulatorMesh = (TriangleNet.Mesh)polygon.Triangulate(); } else { Debug.Log(string.Format("Mesh needs minimum 3 points but did only receive: {0} point(s)", polygon.Count)); } Debug.Log(points.Count); }
public Polygon ToPolygon() { double halfWidth = Width/2; double halfLength = Length/2; Coordinates heading = Heading.Normalize(); Coordinates heading90 = heading.Rotate90(); Coordinates l = heading*halfLength; Coordinates w = heading90*halfWidth; Coordinates pt1 = Position - l - w; Coordinates pt2 = Position + l - w; Coordinates pt3 = Position + l + w; Coordinates pt4 = Position - l + w; Polygon poly = new Polygon(4); poly.Add(pt1); poly.Add(pt2); poly.Add(pt3); poly.Add(pt4); return poly; }
public Polygon ToPolygon(int offset = 0, float overrideWidth = -1) { var result = new Polygon(); var outRadius = (overrideWidth > 0 ? overrideWidth : (offset + Radius) / (float)Math.Cos(2 * Math.PI / CircleLineSegmentN)); for (var i = 1; i <= CircleLineSegmentN; i++) { var angle = i * 2 * Math.PI / CircleLineSegmentN; var point = new Vector2( Center.X + outRadius * (float)Math.Cos(angle), Center.Y + outRadius * (float)Math.Sin(angle)); result.Add(point); } return result; }
/// <summary> /// 保持している全DEMデータ情報をKML形式のテキストデータで返す /// <para>ファイルへ保存する際はUTF-8で保存してください。</para> /// </summary> /// <param name="color">色情報</param> /// <returns>テキスト形式のKMLポリゴン情報</returns> public string ToStringAsKml(Color color) { Polygon polygon = new Polygon(); polygon.name = "dummy"; polygon.color = color; var arr = this.dict.Values.ToArray(); /* if (arr.Length != 0) { for (int i = 1; i < arr.Length; i++) { Polygon poly = new Polygon(); poly.name = arr[i].DataInfo.ID.ToString(); poly.AddToOuter(arr[i].DataInfo.Field.ToArray()); polygon.AddPolygon(poly); } } * */ foreach (DemSet member in arr) { Polygon poly = new Polygon(); poly.name = member.DataInfo.ID.ToString(); poly.AddToOuter(member.DataInfo.Field.ToArray()); StringBuilder sb = new StringBuilder(300); // 予め大きなメモリ容量を確保しておく sb.Append("File name: ").Append(member.FileName).Append(System.Environment.NewLine); sb.Append("Map Model: ").Append(member.DataInfo.MapModel.ToString()).Append(System.Environment.NewLine); sb.Append("Center position: ").Append(member.DataInfo.Field.Center.ToString()); poly.description = sb.ToString(); polygon.Add(poly); } return polygon.GetKmlCode(); }
public Polygon GetBoundingPolygon() { bodyPlygn = new Polygon(); bodyPlygn.Add(new Vector2(this.X + .25, this.Y + .25)); bodyPlygn.Add(new Vector2(this.X + .25, this.Y - .25)); bodyPlygn.Add(new Vector2(this.X - .25, this.Y - .25)); bodyPlygn.Add(new Vector2(this.X - .25, this.Y + .25)); return bodyPlygn; }
private void RefreshAsset(string name) { var asset = _data.GetAsset(name); _sw.Reset(); foreach (var poly in asset.Polygons) { var v = new ContourVertex[poly.Count]; for (int i = 0; i < poly.Count; i++) { v[i].Position = new Vec3 { X = poly[i].X, Y = poly[i].Y }; v[i].Data = poly[i].Color; } _sw.Start(); _tess.AddContour(v, poly.Orientation); _sw.Stop(); } _sw.Start(); _tess.Tessellate(_windingRule, ElementType.Polygons, _polySize, VertexCombine); _sw.Stop(); var output = new PolygonSet(); for (int i = 0; i < _tess.ElementCount; i++) { var poly = new Polygon(); for (int j = 0; j < _polySize; j++) { int index = _tess.Elements[i * _polySize + j]; if (index == -1) continue; var v = new PolygonPoint { X = _tess.Vertices[index].Position.X, Y = _tess.Vertices[index].Position.Y, Color = (Color)_tess.Vertices[index].Data }; poly.Add(v); } output.Add(poly); } statusMain.Text = string.Format("{0:F3} ms - {1} polygons (of {2} vertices) {3}", _sw.Elapsed.TotalMilliseconds, _tess.ElementCount, _polySize, _polySize == 3 ? "... triangles" : ""); _canvas.Input = asset.Polygons; _canvas.Output = output; _canvas.Invalidate(); }
public Polygon ToPolygon(int offset = 0) { var result = new Polygon(); var outRadius = (offset + Radius + RingRadius)/(float) Math.Cos(2*Math.PI/CircleLineSegmentN); var innerRadius = Radius - RingRadius - offset; for (var i = 0; i <= CircleLineSegmentN; i++) { var angle = i*2*Math.PI/CircleLineSegmentN; var point = new Vector2( Center.X - outRadius*(float) Math.Cos(angle), Center.Y - outRadius*(float) Math.Sin(angle)); result.Add(point); } for (var i = 0; i <= CircleLineSegmentN; i++) { var angle = i*2*Math.PI/CircleLineSegmentN; var point = new Vector2( Center.X + innerRadius*(float) Math.Cos(angle), Center.Y - innerRadius*(float) Math.Sin(angle)); result.Add(point); } return result; }
//------------------------------------------------------------------------------ public bool AddPolygon(Polygon pg, PolyType polyType) { int len = pg.Count; if (len < 3) return false; Polygon p = new Polygon(len); p.Add(new IntPoint(pg[0].X, pg[0].Y)); int j = 0; for (int i = 1; i < len; ++i) { Int64 maxVal; if (m_UseFullRange) maxVal = hiRange; else maxVal = loRange; if (Math.Abs(pg[i].X) > maxVal || Math.Abs(pg[i].Y) > maxVal) { if (Math.Abs(pg[i].X) > hiRange || Math.Abs(pg[i].Y) > hiRange) throw new ClipperException("Coordinate exceeds range bounds"); maxVal = hiRange; m_UseFullRange = true; } if (PointsEqual(p[j], pg[i])) continue; else if (j > 0 && SlopesEqual(p[j-1], p[j], pg[i], m_UseFullRange)) { if (PointsEqual(p[j-1], pg[i])) j--; } else j++; if (j < p.Count) p[j] = pg[i]; else p.Add(new IntPoint(pg[i].X, pg[i].Y)); } if (j < 2) return false; len = j+1; while (len > 2) { //nb: test for point equality before testing slopes ... if (PointsEqual(p[j], p[0])) j--; else if (PointsEqual(p[0], p[1]) || SlopesEqual(p[j], p[0], p[1], m_UseFullRange)) p[0] = p[j--]; else if (SlopesEqual(p[j - 1], p[j], p[0], m_UseFullRange)) j--; else if (SlopesEqual(p[0], p[1], p[2], m_UseFullRange)) { for (int i = 2; i <= j; ++i) p[i - 1] = p[i]; j--; } else break; len--; } if (len < 3) return false; //create a new edge array ... List<TEdge> edges = new List<TEdge>(len); for (int i = 0; i < len; i++) edges.Add(new TEdge()); m_edges.Add(edges); //convert vertices to a double-linked-list of edges and initialize ... edges[0].xcurr = p[0].X; edges[0].ycurr = p[0].Y; InitEdge(edges[len-1], edges[0], edges[len-2], p[len-1], polyType); for (int i = len-2; i > 0; --i) InitEdge(edges[i], edges[i+1], edges[i-1], p[i], polyType); InitEdge(edges[0], edges[1], edges[len-1], p[0], polyType); //reset xcurr & ycurr and find 'eHighest' (given the Y axis coordinates //increase downward so the 'highest' edge will have the smallest ytop) ... TEdge e = edges[0]; TEdge eHighest = e; do { e.xcurr = e.xbot; e.ycurr = e.ybot; if (e.ytop < eHighest.ytop) eHighest = e; e = e.next; } while ( e != edges[0]); //make sure eHighest is positioned so the following loop works safely ... if (eHighest.windDelta > 0) eHighest = eHighest.next; if (eHighest.dx == horizontal) eHighest = eHighest.next; //finally insert each local minima ... e = eHighest; do { e = AddBoundsToLML(e); } while( e != eHighest ); return true; }
static public PolyTree FindDistictObjectBounds(ImageBuffer image) { MarchingSquaresByte marchingSquaresData = new MarchingSquaresByte(image, 5, 0); marchingSquaresData.CreateLineSegments(); Polygons lineLoops = marchingSquaresData.CreateLineLoops(1); if (lineLoops.Count == 1) { return null; } // create a bounding polygon to clip against IntPoint min = new IntPoint(long.MaxValue, long.MaxValue); IntPoint max = new IntPoint(long.MinValue, long.MinValue); foreach (Polygon polygon in lineLoops) { foreach (IntPoint point in polygon) { min.X = Math.Min(point.X - 10, min.X); min.Y = Math.Min(point.Y - 10, min.Y); max.X = Math.Max(point.X + 10, max.X); max.Y = Math.Max(point.Y + 10, max.Y); } } Polygon boundingPoly = new Polygon(); boundingPoly.Add(min); boundingPoly.Add(new IntPoint(min.X, max.Y)); boundingPoly.Add(max); boundingPoly.Add(new IntPoint(max.X, min.Y)); // now clip the polygons to get the inside and outside polys Clipper clipper = new Clipper(); clipper.AddPaths(lineLoops, PolyType.ptSubject, true); clipper.AddPath(boundingPoly, PolyType.ptClip, true); PolyTree polyTreeForPlate = new PolyTree(); clipper.Execute(ClipType.ctIntersection, polyTreeForPlate); return polyTreeForPlate; }
/// <summary> /// Converts a list of <see cref="IntPoint" /> to a polygon. /// </summary> /// <param name="v">The int points.</param> /// <returns></returns> public static Polygon ELToPolygon(this List<IntPoint> v) { var polygon = new Polygon(); foreach (var point in v) { polygon.Add(new Vector2(point.X, point.Y)); } return polygon; }
/// <summary> /// Moves the polygone to the set position. It dosent rotate the polygone. /// </summary> /// <param name="polygon">The polygon.</param> /// <param name="moveTo">The move to.</param> /// <returns></returns> public static Polygon ELMovePolygone(this Polygon polygon, Vector2 moveTo) { var p = new Polygon(); p.Add(moveTo); var count = polygon.Points.Count; var startPoint = polygon.Points[0]; for (var i = 1; i < count; i++) { var polygonePoint = polygon.Points[i]; p.Add( new Vector2( moveTo.X + (polygonePoint.X - startPoint.X), moveTo.Y + (polygonePoint.Y - startPoint.Y))); } return p; }
public Polygon ToPolygon(int offset = 0) { var result = new Polygon(); result.Add(RStart + (Width + offset)*Perpendicular - offset*Direction); result.Add(RStart - (Width + offset)*Perpendicular - offset*Direction); result.Add(REnd - (Width + offset)*Perpendicular + offset*Direction); result.Add(REnd + (Width + offset) * Perpendicular + offset* Direction); return result; }
/// <summary> /// Rotates the polygon around the set position. /// Angle is in radians. /// </summary> /// <param name="polygon">The polygon.</param> /// <param name="around">The around.</param> /// <param name="angle">The angle.</param> /// <returns></returns> public static Polygon ELRotatePolygon(this Polygon polygon, Vector2 around, float angle) { var p = new Polygon(); foreach (var polygonePoint in polygon.Points.Select(poinit => poinit.ELRotateAroundPoint(around, angle))) { p.Add(polygonePoint); } return p; }
//------------------------------------------------------------------------------ // OffsetPolygon functions ... //------------------------------------------------------------------------------ internal static Polygon BuildArc(IntPoint pt, double a1, double a2, double r) { Int64 steps = Math.Max(6, (int)(Math.Sqrt(Math.Abs(r)) * Math.Abs(a2 - a1))); if (steps > 0x100) steps = 0x100; int n = (int)steps; Polygon result = new Polygon(n); double da = (a2 - a1) / (n - 1); double a = a1; for (int i = 0; i < n; ++i) { result.Add(new IntPoint(pt.X + Round(Math.Cos(a) * r), pt.Y + Round(Math.Sin(a) * r))); a += da; } return result; }
//------------------------------------------------------------------------------ private void BuildResult(Polygons polyg) { polyg.Clear(); polyg.Capacity = m_PolyOuts.Count; for (int i = 0; i < m_PolyOuts.Count; i++) { OutRec outRec = m_PolyOuts[i]; if (outRec.pts == null) continue; OutPt p = outRec.pts; int cnt = PointCount(p); if (cnt < 3) continue; Polygon pg = new Polygon(cnt); for (int j = 0; j < cnt; j++) { pg.Add(p.pt); p = p.prev; } polyg.Add(pg); } }
public PolyOffsetBuilder(Polygons pts, Polygons solution, double delta, JoinType jointype, double MiterLimit = 2, bool AutoFix = true) { //precondtion: solution != pts if (delta == 0) { solution = pts; return; } this.pts = pts; this.delta = delta; //AutoFix - fixes polygon orientation if necessary and removes //duplicate vertices. Can be set false when you're sure that polygon //orientation is correct and that there are no duplicate vertices. if (AutoFix) { int Len = pts.Count, botI = 0; while (botI < Len && pts[botI].Count == 0) botI++; if (botI == Len) return; //botPt: used to find the lowermost (in inverted Y-axis) & leftmost point //This point (on pts[botI]) must be on an outer polygon ring and if //its orientation is false (counterclockwise) then assume all polygons //need reversing ... IntPoint botPt = pts[botI][0]; for (int i = botI; i < Len; ++i) { if (pts[i].Count == 0) continue; if (UpdateBotPt(pts[i][0], ref botPt)) botI = i; for (int j = pts[i].Count -1; j > 0; j--) { if (PointsEqual(pts[i][j], pts[i][j -1])) pts[i].RemoveAt(j); else if (UpdateBotPt(pts[i][j], ref botPt)) botI = i; } } if (!Orientation(pts[botI])) ReversePolygons(pts); } if (MiterLimit <= 1) MiterLimit = 1; double RMin = 2.0 / (MiterLimit*MiterLimit); normals = new List<DoublePoint>(); double deltaSq = delta*delta; solution.Clear(); solution.Capacity = pts.Count; for (m_i = 0; m_i < pts.Count; m_i++) { int len = pts[m_i].Count; if (len > 1 && pts[m_i][0].X == pts[m_i][len - 1].X && pts[m_i][0].Y == pts[m_i][len - 1].Y) len--; if (len == 0 || (len < 3 && delta <= 0)) continue; else if (len == 1) { Polygon arc; arc = BuildArc(pts[m_i][len - 1], 0, 2 * Math.PI, delta); solution.Add(arc); continue; } //build normals ... normals.Clear(); normals.Capacity = len; for (int j = 0; j < len -1; ++j) normals.Add(GetUnitNormal(pts[m_i][j], pts[m_i][j+1])); normals.Add(GetUnitNormal(pts[m_i][len - 1], pts[m_i][0])); currentPoly = new Polygon(); m_k = len - 1; for (m_j = 0; m_j < len; ++m_j) { switch (jointype) { case JoinType.jtMiter: { m_R = 1 + (normals[m_j].X*normals[m_k].X + normals[m_j].Y*normals[m_k].Y); if (m_R >= RMin) DoMiter(); else DoSquare(MiterLimit); break; } case JoinType.jtRound: DoRound(); break; case JoinType.jtSquare: DoSquare(1); break; } m_k = m_j; } solution.Add(currentPoly); } //finally, clean up untidy corners ... Clipper clpr = new Clipper(); clpr.AddPolygons(solution, PolyType.ptSubject); if (delta > 0) { clpr.Execute(ClipType.ctUnion, solution, PolyFillType.pftPositive, PolyFillType.pftPositive); } else { IntRect r = clpr.GetBounds(); Polygon outer = new Polygon(4); outer.Add(new IntPoint(r.left - 10, r.bottom + 10)); outer.Add(new IntPoint(r.right + 10, r.bottom + 10)); outer.Add(new IntPoint(r.right + 10, r.top - 10)); outer.Add(new IntPoint(r.left - 10, r.top - 10)); clpr.AddPolygon(outer, PolyType.ptSubject); clpr.Execute(ClipType.ctUnion, solution, PolyFillType.pftNegative, PolyFillType.pftNegative); if (solution.Count > 0) { solution.RemoveAt(0); for (int i = 0; i < solution.Count; i++) solution[i].Reverse(); } } }
public Polygon ToPolygon(int offset = 0, float overrideWidth = -1) { var result = new Polygon(); result.Add( RStart + (overrideWidth > 0 ? overrideWidth : Width + offset)*Perpendicular - offset*Direction); result.Add( RStart - (overrideWidth > 0 ? overrideWidth : Width + offset)*Perpendicular - offset*Direction); result.Add( REnd - (overrideWidth > 0 ? overrideWidth : Width + offset)*Perpendicular + offset*Direction); result.Add( REnd + (overrideWidth > 0 ? overrideWidth : Width + offset)*Perpendicular + offset*Direction); return result; }
//--------------------------------------------------------------------- bool LoadFromFile(string filename, Polygons ppg, double scale = 0, int xOffset = 0, int yOffset = 0) { double scaling = Math.Pow(10, scale); ppg.Clear(); if (!File.Exists(filename)) return false; StreamReader sr = new StreamReader(filename); if (sr == null) return false; string line; if ((line = sr.ReadLine()) == null) return false; int polyCnt, vertCnt; if (!Int32.TryParse(line, out polyCnt) || polyCnt < 0) return false; ppg.Capacity = polyCnt; for (int i = 0; i < polyCnt; i++) { if ((line = sr.ReadLine()) == null) return false; if (!Int32.TryParse(line, out vertCnt) || vertCnt < 0) return false; Polygon pg = new Polygon(vertCnt); ppg.Add(pg); for (int j = 0; j < vertCnt; j++) { double x, y; if ((line = sr.ReadLine()) == null) return false; char[] delimiters = new char[] { ',', ' ' }; string [] vals = line.Split(delimiters); if (vals.Length < 2) return false; if (!double.TryParse(vals[0], out x)) return false; if (!double.TryParse(vals[1], out y)) if (vals.Length < 2 || !double.TryParse(vals[2], out y)) return false; x = x * scaling + xOffset; y = y * scaling + yOffset; pg.Add(new IntPoint((int)Math.Round(x), (int)Math.Round(y))); } } return true; }
public Polygon ToPolygon(int offset = 0) { var result = new Polygon(); var outRadius = (Radius + offset)/(float) Math.Cos(2*Math.PI/CircleLineSegmentN); result.Add(Center); var Side1 = Direction.Rotated(-Angle*0.5f); for (var i = 0; i <= CircleLineSegmentN; i++) { var cDirection = Side1.Rotated(i*Angle/CircleLineSegmentN).Normalized(); result.Add(new Vector2(Center.X + outRadius*cDirection.X, Center.Y + outRadius*cDirection.Y)); } return result; }
public static void GenerateHexLinePaths(Polygons in_outline, ref Polygons result, int lineSpacing, int infillExtendIntoPerimeter_um, double rotationDegrees, int layerIndex) { int extraRotationAngle = 0; if (in_outline.Count > 0) { Polygons outlines = in_outline.Offset(infillExtendIntoPerimeter_um); if (outlines.Count > 0) { int perIncrementOffset = (int)(lineSpacing * Math.Sqrt(3) / 2 + .5); PointMatrix matrix = new PointMatrix(-(rotationDegrees + extraRotationAngle)); // we are rotating the part so we rotate by the negative so the lines go the way we expect outlines.ApplyMatrix(matrix); Aabb boundary = new Aabb(outlines); boundary.min.X = ((boundary.min.X / lineSpacing) - 1) * lineSpacing; boundary.min.Y = ((boundary.min.Y / perIncrementOffset) - 2) * perIncrementOffset; boundary.max.X += lineSpacing; boundary.max.Y += perIncrementOffset; Polygons unclipedPatern = new Polygons(); foreach (IntPoint startPoint in StartPositionIterator(boundary, lineSpacing, layerIndex)) { Polygon attachedLine = new Polygon(); foreach (IntPoint center in IncrementPositionIterator(startPoint, boundary, lineSpacing, layerIndex)) { // what we are adding are the little plusses that define the points // | top // | // /\ center // left/ \ right // IntPoint left = center + new IntPoint(-lineSpacing / 2, -perIncrementOffset / 3); IntPoint right = center + new IntPoint(lineSpacing / 2, -perIncrementOffset / 3); IntPoint top = center + new IntPoint(0, perIncrementOffset * 2 / 3); switch (layerIndex % 3) { case 0: // left to right attachedLine.Add(left); attachedLine.Add(center); attachedLine.Add(center); attachedLine.Add(right); unclipedPatern.Add(new Polygon() { top, center }); break; case 1: // left to top attachedLine.Add(left); attachedLine.Add(center); attachedLine.Add(center); attachedLine.Add(top); unclipedPatern.Add(new Polygon() { center, right }); break; case 2: // top to right attachedLine.Add(top); attachedLine.Add(center); attachedLine.Add(center); attachedLine.Add(right); unclipedPatern.Add(new Polygon() { left, center }); break; } } if (attachedLine.Count > 0) { unclipedPatern.Add(attachedLine); } } PolyTree ret = new PolyTree(); Clipper clipper = new Clipper(); clipper.AddPaths(unclipedPatern, PolyType.ptSubject, false); clipper.AddPaths(outlines, PolyType.ptClip, true); clipper.Execute(ClipType.ctIntersection, ret, PolyFillType.pftPositive, PolyFillType.pftEvenOdd); Polygons newSegments = Clipper.OpenPathsFromPolyTree(ret); PointMatrix inversematrix = new PointMatrix((rotationDegrees + extraRotationAngle)); newSegments.ApplyMatrix(inversematrix); result.AddRange(newSegments); } } }