Why isn't this working?

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

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 on other sites
^ is the bitwise xor operator, try to use this instead:
sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))

Share on other sites
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 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 on other sites
Quote:
 Original post by Nomad010Well, 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 on other sites
Quote:
 Original post by TookoYeah I tried that:if((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) <= r) to stuffand it still didn't work.This is rather frustrating.

That should probably be <= r * r

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 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 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 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;

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• Forum Statistics

• Total Topics
634102
• Total Posts
3015536
×