Search code examples
geometrytransformationresolutionscalingcoordinate-transformation

Transform points between two screens


I have a large collection of points that I want to transform from one screen/monitor to another in a script.

Screen A - 2880x1800 resolution
Screen B - 1280x1024 resolution

enter image description here

• Black box: screen/monitor.
• Green box: portion of the screen where all points will be transformed from C(a) to C(b).

The green box is a video feed with an aspect ratio of 16:9 on both screens. I'm not actually sure this green box matters but I included it to show what I'm trying to do.

So far I've tried to follow this post, which seems to work fine for transforming x values:

Rectangle Old
NW coordinate - (X0_Old, Y0_Old)
SE coordinate - (X1_Old, Y1_Old)

Rectangle New
NW coordinate - (X0_New, Y0_New)
SE coordinate - (X1_New, Y1_New)

X_New = X0_New + (X_Old - X0_Old) * X_Coeff
X_New =   0    + (X_Old -   0   ) * X_Coeff
  where 
    X_Coeff = (X1_New - X0_New) / (X1_Old - X0_Old)
    X_Coeff = (1280   -   0   ) / (2880   -   0   )

X_New = (X_Old)*(1280/2880)

That X_New wasn't perfect, but for some reason this one was:
X_New = 2*(X_Old)*(1280/2880)

Maybe this was due to some kind of scaling? The x coordinates were working so I didn't question it and moved on.

My problem came when I tried to transform the Y coordinates:

Y_New = Y0_New + (Y_Old - Y0_Old) * Y_Coeff
Y_Coeff = (Y1_New - Y0_New) / (Y1_Old - Y0_Old)

Y_New = Y0_New + (Y_Old - Y0_Old) * Y_Coeff
Y_New =    0   + (Y_Old -   136 ) * Y_Coeff
  where
    Y_Coeff = (Y1_New - Y0_New) / (Y1_Old - Y0_Old)
    Y_Coeff = (  720  -   174 ) / (1620   -   136)
    Y_Coeff = 546/1484

Y_New = (Y_Old - 136) * (546/1484)

Y values calculated with Y_New are definitely much too low to be correct. What's the right way to calculate Y_New?


Solution

  • Definitely, formula X_New = (X_Old)*(1280/2880) is correct - it transforms 0 to 0, 2880 to 1280, 1440 to 640. Perhaps you really have some kind of scaling?

    About Y - in this case (limited by X-axis) you should use the same coefficient X_Coeff for Y too.

    But I can see that 16x9 rectangle at the second screen has size 1280x720 with empty stripes of height 152, and for the first screen size is 2880x1620 with empty stripes of height 90

    Y_New = 152 + (Y_Old -  90) * X_Coeff
    

    In general case one have to choose either X_Coeff or Y_Coeff depending on ratio between screen and video size rectangle.