In: Computer Science
Write a C program to store the parameters of a circle on a 2D plane: the x and y coordinates of the center point and r, the radius. All three parameters are real (floating point) numbers.
-Define a type to store the parameters of a circle. Write a function that receives the parameters of two circles as function parameters, and returns whether the two circles overlap or not. (Two circles overlap if the distance between their center points - computed by the Pythagorean theorem - is less than the sum of their radius.)
-Write a function that asks the user to enter the parameters of a circle, creates the circle, and returns it.
-Expand the type definition and the functions to a program that reads the data of two circles, decides if they overlap, and displays a message accordingly.
Thanks for the question.
Here is the completed code for this problem. Comments are included,
go through it, learn how things work and let me know if you have
any doubts or if you need anything to change. If you are satisfied
with the solution, please rate the answer. Thanks!
===========================================================================
#include<stdio.h>
#include<math.h>
//-Define a type to store the parameters of a circle.
typedef struct circle{
double x;
double y;
double radius;
} Circle;
//-Write a function that asks the user to enter the
//parameters of a circle, creates the circle, and
// returns it.
Circle getCircle(){
Circle c;
printf("Enter the x coordinate of the
center:") ;
scanf("%lf", &c.x);
printf("Enter the y coordinate of the
center:") ;
scanf("%lf", &c.y);
printf("Enter the radius of the circle:")
;
scanf("%lf", &c.radius);
return c;
}
//Write a function that receives the parameters of
//two circles as function parameters, and returns
//whether the two circles overlap or not.
int overlaps(Circle c1, Circle c2){
double centerDistance, radiusSum;
centerDistance = sqrt(pow(c1.x-c2.x,2) +
pow(c1.y-c2.y,2));
radiusSum = c1.radius+c2.radius;
if(centerDistance<=radiusSum){
return 1;
}else{
return 0;
}
}
int main(){
Circle c1,c2;
printf("Enter details for Circle #1\n");
c1 = getCircle();
printf("\nEnter details for Circle #2\n");
c2 = getCircle();
if(overlaps(c1,c2)==1){
printf("\nThe circles
overlap.\n");
}else{
printf("\nThe circles DONOT
overlap.\n");
}
return 0;
}