Пример #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());
        }
Пример #2
0
        public PointF2D ProjectOn(PointF2D point)
        {
            if (this.Length == 0.0 && this.IsSegment)
            {
                return((PointF2D)null);
            }
            VectorF2D vectorF2D = this.Direction.Rotate90(true);
            PointF2D  b         = new PointF2D((point + vectorF2D).ToArray());

            if (point[0] == b[0] && point[1] == b[1])
            {
                return(b);
            }
            PrimitiveF2D primitiveF2D = this.Intersection(new LineF2D(point, b, false, false), true);

            if (primitiveF2D == null)
            {
                return((PointF2D)null);
            }
            if ((object)(primitiveF2D as PointF2D) != null)
            {
                return(primitiveF2D as PointF2D);
            }
            throw new InvalidOperationException();
        }
Пример #3
0
        /// <summary>
        /// Projects a point onto the given line.
        ///
        /// (= intersection of the line with an angle 90° different from this line through the given point)
        /// </summary>
        /// <param name="point"></param>
        /// <returns>The projection point if it occurs inside the segmented line.</returns>
        public PointF2D ProjectOn(PointF2D point)
        {
            if (this.Length == 0 && this.IsSegment)
            { // cannot project on a line of length zero.
                return(null);
            }

            // get the direction.
            VectorF2D direction = this.Direction;

            // rotate.
            VectorF2D rotated = direction.Rotate90(true);

            // create second point
            PointF2D point2 = new PointF2D((point + rotated).ToArray());

            if (point[0] != point2[0] || point[1] != point2[1])
            {
                // create line.
                LineF2D line = new LineF2D(
                    point,
                    point2,
                    false,
                    false);

                // intersect.
                PrimitiveF2D primitive =
                    this.Intersection(line, true);

                if (primitive == null)
                {
                    return(null);
                }
                else if (primitive is PointF2D)
                {
                    return(primitive as PointF2D);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                return(point2);
            }
        }
Пример #4
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();
 }