user1534664
user1534664

Reputation: 3418

C++ calculate the distance between two circles

Can someone explain this?

double distance( int x1, int y1, int x2, int y2 )
{
    //Return the distance between the two points
    return sqrt( pow( x2 - x1, 2 ) + pow( y2 - y1, 2 ) );
}
bool check_collision( Circle &A, Circle &B )
{
    //If the distance between the centers of the circles is less than the sum of their radii
    if( distance( A.x, A.y, B.x, B.y ) < ( A.r + B.r ) )
    {
        //The circles have collided
        return true;
    }

    //If not
    return false;
}

I dont get how this bit of code

//Return the distance between the two points
return sqrt( pow( x2 - x1, 2 ) + pow( y2 - y1, 2 ) );

returns the distance between two circles.. Code is from http://lazyfoo.net/SDL_tutorials/lesson19/index.php

Upvotes: 0

Views: 4579

Answers (5)

andand
andand

Reputation: 17487

Euclidean distance between the origin and a point (x, y) is defined by:

d = (x2 + y2)(1/2)

So the distance between the center points p1 = (x1, y1) and p2 = (x2, y2) of the two circles is given by translating the entire system so one center point is at the origin, and the distance to the other is computed. We do this by:

d = |p2 - p1|2
  = ((x2-x1)2 + (y2-y1)2)(1/2)

in C++ this would typically look like

return sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );

In this case, however, it looks like the author is making use of a pow(b, e) function which takes the first argument, b, and raises it to the power of the the second argument, e to get be.

Upvotes: 2

user529758
user529758

Reputation:

The distance of two points can be inferred by using Pithagroas' theorem about right-angled triangles. In Descartes's coordinate system, if you define two points: P1(x1, y1) and P2(x2, y2), then you'll have a right-angled triangle between them:

^ y
|
|
|
|
+ ---------x P1(x1, y1) = (12, 8)
|          |
|          | 
|          | 
+----------x-I-------x P2(x2, y2) = (24, 4)
|          |         |
|          |         |
|          |         |
+------------------------------------------------> x

Now the P1, P2, I triangle has a right angle at its point I. So Pithagoras' theorem applies as:

c ^ 2 = a ^ 2 + b ^ 2
distance ^ 2 = (x1 - x2) ^ + (y2 - y1) ^ 2
since n ^ 2 equals (-n) ^ 2 we can finally write:
distance = sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2)

That's why.

Upvotes: 1

mathematician1975
mathematician1975

Reputation: 21351

This

sqrt( pow( x2 - x1, 2 ) + pow( y2 - y1, 2 ) )

returns the euclidean distance between the circle centres. As a formula this distance is simply

sqrt((a1-b1)^2 + (a2-b2)^2)

where (a1,a2) and (b1,b2) are the centres of the 2 circles.

Upvotes: 6

count0
count0

Reputation: 2621

It returns the distance between the centers of the circles. Assumed Circle.x , Circle.y represents the center. This is used in the rest of the computation to check for collision.

Upvotes: 1

It doesn't return the distance between the circles, it does what it says "Return the distance between the two points" with a plain old Cartesean distance calculation. The programmer then passes the centers of the two circles.

Then the programmer subtracts off both radii to get the distance between the circles. Only instead of actually bothering to subtract (s)he just compares because (s)he is only interested in a collision vs. no-collision decision.

Upvotes: 3

Related Questions