This is very programming related but a somewhat non-programming question. I am performing image scaling in a web based application and I need to maintain my image relative to a fixed location even though it scales anchored by its top, left corner. Hope the graphic make this possible.
The idea is that C is a fixed location that I want to maintain as my scaling origin rather than B which which is the current css behavior. C may or may not be within the actual image. So as the image scale, B needs to move relative to C. Example: if the image was scaled 50%, then B would move 1/2 the distance to C. If the image grew to 200% of its size, then B would move twice the distance away from C.
Ultimately looking for a formula for x & y for B given the location of C and a scaling factor for the image. Not sure the size of the image needs to be part of this but I have it if needed.
Thanks for any help!
Things I know:
- I know the width and height of the image rectangle.
- I know the offset of B from A.
- I know the offset of C from A.
- I know the scale factor in percent of the image.
Effectively, you want to treat C as the origin, and just "move" B by the scaling amount. By treating it as a vector from C to B, and scaling it by the amount in question, you can do this fairly easily.
For example, with a scale of 0.5 (50%), this becomes:
With a scale of 2 (200%):
First you need to calculate the distance from B to C, then you just change that to scale, and that is where the new B is relative to C:
As you want the coordinates, it's the same function for x and y:
(The scale value used is not percentage but a size multiplier. An increase in size by 50% gives a scale of
1.5
.)here's a C# snippet that is tested to work:
it really just echo's @Reed's Answer
So you want point
C
in the image which is currently at(C_x, C_y)
to remain at the same position after scaling the image by a factor ofs
?New position of
C
, say,C_new = (s*C_x,s*C_y)
.And you want to move the image so that
C_new = C
.Which means you'll have to shift
B = (B_x,B_y)
by(s*C_x-C_x,s*C_y-C_y)
, or the new origin of the image, sayB_new
is:So now you can display the scaled image at
B_new
--- andC
should remain fixed.If I understand the problem:
The formula seems simple enough, but your sample image can't get any larger than 133x200 (scale = 133%) before it overruns Y=0 (which I assume is your northern limit).
If you want to stop it from moving past Y=0 or X=0, and push-out further to the south and east once it reaches either limit, one approach might be:
I think scale should be converted to height and width, instead of using scale as a variable in these formulas, since your original image could be any size (assuming they're not always going to be 100x150 per your sample)