Write a C program: Thanks! You are to write a program that defines and uses type
ID: 3678248 • Letter: W
Question
Write a C program:
Thanks!
You are to write a program that defines and uses typedefs to implement: a. A user defined type named point that uses a structure to represent a point as two doubles, the x coordinate and the y coordinate b. A user defined type named rectangle that uses a structure to represent a rectangle. The rectangle structure should include the four corners of the rectangle each of which is stored as a point using the point structure. c. A user defined type named circle that uses a structure to represent a circle by its centre and radius. The circle structure uses the point structure for the centre and an additional double variable for the radius Your program is to have the following functions a. A function named readPoint to read a point. Points are read as two doubles: the x value followed by the y Value b. A function named readRectangle to read a rectangle which uses the point reading function. For this problem we only consider rectangles aligned to the x axis and the y axis. They are read as the bottom left corner first followed by the top right corner i.e. as four doubles in the order specified. c. A function named readCircle which reads a circle which is entered as the centre (a point) followed by the radius i.e. as three doubles in the order specified. d. A function named overlap that returns 1 if a rectangle aligned to the x axis and the y axis aligns with a circle and returns 0 if they do not e. A main function that inputs a rectangle followed by a circle and prints a message saying whether or not the rectangle and triangle overlap e. have a common area. Touching does not count as overlap. Your main function is to repeat this process until any nonnumeric (e,g, quit or end) is entered instead of a rectangle. You can add any other functions, types or structures you find helpful for solving this problem e.g. a function to compute the distance between two points, a function to determine if a point is inside a rectangle etc Note: Don't forget the cases of the circle being completely inside the rectangle and the rectangle being completely inside the circle as we are dealing with overlap and not intersectionExplanation / Answer
Comments added.... checked all conditions
#include<stdio.h>
//structs declaring
struct point
{
double x;
double y;
}
//rectangle structure
struct Rectangle
{
struct point x1;
struct point x2;
struct point y1;
struct point y2;
};
//circle structure
struct Circle
{
struct point centre;
double radius;
};
//main
int main(){
struct Rectangle rect;
struct Circle circ;
while(true){
printf("1.check overlap or not 2.exit");
int num;
scanf("%d",&num);
if(num == 2){
break;
}
readRectangle(&rect);
readCircle(&circ);
//checking inyersects or not
double val1 = (rect.x1.x - circ.centre.x)*(rect.x1.x - circ.centre.x) + (rect.x1.y - circ.centre.y)*(rect.x1.y - circ.centre.y);
double val2 = (rect.x2.x - circ.centre.x)*(rect.x2.x - circ.centre.x) + (rect.x2.y - circ.centre.y)*(rect.x2.y - circ.centre.y);
double val3 = (rect.y1.x - circ.centre.x)*(rect.y1.x - circ.centre.x) + (rect.y1.y - circ.centre.y)*(rect.y1.y - circ.centre.y);
double val4 = (rect.y2.x - circ.centre.x)*(rect.y2.x - circ.centre.x) + (rect.y2.y - circ.centre.y)*(rect.y2.y - circ.centre.y);
//it will check also condition. inside circle
if((val1 <= (circ.radius*circ.radius)) || (val2 <= (circ.radius*circ.radius)) || (val3 <= (circ.radius*circ.radius)) || (val4 <= (circ.radius*circ.radius))){
printf(" Intersects");
}
else{
printf("Not intersects.");
}
}
}
//reading point structure
void readPoint(struct point *p1){
printf(" Enter point 1: ");
scanf("%lf",p1.x);
printf(" Enter point 1: ");
scanf("%lf",p1.y);
}
//reading recangle function
void readRectangle(struct Rectangle *rect){
//calling point functions
readPoint(&rect.x1);
readPoint(&rect.x2);
readPoint(&rect.y1);
readPoint(&rect.y2);
}
//cricle function
void readCircle(struct Circle *cir){
readPoint(&cir.centre);
printf(" Enter radius: ");
scanf("%lf",cir.radius);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.