/// <summary> /// 对多边形边进行整体偏移 /// </summary> /// <param name="offsetDist">正值为向外偏,负值为向内偏</param> /// <returns></returns> public Polygon2D CreateOffset(double offsetDist) { List <Line2D> extendLines = new List <Line2D>(); Edges.ForEach(x => extendLines.Add(x.Offset((new Vector2D(x.Direction.Y, -x.Direction.X)) * offsetDist))); List <Line2D> offsetLines = new List <Line2D>(); Vector2D firstPoint = extendLines.First().IntersectStraightLine(extendLines.Last()); List <Vector2D> offsetPoints = new List <Vector2D> { firstPoint }; for (int i = 0; i < extendLines.Count - 1; i++) { Vector2D intersect = extendLines[i].IntersectStraightLine(extendLines[i + 1]); if (intersect != null) { offsetPoints.Add(intersect); } } offsetPoints.Add(firstPoint); for (int i = 0; i < offsetPoints.Count - 1; i++) { offsetLines.Add(Line2D.Create(offsetPoints[i], offsetPoints[i + 1])); } return(new Polygon2D(offsetLines)); }
private void GetRectangle(Vector2D p0, Vector2D p1, Vector2D p2, Vector2D p3) { List <Vector2D> points = new List <Vector2D> { p0, p1, p2, p3 }; points.ForEach(x => x.X = Math.Floor(x.X * 1e9) / 1e9); points.ForEach(x => x.Y = Math.Floor(x.Y * 1e9) / 1e9); Vector2D first = points.OrderBy(x => x.X).ThenBy(y => y.Y).First(); List <Line2D> tryCurve = new List <Line2D>(); for (int i = 0; i < points.Count - 1; i++) { for (int j = i + 1; j < points.Count; j++) { tryCurve.Add(Line2D.Create(points[i], points[j])); } } tryCurve = tryCurve.OrderByDescending(x => x.Length).ToList(); tryCurve.RemoveAt(1); tryCurve.RemoveAt(0); tryCurve = tryCurve.MakeCounterclockwise(); Bottom = tryCurve[0]; Right = tryCurve[1]; Top = tryCurve[2]; Left = tryCurve[3]; }
/// <summary> /// 点到直线的距离 /// </summary> /// <param name="source"></param> /// <returns></returns> public double Distance(Vector2D source) { double space = 0; var a = Length; var b = source.Distance(Start); var c = source.Distance(End); if (c < Extension.SMALL_NUMBER || b < Extension.SMALL_NUMBER) { return(space); } if (a < Extension.SMALL_NUMBER) { space = b; return(space); } if (c * c >= a * a + b * b) { space = b; return(space); } if (b * b >= a * a + c * c) { space = c; return(space); } Vector2D v1 = new Vector2D(source.X, source.Y); Line2D l1 = Line2D.Create(new Vector2D(this.Start.X, this.Start.Y), new Vector2D(this.End.X, this.End.Y)); space = Line2D.Create(v1, v1.ProjectOn(l1)).Length; return(space); }
private List <Line2D> ReArragne(List <Line2D> lines) { var newLines = new List <Line2D>(); for (int i = 0; i < lines.Count; i++) { var copyLines = new List <Line2D>(lines); copyLines.RemoveAt(i); var points = copyLines.Select(t => lines[i].IntersectStraightLine(t)).Where(point => point != null).ToList(); newLines.Add(Line2D.Create(points[0], points[1])); } return(newLines); }