Q2. Suppose you want to represent a circle in the Cartesian plane xy using a str
ID: 3705673 • Letter: Q
Question
Q2. Suppose you want to represent a circle in the Cartesian plane xy using a structure in C. For this you will store three elements: x coordinate of the center of the circle (as floating-point number) y coordinate of the center of the circle (as floating-point number) radius of the circle - - - - Define the structure to represent the circle -Create an alias for the structure - Define a function that receives the memory address ofa circle and two doubles that represent the offset in the x and y axes. This function must shift the circle by that offset and return the length of the shift (distance between the original and shifted position). Hint: below there is a picture to help you visualize this. Length of the shift - Define a function that receives the memory addresses of two different circles and determines if both circles overlap. The function must return true if they overlap and false otherwise. Hint: this can be determine based on their radius and center.Explanation / Answer
PLEASE REFER BELOW CODE
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include<math.h>
typedef struct Circle
{
double x,y;
double radius;
}Circle;
//return length of shift
double circle_shift(Circle *C, double x_offset, double y_offset)
{
double dist;
//calculating distance between two points
dist = ((C->x - x_offset) * (C->x - x_offset)) + ((C->y - y_offset) * (C->y - y_offset));
return sqrt(dist);
}
bool check_overlap(Circle *C1, Circle *C2)
{
double C1C2;
/*
Distance between centers C1 and C2 is calculated as
C1C2 = sqrt((x1 - x2)2 + (y1 - y2)2).
There are three condition arises.
1. If C1C2 == R1 + R2
Circle A and B are touch to each other.
2. If C1C2 > R1 + R2
Circle A and B are not touch to each other.
3. If C1C2 < R1 + R2
Circle intersects each other.
*/
//Distance between two centres
C1C2 = sqrt(((C1->x - C2->x) * (C1->x - C2->x)) + ((C1->y - C2->y) * (C1->y - C2->y)));
if(C1C2 < (C1->radius + C2->radius))
return true;
return false;
}
int main()
{
//creating instance of structure and initialize
Circle C = {6.7,8.9,30.0};
double shift_length;
//calling function to determine length of shift
shift_length = circle_shift(&C, 3.5, 8.8);
printf("length of the shift = %f ", shift_length);
Circle C1 = {-10, 8, 30};
Circle C2 = {14, -24, 10};
if(check_overlap(&C1, &C2))
printf("Circle Overlap with each other ");
else
printf("Circle NOT Overlap with each other ");
return 0;
}
PLEASE REFER BELOW OUTPUT
length of the shift = 3.201562
Circle NOT Overlap with each other
Process returned 0 (0x0) execution time : 0.052 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.