private List <Element> GetRects() { Pts.Clear(); _rects.Clear(); if (Pt1.Equals(Pt2)) { int halfW = PointLineWidth / 2; int halfH = PointLineHeight / 2; for (int i = 0; i < DispersePointCount; i++) { RectangleF rect = new RectangleF(Pt1.X - halfW, Pt1.Y - halfH, PointLineWidth, PointLineHeight); _rects.Add(new RectLineElement(rect) { AutoChangeSize = false, BorderWidth = this.BorderWidth, ForeColor = this.ForeColor }); } } else { A = Pt2.Y - Pt1.Y; B = Pt1.X - Pt2.X; C = Pt2.X * Pt1.Y - Pt1.X * Pt2.Y; float offsetX = (Pt2.X - Pt1.X) / (DispersePointCount - 1); float offsetY = (Pt2.Y - Pt1.Y) / (DispersePointCount - 1); int halfW = PointLineWidth / 2; int halfH = PointLineHeight / 2; double aphla = Math.Atan((Pt2.Y - Pt1.Y) / (Pt2.X - Pt1.X)); int angle = (int)(aphla / Math.PI * 180); if ((Pt2.X - Pt1.X) < 0) { angle = angle + 180; } Debug.WriteLine($"aphla={aphla} angle={angle}"); for (int i = 0; i < DispersePointCount; i++) { float x = Pt1.X + i * offsetX; float y = B == 0 ? Pt1.Y + i * offsetY : -(A * x + C) / B; Pts.Add(new PointF(x, y)); RectangleF rect = new RectangleF(x - halfW, y - halfH, PointLineWidth, PointLineHeight); _rects.Add(new RectLineElement(rect, angle) { AutoChangeSize = false, BorderWidth = this.BorderWidth, ForeColor = this.ForeColor }); } } return(_rects); }
public float GetDistance(float x, float y) { float dis = 0f; if (Pt1.Equals(Pt2)) { dis = GetLength(x, y, Pt1.X, Pt1.Y); } else { float a = Pt2.Y - Pt1.Y; float b = Pt1.X - Pt2.X; float c = Pt2.X * Pt1.Y - Pt1.X * Pt2.Y; dis = (float)(Math.Abs(a * x + b * y + c) / Math.Sqrt(a * a + b * b)); } return(dis); }
public override bool Equals(Shape other) { if (other == null) { return(false); } if (other is LineSegment) { var lineSeg = other as LineSegment; bool equalPt1 = Pt1.Equals(lineSeg.Pt1); bool equalPt2 = Pt2.Equals(lineSeg.Pt2); if (!(equalPt1 || equalPt2)) { return(false); } } return(base.Equals(other)); }