private void HandleLeftEdgeClick(Point mousePoint) { Point middle = State.ClickedEdge.EdgeMiddle; LineFunction pLine = State.ClickedEdge.function.GetPLine(middle); Point newPoint; if (State.ClickedEdge.ends.left.vPoint.X == State.ClickedEdge.ends.right.vPoint.X) { newPoint = new Point(middle.X - 20, middle.Y); } else if (State.ClickedEdge.ends.left.vPoint.Y == State.ClickedEdge.ends.right.vPoint.Y) { newPoint = new Point(middle.X, middle.Y - 20); } else { newPoint = FindLengthPoint(mousePoint, middle, pLine, 40).Value; } Vertex newVertex = new Vertex(newPoint.X, newPoint.Y); Edge left = new Edge((State.ClickedEdge.ends.left, newVertex)); Edge right = new Edge((newVertex, State.ClickedEdge.ends.right)); State.ClickedEdge.ends.left.edges = (State.ClickedEdge.ends.left.edges.left, left); State.ClickedEdge.ends.right.edges = (right, State.ClickedEdge.ends.right.edges.right); newVertex.edges = (left, right); State.CurrentPolygon.Edges.Remove(State.ClickedEdge); State.ClickedEdge = null; State.CurrentPolygon.Edges.Add(left); State.CurrentPolygon.Edges.Add(right); State.CurrentPolygon.Vertices.Add(newVertex); }
public Point?FindLengthPoint(Point p1, Point p2, LineFunction function, double length) { int x2 = p2.X; int y2 = p2.Y; int x1 = p1.X; int y1 = p1.Y; if (function.B == 0) { return(y2 < y1 ? new Point(x2, (int)(y2 + length)) : new Point(x2, (int)(y2 - length))); } double a = -function.A; double b = -function.C; double a2 = a * a; double b2 = b * b; double A = a2 + 1; double B = 2 * a * b - 2 * y2 * a - 2 * x2; double C = x2 * x2 + y2 * y2 - 2 * y2 * b + b2 - length * length; double delta = B * B - 4 * A * C; int xR1 = (int)Math.Round((-B - Math.Sqrt(delta)) / (2 * A)); int yR1 = (int)Math.Round(a * (-B - Math.Sqrt(delta)) / (2 * A) + b); int xR2 = (int)Math.Round((-B + Math.Sqrt(delta)) / (2 * A)); int yR2 = (int)Math.Round(a * (-B + Math.Sqrt(delta)) / (2 * A) + b); double dist1 = Math.Sqrt(Math.Pow(x1 - xR1, 2) + Math.Pow(y1 - yR1, 2)); double dist2 = Math.Sqrt(Math.Pow(x1 - xR2, 2) + Math.Pow(y1 - yR2, 2)); return(dist1 <= dist2 ? new Point(xR1, yR1) : new Point(xR2, yR2)); }
public LineFunction GetPLine(Point p) { LineFunction pLine = new LineFunction { A = -1 / A, B = 1 }; pLine.C = -(p.Y - pLine.a * p.X); return(pLine); }