public static double distFromPointToLine(Point x, System.Windows.Point startPoint, System.Windows.Point finishPoint) { Point a = new Point(startPoint.X, startPoint.Y); Point b = new Point(finishPoint.X, finishPoint.Y); double ret = distFromPointToPoint(x, a); double eps = 1e-4; double l = 0; double r = distFromPointToPoint(a, b); Point directionVector = new Point((b.X - a.X) / r, (b.Y - a.Y) / r); while (Math.Abs(r - l) > eps) { double m1 = l + (r - l) / 3; double m2 = r - (r - l) / 3; Point pointAtM1 = new Point(a.X + directionVector.X * m1, a.Y + directionVector.Y * m1); Point pointAtM2 = new Point(a.X + directionVector.X * m2, a.Y + directionVector.Y * m2); if (distFromPointToPoint(x, pointAtM1) < distFromPointToPoint(x, pointAtM2)) { ret = Math.Min(ret, distFromPointToPoint(x, pointAtM1)); r = m2; } else { ret = Math.Min(ret, distFromPointToPoint(x, pointAtM2)); l = m1; } } return(ret); }
/// <summary> /// Event handler for mouse click on canvas. /// </summary> /// <param name="sender">The <see cref="Canvas"/> that the action is for</param> /// <param name="e">Arguments that the implementor of this event may find useful.</param> private void MyPoly_MouseDown(object sender, MouseButtonEventArgs e) { if (currentChosenBrokenLineId > -1 && currentMode == Mode.Moving) { for (var i = DrawingPanel.Children.Count - 1; i >= 0; i--) { selectedPolyline = DrawingPanel.Children[i] as Shape; clickV = e.GetPosition(selectedPolyline); if (Util.PointIsInBrokenLine(new Point(clickV.X, clickV.Y), selectedPolyline as Polyline)) { currentChosenBrokenLineId = i; dragging = true; return; } } } }
/// <summary> /// Function to move BrokenLine on canvas using arrow keys. /// </summary> /// <param name="sender">The <see cref="Canvas"/> that the action is for</param> /// <param name="e">Arguments that the implementor of this event may find useful.</param> private void MoveBrokenLineWithKeys(object sender, KeyEventArgs e) { try { if (Keyboard.IsKeyDown(Key.Escape)) { ClearExpectedBrokenLine(); } else { if (currentMode == Mode.Moving && currentChosenBrokenLineId > -1 && DrawingPanel.Children.Count > 0) { var newLoc = new System.Windows.Point(0, 0); if (Keyboard.IsKeyDown(Key.Up) && Keyboard.IsKeyDown(Key.Right)) { newLoc.Y -= 5; newLoc.X += 5; } else if (Keyboard.IsKeyDown(Key.Up) && Keyboard.IsKeyDown(Key.Left)) { newLoc.Y -= 5; newLoc.X -= 5; } else if (Keyboard.IsKeyDown(Key.Down) && Keyboard.IsKeyDown(Key.Right)) { newLoc.Y += 5; newLoc.X += 5; } else if (Keyboard.IsKeyDown(Key.Down) && Keyboard.IsKeyDown(Key.Left)) { newLoc.Y += 5; newLoc.X -= 5; } else if (Keyboard.IsKeyDown(Key.Up)) { newLoc.Y -= 5; } else if (Keyboard.IsKeyDown(Key.Right)) { newLoc.X += 5; } else if (Keyboard.IsKeyDown(Key.Down)) { newLoc.Y += 5; } else if (Keyboard.IsKeyDown(Key.Left)) { newLoc.X -= 5; } if (!((DrawingPanel.Children[currentChosenBrokenLineId] as Shape) is Polyline p)) { throw new InvalidDataException("can't move null shape"); } Canvas.SetLeft(p, Canvas.GetLeft(p) + newLoc.X); Canvas.SetTop(p, Canvas.GetTop(p) + newLoc.Y); } } } catch (Exception exc) { FormBl.MessageBoxFatal(exc.ToString()); } }
public void TestOnSegment(System.Windows.Point p, System.Windows.Point q, System.Windows.Point r) { Assert.True(Util.OnSegment(p, q, r)); }