前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >多边形几何

多边形几何

作者头像
sofu456
发布2019-07-09 14:15:33
4210
发布2019-07-09 14:15:33
举报
文章被收录于专栏:sofu456sofu456

任意多边形几何中心

代码语言:javascript
复制
public Point Center
        {
            get
            {
                Point ptCenter = new Point();
                int i, j;
                double ai, atmp = 0, xtmp = 0, ytmp = 0;
                if (m_LogicPoints.Count == 1)
                    return m_LogicPoints[0];
                if ((m_LogicPoints.Count == 2) || (m_LogicPoints.Count == 3 && m_LogicPoints[0] == m_LogicPoints[2]))
                    return new Point() { X = (m_LogicPoints[1].X + m_LogicPoints[0].X) / 2, Y = (m_LogicPoints[1].Y + m_LogicPoints[0].Y) / 2 };

                int n = m_LogicPoints.Count;
                for (i = n - 1, j = 0; j < n; i = j, j++)
                {
                    ai = m_LogicPoints[i].X * m_LogicPoints[j].Y - m_LogicPoints[j].X * m_LogicPoints[i].Y;
                    atmp += ai;
                    xtmp += (m_LogicPoints[j].X + m_LogicPoints[i].X) * ai;
                    ytmp += (m_LogicPoints[j].Y + m_LogicPoints[i].Y) * ai;
                }

                if (atmp != 0)
                {
                    ptCenter.X = Convert.ToInt32(xtmp / (3 * atmp));
                    ptCenter.Y = Convert.ToInt32(ytmp / (3 * atmp));
                }
                return ptCenter;
            }
        }

线段相交

代码语言:javascript
复制
public bool isColide(Line line)//判断是否相交,直线p1p2与线段p3p4
        {
            //判处端点
            if (this.V0 == line.V0 || this.V0 == line.V1 || this.V1 == line.V0 || this.V1 == line.V1)
                return false;

            double deviation = 0.1;//允许误差
            //排斥试验,判断p1p2在q1q2为对角线的矩形区之外
            if (Math.Max(line.V0.X, line.V1.X) < Math.Min(this.V0.X, this.V1.X)- deviation)
            {//P1P2中最大的X比Q1Q2中的最小X还要小,说明P1P2在Q1Q2的最左点的左侧,不可能相交。
                return false;
            }

            if (Math.Min(line.V0.X, line.V1.X) > Math.Max(this.V0.X, this.V1.X)+ deviation)
            {//P1P2中最小的X比Q1Q2中的最大X还要大,说明P1P2在Q1Q2的最右点的右侧,不可能相交。
                return false;
            }

            if (Math.Max(line.V0.Y, line.V1.Y) < Math.Min(this.V0.Y, this.V1.Y)- deviation)
            {//P1P2中最大的Y比Q1Q2中的最小Y还要小,说明P1P2在Q1Q2的最低点的下方,不可能相交。
                return false;
            }

            if (Math.Min(line.V0.Y, line.V1.Y) > Math.Max(this.V0.Y, this.V1.Y)+ deviation)
            {//P1P2中最小的Y比Q1Q2中的最大Y还要大,说明P1P2在Q1Q2的最高点的上方,不可能相交。
                return false;
            }

            //跨立试验
            var crossP1P2Q1 = Cross(line.V0, line.V1, this.V0);
            var crossP1Q2P2 = Cross(line.V0, this.V1, line.V1);
            var crossQ1Q2P1 = Cross(this.V0, this.V1, line.V0);
            var crossQ1P2Q2 = Cross(this.V0, line.V1, this.V1);

            return (crossP1P2Q1 * crossP1Q2P2 >= 0 || Math.Abs(crossP1P2Q1 * crossP1Q2P2) < 0.01) && (crossQ1Q2P1 * crossQ1P2Q2 >= 0 || Math.Abs(crossQ1Q2P1 * crossQ1P2Q2) < 0.01);
        }

参考: https://zh.wikipedia.org/wiki/几何中心 https://blog.csdn.net/xxdddail/article/details/70264399

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年03月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 任意多边形几何中心
  • 线段相交
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档