Minimum and maximum of a box? - algorithm

Given the, width, height and depth of a box and its center point, how could I find the minimum, x, y, and z coordinate and the maximum x, y and z coordinate without bruteforcing through each vertex? its an AABB box.
Thanks
from a top view
---------------
| |
| |
| c |
| |
|--------------|

This should do it:
(xmin, ymin, zmin) = (xcentre, ycentre, zcentre) - (width, height, depth) / 2
(xmax, ymax, zmax) = (xcentre, ycentre, zcentre) + (width, height, depth) / 2
or in full:
xmin = xcentre - width / 2
xmax = xcentre + width / 2
ymin = ycentre - height / 2
...

Related

How to reverse engineer this equation

I got an algorithm from a game engine on how to compute pixel position from tile position on a staggered tile map.
This algorithm gets the tile position using pixel position:
float diffX = 0;
if ((int)tilePos.y % 2 == 1)
diffX = tileSize.width / 2;
return Vec2 (
tilePos.x * tileSize.width + diffX,
(mapSize.height - tilePos.y - 1) * tileSize.height / 2);
How to reverse this algorithm to get tile position using pixel position.
Divide the plane into the rectangles circumscribing the tiles with even y coordinate, then adjust the result if it's in one of the four corner triangles that belong to another tile. Something like
# px is pixel x, py is pixel y
x = round(px / width)
y = 2 * round(py / height)
dx = px / width - x
dy = py / height - y / 2
if dx + dy > 0.5:
y = y + 1
elif dx - dy > 0.5:
y = y - 1
elif -dx + dy > 0.5:
x = x - 1
y = y + 1
elif -dx - dy > 0.5:
x = x - 1
y = y - 1
To solve this, this grid is split into rectangles like in this image. In this solution, it is assumed that x increases rightwards and y increases downwards. The origin, {0,0} in terms of pixels, is where the light blue circle is as in this image. Coordinates of these rectangles will look like this.
int px = pixel.x;
int py = pixel.y;
int w = tileSize.width;
int h = tileSize.height;
// find the coord that the pixel is in, in terms of the small rectangles
int x = 2px / w;
int y = 2py / h;
At this point, determine whether the pixel falls in which of the small rectangle as in this image. For this scenario, pixel is in
A : if [x_,y_] == [0,0]
B : if [x_,y_] == [1,0]
C : if [x_,y_] == [0,1]
D : if [x_,y_] == [1,1]
Knowing which rectangle the pixel is in will help us further modify [x,y] to find the actual coordinate in this staggered tile map. Here, tangent is used to determine if x or y needs to be increased, together with the knowledge of whether the pixel is in A, B, C or D. A is grouped with D while B is grouped with C due to identical tangent value.
// relative x and y value in small rectangle
int px_ = px % w;
int py_ = py % h;
// calculate tangent of tile
float tan_tile = h / w;
bool isInfinite = ( 0 == px );
float tan_pixel = 0.0f;
if ( !isInfinite )
{
tan_pixel = py_ / px_;
}
// pixel is in A or D
if ( 0 == ( x + y ) % 2 )
{
// tangent value is infinite
if ( 0 == px )
{
y += 1;
else
{
// For A and D, tangents are flipped along y-axis
if ( -tan_pixel <= -tan_tile )
{
y += 1;
}
}
}
// pixel is in B or C
else
{
// tangent value is infinite
if ( 0 == px )
{
y += 1;
}
else
{
if ( tan_pixel > tan_tile )
{
y += 1;
}
}
}
The above portion can be simplified further as
-tan_pixel <= -tan_tile
is actually
tan_pixel > tan_tile
Therefore, the part where tangent is compared becomes
// relative x and y value in small rectangle
int px_ = px % w;
int py_ = py % h;
// calculate tangent of tile
float tan_tile = h / w;
if ( 0 == px )
{
y += 1;
}
else
{
float tan_pixel = py_ / px_;
if ( tan_pixel > tan_tile )
{
y += 1;
}
}
Origin is in the center of tile [0,0], so x needs to be incremented by 1. This tile map is staggered such that x increases by 1 with width of 2 rectangles while y increases by 1 height of 1 rectangle, we need to divide x by 2 to get the actual coordinate.
x = ( x + 1 ) / 2;
And there you have it, the tile coordinate of pixel in this staggered map.

Getting dimension of specific segment window

I'm working on comparing the center of the blob with the 20% small box positioned at the center of the blob's bounding box.
I implemented this code first, to find the blob center points:
For y = 0 To bmp.ScaleHeight - 1
For x = 0 To bmp.ScaleWidth - 1
If bmp.Point(x, y) = vbWhite
Then
Xs = Xs + x
Ys = Ys + y
area = area + 1
endIF
Next x
Next y
YCenteroid = Ys / area
XCentroid = Xs / area
Then, the width and the height of the blob is calculated as below:
BlobHeight = MaxY - MinY
BlobWidth = MaxX - MinX
How to get that small box dimensions for comparing it with the center points?
Thanks
Coordinates of small box centered about (XCenteroid, YCenteroid) with width = 20% of blob width
RectLeft = XCentroid - 0.1 * BlobWidth
RectRight = XCentroid + 0.1 * BlobWidth
RectTop = YCentroid - 0.1 * BlobHeight
RectBottom = YCentroid + 0.1 * BlobHeight

Comparing Blob's centroid with the center of the bounding box

I'm trying to compare the blob's centroid with a small window centered in the middle of the blobs' bounding box. The dimensions of this window is 20% of the dimensions of the bounding box.
I implemented this algorithm first, to find the blob centroid
and this is the code:
For y = 0 To bmp.ScaleHeight - 1
For x = 0 To bmp.ScaleWidth - 1
If bmp.Point(x, y) = vbWhite
Then
Xs = Xs + x
Ys = Ys + y
area = area + 1
endIF
Next x
Next y
YCenteroid = Ys / area
XCentroid = Xs / area
Then I found the width and the height of the blob using
BlobHeight = MaxY - MinY
BlobWidth = MaxX - MinX
I have now the bounding box and the centroid How can I compare where is the centroid inside or outside the small centered box about 20% of the bounding box ?
You have the edges of the bounding box:
MinX MaxX
| |
########-MinY
# #
# #
# #
########-MaxY
Given BlobWidth, we know that the centered box starts at .4*BlobWidth, continues for .2*BlobWidth (up to (.4+.2)*BlobWidth = .6*BlobWidth).
MinCenteredX = MinX + 0.4*BlobWidth
MaxCenteredX = MinX + 0.6*BlobWidth
Now you just have to check if XCentroid is between them, that is:
MinCenteredX <= XCentroid And XCentroid <= MaxCenteredX
Now do the same again for the Y coordinates and you're done.

Place rectangle without a bounding box centered on a point

I have a bounding box (0 to 100 in both x and y), a point, a width and
a height. The point centers a rectangle given by height and width. How
do I find out where to place the rectangle so it doesn't go out of the
bounding box?
As an example, x = 100, y = 100, height = 20, width = 20. Here, I
should get the coordinates 80/80 here. Same for x=90 and y=90.
I have been told that this problem has been solved, so a link to the
corresponding wikipedia page is ok.
If I understand the problem right, and with the bounding box being (xmin, ymin, xmax, ymax), you could do it like this:
If x + width / 2 > xmax then x = xmax - width / 2
If y + height / 2 > ymax then y = ymax - height / 2
If x - width / 2 < xmin then x = xmin + width / 2
If y - height / 2 < ymin then y = ymin + height / 2

Optimally place a pie slice in a rectangle

Given a rectangle (w, h) and a pie slice with a radius less or equal to the smaller of both sides (w, h), a start angle and an end angle, how can I place the slice optimally in the rectangle so that it fills the room best (from an optical point of view, not mathematically speaking)?
I'm currently placing the pie slice's center in the center of the rectangle and use the half of the smaller of both rectangle sides as the radius. This leaves plenty of room for certain configurations.
Examples to make clear what I'm after, based on the precondition that the slice is drawn like a unit circle (i.e. 0 degrees on positive X axis, then running clock-wise):
A start angle of 0 and an end angle of PI would lead to a filled lower half of the rectangle and an empty upper half. A good solution here would be to move the center up by 1/4*h.
A start angle of 0 and an end angle of PI/2 would lead to a filled bottom right quarter of the rectangle. A good solution here would be to move the center point to the top left of the rectangle and to set the radius to the smaller of both rectangle sides.
This is fairly easy for the cases I've sketched but it becomes complicated when the start and end angles are arbitrary. I am searching for an algorithm which determines center of the slice and radius in a way that fills the rectangle best. Pseudo code would be great since I'm not a big mathematician.
The extrema of the bounding box of your arc are in the following format:
x + x0 * r = 0
x + x1 * r = w
y + y0 * r = 0
y + y1 * r = h
The values x0, x1, y0 and y1 are found by taking the minimum and maximum values of up to 7 points: any tangential points that are spanned (i.e. 0, 90, 180 and 270 degrees) and the end points of the two line segments.
Given the extrema of the axis-aligned bounding box of the arc (x0, y0), (x1, y1) the radius and center point can be calculated as follows:
r = min(w/(x1-x0), h/(y1-y0)
x = -x0 * r
y = -y0 * r
Here is an implementation written in Lua:
-- ensures the angle is in the range [0, 360)
function wrap(angle)
local x = math.fmod(angle, 2 * math.pi)
if x < 0 then
x = x + 2 * math.pi
end
return x
end
function place_arc(t0, t1, w, h)
-- find the x-axis extrema
local x0 = 1
local x1 = -1
local xlist = {}
table.insert(xlist, 0)
table.insert(xlist, math.cos(t0))
table.insert(xlist, math.cos(t1))
if wrap(t0) > wrap(t1) then
table.insert(xlist, 1)
end
if wrap(t0-math.pi) > wrap(t1-math.pi) then
table.insert(xlist, -1)
end
for _, x in ipairs(xlist) do
if x < x0 then x0 = x end
if x > x1 then x1 = x end
end
-- find the y-axis extrema
local ylist = {}
local y0 = 1
local y1 = -1
table.insert(ylist, 0)
table.insert(ylist, math.sin(t0))
table.insert(ylist, math.sin(t1))
if wrap(t0-0.5*math.pi) > wrap(t1-0.5*math.pi) then
table.insert(ylist, 1)
end
if wrap(t0-1.5*math.pi) > wrap(t1-1.5*math.pi) then
table.insert(ylist, -1)
end
for _, y in ipairs(ylist) do
if y < y0 then y0 = y end
if y > y1 then y1 = y end
end
-- calculate the maximum radius the fits in the bounding box
local r = math.min(w / (x1 - x0), h / (y1 - y0))
-- find x & y from the radius and minimum extrema
local x = -x0 * r
local y = -y0 * r
-- calculate the final axis-aligned bounding-box (AABB)
local aabb = {
x0 = x + x0 * r,
y0 = y + y0 * r,
x1 = x + x1 * r,
y1 = y + y1 * r
}
return x, y, r, aabb
end
function center_arc(x, y, aabb, w, h)
dx = (w - aabb.x1) / 2
dy = (h - aabb.y1) / 2
return x + dx, y + dy
end
t0 = math.rad(60)
t1 = math.rad(300)
w = 320
h = 240
x, y, r, aabb = place_arc(t0, t1, w, h)
x, y = center_arc(x, y, aabb, w, h)
print(x, y, r)
Example output:
Instead of pseudo code, I used python, but it should be usable. For this algorithm, I assume that startAngle < endAngle and that both are within [-2 * PI, 2 * PI]. If you want to use both within [0, 2 * PI] and let startAngle > endAngle, I would do:
if (startAngle > endAngle):
startAngle = startAngle - 2 * PI
So, the algorithm that comes to mind is to calculate the bounds of the unit arc and then scale to fit your rectangle.
The first is the harder part. You need to calculate 4 numbers:
Left: MIN(cos(angle), 0)
Right: MAX(cos(angle), 0)
Top: MIN(sin(angle),0)
Bottom: MAX(sin(angle),0)
Of course, angle is a range, so it's not as simple as this. However, you really only have to include up to 11 points in this calculation. The start angle, the end angle, and potentially, the cardinal directions (there are 9 of these going from -2 * PI to 2 * PI.) I'm going to define boundingBoxes as lists of 4 elements, ordered [left, right, top, bottom]
def IncludeAngle(boundingBox, angle)
x = cos(angle)
y = sin(angle)
if (x < boundingBox[0]):
boundingBox[0] = x
if (x > boundingBox[1]):
boundingBox[1] = x
if (y < boundingBox[2]):
boundingBox[2] = y
if (y > boundingBox[3]):
boundingBox[3] = y
def CheckAngle(boundingBox, startAngle, endAngle, angle):
if (startAngle <= angle and endAngle >= angle):
IncludeAngle(boundingBox, angle)
boundingBox = [0, 0, 0, 0]
IncludeAngle(boundingBox, startAngle)
IncludeAngle(boundingBox, endAngle)
CheckAngle(boundingBox, startAngle, endAngle, -2 * PI)
CheckAngle(boundingBox, startAngle, endAngle, -3 * PI / 2)
CheckAngle(boundingBox, startAngle, endAngle, -PI)
CheckAngle(boundingBox, startAngle, endAngle, -PI / 2)
CheckAngle(boundingBox, startAngle, endAngle, 0)
CheckAngle(boundingBox, startAngle, endAngle, PI / 2)
CheckAngle(boundingBox, startAngle, endAngle, PI)
CheckAngle(boundingBox, startAngle, endAngle, 3 * PI / 2)
CheckAngle(boundingBox, startAngle, endAngle, 2 * PI)
Now you've computed the bounding box of an arc with center of 0,0 and radius of 1. To fill the box, we're going to have to solve a linear equation:
boundingBox[0] * xRadius + xOffset = 0
boundingBox[1] * xRadius + xOffset = w
boundingBox[2] * yRadius + yOffset = 0
boundingBox[3] * yRadius + yOffset = h
And we have to solve for xRadius and yRadius. You'll note there are two radiuses here. The reason for that is that in order to fill the rectangle, we have to multiple by different amounts in the two directions. Since your algorithm asks for only one radius, we will just pick the lower of the two values.
Solving the equation gives:
xRadius = w / (boundingBox[1] - boundingBox[0])
yRadius = h / (boundingBox[2] - boundingBox[3])
radius = MIN(xRadius, yRadius)
Here, you have to check for boundingBox[1] - boundingBox[0] being 0 and set xRadius to infinity in that case. This will give the correct result as yRadius will be smaller. If you don't have an infinity available, you can just set it to 0 and in the MIN function, check for 0 and use the other value in that case. xRadius and yRadius can't both be 0 because both sin and cos would have to be 0 for all angles included above for that to be the case.
Now we have to place the center of the arc. We want it centered in both directions. Now we'll create another linear equation:
(boundingBox[0] + boundingBox[1]) / 2 * radius + x = xCenter = w/2
(boundingBox[2] + boundingBox[3]) / 2 * radius + y = yCenter = h/2
Solving for x and y, the center of the arc, gives
x = w/2 - (boundingBox[0] + boundingBox[1]) * radius / 2
y = h/2 - (boundingBox[3] + boundingBox[2]) * radius / 2
This should give you the center of the arc and the radius needed to put the largest circle in the given rectangle.
I haven't tested any of this code, so this algorithm may have huge holes, or perhaps tiny ones caused by typos. I'd love to know if this algoritm works.
edit:
Putting all of the code together gives:
def IncludeAngle(boundingBox, angle)
x = cos(angle)
y = sin(angle)
if (x < boundingBox[0]):
boundingBox[0] = x
if (x > boundingBox[1]):
boundingBox[1] = x
if (y < boundingBox[2]):
boundingBox[2] = y
if (y > boundingBox[3]):
boundingBox[3] = y
def CheckAngle(boundingBox, startAngle, endAngle, angle):
if (startAngle <= angle and endAngle >= angle):
IncludeAngle(boundingBox, angle)
boundingBox = [0, 0, 0, 0]
IncludeAngle(boundingBox, startAngle)
IncludeAngle(boundingBox, endAngle)
CheckAngle(boundingBox, startAngle, endAngle, -2 * PI)
CheckAngle(boundingBox, startAngle, endAngle, -3 * PI / 2)
CheckAngle(boundingBox, startAngle, endAngle, -PI)
CheckAngle(boundingBox, startAngle, endAngle, -PI / 2)
CheckAngle(boundingBox, startAngle, endAngle, 0)
CheckAngle(boundingBox, startAngle, endAngle, PI / 2)
CheckAngle(boundingBox, startAngle, endAngle, PI)
CheckAngle(boundingBox, startAngle, endAngle, 3 * PI / 2)
CheckAngle(boundingBox, startAngle, endAngle, 2 * PI)
if (boundingBox[1] == boundingBox[0]):
xRadius = 0
else:
xRadius = w / (boundingBox[1] - boundingBox[0])
if (boundingBox[3] == boundingBox[2]):
yRadius = 0
else:
yRadius = h / (boundingBox[3] - boundingBox[2])
if xRadius == 0:
radius = yRadius
elif yRadius == 0:
radius = xRadius
else:
radius = MIN(xRadius, yRadius)
x = w/2 - (boundingBox[0] + boundingBox[1]) * radius / 2
y = h/2 - (boundingBox[3] + boundingBox[2]) * radius / 2
edit:
One issue here is that sin[2 * PI] is not going to be exactly 0 because of rounding errors. I think the solution is to get rid of the CheckAngle calls and replace them with something like:
def CheckCardinal(boundingBox, startAngle, endAngle, cardinal):
if startAngle < cardinal * PI / 2 and endAngle > cardinal * PI / 2:
cardinal = cardinal % 4
if cardinal == 0:
boundingBox[1] = 1
if cardinal == 1:
boundingBox[3] = 1
if cardinal == 2:
boundingBox[0] = -1
if cardinal == 3:
boundingBox[2] = -1
CheckCardinal(boundingBox, startAngle, endAngle, -4)
CheckCardinal(boundingBox, startAngle, endAngle, -3)
CheckCardinal(boundingBox, startAngle, endAngle, -2)
CheckCardinal(boundingBox, startAngle, endAngle, -1)
CheckCardinal(boundingBox, startAngle, endAngle, 0)
CheckCardinal(boundingBox, startAngle, endAngle, 1)
CheckCardinal(boundingBox, startAngle, endAngle, 2)
CheckCardinal(boundingBox, startAngle, endAngle, 3)
CheckCardinal(boundingBox, startAngle, endAngle, 4)
You still need IncludeAngle(startAngle) and IncludeAngle(endAngle)
Just consider a circle and forget the filling. The bounds will either be the center of the circle, the endpoints, or the points at 0, 90, 180, or 270 degrees (if they exist in this slice). The maxima and minima of these seven points will determine your bounding rectangle.
As far as placing it in the center, calculate the average of the max and min for both the rectangle and the pie slice, and add or subtract the difference of these to whichever one you want to move.
I would divide the problem into three steps:
Find the bounding box of a unit pie slice (or if a radius is given the actual pie slice centered at (0, 0)).
Fit the bounding box in your rectangle.
Use the information about fitting the bounding box to adjust the center and radius of the pie slice.
When I have time, I may flush this out with more details.

Resources