C#幾何算法練習1

C#幾何算法練習1

下面的demo實現了

  • 給定 n個線,找出其中兩兩相交的線
using System;
using System.Collections.Generic;

/// <summary>
/// 給定 n個線,找出其中兩兩相交的線
/// </summary>
namespace Algo
{
    /// <summary>
    /// 判斷三個點的方向
    /// </summary>
    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
    }

    /// <summary>
    /// 封裝了一箇中間數據結構,存儲一個點和這個點是否是這個線的左端點
    /// </summary>
    public class MiddleData
    {
        public bool IsLeft { get; set; }
        public Point Ptn { get; set; }
    }
    /// <summary>
    /// 線生成器
    /// </summary>
    public class LinesGenerator
    {
        public List<Line> Lines { get; set; }
        public int Number { get; set; }
        public LinesGenerator(int range, int number)
        {
            this.Lines = Generate(range, number);
        }
        private List<Line> Generate(int range, int number)
        {
            List<Line> res = new List<Line>();
            Random r = new Random();
            for(int i = 0; i < number; i++)
            {
                Point temp1 = new Point(r.Next(range), r.Next(range));
                Point temp2 = new Point(r.Next(range), r.Next(range));
                Line line = new Line(temp1, temp2);
                res.Add(line) ;
            }
            return res;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            LinesGenerator lines = new LinesGenerator(100, 5);
            int count = 0;
            foreach(var item1 in lines.Lines)
            {
                foreach(var item2 in lines.Lines)
                {
                    if(item2!=item1 && DoIntersect(item1,item2))
                    {
                      string res= $"{count}." +CustomPrint(item1, item2);
                        Console.WriteLine(res);
                    }
                    count++;
                }
            }



            Console.ReadKey();
        }
        public static string CustomPrint(Line line1,Line line2)
        {
            string str = string.Empty;
            str += $"線1({line1.StartPoint.X},{line1.StartPoint.Y}),({line1.EndPoint.X},{line1.EndPoint.Y})" +
                $"和線2({line2.StartPoint.X},{line2.StartPoint.Y}),({line2.EndPoint.X},{line2.EndPoint.Y})";
            str += "相交";
          return str;
        }


        /// <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;

        }
    }
}
// 運行結果如下
2.1(84,19),(0,48)和線2(92,60),(65,9)相交
4.1(84,19),(0,48)和線2(71,81),(31,5)相交
9.1(84,19),(0,48)和線2(9,4),(11,61)相交
20.1(92,60),(65,9)和線2(84,19),(0,48)相交
25.1(92,60),(65,9)和線2(51,90),(98,40)相交
34.1(81,0),(12,16)和線2(71,81),(31,5)相交
40.1(71,81),(31,5)和線2(84,19),(0,48)相交
43.1(71,81),(31,5)和線2(81,0),(12,16)相交
45.1(71,81),(31,5)和線2(51,90),(98,40)相交
47.1(71,81),(31,5)和線2(42,44),(73,49)相交
52.1(51,90),(98,40)和線2(92,60),(65,9)相交
54.1(51,90),(98,40)和線2(71,81),(31,5)相交
56.1(51,90),(98,40)和線2(89,63),(71,56)相交
58.1(51,90),(98,40)和線2(97,92),(92,31)相交
65.1(89,63),(71,56)和線2(51,90),(98,40)相交
74.1(42,44),(73,49)和線2(71,81),(31,5)相交
85.1(97,92),(92,31)和線2(51,90),(98,40)相交
90.1(9,4),(11,61)和線2(84,19),(0,48)相交

參考文章

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