如何在任何多边形中找到90度角

时间:2016-08-01 07:08:21

标签: algorithm graphics linear-algebra

我正试图在Java中编写agorythm以在任何多边形中找到90度角。

我有一个表示多边形的PointList(double x,double y)。 最后一点和第一点是相同的。

结果应该是包含这些顶点的ArrayList,其中是90度角。

我试图通过使用垂直线来计算出某些东西并计算它们之间的角度,但这没有用。

你知道怎么做吗?

如何确定我只想检查多边形内部的角度?

如果我有这样的情况:

enter image description here

我只想获得绿色的90个角度

1 个答案:

答案 0 :(得分:1)

正如Axel Kemper在评论中所提到的,你可以利用两个正交(即彼此相差90度)向量的点积为0的事实。

// set tolerance to some small value to handle floating point errors
static final int TOLERANCE = 0.01; 
ArrayList<Point> find90degCorners(ArrayList<Point> points)
{
    ArrayList<Point> corners = new ArrayList<Point>();
    for(int i = 1; i < points.size() - 1; i++)
    {
         Point prev = points.get(i - 1);
         Point current = points.get(i);
         Point next = points.get(i + 1);

         // To get vector, subtract previous point from each point.
         //   vector A = next - current
         //   vector B = current - prev

         // Multiply element-wise for dot product A.B:
         double dotProduct = ((next.x - current.x) * (current.x - prev.x)) +
             ((next.y - current.y) * (current.y - prev.y));

         // normal of 2D vector is found by swapping x and y
         // and flipping sign of second component

         // to check whether it is an exterior or interior angle,
         // take the dot product of one vector with the
         // normal of the other
         double direction = ((next.x - current.x) * (current.y - prev.y)) +
             ((next.y - current.y) * (prev.x - current.x));

         // check if the product is within the tolerance of zero:
         if((dotProduct > -TOLERANCE) && (dotProduct < TOLERANCE) && (direction > 0.0))
         {
              corners.add(current);
         }
    }
    return corners;
}

通过使用矢量法线进行第二个点积并与零比较,可以区分内角或外角是否为90度。您的测试是否应该> 0或&lt; 0取决于多边形绕组(顺时针或逆时针)和正X轴和Y轴的方向。

相关问题