gpt4 book ai didi

c - 三角形边界框

转载 作者:行者123 更新时间:2023-12-04 02:09:34 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com