UAB – 2006: Problem 2: Circle Intersection


In this problem you must write a program that determines if two circles intersect each other. The input to your program will be the coordinates for the center of each circle, followed by the radius of each circle.

The output will state whether the circles overlap, do not overlap, or are tangential (i.e., tangential circles touch each other at just one common point).

Example 1:
Enter the coordinates and radius for circle 1: 10 10 3
Enter the coordinates and radius for circle 2: 10 6 1

The circles are tangential.

Example 2:
Enter the coordinates and radius for circle 1: 8 8 3
Enter the coordinates and radius for circle 2: 8 4 2

The circles overlap.





// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int square (int x)
{
    return x * x;
}

// distance between a and b
int distance (int x1, int y1, int x2, int y2)
{
    return square(x1 - x2) + square(y1 - y2);
}

int main (int argc, char *argv [])
{
    int circle1X, circle1Y, circle1Radius;
    
    printf ("Enter the coordinates and radius for circle 1: ");
    
    scanf ("%d %d %d", &circle1X, &circle1Y, &circle1Radius);
    
    int circle2X, circle2Y, circle2Radius;
    
    printf ("Enter the coordinates and radius for circle 2: ");
    
    scanf ("%d %d %d", &circle2X, &circle2Y, &circle2Radius);
    
    int centerDistance = distance(circle1X, circle1Y, circle2X, circle2Y);
    
    if ( centerDistance == circle1Radius + circle2Radius ) {
        printf ("The circles are tangential.\n");
        
    } else if ( centerDistance < circle1Radius + circle2Radius ) {
        printf ("The circles overlap");
        
    } else {
        printf ("The circles do not overlap");
        
    }

    return 0;
}

// @END_OF_SOURCE_CODE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s