zoukankan      html  css  js  c++  java
  • 平面上两多边形相交

    public static void Run_PolygonsIntersection()
            { 
                int count = 0;
    
                Ps1[0] = new Point(0, 0); 
                Ps1[1] = new Point(3, 0); 
                Ps1[2] = new Point(0, 3); 
                Ps1[3] = new Point(0, 0); 
    
                Ps2[0] = new Point(1, 1);;
                Ps2[1] = new Point(4, 1); 
                Ps2[2] = new Point(4, 4);
                Ps2[3] = new Point(1, 1);
    
                var pointlist1 = new List<Point>(Ps1); 
                var pointlist2 = new List<Point>(Ps2);
    
                for (var i = 0; i < M-1; i++)
                {
                    for (var j = 0; j < N-1; j++)
                    {
                        if (SegmentIntersect(Ps1[i], Ps1[i + 1], Ps2[j], Ps2[j + 1]))
                        {
                            count++;
                            Console.WriteLine(LinesEquation(i,j));
                        }
                    }
                }
    
                if (count != 0)
                {
                    Console.WriteLine();
                    Console.WriteLine("两多边形相交!");
                }
    
                else
                {
                    Console.WriteLine("两多边形不相交!!!");
                }
            }
    
            public static string LinesEquation(int s,int t)
            {
                var ret="相交直线至少有一条方程不存在";
    
                if (Math.Abs((Ps1[s + 1].X - Ps1[s].X)) < 0.0000000000000000000000001 || Math.Abs((Ps2[t + 1].X - Ps2[t].X)) < 0.0000000000000000000000001)
                {
                    return ret;
                }
    
                var k1 = ((Ps1[s + 1].Y - Ps1[s].Y) / (Ps1[s + 1].X - Ps1[s].X));
                var c1 = Ps1[s + 1].Y - k1 * Ps1[s].X;
                var k2 = ((Ps2[t + 1].Y - Ps2[t].Y) / (Ps2[t + 1].X - Ps2[t].X));
                var c2 = Ps2[t + 1].Y - k2 * Ps2[t].X;
                 
                if (c1 < 0 && c2<0)
                {
                    ret ="相交边的方程为:"+ k1 + "x-y" + c1 + "=0和"+k2 + "x-y" + c2 + "=0";
                }
                if (c1 < 0 && c2 >= 0)
                {
                    ret = "相交边的方程为:" + k1 + "x-y" + c1 + "=0和" + k2 + "x-y" + "+" + c2 + "=0";
                }
                if (c1 >= 0 && c2 < 0)
                {
                    ret = "相交边的方程为:" + k1 + "x-y" + "+" + c1 + "=0和" + k2 + "x-y" + c2 + "=0";
                }
                if (c1 >= 0 && c2 >=0)
                {
                    ret = "相交边的方程为:" + k1 + "x-y" + "+" + c1 + "=0和" + k2 + "x-y" + "+" + c2 + "=0";
                }
    
                return ret;
            }
    
    
    
             
          public static double Direction(Point point1, Point point2, Point point3)
            {
                var p1 = new Point((point3.X - point1.X), (point3.Y - point1.Y));
                var p2 = new Point((point2.X - point1.X), (point2.Y - point1.Y));
    
                return p1.X * p2.Y - p1.Y * p2.X;
            }
           
          public static bool OnSegment(Point point1, Point point2, Point point3)
            {
                double xMin, xMax, yMin, yMax;
    
                if (point1.X < point2.X) 
                {
                    xMin = point1.X;
                    xMax = point2.X;
                }
                else
                {
                    xMin = point2.X;
                    xMax = point1.X;
                }
                if (point1.Y < point2.Y)
                {
                    yMin = point1.Y;
                    yMax = point2.Y;
                }
                else
                {
                    yMin = point2.Y; 
                    yMax = point1.Y;
                }
    
                if (point3.X < xMin || point3.X > xMax || point3.Y < yMin || point3.Y > yMax)
                    return false;
                else
                    return true;
            }
          
          public static bool SegmentIntersect(Point point1, Point point2, Point point3, Point point4)
            {
                var d1 = Direction(point3, point4, point1);
                var d2 = Direction(point3, point4, point2);
                var d3 = Direction(point1, point2, point3);
                var d4 = Direction(point1, point2, point4);
    
                if (d1 * d2 < 0 && d3 * d4 < 0)
                    return true;
                if (Math.Abs(d1) < 0.00000000000001 && OnSegment(point3, point4, point1))
                    return true;
                if (Math.Abs(d2) < 0.00000000000001 && OnSegment(point3, point4, point2))
                    return true;
                if (Math.Abs(d3) < 0.00000000000001 && OnSegment(point1, point2, point3))
                    return true;
                if (Math.Abs(d4) < 0.00000000000001 && OnSegment(point1, point2, point4))
                    return true;
                else
                    return false;
            }
    

      

  • 相关阅读:
    $Noip2011/Luogu1311$ 选择客栈
    $Noip2013/Luogu1970$ 花匠 $dp$+思维
    $CF1063B Labyrinth$ $01$最短路/$01BFS$
    $UVA10559 Blocks $区间$dp$
    $SCOI2009 windy$数 数位$dp$
    $cometoj#4 D $求和 不是$dp$
    Manacher算法总结
    题解 P1537 【弹珠】
    题解 P4609 【[FJOI2016]建筑师】
    字符串专题随笔
  • 原文地址:https://www.cnblogs.com/leejxyz/p/5415564.html
Copyright © 2011-2022 走看看