英文:
How to use the interior area?
问题
关于正交多边形的边缘问题,我的目标是确定一条边是顶边(这意味着多边形的内部位于其下方),底边(这意味着多边形的内部位于其上方),右边(多边形的内部在其左侧)还是左边(多边形的内部在其右侧)。
我有多边形的坐标,并按逆时针方向排序(这是通常推荐的方式)。
我编写了一些用于在Java中计算多边形内部的代码。
public static double polygonArea(double X[], double Y[], int n)
{
// 初始化面积
double area = 0.0;
// 计算鞋带公式的值
int j = n - 1;
for (int i = 0; i < n; i++)
{
area += (X[j] + X[i]) * (Y[j] - Y[i]);
// j 是 i 的前一个顶点
j = i;
}
// 返回绝对值
return Math.abs(area / 2.0);
}
... 这在很大程度上是有效的。然而,我绝对不知道如何利用这个来计算或决定一条边(例如,由其顶点和底点定义的垂直边)是左边(内部在右侧)还是右边(内部在左侧)。
也许还有一种更简单的解决方案。然而,如果有人能给我一些建议来解决我的问题,我会非常感谢。
英文:
I have a question regarding the edges of an orthogonal polygon.
My goal is to decide whether an edge is a top edge (this means the interior of the polygon lies below), a bottom edge (this means the interior of the polygon lies above), a right edge (interior of the polygon is left) or a left edge (interior of the polygon is right).
I have the coordinates of the polygon and sorted them counterclockwise (as this is the usual recommended way).
I wrote some code to calculate the interior of the polygon in Java.
[...]
public static double polygonArea(double X[], double Y[], int n)
{
// Initialze area double area = 0.0; `
// Calculate value of shoelace formula
int j = n - 1;
for (int i = 0; i < n; i++)
{
area += (X[j] + X[i]) * (Y[j] - Y[i]);
// j is previous vertex to i
j = i;
}
// Return absolute value
return Math.abs(area / 2.0);
}
... what is working well. However I have absolutely no clue how I can use this to calculate or decide whether an edge (e.g. a vertical edge defined by its top point and bottom point) is a left edge (interior is right) or a right edge (interior left).
Maybe there is also a way more easier solution. However, I would appreciate if someone could give me some advice to approach my problem.
答案1
得分: 1
如果我正确理解您的问题(图示会有帮助),您可以通过比较 x(水平)或 y(垂直)坐标的顺序,相对于一个简单的(无自交点),正交多边形,获取每条边的位置(顶部、底部、左侧、右侧)。
显然,点的顺序很重要,因此对于逆时针多边形,您可以使用类似以下的方法:
enum EdgeType {TOP, BOTTOM, LEFT, RIGHT, EMPTY}
public EdgeType orthoEdgeTypeCCW(double x0, double y0, double x1, double y1)
{
if(x0 == x1) // 垂直
{
return (y0 < y1) ? EdgeType.RIGHT :
(y0 > y1) ? EdgeType.LEFT :
EdgeType.EMPTY;
}
else if(y0 == y1) // 水平
{
return (x0 < x1) ? EdgeType.BOTTOM :
(x0 > x1) ? EdgeType.TOP :
EdgeType.EMPTY;
}
else
{
throw new IllegalArgumentException("边不正交");
}
}
(以上为代码部分的翻译。)
英文:
If I've understood your question correctly (a diagram would help) you can get the position (TOP, BOTTOM, LEFT, RIGHT) of each edge relative to a simple (no self-intersections), orthogonal polygon by comparing the ordering of the x (for horizontal) or y (for vertical) coordinates.
Obviously the ordering of the points matters, so for a counter-clockwise polygon you could use something like this:
enum EdgeType {TOP, BOTTOM, LEFT, RIGHT, EMPTY}
public EdgeType orthoEdgeTypeCCW(double x0, double y0, double x1, double y1)
{
if(x0 == x1) // vertical
{
return (y0 < y1) ? EdgeType.RIGHT :
(y0 > y1) ? EdgeType.LEFT :
EdgeType.EMPTY;
}
else if(y0 == y1) // horizontal
{
return (x0 < x1) ? EdgeType.BOTTOM :
(x0 > x1) ? EdgeType.TOP :
EdgeType.EMPTY;
}
else
{
throw new IllegalArgumentException("Edge not orthogonal");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论