Пример #1
0
        public static PointPair GetLineFromSegment(PointPair segment, PointPair borders)
        {
            PointPair result = new PointPair();

            if (segment.P1 == segment.P2)
            {
                result = segment;
            }
            else if (segment.P1.X == segment.P2.X)
            {
                result.P1.X = segment.P1.X;
                result.P2.X = segment.P1.X;
                result.P1.Y = borders.P1.Y;
                result.P2.Y = borders.P2.Y;
                if (segment.P1.Y > segment.P2.Y)
                {
                    result.P1.Y = borders.P2.Y;
                    result.P2.Y = borders.P1.Y;
                }
            }
            else if (segment.P1.Y == segment.P2.Y)
            {
                result.P1.X = borders.P1.X;
                result.P1.Y = segment.P1.Y;
                result.P2.X = borders.P2.X;
                result.P2.Y = segment.P1.Y;
                if (segment.P1.X > segment.P2.X)
                {
                    result.P1.X = borders.P2.X;
                    result.P2.X = borders.P1.X;
                }
            }
            else
            {
                var deltaX       = segment.P2.X - segment.P1.X;
                var deltaY       = segment.P2.Y - segment.P1.Y;
                var deltaXYRatio = deltaX / deltaY;
                var deltaYXRatio = deltaY / deltaX;

                result.P1.Y = deltaY > 0 ? borders.P1.Y : borders.P2.Y;
                result.P1.X = segment.P1.X + (result.P1.Y - segment.P1.Y) * deltaXYRatio;
                if (result.P1.X < borders.P1.X)
                {
                    result.P1.X = borders.P1.X;
                    result.P1.Y = segment.P1.Y + (result.P1.X - segment.P1.X) * deltaYXRatio;
                }
                else if (result.P1.X > borders.P2.X)
                {
                    result.P1.X = borders.P2.X;
                    result.P1.Y = segment.P1.Y + (result.P1.X - segment.P1.X) * deltaYXRatio;
                }

                result.P2.X = deltaX > 0 ? borders.P2.X : borders.P1.X;
                result.P2.Y = segment.P2.Y + (result.P2.X - segment.P2.X) * deltaYXRatio;
                if (result.P2.Y < borders.P1.Y)
                {
                    result.P2.Y = borders.P1.Y;
                    result.P2.X = segment.P2.X + (result.P2.Y - segment.P2.Y) * deltaXYRatio;
                }
                else if (result.P2.Y > borders.P2.Y)
                {
                    result.P2.Y = borders.P2.Y;
                    result.P2.X = segment.P2.X + (result.P2.Y - segment.P2.Y) * deltaXYRatio;
                }
            }

            return(result);
        }
Пример #2
0
        public static bool IsPointInSegmentBoundingRect(PointPair segment, Point point)
        {
            var boundingRect = segment.GetBoundingRect().Inflate(0.00001);

            return(boundingRect.Contains(point));
        }