I've got a project where I'm designing an image viewer for tiled images. Every image tile is 256x256 pixels. For each level of scaling, I'm increasing the size of each image by 5%. I represent the placement of the tiles by dividing the screen into tiles the same size as each image. An offset is used to precicely place each image where needed. When the scaling reaches a certain point(1.5), I switch over to a new layer of images that altogether has a greater resolution than the previous images. The zooming method itself looks like this:
def zoomer(self, mouse_pos, zoom_in): #(tuple, bool)
x, y = mouse_pos
x_tile, y_tile = x / self.tile_size, y / self.tile_size
old_scale = self.scale
if self.scale > 0.75 and self.scale < 1.5:
if zoom_in:
self.scale += SCALE_STEP # SCALE_STEP = 5% = 0.05
ratio = (SCALE_STEP + 1)
else:
self.scale -= SCALE_STEP
ratio = 1 / (SCALE_STEP + 1)
else:
if zoom_in:
self.zoom += 1
self.scale = 0.8
ratio = (SCALE_STEP + 1)
else:
self.zoom -= 1
self.scale = 1.45
ratio = 1 / (SCALE_STEP + 1)
# Results in x/y lengths of the relevant full image
x_len = self.size_list[self.levels][0] / self.power()
y_len = self.size_list[self.levels][1] / self.power()
# Removing extra pixel if present
x_len = x_len - (x_len % 2)
y_len = y_len - (y_len % 2)
# The tile's picture coordinates
tile_x = self.origo_tile[0] + x_tile
tile_y = self.origo_tile[1] + y_tile
# The mouse's picture pixel address
x_pic_pos = (tile_x * self.tile_size) -
self.img_x_offset + (x % self.tile_size)
y_pic_pos = (tile_y * self.tile_size) -
self.img_y_offset + (y % self.tile_size)
# Mouse percentile placement within the image
mouse_x_percent = (x_pic_pos / old_scale) / x_len
mouse_y_percent = (y_pic_pos / old_scale) / y_len
# The mouse's new picture pixel address
new_x = (x_len * self.scale) * mouse_x_percent
new_y = (y_len * self.scale) * mouse_y_percent
# Scaling tile size
self.tile_size = int(TILE_SIZE * self.scale)
# New mouse screen tile position
new_mouse_x_tile = x / self.tile_size
new_mouse_y_tile = y / self.tile_size
# The mouse's new tile address
new_tile_x = new_x / self.tile_size
new_tile_y = new_y / self.tile_size
# New tile offsets
self.img_x_offset = (x % self.tile_size) - int(new_x % self.tile_size)
self.img_y_offset = (y % self.tile_size) - int(new_y % self.tile_size)
# New origo tile
self.origo_tile = (int(new_tile_x) - new_mouse_x_tile,
int(new_tile_y) - new_mouse_y_tile)
Now, the issue arising from this is that the mouse_.._percent variables never seem to match up with the real position. For testing purposes, I feed the method with a mouse position centered in the middle of the screen and the picture centered in the middle too. As such, the resulting mouse_.._percent variable should, in a perfect world, always equal 50%. For the first level, it does, but quickly wanders off when scaling. By the time I reach the first zoom breakpoint (self.scale == 1.5), the position has drifted to x = 48%, y = 42%.
The self.origo_tile is a tuple containing the x/y coordinate for the tile to be drawn on screen tile (0, 0)
I've been staring at this for hours, but can't seen to find a remedy for it...
How the program works:
I apologize that I didn't have enough time to apply this to your code, but I wrote the following zooming simulator. The program allows you to zoom the same "image" multiple times, and it outputs the point of the image that would appear in the center of the screen, along with how much of the image is being shown.
The code:
from __future__ import division #double underscores, defense against the sinister integer division
width=256 #original image size
height=256
posx=128 #original display center, relative to the image
posy=128
while 1:
print "Display width: ", width
print "Display height: ", height
print "Center X: ", posx
print "Center Y: ", posy
anchx = int(raw_input("Anchor X: "))
anchy = int(raw_input("Anchor Y: "))
zmag = int(raw_input("Zoom Percent (0-inf): "))
zmag /= 100 #convert from percent to decimal
zmag = 1/zmag
width *= zmag
height *= zmag
posx = ((anchx-posx)*zmag)+posx
posy = ((anchy-posy)*zmag)+posy
Sample output:
If this program outputs the following:
Display width: 32.0
Display height: 32.0
Center X: 72.0
Center Y: 72.0
Explanation:
This means the zoomed-in screen shows only a part of the image, that part being 32x32 pixels, and the center of that part being at the coordinates (72,72). This means on both axes it is displaying pixels 56 - 88 of the image in this specific example.
Solution/Conclusion:
Play around with that program a bit, and see if you can implement it into your own code. Keep in mind that different programs move the Center X and Y differently, change the program I gave if you do not like how it works already (though you probably will, it's a common way of doing it). Happy Coding!
Okay algebra and trig are not my strong suit by any means so here is what I need to do.
I have a circle which is measured in degrees from +180 to -180 (360 total)
Given the center point of the circle stays the same, Cx , Cy.
The angle varies from -180 to +180
I need to locate a point that regardless the given angle is + 3 units away that is at the 90 degree position and the 270 degree position (from the given degrees)
So like...
Angle = 0
Point 1 -> x = 0, y -3
Point 2 -> x = 0, y + 3
And if the angle was say 90 (provided its measured Clockwise)
Point 1 -> x = -3, y = 0
Point 2 -> x = 3, y = 0
What I need is a forumla that will accept Angle, then tell me what my x/y should be 3 units away from the origin.
I have tried: EDIT Updated to double precision using Java.
`double x = Cx + 3 * Math.cos((d + 90) * Math.PI / 180);'
'double y = Cy + 3 * Math.sin((d + 90) * Math.PI / 180);`
this gives me mixed results, I mean sometimes it's where I think it should be and other times its quite wrong.
Assuming Cx = 0.500, Cy = 0.500
Sample Data: Result:
Deg = 0 x = 2 / y = 5
Deg = 90 x = -1 / y = 2
Deg = 125 x = -0.457 / y = 0.297
Deg = 159 x = 0.924 / y = -0.800
I realize I am only calculating one point at this point but do you have any suggestions on how to get the first point working? at say 90 degrees from whatever degree I start with?
x = Cx + r * Math.cos( (d+90) * Math.PI / 180 );
y = Cy + r * Math.sin( (d+90) * Math.PI / 180 );
Seems that this is the correct formula for what I was trying to accomplish. This will take any value for Cx/Cy's origin add the Radius r, then calculate the degrees + 90 and convert to radians.. Once all that magic takes place, you're left with an x/y coord that is 90 degrees of where you started.
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
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
I'm frequently fitting one rectangle into another so that it fits nicely and is centered.
I would draw something on the whiteboard and take a picture of what the logic is but it's getting dark and candle light makes it not as fun to do.
Anyway, it's quite straightforward and easy to understand. Here is the function I just had to write out from scratch again (this time in PHP):
// Fit rectangle 2 into rectangle 1 to get rectangle 3
// Rectangle 3 must be centered
// Return dimensions of rectangle and position relative to rectangle 1
function fitrect($w1,$h1,$w2,$h2){
// Let's take a chance with rectangle 3 width being equal to rectangle 1 width
$w3=$w1;
$h3=$w3*($h2/$w2);
// Check if height breaks rectangle 1 height
if($h3>$h1){
// Recalculate dimensions and then position
$h3=$h1;
$w3=$h3*($w2/$h2);
$x3=($w1-$w3)/2;
$y3=0;
}else{
// Just calculate position
$y3=($h1-$h3)/2;
$x3=0;
}
// Tidy up
$x3=round($x3);
$y3=round($y3);
$w3=round($w3);
$h3=round($h3);
// Result array
$res=array($x3,$y3,$w3,$h3);
return($res);
}
I'd like to understand this algorithm and other versions of it so that my brain groks the foundations so that I never have to rely on pen and paper (or the whiteboard) again.
So, how would you do this? What fluff can be removed?
EDIT: As an example - say we have rectangle 1 to have dimensions 256x256 and rectangle 2 to be 44x167. Then we need to scale rectangle 2 to 67x256 and position it at 94,0 relative to rectangle 1 so that it sits maximized and centralized in rectangle 1.
Here's how I would do it.
let's define a term, fatness, that is equal to the ratio of a rectangle's width to its height. a rectangle with height 1 and width 10 has fatness 10. A rectangle with height 20 and width 10 has fatness 0.5. When you resize a rectangle, its fatness doesn't change.
When you scale rectangle 2 up or down in size so that its width is equal to rectangle 1, it will not overflow the top or bottom as long as rectangle 2 is fatter than rectangle 1. It will overflow if 1 is fatter than 2. Now you know ahead of time whether to resize for a snug width, or a snug height. Furthermore, the translation logic is the same for both cases, so that can go outside of the if/else block.
in pseudocode: (sorry, I don't know PHP)
fatness1 = w1 / h1
fatness2 = w2 / h2
#adjust scaling
if fatness2 >= fatness1:
#scale for a snug width
scaleRatio = w1 / w2
else:
#scale for a snug height
scaleRatio = h1 / h2
w3 = w2 * scaleRatio
h3 = h2 * scaleRatio
#adjust rectangle 3's center so it is the same as 1's center
xCenterOf1 = x1 + (w1 / 2)
yCenterOf1 = y1 + (h1 / 2)
x3 = xCenterOf1 - (w3 / 2)
y3 = yCenterOf1 - (h3 / 2)
return (x3, y3, w3, h3)
Testing in python, assuming rectangle 1 is at (0,0), scale(256,256, 44, 167) returns (0.0, 94.3, 256.0, 67.4).
Here's a handy function written in Java.
public static RectF fitRectWithin(Rect inner, Rect outer) {
float innerAspectRatio = inner.width() / (float) inner.height();
float outerAspectRatio = outer.width() / (float) outer.height();
float resizeFactor = (innerAspectRatio >= outerAspectRatio) ?
(outer.width() / (float) inner.width()) :
(outer.height() / (float) inner.height());
float newWidth = inner.width() * resizeFactor;
float newHeight = inner.height() * resizeFactor;
float newLeft = outer.left + (outer.width() - newWidth) / 2f;
float newTop = outer.top + (outer.height() - newHeight) / 2f;
return new RectF(newLeft, newTop, newWidth + newLeft, newHeight + newTop);
}
Here's how I did it. (This algorithm works great with images.)
Let's say you have a rectangle, and a container (also a rectangle):
aspectRatio = screen.width / screen.height
if (rectangle.width >= rectangle.height)
{
resizeFactor = container.width / rectangle.width
rectangle.width = rectangle.width * resizeFactor
rectangle.height = rectangle.height * resizeFactor * aspectRatio
}
else
{
resizeFactor = container.height / rectangle.height
rectangle.width = rectangle.width * resizeFactor / aspectRatio
rectangle.height = rectangle.height * resizeFactor
}
You can optimise this algorithm a little bit by changing line 6 to:
rectangle.width = container.width
and the same to line 13 if you want to.
I just had to deal with something similar: let's call rectangle2 the image, and rectangle1 the window. So our task is to fit an image into the window.
Also in my scenario, both the image and the window have an "inner" coordinates system of [-1,1]X[-1,1]. In fact, inside the window, there is a viewport (rectangle3 in the question) where the image will be projected, and so our task is to find the largest viewport (a rectangle inside window) that has the same aspect ratio (width/height) as image.
The viewport is determined by a fraction of the width/height of the image:
viewport_width = scale_w * window_w
viewport_height = scale_h * window_h
So our goal is to find the right scaling of the width and height of window, such that the viewport will have the same aspect ratio (width/height) as the image:
# goal:
scale_w * window_w == image_w
scale_h * window_h == image_h
Which means that the scale we are looking for satisfies:
(scale_w / scale_h) == ((image_w / image_h) / (window_w / window_h))
Let's denote the right hand side of this equation by s for a moment.
Since we are looking for the maximal viewport, surely at least one out of scale_w and scale_h will be equal to 1. So - if s<1, meaning the window is wider than the image, we will have scale_w=s, scale_h=1 (this corresponds to the figure above, with scale_w=0.5). And if s>1, meaning the image is wider than the window, we will have scale_w=1, scale_h=1/s.
In python code it looks like this:
def compute_viewport_scale(image_size, window_size):
image_w, image_h = image_size
window_w, window_h = window_size
im_ratio, win_ratio = image_w / image_h, window_w / window_h
scale = im_ratio / win_ratio
if scale > 1: # image is wider than screen
scale_w = 1
scale_h = 1 / scale
else: # window is wider then image
scale_w = scale
scale_h = 1
viewport_w, viewport_h = window_w * scale_w, window_h * scale_h
assert (round(viewport_w / viewport_h, 5) == round(image_w / image_h, 5))
return scale_w, scale_h
Since I wanted to be absolutely sure I have all the cases covered, I constructed a list of examples for sizes, to make sure indeed this formula is correct. And indeed, if you run all these lines, you will get no assert errors :)
# aspect ratio = 1
compute_viewport_scale((100, 100), (100, 100))
compute_viewport_scale((100, 100), (200, 200))
compute_viewport_scale((200, 200), (100, 100))
# image is wider than screen: (im_w / im_h) > (win_w / win_h) [ i.e. im_ratio > win_ratio ]
# (im_ratio < 1 and win_ratio > 1 cant happen)
# im_w > win_w and im_h > win_h
compute_viewport_scale((300, 200), (100, 90)) # im_ratio > 1 and win_ratio > 1
compute_viewport_scale((200, 300), (100, 200)) # im_ratio < 1 and win_ratio < 1
compute_viewport_scale((300, 200), (100, 150)) # im_ratio > 1 and win_ratio < 1
# im_w > win_w and im_h < win_h
compute_viewport_scale((150, 50), (110, 100)) # im_ratio > 1 and win_ratio > 1
compute_viewport_scale((200, 300), (100, 400)) # im_ratio < 1 and win_ratio < 1
compute_viewport_scale((300, 100), (100, 150)) # im_ratio > 1 and win_ratio < 1
# (im_w < win_w and im_h > win_h cant happen)
# im_w < win_w and im_h < win_h
compute_viewport_scale((150, 50), (200, 100)) # im_ratio > 1 and win_ratio > 1
compute_viewport_scale((90, 100), (100, 200)) # im_ratio < 1 and win_ratio < 1
compute_viewport_scale((110, 100), (100, 200)) # im_ratio > 1 and win_ratio < 1
# image is wider than screen: (im_w / im_h) < (win_w / win_h) [ i.e. im_ratio < win_ratio ]
# (im_ratio > 1 and win_ratio < 1 cant happen)
# im_w > win_w and im_h > win_h
compute_viewport_scale((300, 200), (100, 50)) # im_ratio > 1 and win_ratio > 1
compute_viewport_scale((200, 400), (100, 150)) # im_ratio < 1 and win_ratio < 1
compute_viewport_scale((200, 400), (150, 100)) # im_ratio < 1 and win_ratio > 1
# (im_w > win_w and im_h < win_h cant happen)
# im_w < win_w and im_h > win_h
compute_viewport_scale((150, 100), (200, 50)) # im_ratio > 1 and win_ratio > 1
compute_viewport_scale((200, 400), (380, 390)) # im_ratio < 1 and win_ratio < 1
compute_viewport_scale((200, 400), (390, 380)) # im_ratio < 1 and win_ratio > 1
# im_w < win_w and im_h < win_h
compute_viewport_scale((300, 200), (600, 300)) # im_ratio > 1 and win_ratio > 1
compute_viewport_scale((200, 300), (500, 600)) # im_ratio < 1 and win_ratio < 1
compute_viewport_scale((50, 100), (300, 200)) # im_ratio < 1 and win_ratio > 1