/// <summary> /// Transforms the given the coordinates to a coordinate system defined inside this rectangle. /// </summary> /// <param name="width">Width.</param> /// <param name="height">Height.</param> /// <param name="reverseX">If set to <c>true</c> reverse x.</param> /// <param name="reverseY">If set to <c>true</c> reverse y.</param> /// <param name="point">Point.</param> /// <param name="transformed">Transformed.</param> public void TransformTo(double width, double height, bool reverseX, bool reverseY, PointF2D point, double[] transformed) { if (transformed == null) { throw new ArgumentNullException(); } if (transformed.Length != 2) { throw new ArgumentException("Tranformed array needs to be of length 2."); } PointF2D reference = _bottomLeft; VectorF2D vectorX = _vectorX; VectorF2D vectorY = _vectorY; if (reverseX && !reverseY) { reference = this.BottomRight; vectorX = _vectorX * -1; } else if (!reverseX && reverseY) { reference = this.TopLeft; vectorY = _vectorY * -1; } else if (reverseX && reverseY) { reference = this.TopRight; vectorX = _vectorX * -1; vectorY = _vectorY * -1; } LineF2D xLine = new LineF2D(point, point + vectorX, false); PointF2D yIntersection = xLine.Intersection(new LineF2D(reference, reference + vectorY)) as PointF2D; VectorF2D yIntersectionVector = (yIntersection - reference); double yFactor = yIntersectionVector.Size / vectorY.Size; if (!yIntersectionVector.CompareNormalized(vectorY, 0.0001)) { yFactor = -yFactor; } LineF2D yLine = new LineF2D(point, point + vectorY); PointF2D xIntersection = yLine.Intersection(new LineF2D(reference, reference + vectorX)) as PointF2D; VectorF2D xIntersectionVector = (xIntersection - reference); double xFactor = xIntersectionVector.Size / vectorX.Size; if (!xIntersectionVector.CompareNormalized(vectorX, 0.0001)) { xFactor = -xFactor; } transformed [0] = xFactor * width; transformed [1] = yFactor * height; }
public double[][] TransformTo(double width, double height, bool reverseX, bool reverseY, double[] x, double[] y) { PointF2D a1 = this._bottomLeft; VectorF2D vectorF2D1 = this._vectorX; VectorF2D vectorF2D2 = this._vectorY; if (reverseX && !reverseY) { a1 = this.BottomRight; vectorF2D1 = this._vectorX * -1.0; } else if (!reverseX & reverseY) { a1 = this.TopLeft; vectorF2D2 = this._vectorY * -1.0; } else if (reverseX & reverseY) { a1 = this.TopRight; vectorF2D1 = this._vectorX * -1.0; vectorF2D2 = this._vectorY * -1.0; } double[][] numArray = new double[x.Length][]; for (int index = 0; index < x.Length; ++index) { PointF2D a2 = new PointF2D(x[index], y[index]); VectorF2D vectorF2D3 = vectorF2D1; PointF2D b1 = a2 + vectorF2D3; int num1 = 0; VectorF2D vectorF2D4 = (new LineF2D(a2, b1, num1 != 0).Intersection(new LineF2D(a1, a1 + vectorF2D2)) as PointF2D) - a1; double num2 = vectorF2D4.Size / vectorF2D2.Size; VectorF2D other1 = vectorF2D2; double epsilon1 = 0.0001; if (!vectorF2D4.CompareNormalized(other1, epsilon1)) { num2 = -num2; } VectorF2D vectorF2D5 = vectorF2D2; PointF2D b2 = a2 + vectorF2D5; VectorF2D vectorF2D6 = (new LineF2D(a2, b2).Intersection(new LineF2D(a1, a1 + vectorF2D1)) as PointF2D) - a1; double num3 = vectorF2D6.Size / vectorF2D1.Size; VectorF2D other2 = vectorF2D1; double epsilon2 = 0.0001; if (!vectorF2D6.CompareNormalized(other2, epsilon2)) { num3 = -num3; } numArray[index] = new double[2] { num3 *width, num2 *height }; } return(numArray); }
public void TransformTo(double width, double height, bool reverseX, bool reverseY, PointF2D point, double[] transformed) { if (transformed == null) { throw new ArgumentNullException(); } if (transformed.Length != 2) { throw new ArgumentException("Tranformed array needs to be of length 2."); } PointF2D a = this._bottomLeft; VectorF2D vectorF2D1 = this._vectorX; VectorF2D vectorF2D2 = this._vectorY; if (reverseX && !reverseY) { a = this.BottomRight; vectorF2D1 = this._vectorX * -1.0; } else if (!reverseX & reverseY) { a = this.TopLeft; vectorF2D2 = this._vectorY * -1.0; } else if (reverseX & reverseY) { a = this.TopRight; vectorF2D1 = this._vectorX * -1.0; vectorF2D2 = this._vectorY * -1.0; } VectorF2D vectorF2D3 = (new LineF2D(point, point + vectorF2D1, false).Intersection(new LineF2D(a, a + vectorF2D2)) as PointF2D) - a; double num1 = vectorF2D3.Size / vectorF2D2.Size; VectorF2D other1 = vectorF2D2; double epsilon1 = 0.0001; if (!vectorF2D3.CompareNormalized(other1, epsilon1)) { num1 = -num1; } VectorF2D vectorF2D4 = (new LineF2D(point, point + vectorF2D2).Intersection(new LineF2D(a, a + vectorF2D1)) as PointF2D) - a; double num2 = vectorF2D4.Size / vectorF2D1.Size; VectorF2D other2 = vectorF2D1; double epsilon2 = 0.0001; if (!vectorF2D4.CompareNormalized(other2, epsilon2)) { num2 = -num2; } transformed[0] = num2 * width; transformed[1] = num1 * height; }
/// <summary> /// Transforms the given the coordinates to a coordinate system defined inside this rectangle. /// </summary> /// <param name="width">The width of the rectangle in the coordinate system of the given coordinates.</param> /// <param name="height">The height of the rectangle in the coordinate system of the given coordinates.</param> /// <param name="reverseX">Assumes that the origin of the x-axis is on the top of this rectangle if false.</param> /// <param name="reverseY">Assumes that the origin of the y-axis is on the right of this rectangle if false.</param> /// <param name="x">The x-coordinate to transform.</param> /// <param name="y">The y-coordinate to transform.</param> public double[][] TransformTo(double width, double height, bool reverseX, bool reverseY, double[] x, double[] y) { PointF2D reference = _bottomLeft; VectorF2D vectorX = _vectorX; VectorF2D vectorY = _vectorY; if (reverseX && !reverseY) { reference = this.BottomRight; vectorX = _vectorX * -1; } else if (!reverseX && reverseY) { reference = this.TopLeft; vectorY = _vectorY * -1; } else if (reverseX && reverseY) { reference = this.TopRight; vectorX = _vectorX * -1; vectorY = _vectorY * -1; } double[][] transformed = new double[x.Length][]; for (int idx = 0; idx < x.Length; idx++) { PointF2D point = new PointF2D(x [idx], y [idx]); LineF2D xLine = new LineF2D(point, point + vectorX, false); PointF2D yIntersection = xLine.Intersection(new LineF2D(reference, reference + vectorY)) as PointF2D; VectorF2D yIntersectionVector = (yIntersection - reference); double yFactor = yIntersectionVector.Size / vectorY.Size; if (!yIntersectionVector.CompareNormalized(vectorY, 0.0001)) { yFactor = -yFactor; } LineF2D yLine = new LineF2D(point, point + vectorY); PointF2D xIntersection = yLine.Intersection(new LineF2D(reference, reference + vectorX)) as PointF2D; VectorF2D xIntersectionVector = (xIntersection - reference); double xFactor = xIntersectionVector.Size / vectorX.Size; if (!xIntersectionVector.CompareNormalized(vectorX, 0.0001)) { xFactor = -xFactor; } transformed[idx] = new double[] { xFactor *width, yFactor *height }; } return(transformed); }
/// <summary> /// Transforms the given the coordinates to a coordinate system defined inside this rectangle. /// </summary> /// <param name="width">The width of the rectangle in the coordinate system of the given coordinates.</param> /// <param name="height">The height of the rectangle in the coordinate system of the given coordinates.</param> /// <param name="reverseX">Assumes that the origin of the x-axis is on the top of this rectangle if false.</param> /// <param name="reverseY">Assumes that the origin of the y-axis is on the right of this rectangle if false.</param> /// <param name="point">The coordinate to transform.</param> public double[] TransformTo(double width, double height, bool reverseX, bool reverseY, PointF2D point) { PointF2D reference = _bottomLeft; VectorF2D vectorX = _vectorX; VectorF2D vectorY = _vectorY; if (reverseX && !reverseY) { reference = this.BottomRight; vectorX = _vectorX * -1; } else if (!reverseX && reverseY) { reference = this.TopLeft; vectorY = _vectorY * -1; } else if (reverseX && reverseY) { reference = this.TopRight; vectorX = _vectorX * -1; vectorY = _vectorY * -1; } LineF2D xLine = new LineF2D(point, point + vectorX, false); PointF2D yIntersection = xLine.Intersection(new LineF2D(reference, reference + vectorY)) as PointF2D; VectorF2D yIntersectionVector = (yIntersection - reference); double yFactor = yIntersectionVector.Size / vectorY.Size; if (!yIntersectionVector.CompareNormalized(vectorY, 0.0001)) { yFactor = -yFactor; } LineF2D yLine = new LineF2D(point, point + vectorY); PointF2D xIntersection = yLine.Intersection(new LineF2D(reference, reference + vectorX)) as PointF2D; VectorF2D xIntersectionVector = (xIntersection - reference); double xFactor = xIntersectionVector.Size / vectorX.Size; if (!xIntersectionVector.CompareNormalized(vectorX, 0.0001)) { xFactor = -xFactor; } return(new double[] { xFactor *width, yFactor *height }); }
public double[] TransformTo(double width, double height, bool reverseX, bool reverseY, PointF2D point) { PointF2D a = this._bottomLeft; VectorF2D vectorF2D1 = this._vectorX; VectorF2D vectorF2D2 = this._vectorY; if (reverseX && !reverseY) { a = this.BottomRight; vectorF2D1 = this._vectorX * -1.0; } else if (!reverseX & reverseY) { a = this.TopLeft; vectorF2D2 = this._vectorY * -1.0; } else if (reverseX & reverseY) { a = this.TopRight; vectorF2D1 = this._vectorX * -1.0; vectorF2D2 = this._vectorY * -1.0; } VectorF2D vectorF2D3 = (new LineF2D(point, point + vectorF2D1, false).Intersection(new LineF2D(a, a + vectorF2D2)) as PointF2D) - a; double num1 = vectorF2D3.Size / vectorF2D2.Size; VectorF2D other1 = vectorF2D2; double epsilon1 = 0.0001; if (!vectorF2D3.CompareNormalized(other1, epsilon1)) { num1 = -num1; } VectorF2D vectorF2D4 = (new LineF2D(point, point + vectorF2D2).Intersection(new LineF2D(a, a + vectorF2D1)) as PointF2D) - a; double num2 = vectorF2D4.Size / vectorF2D1.Size; VectorF2D other2 = vectorF2D1; double epsilon2 = 0.0001; if (!vectorF2D4.CompareNormalized(other2, epsilon2)) { num2 = -num2; } return(new double[2] { num2 *width, num1 *height }); }
/// <summary> /// Calculates and returns the line intersection. /// </summary> /// <param name="line"></param> /// <param name="doSegment"></param> /// <returns></returns> public PrimitiveF2D Intersection(LineF2D line, bool doSegment) { if (line == this) { // if the lines equal, the full lines intersect. return(line); } else if (line.A == this.A && line.B == this.B && line.C == this.C) { // if the lines equal in direction and position; return the smallest segment. KeyValuePair <double, PointF2D> point1 = new KeyValuePair <double, PointF2D>( 0, this.Point1); KeyValuePair <double, PointF2D> point2 = new KeyValuePair <double, PointF2D>( this.Point1.Distance(this.Point2), this.Point2); // sort. KeyValuePair <double, PointF2D> temp; if (point2.Key < point1.Key) { // point2 smaller than point1. temp = point2; point2 = point1; point1 = temp; } KeyValuePair <double, PointF2D> point = new KeyValuePair <double, PointF2D>( this.Point1.Distance(line.Point1), line.Point1); if (point.Key < point2.Key) // sort. { // point smaller than point2. temp = point; point = point2; point2 = temp; } if (point2.Key < point1.Key) { // point2 smaller than point1. temp = point2; point2 = point1; point1 = temp; } point = new KeyValuePair <double, PointF2D>( this.Point1.Distance(line.Point2), line.Point2); if (point.Key < point2.Key) // sort. { // point smaller than point2. temp = point; point = point2; point2 = temp; } if (point2.Key < point1.Key) { // point2 smaller than point1. temp = point2; point2 = point1; point1 = temp; } // this line is a segment. if (doSegment && (this.IsSegment1 || this.IsSegment2)) { // test where the intersection lies. double this_distance = this.Point1.Distance(this.Point2); if (this.IsSegment) { // if in any directions one of the points are further away from the point. double this_distance_1 = System.Math.Min(this.Point1.Distance(point1.Value), this.Point1.Distance(point2.Value)); if (this_distance_1 > this_distance) { // the point is further away. return(null); } } else if (this.IsSegment1) { // if in any directions one of the points are further away from the point. double this_distance_1 = this.Point1.Distance(point1.Value); if (this_distance_1 > this_distance) { // the point is further away. return(null); } } else if (this.IsSegment2) { // if in any directions one of the points are further away from the point. double this_distance_2 = this.Point2.Distance(point1.Value); if (this_distance_2 > this_distance) { // the point is further away. return(null); } } } // other line is a segment. if (doSegment && (line.IsSegment1 || line.IsSegment2)) { // test where the intersection lies. double this_distance = line.Point1.Distance(line.Point2); if (line.IsSegment) {// if in any directions one of the points are further away from the point. double this_distance_1 = System.Math.Min(line.Point1.Distance(point1.Value), line.Point1.Distance(point2.Value)); if (this_distance_1 > this_distance) { // the point is further away. return(null); } } else if (line.IsSegment1) { // if in any directions one of the points are further away from the point. double this_distance_1 = line.Point1.Distance(point1.Value); if (this_distance_1 > this_distance) { // the point is further away. return(null); } } else if (line.IsSegment2) { // if in any directions one of the points are further away from the point. double this_distance_2 = line.Point2.Distance(point2.Value); if (this_distance_2 > this_distance) { // the point is further away. return(null); } } } return(new LineF2D( point1.Value, point2.Value, true)); } else { // line.A = A1, line.B = B1, line.C = C1, this.A = A2, this.B = B2, this.C = C2 double det = (line.A * this.B - this.A * line.B); if (det == 0) // TODO: implement an accuracy threshold epsilon. { // lines are parallel; no intersections. return(null); } else { // lines are not the same and not parallel so they will intersect. double x = (this.B * line.C - line.B * this.C) / det; double y = (line.A * this.C - this.A * line.C) / det; // create the point. PointF2D point = new PointF2D(new double[] { x, y }); // this line is a segment. if (doSegment && (this.IsSegment1 || this.IsSegment2)) { // test where the intersection lies. double this_distance = this.Point1.Distance(this.Point2); if (this.IsSegment) {// if in any directions one of the points are further away from the point. double this_distance_1 = this.Point1.Distance(point); if (this_distance_1 > this_distance) { // the point is further away. return(null); } double this_distance_2 = this.Point2.Distance(point); if (this_distance_2 > this_distance) { // the point is further away. return(null); } } else if (this.IsSegment1 && this.Point2.Distance(point) > this_distance) { // only check this direction and only if the points lies far enough away from point2. VectorF2D pointDirection = point - this.Point2; VectorF2D point2ToPoint1 = this.Direction.Inverse; if (pointDirection.CompareNormalized(point2ToPoint1, 0.001)) { // point lies in the direction of the segmented point1 and far away from point2 return(null); } } else if (this.IsSegment1 && this.Point1.Distance(point) > this_distance) { // only check this direction and only if the points lies far enough away from point1. VectorF2D pointDirection = point - this.Point1; VectorF2D point1ToPoint2 = this.Direction; if (pointDirection.CompareNormalized(point1ToPoint2, 0.001)) { // point lies in the direction of the segmented point1 and far away from point2 return(null); } } } // line this is a segment. if (doSegment && (line.IsSegment1 || line.IsSegment2)) { // test where the intersection lies. double line_distance = line.Point1.Distance(line.Point2); if (line.IsSegment) {// if in any directions one of the points are further away from the point. double line_distance_1 = line.Point1.Distance(point); if (line_distance_1 > line_distance) { // the point is further away. return(null); } double line_distance_2 = line.Point2.Distance(point); if (line_distance_2 > line_distance) { // the point is further away. return(null); } } else if (line.IsSegment1 && line.Point2.Distance(point) > line_distance) { // only check line direction and only if the points lies far enough away from point2. VectorF2D pointDirection = point - line.Point2; VectorF2D point2ToPoint1 = line.Direction.Inverse; if (pointDirection.CompareNormalized(point2ToPoint1, 0.001)) { // point lies in the direction of the segmented point1 and far away from point2 return(null); } } else if (line.IsSegment1 && line.Point1.Distance(point) > line_distance) { // only check line direction and only if the points lies far enough away from point1. VectorF2D pointDirection = point - line.Point1; VectorF2D point1ToPoint2 = line.Direction; if (pointDirection.CompareNormalized(point1ToPoint2, 0.001)) { // point lies in the direction of the segmented point1 and far away from point2 return(null); } } } // the intersection is valid. return(point); } } }