Jump to content
  • Advertisement
Sign in to follow this  
Tooko

Why isn't this working?

This topic is 4500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to see if a point lies inside a circle, and I thought the easiert (though not quickest) way was to get the distance from the point to the center of the circle, and compare it to the radius. Assuming [x1,y1] is the point and [x2,y2] and r is the radius... To get the distance, I'm using sqrt(x2-x1)^2 + (y2-y1)^2)... but it's not working at all. For what it's worth, here's my code: float dist = sqrt((int)(x2-x1)^2 + (int)(y2-y1)^2)); if(dist <= r) return 1; But, it always returns 1 no matter what. I also tried removing the (int) statements by writing it as (x2-x1)*(x2-x1) thinking maybe it could be for some freak rounding error, but that didn't help.

Share this post


Link to post
Share on other sites
Advertisement
Common mistake: in C++, operator^() is a bitwise XOR, not a power operator. Just change it to:
float dist = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
And you should be good to go.

Share this post


Link to post
Share on other sites
Well, you could avoid the square-root by squaring both sides and checking if the distance squared between the centre and the point was smaller than the radius squared. This would make it a bit faster and wouldn't complicate it too much

^ is the bit-wise xor operation. Isn't there a function you can use that does powers? eg: pow(base, exponent)

Also, the int typecasting is going to give you an incorrect answer anyway due to rounding.

Edit: Typo fixed, grammar fixed [smile]
How come all you people are so damn fast!?

Share this post


Link to post
Share on other sites
Quote:
Original post by Nomad010
Well, you could avoid the square-root by squaring both sides and checking if the distance squared between the centre and the point was smaller than the radius squared. This would make it a bit faster and wouldn't complicate it too much


Yeah I tried that:
if((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) <= r) to stuff
and it still didn't work.

This is rather frustrating.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tooko
Yeah I tried that:
if((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) <= r) to stuff
and it still didn't work.

This is rather frustrating.

That should probably be <= r * r

Share this post


Link to post
Share on other sites
Hmm, come to think of it I can't remember if I squared that, back in a sec.

(The code's on the other comp)

Share this post


Link to post
Share on other sites
Ok, yeah that worked.

But I just realized something else, I actually need to find out the distance from the center of the circle, as I'm going to apply an outward gradient to it.

I'll figure it out eventually.

But yeah, cheers for the help.

Share this post


Link to post
Share on other sites
If you need the actual distance, then you will have to do a square root:

double distance = sqrt( ( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 ) );

Share this post


Link to post
Share on other sites
If you need the actual distance, then do this:

float dist = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
if(dist <= r) return 1;

Also, why are you doing the int casts? The truncation involved in casting a double or a float to int could be causing errors, especially if y2-y1 or x2-x1 is small (as in, less than 1.0f).

EDIT: I just noticed you already tried removing it, the other problem might be that I think you're x1 and x2 (and y1 and y2) are backwards. If [x1, y1] is the point, and [x2, y2] is the center of the circle with radius r, then it should be:

float dist = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
if(dist <= r) return 1;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!