I'm supposed to write code that calculates the bounding box of a triangle. The bounding box coordinates should be written to
triangle->bx, triangle->by, triangle->bw, triangle->bh
where
bx, by is the upper left corner of the box
bw, bh is the width and height of the box
Do I treat my points as coordinates or should I choose a more geometry-based solution?
I tried finding the minimum and maximum values for every coordinate, but it didn't work. Any help would be much appreciated!
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);