Why isn't this working?

Started by
12 comments, last by jflanglois 17 years, 9 months ago
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.
the future is just like the past, just later. - TANSTAAFL
Advertisement
^ is the bitwise xor operator, try to use this instead:
sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
:wq!
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.
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!?
"..."
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.
the future is just like the past, just later. - TANSTAAFL
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
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)
the future is just like the past, just later. - TANSTAAFL
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.
the future is just like the past, just later. - TANSTAAFL
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 ) );
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;

This topic is closed to new replies.

Advertisement