Microscopic Traffic Simulator
VisualCanvasRenderer.cs
Go to the documentation of this file.
1 using System.Windows;
2 using System.Windows.Media;
3 
4 namespace Microscopic_Traffic_Simulator.Renderers
5 {
9  abstract class VisualCanvasRenderer
10  {
14  protected DrawingVisual visual;
15 
19  private Point referenceViewPoint = new Point(0.0, 0.0);
20 
24  private double pixelsPerMeter = 1.0;
28  protected double PixelsPerMeter { get { return pixelsPerMeter; } }
29 
33  private Point? lastMouseLocationOfCanvasMove = null;
34 
40  protected VisualCanvasRenderer(DrawingVisual visual)
41  {
42  this.visual = visual;
43  }
44 
52  internal void ChangeZoom(double newPixelsPerMeter, Point zoomPoint)
53  {
54  //x/z1 + r1 = x/z2 + r2
55  //r2 = x/z1 + r1 - x/z2
56  Vector zoomPointAsVector = zoomPoint - new Point(0.0, 0.0);
57 
58  referenceViewPoint = zoomPointAsVector / pixelsPerMeter + referenceViewPoint - zoomPointAsVector / newPixelsPerMeter;
59 
60  pixelsPerMeter = newPixelsPerMeter;
61  Render(zoomPoint);
62  }
63 
69  protected Point TransformRealWorldPoint(Point point)
70  {
71  //compute transform
72  return (point - referenceViewPoint) * pixelsPerMeter + new Point(0.0, 0.0);
73  }
74 
80  protected Point TransformCanvasPoint(Point point)
81  {
82  return (point - new Point(0.0, 0.0)) / pixelsPerMeter + referenceViewPoint;
83  }
84 
90  internal void PushNewMouseLocationOfCanvasMove(Point mouseLocationOnCanvas)
91  {
92  if (lastMouseLocationOfCanvasMove != null)
93  {
94  Point currentMouseLocationOfCanvasMove = mouseLocationOnCanvas;
95  referenceViewPoint += (lastMouseLocationOfCanvasMove.Value - currentMouseLocationOfCanvasMove)
96  / pixelsPerMeter;
97  Render(mouseLocationOnCanvas);
98  }
99  lastMouseLocationOfCanvasMove = mouseLocationOnCanvas;
100  }
101 
105  internal void ResetCanvasMove()
106  {
107  lastMouseLocationOfCanvasMove = null;
108  }
109 
114  protected abstract void Render(Point currentMouseLocation);
115  }
116 }
Point TransformCanvasPoint(Point point)
Transform point on canvas to real-world point.
VisualCanvasRenderer(DrawingVisual visual)
Initialization of visual canvas renderer.
Point TransformRealWorldPoint(Point point)
Transforms real-world point to point on canvas.