在进行几何实体操作时,有时候是需要将实体安装”最长边“进行旋转,例如:

将多边形实体进行旋正
旋转之前
将多边形实体进行旋正
旋转之后

       从第一个图中可以看出,实体最长的边是有很多个节点,如果我们直接安照多边形中最长的边进行旋转那么最终的结果是下图这样的:

将多边形实体进行旋正
直接按照最长边

       这样就不是我们想要的结果,所以在计算旋转角度的时候,我们需要先将一些共线的点给忽略掉,这样我们就可以计算出最长的边。

       移除共线的点方法:

//点与线的位置关系

enum PointLocation
{
    Line_Left,
    Line_Right,
    Collinear_Left,
    Collinear_Right,
    Collinear_Contain
};

PointLocation collinearTest(const Point &P, const Point &Q1, const Point &Q2)
{
    Vector A = Vector(P.x - Q1.x, P.y - Q1.y);
    Vector D = Vector(Q2.x - Q1.x, Q2.y - Q1.y);
    Vector N = -D.perpVector();
    double val = N.dotProduct(A);
    if(val > 0) return Line_Left;
    else if(val < 0) return Line_Right;
    val = D.dotProduct(A);
    if(val < 0) return Collinear_Left;
    else if(val > D.dotProduct(D)) return Collinear_Right;
    return Collinear_Contain;
}

PointArray removeSurplusPoint(const PointArray &node)
{
    int len = node.length(); PointArray node1;
    for(int idx = 1; idx < node.length(); ++idx)
    {
        Point p1 = node[(idx - 1) % len];
        Point p2 = node[(idx + 0) % len];
        Point p3 = node[(idx + 1) % len];
        if(Collinear_Contain == collinearTest(p2, p1, p3)) continue;
        node1.append(p2);
    }
    return node1;
}

//计算旋转角度
double rotateAngle(const PointArray &node)
{
    double ang= 0.0, dist = 0.0;
    int len = node.length();
    for(int idx = 0; idx < len; ++idx)
    {
        Point spt = node[idx];
        Point ept = node[(idx + 1) % len];
        double temp = spt.distance(ept);
        if(temp > dist) { dist = temp; ang= angle(spt, ept); }
    }
    return -ang;
}

计算出旋转角度之后,我们就可以对实体进行旋转。旋转的结果就是第二个图。

 

相关文章:

  • 2021-04-14
  • 2022-12-23
  • 2022-12-23
  • 2021-05-11
  • 2021-12-10
  • 2022-12-23
  • 2021-07-19
  • 2022-01-14
猜你喜欢
  • 2022-12-23
  • 2022-01-26
  • 2021-11-25
  • 2021-04-28
  • 2022-02-24
  • 2022-12-23
  • 2021-11-07
相关资源
相似解决方案