Compare Doubles in C ++

I want to determine if a point is inside a circle or not. So I do this:

(x - center_x)^2 + (y - center_y)^2 < radius^2

But my coordinates double, and I think I should do it with epsilon, so which is fabs ((x - center_x)^2 + (y - center_y)^2 - radius^2 ) < EPSbetter?

+4
source share
4 answers

You don't need epsilon when you are comparing with <or >, all of this is fine. You need this instead ==. In your case, you just added a small amount of radius, which is probably undesirable. Also note that ^does not match pow(a, b).

+5
source

'^' ++ . (x - center_x)^2 + (y - center_y)^2 < radius^2 do (x - center_x)*(x - center_x) + (y - center_y)*(y - center_y) < radius*radius. .

+4

.

, .

- , , . , , .

, , .

+4

No. As noted in other cases, the operator ^in C is beaten out or not power. But you can use the built-in function:

inline double Sqr(double x) {return x*x;}
// ...
if (Sqr(x - center_x) + Sqr(y - center_y) < Sqr(radius)) // ...

Regarding your question,

fabs (Sqr(x - center_x) + Sqr(y - center_y) - Sqr(radius) ) < EPS

means that (x, y) is on the circumference of a circle.

+4
source

All Articles