Place rectangle without a bounding box centered on a point - algorithm

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

Related

Trying to make Conway's game of life, how do I get the rect to fit into a grid square in pygame

import pygame, copy,random
w,h = 500,500
cellsize=5
cells=[]
pygame.init()
width, height = w/cellsize, h/cellsize
width = int(width)
height=int(height)
dis= pygame.display.set_mode((w,h))
dis.fill((0,0,0))
randomBool=[]
cellPc=0.001
count = totalcount = 0
for y in range(h):
randomBool.append([])
for x in range(w):
if random.random() < cellPc:
randomBool[y].append(True)
count += 1
else:
randomBool[y].append(False)
totalcount +=1
#sojipo ajs;koojihhasuiio h;asjioasddfoiaidhoiiosaiof
running=True
while running:
for event in pygame.event.get():
if event.type==pygame.QUIT:
quit()
for x in range(0, w, cellsize):
pygame.draw.line(dis,(123,123,123),(x,0),(x,h))
for y in range(0, h, cellsize):
pygame.draw.line(dis,(123,123,123),(0,y),(w,y))
pygame.display.update()
for y in range(h):
for x in range(w):
if randomBool[y][x]==True:
pygame.draw.rect(dis,(255,0,0),(x,y,cellsize,cellsize))
I thought it would just fit in the grid but the matrix wont work. I'm semi-new to the idea of matrices in python, so I'm mostly out of my league here. How exactly can I make them fit on the grid. Any help will be appreciated, thanks.
Use the // (floor division) operator to calculate the number of rows and columns:
width, height = w/cellsize, h/cellsize
width, height = w // cellsize, h // cellsize
The number of columns and rows of the grid is width x height and not w x h. The top left position of a cell is (col * cellsize, row * cellsize):
for row in range(height):
for col in range(width):
if randomBool[row][col]==True:
x, y = col * cellsize, row * cellsize
pygame.draw.rect(dis, (255,0,0), (x, y, cellsize, cellsize))

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.

How do I translate and scale points within a bounding box?

I have a number of points P of the form (x, y) where x,y are real numbers. I want to translate and scale all these points within a bounding box (rectangle) which begins at the point (0,0) (top left) and extends to the point (1000, 1000) (bottom right).
Why is it that the following algorithm does not produce points in that bounding box?
for Point p in P:
max = greatest(p.x, p.y, max)
scale = 1000 / max
for Point p in P:
p.x = (p.x - 500) * scale + 500
p.y = (p.y - 500) * scale + 500
I fear that this won't work when p.x or p.y is a negative number.
I would also like to maintain the "shape" of the points.
Find all of yMin, yMax, xMin, xMax, xDelta = xMax-xMin and yDelta = yMax-yMin for your set of points.
Set max = greatest(xDelta,yDelta).
Foreach Point p set p.X = (p.X - xMin) * scale and p.Y = (p.Y - yMin) * scale

Minimum and maximum of a box?

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
...

Resources