Map Screen coordinates to 4 sided polygon
I have a 4 sided polygon on which the user will be able to draw (lines).
I also want to allow the user to move the vertices of this polygon, and have the drawing alter accordingly. Importantly, i need to know the new position of all the lines once the user has moved the polygon.
I am thinking that i can use a similar formula to texture mapping, although i'm not very familiar with these.
Does anyone know what the formulas (sp?) would be for translating a screen coordinate into a 'polygon' coordinate and back again?
I don't care how the polygon coordinate is stored (2 doubles etc.), as long as i can get back to a screen position.
Thanks for any help.
Re: Map Screen coordinates to 4 sided polygon
If we call the vertices TL=top left, TR=top right,BL=bottom left, BR=bottom right
A point X/screen width,Y/screen height on the screen would be at
(TRX+ TL(X-1))Y + (BRX+ BL(X-1))(Y-1)
Vectors in bold.
You should get two equations, so solve and substitute either X or Y and solve the other to get it as a function of your polygon coordinate.
Re: Map Screen coordinates to 4 sided polygon
Thanks for the reply.
I'm not sure how i get 2 equations from that expression.
Are you saying that the expresison you gave will give a vector, which is where it is on the screen (assuming the screen is 1 by 1) and if i rearrange the equation i'll get 2, 1 for the X and one for the Y (polygon coords)?
Re: Map Screen coordinates to 4 sided polygon
Yes, except the expression I gave is the "'polygon' coordinate", lets call it PC, so the two equations you get should be:
PC.x = (TR.x * X+ TL.x * (X-1))Y + (BR.x * X+ BL.x *(X-1))(Y-1)
PC.y = (TR.y * X+ TL.y * (X-1))Y + (BR.y * X+ BL.y *(X-1))(Y-1)
The equation system has two equations, two unknowns (in bold), therefore you can solve it. I suggested that you first solve for instance X out of one of the equations and then substitute the expression with X in the other equation, but there may be other ways of doing it.
Re: Map Screen coordinates to 4 sided polygon
Thanks for your continued help. :thumb:
So to go from screen coords to polygon coordinates i plug in the X and Y values to that equation (having scaled them), and to convert them back i rearrange the formula so i get X = and Y =
Is this about right?
1 Attachment(s)
Re: Map Screen coordinates to 4 sided polygon
NP. Yes, thats about right.
Just in case, since there may be other solutions, like taking three points and treating the polygons like two triangles, which may be a faster way to do rendering, but may produce an unwanted diagonal in it, I decided to post the theory behind my thought. Presume we draw a straight line on the screen, and we want that line also to be straight on the polygon, then if we draw a vertial line, from 0,X to 1,X it should start at a point at proper proportion between the top left and right vertices, and similarly end at a point at proper proportion on the bottom left right. If you do the same with a vertical line, they should intersect at X,Y.
Re: Map Screen coordinates to 4 sided polygon
Ok thanks, i'll post back if i have any problems.
Re: Map Screen coordinates to 4 sided polygon
I tried to get your formula to work, but was unsuccessfull. I had a good long look at your diagram, and was able to come up with this:
TL = Top Left vertex, TR = Top Right, BL = Bottom Left, BR = Bottom Right
alpha = 'Width' Texture/Polygon Coordinate
beta = 'Height' Texture/Polygon Coordinate
TopEdge = TR - TL
BottomEdge = BR - BL
Screen Coordinate = ((BottomEdge * alpha + BL) - (TopEdge * alpha + TL)) * beta + (TopEdge * alpha) + TL
Basically i work out the vector from the top to the bottom edge. Then multiply that by beta, to get how far along that vector we are. I then add on all relevent position vectors (the start of this vertical line and the start of the top edge).
This works fine when i manually give some alpha and beta values, but i'm not sure how to do the opposite translation, i.e. from screen to polygon coordinates.
Any help on this would be greatly appreciated.
Re: Map Screen coordinates to 4 sided polygon
doh :o
1-X and 1-Y not X-1 and Y-1 in other words:
(TRX+ TL(1-X))Y + (BRX+ BL(1-X))(1-Y)
Re: Map Screen coordinates to 4 sided polygon
Hehe, an easy mistake to make!
Don't suppose you know how to do the translation from screen coordinates into the polygon coordinates?
Re: Map Screen coordinates to 4 sided polygon
Px=(TRxX+ TLx(1-X))Y + (BRxX+ BLx(1-X))(1-Y)
Py=(TRyX+ TLy(1-X))Y + (BRyX+ BLy(1-X))(1-Y)
bog the equations down to terms
TRxXY+ TLxY-XY + BRxX-BRxXY+ BLx-BLxY-BLxX+BLxXY-Px=0
TRyXY+ TLyY-XY + BRyX-BRyXY+ BLy-BLyY-BLyX+BLyXY-Py=0
factor out Y
Y(TRxX+TLx-X-BRxX-BLx+BLxX)-BLxX+BLx-Px+BRxX=0
Y(TRyX+TLy-X-BRyX-BLy+BLyX)-BLyX+BLy-Py+BRyX=0
solve Y
Y=(BLxX-BLx+Px-BRxX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RxX)/(TRxX+TLx-X-BRxX-BLx+BLxX)
you get two solutions, and i'm not certain which you should choose, but try each and see what happens..
Re: Map Screen coordinates to 4 sided polygon
Wow, thanks a lot for all that!
I've had a read through and can just about follow it all, i'll have a go and let you know how i do.
Re: Map Screen coordinates to 4 sided polygon
Quote:
Originally Posted by kedaman
X^2((TRy-1-BRy+BLx)(BLx-BRx)+((TLy-BLy)(BLx-BRx)-(TRx-1-BRx+BLx)(BLy-BRy)+((TLx-BLx)(BLy-BRy))
+X((TRy-1-BRy+BLx)(Px-BLx))-(TRx-1-BRx+BLx)(Px-BLy)))
+(TLy-BLy)(Px-BLx)-(TLx-BLx)(Px-BLy)=0
X=(-b+-sqrt(b^2-4ac))/2a
where a=((TRy-1-BRy+BLx)(BLx-BRx)+((TLy-BLy)(BLx-BRx)-(TRx-1-BRx+BLx)(BLy-BRy)+((TLx-BLx)(BLy-BRy))
b=((TRy-1-BRy+BLx)(Px-BLx))-(TRx-1-BRx+BLx)(Px-BLy)))
and c=(TLy-BLy)(Px-BLx)-(TLx-BLx)(Px-BLy)
This bit i don't follow, because if you look at where your brackets are the X^2 term doesn't end on the top line (thus shouldn't be what a is equal to).
Is this a simple case of missing the brackets off at the end, or a mistake from the step above, which i'm having trouble following?
Thanks again for all your help.