/Main/src/DynamicDataDisplay/Charts/Shapes/ViewportLine.cs
C# | 98 lines | 65 code | 19 blank | 14 comment | 0 complexity | 27ed6fb7f96d75990436af2bb0bf628e MD5 | raw file
Possible License(s): CC-BY-SA-3.0
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Media;
6using System.Windows;
7
8namespace Microsoft.Research.DynamicDataDisplay.Charts.Shapes
9{
10 /// <summary>
11 /// Represents a line in viewport space, coming through given points.
12 /// </summary>
13 public class ViewportLine : ViewportShape
14 {
15 private LineGeometry lineGeometry = new LineGeometry();
16
17 /// <summary>
18 /// Initializes a new instance of the <see cref="ViewportLine"/> class.
19 /// </summary>
20 public ViewportLine() { }
21
22 #region Properties
23
24 #region Point1 property
25
26 /// <summary>
27 /// Gets or sets the first point. It is a DependencyProperty.
28 /// </summary>
29 /// <value>The point1.</value>
30 public Point Point1
31 {
32 get { return (Point)GetValue(Point1Property); }
33 set { SetValue(Point1Property, value); }
34 }
35
36 public static readonly DependencyProperty Point1Property = DependencyProperty.Register(
37 "Point1",
38 typeof(Point),
39 typeof(ViewportLine),
40 new FrameworkPropertyMetadata(new Point(0, 0), OnPointReplaced));
41
42 private static void OnPointReplaced(DependencyObject d, DependencyPropertyChangedEventArgs e)
43 {
44 ViewportLine owner = (ViewportLine)d;
45 owner.UpdateUIRepresentation();
46 }
47
48 #endregion
49
50 #region Point2 property
51
52 /// <summary>
53 /// Gets or sets the second point. It is a DependencyProperty.
54 /// </summary>
55 /// <value>The point2.</value>
56 public Point Point2
57 {
58 get { return (Point)GetValue(Point2Property); }
59 set { SetValue(Point2Property, value); }
60 }
61
62 public static readonly DependencyProperty Point2Property = DependencyProperty.Register(
63 "Point2",
64 typeof(Point),
65 typeof(ViewportLine),
66 new FrameworkPropertyMetadata(new Point(1, 1), OnPointReplaced));
67
68 #endregion
69
70 #endregion
71
72 protected override void UpdateUIRepresentationCore()
73 {
74 base.UpdateUIRepresentationCore();
75
76 Viewport2D viewport = Plotter.Viewport;
77 double deltaX = Point1.X - Point2.X;
78 double deltaY = Point1.Y - Point2.Y;
79 double m = deltaY / deltaX;
80 double b = Point1.Y - Point1.X * deltaY / deltaX;
81
82 Func<double, double> func = x => m * x + b;
83
84 double xMin = viewport.Visible.XMin;
85 double xMax = viewport.Visible.XMax;
86 Point p1 = new Point(xMin, func(xMin)).DataToScreen(viewport.Transform);
87 Point p2 = new Point(xMax, func(xMax)).DataToScreen(viewport.Transform);
88
89 lineGeometry.StartPoint = p1;
90 lineGeometry.EndPoint = p2;
91 }
92
93 protected override Geometry DefiningGeometry
94 {
95 get { return lineGeometry; }
96 }
97 }
98}