Exemple #1
0
        /// <summary>
        /// Returns all the intersections the line has with this polygon.
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public PointF2D[] Intersections(LineF2D line)
        {
            List <PointF2D> points = new List <PointF2D>();

            foreach (LineF2D polygon_line in this.LineEnumerator)
            {
                // calculate the intersection.
                PrimitiveF2D primitive = line.Intersection(polygon_line);

                // the primitive not null.
                if (primitive != null)
                {
                    if (primitive is LineF2D)
                    { // primitive is a line; convert.
                        LineF2D intersect_line =
                            (primitive as LineF2D);

                        // we are sure the line is a segment.
                        // if the line is not a segment this means that the polygon contains an line with infinite length; impossible.

                        // TODO: how to determine the order?
                        points.Add(intersect_line.Point1);
                        points.Add(intersect_line.Point2);
                    }
                    else if (primitive is PointF2D)
                    { // primitive is a point; convert.
                        PointF2D point = (primitive as PointF2D);
                        points.Add(point);
                    }
                }
            }

            return(points.ToArray());
        }
Exemple #2
0
        /// <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;
        }
Exemple #3
0
        /// <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);
        }
Exemple #4
0
        /// <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 });
        }
Exemple #5
0
 public PointF2D[] Intersections(LineF2D line)
 {
     List<PointF2D> pointF2DList = new List<PointF2D>();
       foreach (LineF2D line1 in this.LineEnumerator)
       {
     PrimitiveF2D primitiveF2D = line.Intersection(line1);
     if (primitiveF2D != null)
     {
       if (primitiveF2D is LineF2D)
       {
     LineF2D lineF2D = primitiveF2D as LineF2D;
     pointF2DList.Add(lineF2D.Point1);
     pointF2DList.Add(lineF2D.Point2);
       }
       else if ((object) (primitiveF2D as PointF2D) != null)
       {
     PointF2D pointF2D = primitiveF2D as PointF2D;
     pointF2DList.Add(pointF2D);
       }
     }
       }
       return pointF2DList.ToArray();
 }
Exemple #6
0
        /// <summary>
        /// Transforms the given the coordinates to a coordinate system defined inside this rectangle.
        /// </summary>
        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 };
        }
Exemple #7
0
        /// <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;
        }
Exemple #8
0
        /// <summary>
        /// Returns all the intersections the line has with this polygon.
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public PointF2D[] Intersections(LineF2D line)
        {
            List<PointF2D> points = new List<PointF2D>();
            foreach (LineF2D polygon_line in this.LineEnumerator)
            {
                // calculate the intersection.
                PrimitiveF2D primitive = line.Intersection(polygon_line);

                // the primitive not null.
                if (primitive != null)
                {
                    if (primitive is LineF2D)
                    { // primitive is a line; convert.
                        LineF2D intersect_line =
                            (primitive as LineF2D);

                        // we are sure the line is a segment.
                        // if the line is not a segment this means that the polygon contains an line with infinite length; impossible.

                        // TODO: how to determine the order?
                        points.Add(intersect_line.Point1);
                        points.Add(intersect_line.Point2);
                    }
                    else if (primitive is PointF2D)
                    { // primitive is a point; convert.
                        PointF2D point = (primitive as PointF2D);
                        points.Add(point);
                    }
                }
            }

            return points.ToArray();
        }
Exemple #9
0
        public void LineSegmentIntersectionTests()
        {
            // double segments.
            LineF2D segment1 = new LineF2D(0, 0, 5, 0, true);
            LineF2D segment2 = new LineF2D(0, 0, 0, 5, true);
            LineF2D segment3 = new LineF2D(0, 3, 3, 0, true);
            LineF2D segment4 = new LineF2D(1, 1, 2, 2, true);
            LineF2D segment5 = new LineF2D(3, 3, 4, 4, true);
            LineF2D segment6 = new LineF2D(3, 1, 3, -1, true);

            PrimitiveF2D primitive = segment1.Intersection(segment2);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(0, 0), primitive as PointF2D);

            primitive = segment1.Intersection(segment3);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(3, 0), primitive as PointF2D);

            primitive = segment2.Intersection(segment3);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(0, 3), primitive as PointF2D);

            primitive = segment3.Intersection(segment4);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(1.5, 1.5), primitive as PointF2D);

            primitive = segment5.Intersection(segment1);
            Assert.IsNull(primitive);
            primitive = segment5.Intersection(segment2);
            Assert.IsNull(primitive);
            primitive = segment5.Intersection(segment3);
            Assert.IsNull(primitive);
            primitive = segment5.Intersection(segment4);
            Assert.IsNull(primitive);
            primitive = segment5.Intersection(segment6);
            Assert.IsNull(primitive);

            primitive = segment6.Intersection(segment3);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(3, 0), primitive as PointF2D);

            primitive = segment6.Intersection(segment1);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(3, 0), primitive as PointF2D);

            primitive = segment6.Intersection(segment3);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(3, 0), primitive as PointF2D);

            // half segments.
            LineF2D segment7 = new LineF2D(1.5, 2, 1.5, 0, true, false); // only closed upwards.
            LineF2D segment9 = new LineF2D(1.5, 2, 1.5, 4, true, false); // only closed downwards.

            LineF2D segment8 = new LineF2D(1.5, 1, 1.5, 0, true, false); // only closed upwards.
            LineF2D segment10 = new LineF2D(1.5, 1, 1.5, 4, true, false); // only closed downwards.

            primitive = segment7.Intersection(segment3);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(1.5, 1.5), primitive as PointF2D);
            primitive = segment3.Intersection(segment7);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(1.5, 1.5), primitive as PointF2D);
            primitive = segment9.Intersection(segment3);
            Assert.IsNull(primitive);
            primitive = segment3.Intersection(segment9);
            Assert.IsNull(primitive);

            primitive = segment10.Intersection(segment3);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(1.5, 1.5), primitive as PointF2D);
            primitive = segment3.Intersection(segment10);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(1.5, 1.5), primitive as PointF2D);
            primitive = segment8.Intersection(segment3);
            Assert.IsNull(primitive);
            primitive = segment3.Intersection(segment8);
            Assert.IsNull(primitive);

            LineF2D segment11 = new LineF2D(-1, 1, 0, 1, true, false);
            LineF2D segment12 = new LineF2D(0, 3, 3, 0, true, true);
            primitive = segment11.Intersection(segment12);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(2, 1), primitive as PointF2D);
            primitive = segment12.Intersection(segment11);
            Assert.IsNotNull(primitive);
            Assert.IsInstanceOf<PointF2D>(primitive);
            Assert.AreEqual(new PointF2D(2, 1), primitive as PointF2D);
        }