I am trying to write a program where it takes the X-Y Coordinates and Radii of 2 Circles from the User. Then, it would determine if the 2 Circles intersect or is contained in one another.
For the contain() function, when I tried printing out the values of c1.x and c2.x, only c1.x appears to give me the user input while c2.x prints out a very large number that has no meaning for me. Why can't I retrieve my user input for c2.x? Thank you
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct circle {
double radius;
double x;
double y;
};
// Returns 1 if the Circle intersects
int intersect(struct circle c1, struct circle c2);
int contain(struct circle *c1, struct circle *c2);
int main(){
struct circle c1, c2;
int answer, selection;
do{
printf("Please choose a Function to be used!!!\n");
printf("1: Enter the Parameters of the 2 Circles:\n");
printf("2: intersect():\n");
printf("3: contain():\n");
printf("4: Quit!!\n");
scanf("%d", &selection);
switch(selection){
case 1:{
// First Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the First Circle\n");
printf("Radius:\n");
scanf("%d", &c1.radius);
printf("X-Coordinate:\n");
scanf("%d", &c1.x);
printf("Y-Coordinate:\n");
scanf("%d", &c1.y);
// Second Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the Second Circle\n");
printf("Radius:\n");
scanf("%d", &c2.radius);
printf("X-Coordinate:\n");
scanf("%d", &c2.x);
printf("Y-Coordinate:\n");
scanf("%d", &c2.y);
break;
}
case 2:{
int choice = intersect(c1, c2);
switch(choice){
case 1:{
printf("The 2 Circles intersects!\n");
break;
}
case 2:{
printf("The 2 Circles does not intersects!!\n");
}
}
break;
}
case 3:{
answer = contain(&c1,&c2);
if(answer == 1){
printf("Circle C1 contains Circle C2!!\n");
}
else{
printf("Circle C1 does not contain Circle C2!!\n");
}
}
}
} while (selection < 4);
return 0;
}
int intersect(struct circle c1, struct circle c2){
// Calculate the Distance between the Centers of 2 Circles
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1.x,2) - pow(c2.x,2);
printf("X Coordinate of C1 and C2: %d, %d\n", c1.x,c2.x);
printf("%d", xDiff);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1.y,2) - pow(c2.y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff yDiff);
dRadii = c1.radius c2.radius;
if((dist == dRadii) || (dist < dRadii)){
return 1;
}
else{
return 0;
}
}
int contain(struct circle *c1, struct circle *c2){
double specialValue;
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1->x,2) - pow(c2->x,2);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1->y,2) - pow(c2->y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff yDiff);
specialValue = dist c2->radius;
// Checking
if((c1->radius > specialValue) || (c1->radius == specialValue)){
return 1;
}
else{
return 0;
}
}
CodePudding user response:
You have a few problems with argument passing for the intersect
function and some other problems with the format specifier for printf
.
Try something like that:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct circle {
double radius;
double x;
double y;
};
// Returns 1 if the Circle intersects
int intersect(struct circle *c1, struct circle *c2);
int contain(struct circle *c1, struct circle *c2);
int main(){
struct circle c1, c2;
int answer, selection;
do{
printf("Please choose a Function to be used!!!\n");
printf("1: Enter the Parameters of the 2 Circles:\n");
printf("2: intersect():\n");
printf("3: contain():\n");
printf("4: Quit!!\n");
scanf("%d", &selection);
switch(selection){
case 1:{
// First Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the First Circle\n");
printf("Radius:\n");
scanf("%lf", &c1.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c1.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c1.y);
// Second Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the Second Circle\n");
printf("Radius:\n");
scanf("%lf", &c2.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c2.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c2.y);
break;
}
case 2:{
int choice = intersect(&c1, &c2);
switch(choice){
case 1:{
printf("The 2 Circles intersects!\n");
break;
}
case 2:{
printf("The 2 Circles does not intersects!!\n");
}
}
break;
}
case 3:{
answer = contain(&c1,&c2);
if(answer == 1){
printf("Circle C1 contains Circle C2!!\n");
}
else{
printf("Circle C1 does not contain Circle C2!!\n");
}
}
}
} while (selection < 4);
return 0;
}
int intersect(struct circle *c1, struct circle *c2){
// Calculate the Distance between the Centers of 2 Circles
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1->x,2) - pow(c2->x,2);
printf("X Coordinate of C1 and C2: %lf, %lf\n", c1->x,c2->x);
printf("%lf\n", xDiff);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1->y,2) - pow(c2->y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff yDiff);
dRadii = c1->radius c2->radius;
if((dist == dRadii) || (dist < dRadii)){
return 1;
}
else{
return 0;
}
}
int contain(struct circle *c1, struct circle *c2){
double specialValue;
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1->x,2) - pow(c2->x,2);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1->y,2) - pow(c2->y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff yDiff);
specialValue = dist c2->radius;
// Checking
if((c1->radius > specialValue) || (c1->radius == specialValue)){
return 1;
}
else{
return 0;
}
}
For further questions, please add a demo user input and the expected output.
CodePudding user response:
struct circle {
double radius;
double x;
double y;
};
Here , members of structure is of double data type. So you have to use %lf instead of %d which is format specifier for integer.
Do this in main():
scanf("%lf", &c1.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c1.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c1.y);
// Second Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the Second Circle\n");
printf("Radius:\n");
scanf("%lf", &c2.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c2.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c2.y);
In intersect() :
printf("%lf", xDiff);