DesignerGeometryHelper.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Core.Presentation / System / Activities / Core / Presentation / DesignerGeometryHelper.cs / 1305376 / DesignerGeometryHelper.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace System.Activities.Core.Presentation 
{
    using System.Windows; 
    using System.Windows.Media; 
    using System.Windows.Shapes;
    internal static class DesignerGeometryHelper 
    {
        const double EPS = 1e-6;

        public static double DistanceBetweenPoints(Point point1, Point point2) 
        {
            double d = Math.Sqrt(Math.Pow(point2.X - point1.X, 2) + Math.Pow(point2.Y - point1.Y, 2)); 
            return d; 
        }
 
        //Distance from point to nearest linesegment on a connector.
        //Assuming lines are horizontal or vertical.
        //Note that this might not always be the perpendicular distance from point to a line
        public static double DistanceFromPointToConnector(Point point, Connector connector) 
        {
            double minDist = Double.PositiveInfinity; 
            double dist = 0; 
            for (int i = 0; i < connector.Points.Count - 1; i++)
            { 
                Point p1 = connector.Points[i];
                Point p2 = connector.Points[i + 1];
                //This condition checks if a perpendicular from the point to the linesegment can be drawn.
                if (p1.X == p2.X && point.Y >= Math.Min(p1.Y, p2.Y) && point.Y <= Math.Max(p1.Y, p2.Y)) 
                {
                    dist = Math.Abs(p1.X - point.X); 
                } 
                else if(p1.Y == p2.Y && point.X >= Math.Min(p1.X, p2.X) && point.X <= Math.Max(p1.X, p2.X))
                { 
                    dist = Math.Abs(p1.Y - point.Y);
                }
                else
                { 
                    dist = Math.Min(DistanceBetweenPoints(point, p1), DistanceBetweenPoints(point, p2));
                } 
                minDist = (dist < minDist) ? dist : minDist; 
            }
            return minDist; 
        }

        //Distance perpendicular from point to line segment
        //Returns the length of the perpendicular from point to the line containing the linesegment. 
        public static double DistanceFromPointToLineSegment(Point point, Point[] line)
        { 
            int area = (int)Math.Abs((((point.Y - line[0].Y) * (line[1].X - line[0].X)) - ((point.X - line[0].X) * (line[1].Y - line[0].Y)))); 
            return Math.Sqrt(Math.Pow(area, 2) / (Math.Pow((line[1].X - line[0].X), 2) + Math.Pow((line[1].Y - line[0].Y), 2)));
        } 

        //This function calculates the total length of line segments by adding individual lengths
        public static double DistanceOfLineSegments(Point[] segments)
        { 
            double distance = 0;
            for (int i = 1; i < segments.Length; i++) 
            { 
                distance += DistanceBetweenPoints(segments[i - 1], segments[i]);
            } 
            return distance;
        }

        //Midpoint of line mid = X1 + X2 / 2, Y1 + Y2 /2 
        public static Point MidPointOfLineSegment(Point point1, Point point2)
        { 
            return new Point(Math.Round((point1.X + point2.X) / 2), Math.Round((point1.Y + point2.Y) / 2)); 
        }
 
        public static double SlopeOfLineSegment(Point start, Point end)
        {
            //If line is vertical then the slope is infinite
            if (start.X == end.X) 
            {
                return double.MaxValue; 
            } 

            //If the line is horizontal then slope is 0 
            if (start.Y == end.Y)
            {
                return 0;
            } 

            return ((end.Y - start.Y) / (end.X - start.X)); 
        } 

 
        //This function returns the length of the longest segment in a PointsCollection.
        //The segments are assumed to be HORIZONTAL or VERTICAL.
        //the out parameter returns the start point of the longest segment.
        //Use equal to avoid the precision problem for double. 
        //We always choose the first segment among the segements with max length.
        public static double LongestSegmentLength(PointCollection points, out int longestSegmentIndex) 
        { 
            double maxLength = 0;
            longestSegmentIndex = -1; 
            for (int i = 0; i < points.Count - 1; i++)
            {
                double length = Math.Abs((points[i].X == points[i + 1].X) ? points[i].Y - points[i + 1].Y : points[i].X - points[i + 1].X);
                if (!Equal(length, maxLength) && length > maxLength) 
                {
                    maxLength = length; 
                    longestSegmentIndex = i; 
                }
            } 

            return maxLength;
        }
 
        static bool Equal(double num1, double num2)
        { 
            return Math.Abs(num1 - num2) < EPS; 
        }
    } 

}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK