#include<iostream>
#include<cmath>
usingnamespace std;
double dist=0;
struct Point {
int x;
int y;
};
struct Circle {
Point centre;
int radius;
};
void print_circle(const Circle& circle)
{
cout<<"The Centre is: ("<<circle.centre.x<<", "<<circle.centre.y<<")"<<endl;
cout<<"The Radius is: "<<circle.radius<<endl;
}
double circle_distance(const Circle& circle1,const Circle& circle2)
{
double a, b, c;
a=abs(circle1.centre.y-circle1.centre.x);
b=abs(circle2.centre.y-circle2.centre.x);
c=pow(a,2)+pow(b,2);
::dist=sqrt(c);
return dist;
}
int circle_test(const Circle& circle1, const Circle& circle2)
{
circle_distance(circle1, circle2);
int x;
x=abs(circle2.radius-circle1.radius);
if (circle1.centre.x==circle2.centre.x&&circle1.centre.y==circle2.centre.y&&circle1.radius==circle2.radius)
return 0;
if (dist<x)
return 1;
if (dist>(circle2.radius+circle1.radius))
return 2;
elsereturn 3;
}
int main()
{
Circle c1;
Circle c2;
cout<<"Please enter the centre and radius of circle #1: ";
cin>>c1.centre.x>>c1.centre.y>>c1.radius;
cout<<"Please enter the centre and radius of circle #2: ";
cin>>c2.centre.x>>c2.centre.y>>c2.radius;
cout<<"\nCircle #1:"<<endl;
print_circle(c1);
cout<<"\nCircle #2:"<<endl;
print_circle(c2);
switch (circle_test(c1, c2)) {
case 0:
cout<<"\nThe circles are identical!"<<endl;
break;
case 1:
cout<<"\nOne circle is completely inside the other!"<<endl;
break;
case 2:
cout<<"\nThe circles are seperate!"<<endl;
break;
default:
cout<<"\nThe circles intersect!"<<endl;
}
return 0;
}
EDIT: When I plug in 0 0 3 for circle 1 and 10 10 5 for circle 2, I should get "the circles are separate" but instead I get that one is inside the other. I'm assuming this is because it is reading that the distance=0.
Hello,
First I think you have write wrong the expression for calculating the distance (x1-x2)^2+(y1-y2)^2 instead of your expression.
Secondly the piece of code you wrote gives dist a value (but because of the error mentioned before) but a wrong one. I don't get why you experienced this kind of problem. I just added a struct Point also.
Now when I plug in 0 0 3 and 10 10 5 I get the correct answer: The circles are separate. However, when I plug in 0 0 5 and 4 0 2 I also get: The circles are separate when I should be getting 'The circles intersect', so there must be something that is still wrong...
P.S The rest of the code is exactly the same, I only modified the expression for calculating the distance.
Bah, fuck it. I just submitted the assignment with the error. Hopefully it's a small mistake and the professor won't deduct too many points. I'd still like to know what my mistake was since I'm a computer science major if anyone would care to explain :))
What's that test supposed to be for? Why do you care about equality of circles?
Anyways - don't do stuff with return codes unless it is somehow necessary. In your case, you are testing once for equality and once for overlap. You should have a function that tests for equality and another that tests for overlap - testing both at the same time makes no sense, you either want to know if the circles are equal or if they do overlap, never both.
Your new circle_dist function returns the square of the distance - your old one was correct. Just don't use pow for calculating squares, just write x*x or something. And use a local dist instead of a global one.
Why do you even use a global there? It doesn't make any sense. Just do this:
int dist = circle_distance(circle1, circle2);
in your circle_test function.
I guess you got into CS major without previous programming experience?
Yeah I don't really have much of a "background" in programming, but I love it. I feel like my professor is an idiot, the return function and the equality of the circles test were both required - that confused me a lot too.
//header files
#include<iostream>
#include<cmath>
usingnamespace std;
//defining the two structs
struct Point {
int x;
int y;
};
struct Circle {
Point centre;
int radius;
};
//defining the function that will print the radii and centre's of the circles.
void print_circle(const Circle& circle)
{
cout<<"The Centre is: ("<<circle.centre.x<<", "<<circle.centre.y<<")"<<endl;
cout<<"The Radius is: "<<circle.radius<<endl;
}
//defining the function that will calculate the distance between the centre's of
//the two circles.
double circle_distance(const Circle& circle1,const Circle& circle2)
{
double a, b, c;
a=abs((circle1.centre.x-circle2.centre.x));
b=abs((circle1.centre.y-circle2.centre.y));
c=(a*a)+(b*b);
int dist=sqrt(c);
return dist;
}
//defining the function that will evaluate whether the circles are the same, intersecting, etc...
int circle_test(const Circle& circle1, const Circle& circle2)
{
circle_distance(circle1, circle2);
int x;
int dist = circle_distance(circle1, circle2);
x=abs(circle2.radius-circle1.radius);
if (circle1.centre.x==circle2.centre.x&&circle1.centre.y==circle2.centre.y&&circle1.radius==circle2.radius)
return 0;
if (dist<x)
return 1;
if (dist>(circle2.radius+circle1.radius))
return 2;
elsereturn 3;
}
//begin main function
int main()
{
//define two circles c1 and c2.
Circle c1;
Circle c2;
//ask user for input and store data
cout<<"Please enter the centre and radius of circle #1: ";
cin>>c1.centre.x>>c1.centre.y>>c1.radius;
cout<<"Please enter the centre and radius of circle #2: ";
cin>>c2.centre.x>>c2.centre.y>>c2.radius;
//Call upon the print_circle function to output the data to the user.
cout<<"\nCircle #1:"<<endl;
print_circle(c1);
cout<<"\nCircle #2:"<<endl;
print_circle(c2);
//Call upon the circle_test function and use a switch statement to ouput
//various information based on what the function returns to the user.
switch (circle_test(c1, c2)) {
case 0:
cout<<"\nThe circles are identical!"<<endl;
break;
case 1:
cout<<"\nOne circle is completely inside the other!"<<endl;
break;
case 2:
cout<<"\nThe circles are seperate!"<<endl;
break;
default:
cout<<"\nThe circles intersect!"<<endl;
}
int end;
cin>>end;
return 0;
}
//end main
Sorry, I didn't see that circle distance returns a double - in that case, you of course have to use a double for the distance. In the circle_distance function you can leave out the declaration of dist - just return the sqrt of c.
In circle test you forgot to remove your old call to circle_dist. Also, use a double here again (sorry again, I somehow assumed you were using ints for the distance... I just realized that wouldn't make too much sense).
Otherwise this should be ok as it is (though don't rely on me for that, I may have missed something).
Yeah thanks a heap for the help, hopefully by the time I reach as many posts as you have - I will have some idea of how to do these assignments myself without too many errors :P