Need Help With Determinig A Formula
Moderator: Coders of Rage
-
- Chaos Rift Newbie
- Posts: 3
- Joined: Wed Oct 29, 2008 2:32 pm
Need Help With Determinig A Formula
What formula could i use if i wanted to determine if a single point landed inside a set of points (i.e., triangle or circle)?
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Need Help With Determinig A Formula
Use the vertices (corners) of what you're checking.
Square's easy.
The point's x coordinate is greater than the left hand side's vertices x-coordinates and less than the right hand side's vertices x-coordinates (which are equal so you only have to use the left and right hand sides', not all four) and similarly it's between the y-coordinates of the top and bottom vertices (which are equal so you only have to use one from the top and one from the bottom too)
For a triangle, it'd be a bit more complicated... you'd have to check what the bounds are at that particular point, I think. Not really sure...
Circle, check that the distance between the point and the center of the circle is less than the radius of the circle. (x2-x1 + y2-y1 < r, I think).
Square's easy.
Code: Select all
+-----+
| . |
+-----+
For a triangle, it'd be a bit more complicated... you'd have to check what the bounds are at that particular point, I think. Not really sure...
Circle, check that the distance between the point and the center of the circle is less than the radius of the circle. (x2-x1 + y2-y1 < r, I think).
Last edited by MarauderIIC on Fri Nov 14, 2008 6:23 pm, edited 1 time in total.
Reason: see sig
Reason: see sig
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: Need Help With Determinig A Formula
EDIT: this code works for any geometry. skew lines and so on.I_Mystic_I wrote:What formula could i use if i wanted to determine if a single point landed inside a set of points (i.e., triangle or circle)?
just some code from my old stuff, good code.
let me know if you need help implimenting it.
Code: Select all
bool SimCol::pointInSphere(float sX, float sY, float sR, float x, float y)
{
float dist = (sX - x)*(sX - x) + (sY - y)*(sY - y);
if(dist < sR*sR)
return true;
return false;
}
bool SimCol::lineInter(line &AB, line &CD)
{
if(((CD.y2 - CD.y1)*(AB.x2 - AB.x1) - (CD.x2 - CD.x1)*(AB.y2 - AB.y1)) == 0)
return false;
if(((CD.y2 - CD.y1)*(AB.x2 - AB.x1) - (CD.x2 - CD.x1)*(AB.y2 - AB.y1)) != 0)
{
if(((((CD.x2 - CD.x1)*(AB.y1 - CD.y1) - (CD.y2 - CD.y1)*(AB.x1 - CD.x1))/
((CD.y2 - CD.y1)*(AB.x2 - AB.x1) - (CD.x2 - CD.x1)*(AB.y2 - AB.y1))) > 0) && ((((CD.x2 - CD.x1)*(AB.y1 - CD.y1) - (CD.y2 - CD.y1)*(AB.x1 - CD.x1))/
((CD.y2 - CD.y1)*(AB.x2 - AB.x1) - (CD.x2 - CD.x1)*(AB.y2 - AB.y1))) < 1))
{
if((((AB.x2 - AB.x1)*(AB.y1 - CD.y1) - (AB.y2 - AB.y1)*(AB.x1 - CD.x1))/
((CD.y2 - CD.y1)*(AB.x2 - AB.x1) - (CD.x2 - CD.x1)*(AB.y2 - AB.y1)) > 0) && (((AB.x2 - AB.x1)*(AB.y1 - CD.y1) - (AB.y2 - AB.y1)*(AB.x1 - CD.x1))/
((CD.y2 - CD.y1)*(AB.x2 - AB.x1) - (CD.x2 - CD.x1)*(AB.y2 - AB.y1)) < 1))
{
return true;
}
}
}
return false;
}
bool SimCol::lineInSphere(float sX, float sY, float sR, line &AB)
{
if(this->pointInSphere(sX, sY, sR, AB.x1, AB.y1))
return true;
if(this->pointInSphere(sX, sY, sR, AB.x2, AB.y2))
return true;
float dx = AB.x2 - AB.x1;
float dy = AB.y2 - AB.y1;
float d = dx*dx + dy*dy;
float D = (AB.x1 - sX)*(AB.y2 - sY) - (AB.x2 - sX)*(AB.y1 - sY);
float delta = ((sR*sR) * d) - (D*D);
float sgn = 0;
float dyP = dy;
if(dyP<0)
dyP = -1 *dyP;
if(dy < 0)
{
sgn = -1;
}else{
sgn = 1;
}
if(((sR*sR) * (((AB.x2 - AB.x1)*(AB.x2 - AB.x1)) + ((AB.y2 - AB.y1)*(AB.y2 - AB.y1)))) - (((AB.x1 - sX)*(AB.y2 - sY) - (AB.x2 - sX)*(AB.y1 - sY))*((AB.x1 - sX)*(AB.y2 - sY) - (AB.x2 - sX)*(AB.y1 - sY))) >= 1)
{
if(((D*dy + sgn * dx * sqrt(sR*sR*d - D*D)) / d) >= AB.x1 && ((D*dy + sgn * dx * sqrt(sR*sR*d - D*D)) / d) <= AB.x2)
return true;
if(((D*dy + sgn * dx * sqrt(sR*sR*d - D*D)) / d) >= AB.x2 && ((D*dy + sgn * dx * sqrt(sR*sR*d - D*D)) / d) <= AB.x1)
return true;
}
return false;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Need Help With Determinig A Formula
Wow, avansc, those are some extremely useful functions. We should make a thread of random useful functions like that.
-
- Chaos Rift Newbie
- Posts: 3
- Joined: Wed Oct 29, 2008 2:32 pm
Re: Need Help With Determinig A Formula
thnx for all the help i think i found what im going to use...