Vous êtes sur la page 1sur 4

# 10/23/2017 UAB – 2006: Problem 2: Circle Intersection | Solved Programming Problems

## UAB – 2006: Problem 2:

Circle Intersection

i

## 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.

https://tausiq.wordpress.com/2013/08/11/uab-2006-problem-2-circle-intersection/ 1/4
10/23/2017 UAB – 2006: Problem 2: Circle Intersection | Solved Programming Problems

## The circles overlap.

?
Critical TestCases
1
?
Solution in C/C++
1 // @BEGIN_OF_SOURCE_CODE
2
3 #include <cstdio>
4
5 int square (int x)
6 {
7 return x * x;
8 }
9
10 // distance between a and b
11 int distance (int x1, int y1, int x2, int y2)
12 {
13 return square(x1 - x2) + square(y1 - y2);
14 }
15
16 int main (int argc, char *argv [])
17 {
19
20 printf ("Enter the coordinates and radius for circle 1: "
21
22 scanf ("%d %d %d", &circle1X, &circle1Y, &circle1Radius);
23
25
26 printf ("Enter the coordinates and radius for circle 2: "
27
28 scanf ("%d %d %d", &circle2X, &circle2Y, &circle2Radius);
29
30 int centerDistance = distance(circle1X, circle1Y, circle2X, circle2Y);
31
33 printf ("The circles are tangential.\n");
34
35 } else if ( centerDistance < circle1Radius + circle2Radius ) {
36 printf ("The circles overlap");
37
38 } else {
39 printf ("The circles do not overlap");
40
41 }
42
43 return 0;
44 }
45
46 // @END_OF_SOURCE_CODE

https://tausiq.wordpress.com/2013/08/11/uab-2006-problem-2-circle-intersection/ 2/4
10/23/2017 UAB – 2006: Problem 2: Circle Intersection | Solved Programming Problems

Easy Problems

## 2 thoughts on “UAB – 2006: Problem

2: Circle Intersection”

1. Mohemmed Fayas M
JUNE 17, 2017 AT 4:58 PM
Your porgram wont work. You need not to square the
distance… Check it out my program

import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int cx = in.nextInt(), cy = in.nextInt(), cr = in.nextInt();
int dx = in.nextInt(), dy = in.nextInt(), dr = in.nextInt();

## int distance = ( Math.abs( cx – dx) + Math.abs ( cy- dy) );

if( distance == cr + dr )
System.out.println(“The circles are tangential”);
else if( distance < cr + dr )
System.out.println("The circles overlap");
else
System.out.println("The circles do not overlap");
}
}

https://tausiq.wordpress.com/2013/08/11/uab-2006-problem-2-circle-intersection/ 3/4
10/23/2017 UAB – 2006: Problem 2: Circle Intersection | Solved Programming Problems

i
Rate This

2. coder94
JUNE 17, 2017 AT 5:12 PM
This code is bogus. Doesn’t give the required output.

i
Rate This

https://tausiq.wordpress.com/2013/08/11/uab-2006-problem-2-circle-intersection/ 4/4