private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { //remove zoom rectangle if (ZoomStarted) { ZoomStarted = false; try { canvas.Children.Remove(ZoomRect); } catch { } double x1 = 0; double y1 = 0; double x2 = 0; double y2 = 0; //zoom graph PrepareTransformations(); //make sure the current matrices are calculated switch (ZoomType) { case 0: return; case 1: //vertical scrolling window x1 = Math.Min(ZoomStart.X, ZoomEnd.X); y1 = 0; x2 = Math.Max(ZoomStart.X, ZoomEnd.X); y2 = canvas.Height; break; case 2: //horizontal scrolling window x1 = 0; y1 = Math.Min(ZoomStart.Y, ZoomEnd.Y); x2 = canvas.Width; y2 = Math.Max(ZoomStart.Y, ZoomEnd.Y); break; case 3: //standard square zooming rectangle x1 = Math.Min(ZoomStart.X, ZoomEnd.X); y1 = Math.Min(ZoomStart.Y, ZoomEnd.Y); x2 = Math.Max(ZoomStart.X, ZoomEnd.X); y2 = Math.Max(ZoomStart.Y, ZoomEnd.Y); break; default: return; } //calculate what these coordinates are in world coordinates Point w1 = DtoWMatrix.Transform(new Point(x1, y1)); Point w2 = DtoWMatrix.Transform(new Point(x2, y2)); //set the new world coordinates to the new values World.Xmin = w1.X; World.Xmax = w2.X; World.Ymin = w1.Y; World.Ymax = w2.Y; PrepareTransformations();//recalculate the transformation matrices //show graph Update(false); ZoomStarted = false; } }
// Transform a point from device to world coordinates. private Point DtoW(Point point) { return(DtoWMatrix.Transform(point)); }
// Transform a point from device to world coordinates. public static Point DtoW(Point point) { return(DtoWMatrix.Transform(point)); }
private void canvas_MouseMove(object sender, MouseEventArgs e) { ZoomEnd = e.GetPosition(canvas); ZoomType = 0; if (Mouse.LeftButton == MouseButtonState.Pressed) { ZoomRect.Width = Math.Abs(ZoomEnd.X - ZoomStart.X); ZoomRect.Height = Math.Abs(ZoomEnd.Y - ZoomStart.Y); if (ZoomRect.Width >= ZoomMargin && ZoomRect.Height < ZoomMargin) { ZoomType = 1; } else if (ZoomRect.Width < ZoomMargin && ZoomRect.Height >= ZoomMargin) { ZoomType = 2; } else if (ZoomRect.Width >= ZoomMargin && ZoomRect.Height >= ZoomMargin) { ZoomType = 3; } if (ZoomType != 0) { ZoomStarted = true; } } else { ZoomStart = Mouse.GetPosition(canvas); } //first remove the zoomrect from view try { canvas.Children.Remove(ZoomRect); } catch { } if (ZoomStarted) { DrawCursor(ZoomEnd, false); if (ZoomType == 1) { ZoomRect.Height = canvas.Height; Canvas.SetLeft(ZoomRect, Math.Min(ZoomEnd.X, ZoomStart.X)); Canvas.SetTop(ZoomRect, 0); canvas.Children.Add(ZoomRect); } else if (ZoomType == 2) { ZoomRect.Width = canvas.Width; Canvas.SetLeft(ZoomRect, 0); Canvas.SetTop(ZoomRect, Math.Min(ZoomEnd.Y, ZoomStart.Y)); canvas.Children.Add(ZoomRect); } else if (ZoomType == 3) { Canvas.SetLeft(ZoomRect, Math.Min(ZoomEnd.X, ZoomStart.X)); Canvas.SetTop(ZoomRect, Math.Min(ZoomEnd.Y, ZoomStart.Y)); canvas.Children.Add(ZoomRect); } } else { DrawCursor(ZoomEnd, true); } if (DragStarted) { if (Mouse.RightButton == MouseButtonState.Pressed) { DragEnd = Mouse.GetPosition(canvas); Point w1 = DtoWMatrix.Transform(DragStart); Point w2 = DtoWMatrix.Transform(DragEnd); Point w = new Point(); w.X = -w1.X + w2.X; w.Y = -w1.Y + w2.Y; World.Xmin = World.Xmin - w.X; World.Xmax = World.Xmax - w.X; World.Ymin = World.Ymin - w.Y; World.Ymax = World.Ymax - w.Y; //recalculate the transformation matrices PrepareTransformations(); //show graph Update(false); DragStart = DragEnd; } else { DragStarted = false; } } }
//Draw the cursor on the canvas private void DrawCursor(Point loc, bool draw) { Point q = loc; Mouse.OverrideCursor = System.Windows.Input.Cursors.Cross; try { canvas.Children.Remove(Xcur); canvas.Children.Remove(Ycur); } catch { } Xcur.X1 = 0; Xcur.Y1 = loc.Y; Xcur.X2 = canvas.Width; Xcur.Y2 = loc.Y; Ycur.X1 = loc.X; Ycur.Y1 = 0; Ycur.X2 = loc.X; Ycur.Y2 = canvas.Height; //calculate cursor position in world coordinates Point p = DtoWMatrix.Transform(q); //cursor coordinates text string sx = "", sy = ""; string log = ""; double xc = p.X; double yc = ConvertBack(-p.Y); if (Math.Abs(xc) < 0.1) { sx = xc.ToString("E3"); } else { sx = xc.ToString("N3"); } if (Math.Abs(yc) < 0.1) { sy = yc.ToString("E3"); } else { sy = yc.ToString("N3"); } if (_Logscale) { log = "(Log scale)"; } cursor_text.Text = $"X: {sx}, Y: {sy} {log}"; if (draw) { canvas.Children.Add(Xcur); canvas.Children.Add(Ycur); cursor_text.Visibility = Visibility.Visible; } else { cursor_text.Visibility = Visibility.Hidden; } }