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; } }
private void CreateTransformationMatrix(double wxmin, double wxmax, double wymin, double wymax, double dxmin, double dxmax, double dymin, double dymax) { // Make WtoD. WtoDMatrix = Matrix.Identity; WtoDMatrix.Translate(-wxmin, -wymin); double xscale = (dxmax - dxmin) / (wxmax - wxmin); double yscale = (dymax - dymin) / (wymax - wymin); WtoDMatrix.Scale(xscale, yscale); WtoDMatrix.Translate(dxmin, dymin); // Make DtoW. DtoWMatrix = WtoDMatrix; DtoWMatrix.Invert(); }
private void PrepareTransformations(double minWx, double maxWx, double minWy, double maxWy, double minDx, double maxDx, double maxDy, double minDy) { // Make WtoD matrix WtoDMatrix = Matrix.Identity; WtoDMatrix.Translate(-minWx, -minWy); double scaleX = (maxDx - minDx) / (maxWx - minWx); double scaleY = (maxDy - minDy) / (maxWy - minWy); WtoDMatrix.Scale(scaleX, scaleY); WtoDMatrix.Translate(minDx, minDy); // Make DtoW matrix DtoWMatrix = WtoDMatrix; DtoWMatrix.Invert(); }
public static void PrepareTransformations( double wxmin, double wxmax, double wymin, double wymax, double dxmin, double dxmax, double dymax, double dymin) { // Make WtoD. WtoDMatrix = Matrix.Identity; WtoDMatrix.Translate(-wxmin, -wymin); double xscale = (dxmax - dxmin) / (wxmax - wxmin); double yscale = (dymax - dymin) / (wymax - wymin); WtoDMatrix.Scale(xscale, yscale); WtoDMatrix.Translate(dxmin, dymin); // Make DtoW. DtoWMatrix = WtoDMatrix; DtoWMatrix.Invert(); }
// Prepare values for perform transformations. private void PrepareTransformations() { // Make WtoD. WtoDMatrix = Matrix.Identity; WtoDMatrix.Translate(-World.Xmin, -World.Ymin); try //make sure we don't divide by zero { double xscale = Device.Xmax / (World.Xmax - World.Xmin); double yscale = Device.Ymax / (World.Ymax - World.Ymin); WtoDMatrix.Scale(xscale, yscale); WtoDMatrix.Translate(Device.Xmin, Device.Ymin); } catch { } // Make DtoW. DtoWMatrix = WtoDMatrix; try { DtoWMatrix.Invert(); } catch { } }
// 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 DrawGraph() { //} //txtStartDate.Invalidate(); int wid = GrphPlace.ClientSize.Width; int hgt = GrphPlace.ClientSize.Height; GraphBm = new Bitmap(wid, hgt); using (Graphics gr = Graphics.FromImage(GraphBm)) { gr.SmoothingMode = SmoothingMode.AntiAlias; gr.Clear(Color.White); // Scale the data to fit. int num_points = Tmprr.Count; float min_price = (float)Tmprr.Min(data => data.Price); float max_price = (float)Tmprr.Max(data => data.Price); const int margin = 10; WtoDMatrix = MappingMatrix( 0, num_points - 1, min_price, max_price, margin, wid - margin, margin, hgt - margin); gr.Transform = WtoDMatrix; DtoWMatrix = WtoDMatrix.Clone(); DtoWMatrix.Invert(); // Draw the graph. using (Pen pen = new Pen(Color.Black, 0)) { // Draw tic marks. PointF[] pts = { new PointF(10, 10) }; DtoWMatrix.TransformVectors(pts); float dy = pts[0].Y; float dx = pts[0].X; for (int x = 0; x < Tmprr.Count; x++) { gr.DrawLine(pen, x, min_price, x, min_price + dy); } for (int y = (int)min_price; y <= (int)max_price; y++) { gr.DrawLine(pen, 0, y, dx, y); } // Get a small distance in world coordinates. dx = Math.Abs(dx / 5); dy = Math.Abs(dy / 5); // Draw the data. PointF[] points = new PointF[num_points]; for (int i = 0; i < num_points; i++) { float price = (float)Tmprr[i].Price; points[i] = new PointF(i, price); gr.FillRectangle(Brushes.Red, i - dx, price - dy, 2 * dx, 2 * dy); } pen.Color = Color.Blue; gr.DrawLines(pen, points); } } // Display the result. GrphPlace.Image = GraphBm; }
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; } }