/Main/src/DynamicDataDisplay/Charts/Shapes/ViewportLine.cs

# · C# · 98 lines · 65 code · 19 blank · 14 comment · 0 complexity · 27ed6fb7f96d75990436af2bb0bf628e MD5 · raw file

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