Very Simple Math Question
Very simple math question.
Say I have an image with a point being tracked in it. Here are my variables:
Image Height
Image Width Point (pixles from left) coordinate X Point (pixles from top) coordinate YFor example the wid开发者_开发技巧th, I want it to return a value of -0.5, which represents the distance from the center, such that 1 would be the total right, and -1 would be the total left.
So, how would I calculate so that
The point was (width) a quarter way across the entire frame, or a half way across the left SIDE of the frame. The variables would equal:
Image width: 40
Point X: 10I know this is basic, but I seriously am having a mind cramp right now O_o.
Thanks,
ChristianXnew = 2*X/Width - 1
Ynew = 2*Y/Height - 1
Explanation:
X/Width
gives you value from 0 (total left) to 1 (total right). 2*X/Width
then gives a value from 0 (total left) to 2 (total right). Subtract 1 to get a value from -1 (total left) to 1 (total right).
The same for Y.
If image width is 40, and Point x is 10, then in "your" coordinates PointX will be 0.5 (assuming that coordinates are from -20 to 20). So:
PointX = 1 - 2 * (X / ImageWidth)
PointY = 1 - 2 * (Y / ImageHeight)
Checkout:
PointX = 1 - 2 * (10 / 40) = 0.5
(or 10 pixels to the right side)
精彩评论