Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Finding out if a point is within a quadrilateral ...

Author
Message
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 7th Jul 2003 04:33
I'm in need of a function that can find out if a point is within a quadrilateral. I have one working method, but its quite long winded and it'd be nicer if I could come up with a quicker one. I'm sure there must be a fast way.

The quadrilateral is stored as corner points:
x1,y1
x2,y2
x3,y3
x4,y4
Where:
Side 1 is given by coords 1 to 2
Side 2 is given by coords 2 to 3
Side 3 is given by coords 3 to 4
Side 4 is given by coords 4 to 1

The quadrilateral can have sides of any length and the internal angles can be any angle (but sides cannot cross, giving that figure of 8 bow tie shape - the sides enclose only one area within them).

Hopefully thats all clear. I need to find if a given point x,y is within the quadrilateral.

Any maths gurus got any clever ways? I've got a method that takes about 8 sqrt commands and about 40 lines of code, but there must be a quicker way. :-s

Cheers.
Machine: P4 2200, 1GB RAM, GeForce4 64MB, Audigy Platinum
http://www.breakbeat-terrorism.co.uk
(It's not all about the coding)
Easily Confused
23
Years of Service
User Offline
Joined: 22nd Oct 2002
Location: U.K. Earth. (turn right at Venus)
Posted: 7th Jul 2003 09:03 Edited at: 7th Jul 2003 09:05
Hmmm, tricky problem, but I think this should do it, it even allows for concave quadrilaterals (shaped like arrowheads)



...the only problem you might have is when areas of the quadrilateral become to thin to check if the point is in or out, but larger areas are no problem.

Hope this helps

edit: Oh yes, I almost forgot, those two functions are 22 lines and no sqrts

Programming anything is an art, and you can't rush art.
Unless your name is Bob Ross, then you can do it in thirty minutes.
Codger
23
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 7th Jul 2003 09:31
this is simple but seems to work

make 3 object as triangle
color them red
check if the cursor is over red
( choose a color you are not usiing elsewhere )

test if it is your within bounds

Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 7th Jul 2003 14:54 Edited at: 7th Jul 2003 14:55
Thanks for the help guys. Nice to see people willing to assist in some problems that take more than a few minutes thought. Easily Confused - dont tell me that took you less than a few minutes, else I'll be very upset. hehe.

Thanks Codger, but I'm afraid your solution wouldnt work in my game, because although this is a 2D problem, it going to be used in a 3D environment, and I can't be drawing triangles all over the place testing colours!

EC, that's a cunning method and 10 times quicker than mine. I'm sure I'll be able to port it to my 3D code, no problem. Thanks again for the help - much appreciated.

Machine: P4 2200, 1GB RAM, GeForce4 64MB, Audigy Platinum
http://www.breakbeat-terrorism.co.uk
(It's not all about the coding)
Codger
23
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 7th Jul 2003 19:11
DBP Triangles are 3d.
Test against a small object placed at an appropriate x,y,z(such as as a sphere for collision )

Remember that when you have your answer you can delete the triangles prior to sync so they do not show up on screen.

Ishamael Sedai
23
Years of Service
User Offline
Joined: 1st Jul 2003
Location: France
Posted: 7th Jul 2003 21:55
if you can understand math... oh the prblem is that i dunno howto say it in english...but just in case you'd understand
vectorial product...or scalar *scalaire in french* product to delimit plans....

I am the Betrayer of hope naebl'is by the will of
the Great Lord of the Dark. If you do not want to surrender, your soul will burn in the deepest hole of the Pit of doom.
Witch Bomber
23
Years of Service
User Offline
Joined: 25th Jan 2003
Location: Scotland
Posted: 8th Jul 2003 01:07
I can't be bothered writing a source example, but I would have thought it would have been pretty easy to calculate the formulae of the 4 lines that make up the perimeter of the quadrilateral, and you could see for each one whether the cursor is above or below each line, and take it from there...

The leprechaun tells me to burn things
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 8th Jul 2003 14:49
Don't worry guys. Thanks, but ECs example is by far the quickest and does the job fine. It doesn't need to be conducted physically, just logically.

Machine: P4 2200, 1GB RAM, GeForce4 64MB, Audigy Platinum
http://www.breakbeat-terrorism.co.uk
(It's not all about the coding)

Login to post a reply

Server time is: 2026-07-18 07:31:29
Your offset time is: 2026-07-18 07:31:29