Go Down

Topic: Touchscreen and fixed axis (Read 2796 times) previous topic - next topic

JRMN

I am working with a touchscreen.  It's wired and working correctly. 

Not sure how to explain this, this is the current coordinate system and I am able to get (x,y) coordinates. 




Virtually speaking, if I rotate the quadrant right with a fix origin (0,0).



How would I go about writing write a sketch to remap (x,y) to the rotated quadrant with the fix origin?  I have been looking at this for about two hours and I can't wrap my head around it.

PaulS

If you simply swap X and Y values, your

1   2

3   4

picture becomes

1   3

2   4

If you then subtract each swapped X value from Wmax (maximum width value), the picture becomes

3   1

4   2

which looks like your second picture.

JRMN

Can you explain the last part about subtracting each swapped X value from Wmax?  X and Y ranges from from 0 to 1023, are you saying subtract 1023 from X?

PaulS

#3
Sep 25, 2011, 02:06 pm Last Edit: Sep 25, 2011, 02:26 pm by PaulS Reason: 1
Quote
are you saying subtract 1023 from X?

No. Subtract x from 1023.

If you swap X and Y, X now ranges from from 0 to 1023 (1 to 3). To make the values range from 1023 to 0 (3 to 1) instead, subtract the value from 1023. 1023 - 0 = 1023. 1023 - 1023 = 0.

Lets use some variables to make this clear. You have hMin and hMax for the horizontal axis, and vMin and vMax for the vertical axis.

The points on the initial diagram:
1 = hMin, vMin
2 = hMax, vMin
3 = hMin, vMax
4 = hMax, vMax

You want the points to be:
3 = hMin, vMin
1 = hMax, vMin
4 = hMin, vMax
2 = hMax, vMax

You have some reading h, v. h increases down the screen, and v increases to the right. You want the values to increase to the right, and down the screen.

Code: [Select]
int h, v;

// Swap h and v
int temp = h;
h = v;
v = temp;


If h is 0 and v is 0 (point 1 on view 1), which you want to become hMax, vMin (point 3 on view 2). v is already vMin, but h is at the opposite end of the scale, so h needs to be set to hMax - h.

If h is 1023 and v is 0 (point 2 on view 1), which you want to become hMax, vMax (point 2 on view 2), but v is at the opposite end of the scale, so v needs to be set to vMax - v.

So:
Code: [Select]
h = hMax - h;
v = vMax - v;

JRMN

Thanks you, it works!!!  You sir are a life saver.  I couldn't wrap my head around this one for the life of me.  You're explanation was crystal clear and brilliant.  I don't think I ever would have been able figured it out on my own.

PaulS

I'm happy that it works for you. Thanks for the feedback (and kind words).

Go Up