c - 三角形边界框

标签 c bounding

我应该编写代码来计算三角形的边界框。 边界框坐标应写入

triangle->bx, triangle->by, triangle->bw, triangle->bh

在哪里

bx, by is the upper left corner of the box
bw, bh is the width and height of the box

我是将我的点视为坐标还是应该选择更基于几何的解决方案?

我尝试找出每个坐标的最小值和最大值,但没有成功。任何帮助将不胜感激!

if (triangle->sx1 <= triangle->sx2 <= triangle->sx3)
{
    triangle->bx = triangle->sx1;
}
else if (triangle->sx2 <= triangle->sx1 <= triangle->sx3)
{
    triangle->bx = triangle->sx2;
}
else (triangle->bx = triangle->sx3);

if (triangle->sy1 <= triangle->sy2 <= triangle->sy3)
{
    triangle->by = triangle->sy1;
}
else if (triangle->sy2 <= triangle->sy1 <= triangle->sy3)
{
    triangle->by = triangle->sy2;
}
else (triangle->by = triangle->sy3);

if (triangle->sx1 >= triangle->sx2 >= triangle->sx3)
{
    triangle->bw = triangle->sx1;
}
else if (triangle->sx2 >= triangle->sx1 >= triangle->sx3)
{
    triangle->bw = triangle->sx2;
}
else (triangle->bw = triangle->sx3);

if (triangle->sy1 >= triangle->sy2 >= triangle->sy3)
{
    triangle->bh = triangle->sy1;
}
else if (triangle->sy2 >= triangle->sy1 >= triangle->sy3)
{
    triangle->bh = triangle->sy2;
}
else (triangle->bh = triangle->sy3);

最佳答案

要找到包含三角形的框的边界,您只需从构成三角形的三个坐标中找到最小和最大的 x 和 y 坐标。您可以使用三元表达式进行比较,这会使代码不那么难看。在下面的代码中,我将 x 和 y 三角形坐标移植到单独的变量中,以便可以更轻松地阅读三元表达式。

int sx1 = triangle->sx1;
int sx2 = triangle->sx2;
int sx3 = triangle->sx3;
int sy1 = triangle->sy1;
int sy2 = triangle->sy2;
int sy3 = triangle->sy3;

int xmax = sx1 > sx2 ? (sx1 > sx3 ? sx1 : sx3) : (sx2 > sx3 ? sx2 : sx3);
int ymax = sy1 > sy2 ? (sy1 > sy3 ? sy1 : sy3) : (sy2 > sy3 ? sy2 : sy3);
int xmin = sx1 < sx2 ? (sx1 < sx3 ? sx1 : sx3) : (sx2 < sx3 ? sx2 : sx3);
int ymin = sy1 < sy2 ? (sy1 < sy3 ? sy1 : sy3) : (sy2 < sy3 ? sy2 : sy3);

triangle->bx = xmin;
triangle->by = ymax;
triangle->bw = xmax - xmin;
triangle->bh = ymax - ymin;

关于c - 三角形边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39974191/

相关文章:

c - 段错误 - 大数组

C - do while 循环不会停止运行

c - strstr == NULL 不起作用,

c - 如何检索最接近平均值及其位置 C

python - Python 中的最小封闭平行四边形

c - kbhit 和函数的多次调用

string - 如何用矩形边界框构造 R 树(STR 方法)?

algorithm - 找到两张图像之间差异的边界框?

three.js - 如何在 Three.js 中获取整个场景的boundingSphere?