private static LineEquationNumbers GetLineEquationNumbers(Line line) { LineEquationNumbers ln = new LineEquationNumbers(); ln.k = (line.Y2 - line.Y1) / (line.X2 - line.X1); ln.b = line.Y1 - ln.k * line.X1; return ln; }
private static Point GetIntersectionOfTwoLines(LineEquationNumbers len1, LineEquationNumbers len2) { Point p = new Point(); if (len1.k == len2.k) { return p; } p.X = (len2.b - len1.b) / (len1.k - len2.k); p.Y = len1.k * p.X + len1.b; return p; }
public static Point GetIntersectionOfLineAndPerpendicularFromPoint(Line line, Point point) { LineEquationNumbers ln1 = GetLineEquationNumbers(line); LineEquationNumbers ln2 = new LineEquationNumbers(); //Find orthogonal line //y = (-1/k1)*x + y0 + (1/k1)*x0 ln2.k = -1 / ln1.k; ln2.b = point.Y + (1 / ln1.k) * point.X; //Find intersectionof 2 lines Point result = new Point(); result.X = (ln2.b - ln1.b) / (ln1.k - ln2.k); result.Y = ln1.k * result.X + ln1.b; return result; }
public static LineEquationNumbers GetPerpendicularLine(Line line) { LineEquationNumbers ln1 = GetLineEquationNumbers(line); LineEquationNumbers ln2 = new LineEquationNumbers(); //Find orthogonal line //y = (-1/k1)*x + y0 + (1/k1)*x0 ln2.k = -1 / ln1.k; ln2.b = 0; return ln2; }
public static List<Point> GetIntersectionOf(LineEquationNumbers len, Polygon polygon) { List<Point> list = new List<Point>(); for (int index = 0; index < polygon.Points.Count; index++) { int index2 = (index + 1) % polygon.Points.Count; Line line1 = new Line(); line1.X1 = polygon.Points[index].X; line1.X2 = polygon.Points[index2].X; line1.Y1 = polygon.Points[index].Y; line1.Y2 = polygon.Points[index2].Y; LineEquationNumbers len1 = GetLineEquationNumbers(line1); Point p = GetIntersectionOfTwoLines(len, len1); if (p.X >= Math.Min(line1.X1, line1.X2) && p.X <= Math.Max(line1.X1, line1.X2) && p.Y >= Math.Min(line1.Y1, line1.Y2) && p.Y <= Math.Max(line1.Y1, line1.Y2)) { list.Add(p); } } return list; }