コード例 #1
0
        public int GrahamSort(Vector v1, Vector v2)
        {
            if (v1 == v2 || (v1.X == v2.X && v1.Y == v2.Y))
            {
                return(0);
            }

            LowestPoint = FindLowestPoint();

            double thetaA = Math.Atan2(v1.Y - LowestPoint.Y, v1.X - LowestPoint.X);
            double thetaB = Math.Atan2(v2.Y - LowestPoint.Y, v2.X - LowestPoint.X);

            if (thetaA < thetaB)
            {
                return(-1);
            }
            else if (thetaA > thetaB)
            {
                return(1);
            }

            double distanceA = GeomMath.Distance(LowestPoint, v1);
            double distanceB = GeomMath.Distance(LowestPoint, v2);

            if (distanceA < distanceB)
            {
                return(-1);
            }
            else
            {
                return(1);
            }
        }
コード例 #2
0
ファイル: ConvexPolyIntersection.cs プロジェクト: jsgmu/cs633
        public int VectorCompare(Vector v1, Vector v2)
        {
            if (v1 == v2 || (v1.X == v2.X && v1.Y == v2.Y))
            {
                return(0);
            }

            double thetaA = Math.Atan2(v1.Y - LowestPoint.Y, v1.X - LowestPoint.X);
            double thetaB = Math.Atan2(v2.Y - LowestPoint.Y, v2.X - LowestPoint.X);

            // Angle of difference vector v1 and LowestPoint, and the x-axis
            // If v1 has the smaller angle, return -1
            if (thetaA < thetaB)
            {
                return(-1);
            }
            // If v2 has the smaller angle, return 1
            else if (thetaA > thetaB)
            {
                return(1);
            }

            // If angles are the same, then choose the point that is closer to LowestPoint

            double distanceA = GeomMath.Distance(LowestPoint, v1);
            double distanceB = GeomMath.Distance(LowestPoint, v2);

            if (distanceA < distanceB)
            {
                return(-1);
            }
            else
            {
                return(1);
            }
        }