C#幾何算法練習0

C#幾何算法練習

下面的demo實現了

  • 判斷點是否在線上
  • 判斷三個點的方向
  • 和判斷由四個點組成的兩天直線是否相交
using System;

namespace Algo
{
    public class Point
    {
        public double Y { get; set; }
        public double X { get; set; }
        public Point(double x, double y)
        {
            this.X = x;
            this.Y = y;
        }
    }
    public class Line
    {
        public Point EndPoint { get; set; }
        public Point StartPoint { get; set; }
        public Line(Point p1, Point p2)
        {
            this.StartPoint = p1;
            this.EndPoint = p2;
        }
    }
    public enum Orientation
    {
        Colinear,
        Clockwise,
        Counterclockwise
    }
    class Program
    {
        static Point p1 = new Point(0, 0);
        static Point p2 = new Point(10, 10);
        static Point p3 = new Point(20, 20);
        static Point p4 = new Point(20, 21);
        static Point p5 = new Point(20, 19);

        static Point p6 = new Point(-10, -10);
        static Point p7 = new Point(-10, 10);
        static Point p8 = new Point(10, -10);

        static void Main(string[] args)
        {
            // 判斷點是否在線上
            //string res = OnSegment(p2, p1, p3).ToString();
            //Console.WriteLine(res);
			// 獲取三個點的方向
            //string res = GetOrientation(p1, p2,p3).ToString();
            //string res1 = GetOrientation(p1, p2,p4).ToString();
            //string res2 = GetOrientation(p1, p2,p5).ToString();
            //Console.WriteLine($"{res}\n{res1}\n{res2}");
			
			// 判斷直線是否相交
            //Line line1 = new Line(p6, p2);
            //Line line2 = new Line(p7, p8);
            //Line line3 = new Line(p1, p6);
            //Console.WriteLine(DoIntersect(line1,line2).ToString()) ;
            //Console.WriteLine(DoIntersect(line1,line3).ToString()) ;

            Console.ReadKey();
        }
        /// <summary>
        /// 判斷兩條線是否相交
        /// </summary>
        /// <param name="line1"></param>
        /// <param name="line2"></param>
        /// <returns></returns>
        public static bool DoIntersect(Line line1, Line line2)
        {
            Orientation o1 = GetOrientation(line1.StartPoint, line1.EndPoint, line2.StartPoint);
            Orientation o2 = GetOrientation(line1.StartPoint, line1.EndPoint, line2.EndPoint);
            Orientation o3 = GetOrientation(line2.StartPoint, line2.EndPoint, line1.StartPoint);
            Orientation o4 = GetOrientation(line2.StartPoint, line2.EndPoint, line1.EndPoint);

            // 一般情況
            if (o1 != o2 && o3 != o4)
                return true;


            // 特殊情況
            //p1=line1.StartPoint
            //q1=line1.EndPoint
            //p2=line2.StartPoint
            //q2=line2.EndPoint

            //p1,p2,q2共線,p2在p1q1上
            if (o1 == Orientation.Colinear && OnSegment(line1.StartPoint, line2.StartPoint, line1.EndPoint)) return true;

            //p1,q1,q2共線,q2在p1q1上
            if (o2 == Orientation.Colinear && OnSegment(line1.StartPoint, line2.StartPoint, line2.EndPoint)) return true;

            // p2 ,q2, p1共線,p1在p2q2上
            if (o3 == Orientation.Colinear && OnSegment(line2.StartPoint, line2.EndPoint, line1.StartPoint)) return true;

            // p2,q2,q1共線,q1在p2q2上
            if (o4 == Orientation.Colinear && OnSegment(line2.StartPoint, line2.EndPoint, line1.StartPoint)) return true;

            return false; // 不滿足任何上述情況
        }



        /// <summary>
        /// 獲取三個點的方向
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="p3"></param>
        /// <returns></returns>
        public static Orientation GetOrientation(Point p1, Point p2, Point p3)
        {
            var value = (p2.Y - p1.Y) * (p3.X - p2.X) - (p2.X - p1.X) * (p3.Y - p2.Y);
            if (value.Equals(0))
            {
                return Orientation.Colinear;
            }
            else if (value > 0)
            {
                return Orientation.Clockwise;
            }
            else
            {
                return Orientation.Counterclockwise;
            }
        }

        /// <summary>
        /// 給定三個共線的點,判斷第一個點是否在由後面的兩個點組成的線上
        /// </summary>
        /// <param name="q"></param>
        /// <param name="p"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool OnSegment(Point q, Point p, Point r)
        {
            if (q.X <= Math.Max(p.X, r.X) && q.X >= Math.Min(p.X, r.X) &&
                q.Y <= Math.Max(p.Y, r.Y) && q.Y >= Math.Min
                (p.Y, r.Y))
            {
                return true;
            }
            else return false;

        }
    }
}

參考文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章