annotate SilverlightGlimpse/SilverFlow.Controls/Controllers/InertiaController.cs @ 63:536498832a79

Latest version before changing bindings to Listbox
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sun, 22 Apr 2012 13:33:42 +0100
parents
children
rev   line source
63
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
1 using System;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
2 using System.Collections.Generic;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
3 using System.Windows;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
4 using System.Windows.Media.Animation;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
5 using SilverFlow.Controls.Extensions;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
6 using SilverFlow.Geometry;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
7
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
8 namespace SilverFlow.Controls.Controllers
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
9 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
10 /// <summary>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
11 /// Calculates inertial motion of a dragged element.
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
12 /// </summary>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
13 public class InertiaController
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
14 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
15 private const double SCREEN_DPI = 96;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
16 private const double INCH = 0.0254;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
17 private const double GRAVITATIONAL_ACCELERATION = 9.81;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
18 private const double COEFFICIENT_OF_SLIDING_FRICTION = 0.015;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
19 private const double MIN_DELTA = 0.001;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
20 private const double DELAY_BEFORE_MOUSE_UP_IN_MILLISECONDS = 100;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
21 private const int MAX_LAST_POINTS_TO_COUNT = 4;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
22
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
23 private double pixelsPerMeter = SCREEN_DPI / INCH;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
24
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
25 private Queue<Trail> mouseTrails = new Queue<Trail>();
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
26
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
27 /// <summary>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
28 /// An element starts its motion.
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
29 /// </summary>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
30 /// <param name="point">The starting point of the motion.</param>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
31 public void StartMotion(Point point)
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
32 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
33 mouseTrails.Clear();
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
34 mouseTrails.Enqueue(new Trail(point, DateTime.Now));
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
35 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
36
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
37 /// <summary>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
38 /// Saves the last position of the moved element.
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
39 /// </summary>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
40 /// <param name="point">Current position.</param>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
41 public void MoveToPoint(Point point)
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
42 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
43 while (mouseTrails.Count >= MAX_LAST_POINTS_TO_COUNT)
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
44 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
45 // Remove all points except the last ones
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
46 mouseTrails.Dequeue();
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
47 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
48
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
49 mouseTrails.Enqueue(new Trail(point, DateTime.Now));
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
50 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
51
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
52 /// <summary>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
53 /// Gets inertial motion parameters: distance and duration.
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
54 /// </summary>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
55 /// <param name="hostBounds">The host bounds.</param>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
56 /// <param name="windowBounds">The window bounds.</param>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
57 /// <returns>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
58 /// The inertial motion parameters: distance and duration.
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
59 /// </returns>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
60 public InertialMotion GetInertialMotionParameters(Rect hostBounds, Rect windowBounds)
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
61 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
62 if (mouseTrails.Count < 2) return null;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
63
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
64 var mouseTrailsArray = mouseTrails.ToArray();
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
65
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
66 Point startPosition = mouseTrailsArray[0].Position;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
67 DateTime startTime = mouseTrailsArray[0].Timestamp;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
68 Point endPosition = mouseTrailsArray[mouseTrails.Count - 1].Position;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
69 DateTime endTime = mouseTrailsArray[mouseTrails.Count - 1].Timestamp;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
70
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
71 double timeBetweenNowAndLastMove = (DateTime.Now - endTime).TotalMilliseconds;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
72 Vector2 vector = new Vector2(startPosition, endPosition);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
73
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
74 if (timeBetweenNowAndLastMove < DELAY_BEFORE_MOUSE_UP_IN_MILLISECONDS && !vector.IsZero)
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
75 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
76 double time = (endTime - startTime).TotalSeconds;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
77 time = (time == 0) ? 0.001 : time;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
78
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
79 double distance = vector.Length / pixelsPerMeter;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
80
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
81 double intialVelocity = distance / time;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
82
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
83 double expectedDistance = ((intialVelocity * intialVelocity) / (2 * COEFFICIENT_OF_SLIDING_FRICTION * GRAVITATIONAL_ACCELERATION));
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
84 double expectedTime = (2 * expectedDistance) / intialVelocity;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
85
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
86 double shiftX = Math.Round(vector.LengthX * expectedDistance / distance);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
87 double shiftY = Math.Round(vector.LengthY * expectedDistance / distance);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
88
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
89 // New Inertial Motion Vector
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
90 Vector2 imVector = new Vector2(endPosition, shiftX, shiftY).Round();
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
91 double expectedLength = imVector.Length;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
92
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
93 Rect bounds = hostBounds.Add(-windowBounds.Width, -windowBounds.Height);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
94
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
95 if (bounds.Contains(endPosition))
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
96 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
97 imVector = EnsureEndPointInBounds(imVector, bounds).Round();
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
98 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
99 else if (hostBounds.Contains(endPosition))
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
100 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
101 imVector = EnsureEndPointInBounds(imVector, hostBounds).Round();
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
102 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
103
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
104 // Reduce expected time if the Inertial Motion Vector was truncated by the bounds
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
105 double realTime = (expectedLength == 0) ? 0 : (expectedTime * imVector.Length / expectedLength);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
106
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
107 var motion = new InertialMotion()
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
108 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
109 Seconds = realTime,
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
110 EndPosition = imVector.End,
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
111 EasingFunction = new CubicEase()
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
112 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
113 EasingMode = EasingMode.EaseOut
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
114 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
115 };
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
116
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
117 return motion;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
118 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
119
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
120 return null;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
121 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
122
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
123 /// <summary>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
124 /// Ensures the end point is in bounds.
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
125 /// </summary>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
126 /// <param name="vector">The vector to check its ending point.</param>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
127 /// <param name="bounds">The bounds.</param>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
128 /// <returns>A vector with the ending point within specified bounds.</returns>
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
129 private Vector2 EnsureEndPointInBounds(Vector2 vector, Rect bounds)
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
130 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
131 if (!vector.IsZero && !bounds.Contains(vector.End))
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
132 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
133 if (vector.IsVertical)
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
134 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
135 vector.End = vector.End.EnsureInVerticalBounds(bounds);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
136 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
137 else if (vector.IsHorizontal)
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
138 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
139 vector.End = vector.End.EnsureInHorizontalBounds(bounds);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
140 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
141 else
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
142 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
143 double k = (vector.LengthY) / (vector.LengthX);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
144 Point point = vector.End;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
145
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
146 if (vector.End.X < bounds.Left || vector.End.X > bounds.Right)
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
147 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
148 point = point.EnsureInHorizontalBounds(bounds);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
149 point.Y = (k * (point.X - vector.Start.X)) + vector.Start.Y;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
150 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
151
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
152 if (point.Y < bounds.Top || point.Y > bounds.Bottom)
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
153 {
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
154 point = point.EnsureInVerticalBounds(bounds);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
155 point.X = ((point.Y - vector.Start.Y) / k) + vector.Start.X;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
156 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
157
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
158 vector.End = point.EnsureInBounds(bounds);
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
159 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
160 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
161
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
162 return vector;
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
163 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
164 }
536498832a79 Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
165 }