192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
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.
Last edited by SLH; Mar 20th, 2005 at 08:41 AM.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
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.
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
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)?
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
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.
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Re: Map Screen coordinates to 4 sided polygon
Thanks for your continued help.
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?
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
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.
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Re: Map Screen coordinates to 4 sided polygon
Ok thanks, i'll post back if i have any problems.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
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.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
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?
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
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)
then you can use
Y=(BLxX-BLx+Px-BRxX)/(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..
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
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.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
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.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons