0

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 Y

For example the width, 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: 10

I know this is basic, but I seriously am having a mind cramp right now O_o.

Thanks,
Christian

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
  • Whatever you do, avoid dividing by zero. Assuming this problem is in relation to programming you are doing for the LHC, I do not wish for the universe to end just yet. – THE DOCTOR Dec 24 '10 at 22:05
  • You need to clarify your coordinate systems. At one stage the width is in pixels (e.g. 40) and elsewhere in relative coordinates (-0.5). Also how can the width be -0.5 if the extent is -1 to +1. You need to be clear what your quantities are and then the answer may be clearer – peter.murray.rust Dec 24 '10 at 22:05
  • I guess if you do a drawing, it'll be a lot easier to answer and perhaps you could realize better what you are trying to achieve. – Dr. belisarius Dec 24 '10 at 22:06
  • check out [Math - mapping numbers](http://stackoverflow.com/questions/345187/math-mapping-numbers) – Nick Dandoulakis Dec 24 '10 at 22:06
  • Dividing by zero = Black Hole – Christian Stewart Dec 25 '10 at 04:02

2 Answers2

3
Xnew = 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.

Vilx-
  • 104,512
  • 87
  • 279
  • 422
1

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)

Vladimir
  • 1,781
  • 13
  • 12