平面上两多边形相交

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

  

时间: 2024-12-18 20:22:55

平面上两多边形相交的相关文章

判断平面上两线段是否相交

计算几何基础——矢量和叉积 矢量 如果一条线段的端点是有次序之分的话,那么这种线段就称为 有向线段,如果有向线段p1p2的起点p1在坐标的原点,则可以把它称为矢量 p2 矢量的加减 设二维矢量 P = (x1, y1), Q = (x2, y2),则 P + Q = (x1 + x2, y1 + y2), P - Q = (x1 - x2, y1 - y2),且有 P + Q = Q + P, P - Q = -(Q - P) 矢量叉积 设矢量 P = (x1, y1), Q = (x2, y2

Intersecting Lines - POJ 1269(判断平面上两条直线的关系)

分析:有三种关系,共线,平行,还有相交,共线和平行都可以使用叉积来进行判断(其实和斜率一样),相交需要解方程....在纸上比划比划就出来了.... 代码如下: ====================================================================================================================================== #include<math.h> #include<algor

绘制平面上的多边形

计算机里的3D图形其实是由许多个平面组合而成的,所谓“绘制3D图形”,其实是通过多个平面图形形成的.下面先从绘制平面图形开始. 调用GL10图形绘制2D图形的步骤如下: 1.调用GL10的glEnableClientState(GL10.GL_VERTEX_ARRAY):方法启用顶点坐标数组. 2.调用GL10的glEnableClientState(GL10.GL_COLOR_ARRAY):方法启用顶点颜色数组. 3.调用GL10的glVertexPointer(int  size , int

并行计算大作业之多边形相交(OpenMP、MPI、Java、Windows)

***************************************转载请注明出处:http://blog.csdn.net/lttree******************************************** 吐槽: 话说,相当郁闷,2015年,第一次打开博客,准备总结一下这一年.. 结果博客被封了= =! 今天,终于解封了,换了密码,换了密保.... 但是,写回顾的激情有点退散了.. 明后两天要上课,明天还要验收一个综合设计大作业,再后两天要考试,再后两天继续上课,

dtIntersectSegmentPoly2D 2D上的线段与多边形相交计算 产生结果:是否相交,线段跨越的开始和结束百分比,相交的边

dtIntersectSegmentPoly2D(startPos, endPos, verts, nv, tmin, tmax, segMin, segMax): http://geomalgorithms.com/vector_products.html perp product也就是 2D外积 inline float dtVperp2D(const float* u, const float* v) { return u[2]*v[0] - u[0]*v[2]; } 所有的都是映射到xz

Inheritance - SGU 129(线段与多边形相交的长度)

题目大意:给一个凸多边形(点不是按顺序给的),然后计算给出的线段在这个凸多边形里面的长度,如果在边界不计算. 分析:WA2..WA3...WA4..WA11...WA的无话可说,总之细节一定考虑清楚,重合的时候一定是0 代码如下: ========================================================================================================= #include<stdio.h> #include&

poj 3082多边形相交 &#39;Roid Rage

题意是判断多边形是否相交 主要的思路就是判断每一个点是否在另外的多变形内 判断一个点是否在另一个多边形内主要思路是: 判断的那个点向左边做射线,如果射线与多边形的交点为奇数个则在多边形内,偶数个则不在,其中有特殊情况: 1.如果判断的点与所要判断的边在平行且在所要判断的边上,则在多边形上 2.如果向左做射线恰好在某一点上,不特殊处理会计算两次,因为在两条边上,判断射线与多变形的交点数目,所以要在一个情况下忽略 3.就是判断点是否在多边形的左边了 but:WA了好久因为一个多边形可能包涵另一个多边

空间中任意多边形相交算法

public static string Mypf_Function() { var lee = 0; float d1 = 0, d2 = 0; var ret = "多面体不相交!!"; //空间两多面体 p1[0] = new PolytopePoint(0, 0, 1); p1[1] = new PolytopePoint(0, 0, 2); p1[2] = new PolytopePoint(2, 2, 2); p1[3] = new PolytopePoint(2, 2,

平面上欧拉定理:poj 2284( LA 3263 ) That Nice Euler Circuit

3263 - That Nice Euler Circuit Time limit: 3.000 seconds Description Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his primary school Joey heard about the nice story of how Euler started the study abo