示例#1
0
        /// <summary>
        /// Clip a line to the shape
        /// </summary>
        /// <param name="sourcePoint">The source of the line</param>
        /// <returns>The point which clips at the boundary</returns>
        internal override PointF ClipLine(PointF sourcePoint)
        {
            PointF intersect;

            if (GraphUtils.IntersectLine(sourcePoint, this.Center,
                                         new PointF(Boundary.Left, Boundary.Top),
                                         new PointF(Boundary.Left, Boundary.Bottom), out intersect))
            {
                return(intersect);
            }

            if (GraphUtils.IntersectLine(sourcePoint, this.Center,
                                         new PointF(Boundary.Left, Boundary.Top),
                                         new PointF(Boundary.Right, Boundary.Top), out intersect))
            {
                return(intersect);
            }

            if (GraphUtils.IntersectLine(sourcePoint, this.Center,
                                         new PointF(Boundary.Left, Boundary.Bottom),
                                         new PointF(Boundary.Right, Boundary.Bottom), out intersect))
            {
                return(intersect);
            }

            if (GraphUtils.IntersectLine(sourcePoint, this.Center,
                                         new PointF(Boundary.Right, Boundary.Top),
                                         new PointF(Boundary.Right, Boundary.Bottom), out intersect))
            {
                return(intersect);
            }

            return(Center);
        }
示例#2
0
        /// <summary>
        /// Intersect a line with a rectangle, returns the first intersecting point
        /// </summary>
        /// <param name="boundary">The rectangle boundary</param>
        /// <param name="sourcePoint">The source point of the line</param>
        /// <param name="destPoint">The destination point of the line</param>
        /// <param name="intersect">Parameter to store the intersection point if found</param>
        /// <returns>True if the line intersected at least one side of the rectangle</returns>
        public static bool IntersectRectangle(RectangleF boundary, PointF sourcePoint, PointF destPoint, out PointF intersect)
        {
            if (IntersectLine(sourcePoint, destPoint,
                              new PointF(boundary.Left, boundary.Top),
                              new PointF(boundary.Left, boundary.Bottom), out intersect))
            {
                return(true);
            }

            if (GraphUtils.IntersectLine(sourcePoint, destPoint,
                                         new PointF(boundary.Left, boundary.Top),
                                         new PointF(boundary.Right, boundary.Top), out intersect))
            {
                return(true);
            }

            if (GraphUtils.IntersectLine(sourcePoint, destPoint,
                                         new PointF(boundary.Left, boundary.Bottom),
                                         new PointF(boundary.Right, boundary.Bottom), out intersect))
            {
                return(true);
            }

            if (GraphUtils.IntersectLine(sourcePoint, destPoint,
                                         new PointF(boundary.Right, boundary.Top),
                                         new PointF(boundary.Right, boundary.Bottom), out intersect))
            {
                return(true);
            }

            return(false);
        }
示例#3
0
        /// <summary>
        /// Clip the line, defaults to flattening the path and intersecting every line.
        /// If a more efficient method is available it can be overridden
        /// </summary>
        /// <param name="sourcePoint"></param>
        /// <returns></returns>
        internal virtual PointF ClipLine(PointF sourcePoint)
        {
            PointF intersect = Center;
            float  distance  = GraphUtils.CalculateDistance(sourcePoint, Center);

            using (GraphicsPath path = GetPath())
            {
                path.Flatten();

                if (path.PointCount > 0)
                {
                    PointF lastPoint = path.GetLastPoint();

                    // Intersect all the lines in the path with our line and choose the closest
                    for (int i = 0; i < path.PointCount; ++i)
                    {
                        PointF currIntersect;

                        if (GraphUtils.IntersectLine(sourcePoint, this.Center,
                                                     lastPoint, path.PathPoints[i], out currIntersect))
                        {
                            float newDistance = GraphUtils.CalculateDistance(sourcePoint, currIntersect);
                            if (newDistance < distance)
                            {
                                distance  = newDistance;
                                intersect = currIntersect;
                            }
                        }

                        lastPoint = path.PathPoints[i];
                    }
                }
            }

            return(intersect);
        }