简体   繁体   中英

Convert X Coordinate from one Resolution to another

usually i would do this to convert from one resolution into another:

int newX = (x / oldResolutionX) * newResolutionX;

But this time i can't use it and i can't get my head around the math (never was really good at math, forgive me please). Anyways, my Resolution is 1280x720 and i want to convert a point like (720/360) to this: Picture of new Resolution

The Width is 854, Height is 720, so i don't have to do a conversion for the y coordinate. But here's the (for me) tricky part: 0 is not the actual 0. X starts at -107 and ends at 747. Could you guys explain to me how i can convert (720/360) from 1280x720 into this Resolution? Thanks in advance and sorry for being bad at math...

So, you need a linear mapping from [0...1280] to [-107...747], leaving y untouched.

int newX = (int)((x / 1280.0) * 854.0) -107

So, whats happening here?

  • x will vary between 0 and 1280.
  • if x == 0: (0/1280) * 854 will be 0 and the result equals -107.
  • if x == 1280: (1280/1280) * 854 = 854: the result will be 747

Some remarks:

  • the .0 is used to force a floating-point type.
  • the (int) is to cast it back to a integer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM