public override double Distance(PointF2D point) { VectorF2D vectorF2D = point - this._a; double num1 = VectorF2D.Cross(this.Direction, vectorF2D); double length = this.Length; double num2 = point.Distance(this._a); double num3 = point.Distance(this._b); double num4 = System.Math.Abs(num1 / length); if (this.IsSegment) { if (VectorF2D.Dot(vectorF2D, this.Direction) < 0.0 && num1 != 0.0) { num4 = this._a.Distance(point); } else if (num1 == 0.0 && (num2 >= length || num3 >= length)) { num4 = num2 <= num3?this._a.Distance(point) : this._b.Distance(point); } if (VectorF2D.Dot(point - this._b, this.Direction.Inverse) < 0.0 && num1 != 0.0) { num4 = this._b.Distance(point); } } return(num4); }
/// <summary> /// Returns the distance from the point to this line. /// </summary> /// <param name="point"></param> /// <returns></returns> public override double Distance(PointF2D point) { double distance = 0.0f; // get the second vector for the cross product. VectorF2D a_point = point - _a; // get the cross product. double cross = VectorF2D.Cross(this.Direction, a_point); // distances between a, b and point. double distance_a_b = this.Length; double distance_a = point.Distance(_a); double distance_b = point.Distance(_b); // calculate distance to line as if it were no segment. distance = System.Math.Abs(cross / distance_a_b); // if this line is a segment. if (this.IsSegment) { double dot1 = VectorF2D.Dot(a_point, this.Direction); if (dot1 < 0 && cross != 0) { distance = _a.Distance(point); } else if (cross == 0 && (distance_a >= distance_a_b || distance_b >= distance_a_b)) { // check if the point is between the points. if (distance_a > distance_b) { // if the distance to a is greater then the point is at the b-side. distance = _b.Distance(point); } else {// if the distance to b is greater then the point is at the a-side. distance = _a.Distance(point); } } VectorF2D b_point = point - _b; double dot2 = VectorF2D.Dot(b_point, this.Direction.Inverse); if (dot2 < 0 && cross != 0) { distance = _b.Distance(point); } } return(distance); }
public void Vector2DTest() { // create the test cases. VectorF2D a_b = new VectorF2D(1, 1); VectorF2D b_a = new VectorF2D(-1, -1); VectorF2D a_c = new VectorF2D(-1, 1); VectorF2D a_d = new VectorF2D(0, 1); VectorF2D a_e = new VectorF2D(-1, 0); VectorF2D a_f = new VectorF2D(0, 1); VectorF2D a_g = new VectorF2D(0, -1); // calculate the results double sqrt_2 = (double)System.Math.Sqrt(2); // check the sizes. Assert.AreEqual(a_b.Size, sqrt_2, string.Format("Size should be {0}!", sqrt_2)); Assert.AreEqual(b_a.Size, sqrt_2, string.Format("Size should be {0}!", sqrt_2)); // check the equality. Assert.IsTrue(a_b.Inverse == b_a, "The inverse of ab should be ba!"); // check the cross product. Assert.AreEqual(VectorF2D.Cross(a_b, b_a), 0, "Cross product of two parallel vectors should be 0!"); Assert.AreEqual(VectorF2D.Cross(a_b, a_c), 2, string.Format("Cross product of two perpendicular vectors should be maximized; in this case {0}!", 2)); Assert.AreEqual(VectorF2D.Cross(b_a, a_b), 0, "Cross product of two parallel vectors should be 0!"); Assert.AreEqual(VectorF2D.Cross(a_c, a_b), -2, string.Format("Cross product of two perpendicular vectors should be maximized; in this case {0}!", -2)); // check the dot product. Assert.AreEqual(VectorF2D.Dot(a_b, b_a), -2, string.Format("Cross product of two parallel vectors should be maximized (absolute value); in this case {0}!", -2)); Assert.AreEqual(VectorF2D.Dot(a_b, a_c), 0, string.Format("Cross product of two perpendicular vectors should be {0}!", 0)); Assert.AreEqual(VectorF2D.Dot(a_b, a_d), 1); Assert.AreEqual(VectorF2D.Dot(a_b, a_e), -1); Assert.AreEqual(VectorF2D.Dot(a_b, a_f), 1); Assert.AreEqual(VectorF2D.Dot(a_b, a_g), -1); Assert.AreEqual(VectorF2D.Dot(b_a, a_b), -2, string.Format("Cross product of two parallel vectors should be maximized; in this case {0}!", -2)); Assert.AreEqual(VectorF2D.Dot(a_c, a_b), 0, string.Format("Cross product of two perpendicular vectors should be {0}!", 0)); }