For part A the default center of the circle is (0,0) as declared by the constructor and the formula I use to calculate the distance from center to the specified point. So the x and y that the user enters is the actual point either inside, upon, or outside the circle.
#include <iostream>
#include <string>
#include "circle.h"
#include <cmath>
#include <iomanip>
usingnamespace std;
int main()
{
Circle2D myCircle; // Instantiate myCircle object
double radius;
double x;
double y;
char again;
do
{
cout << "This program will display whether or not the point and radius you specify are inside, outside, or upon the circle.\n";
/*cout << "Enter the radius: ";
cin >> radius;
myCircle.setRadius(radius);
cout << "\nEnter the x point: ";
cin >> x;
myCircle.setx(x);
cout << "\nEnter the y point: ";
cin >> y;
myCircle.setx(y); //should be sety(y) - that's why this->x was giving you the Y coordinate :-)
So let's not set x or y on the circle and use the x and y points as the test points, leaving
the circle's coordinates at (0, 0) with radius 1
*/
cout << "\nEnter the X point: ";
cin >> x;
cout << "\nEnter the Y point: ";
cin >> y;
// Display the results of the function contain
if (myCircle.contains(x, y) == true)
cout << "Your point is inside the circle.";
else
cout << "Your point is outside the circle.";
cout << "\n\nWant to try again? (Y/N) ";
cin >> again;
}while(toupper(again) == 'Y');
return 0;
}
That should give you the expected result :)
There were a few logic problems, the first being setx(y) instead of sety(y), and the second being using the x and y inputs to change the circle you're testing.
Oh I get what you mean. By having setx(x) I am telling the object that "x" which is the center is now set to whatever they input. So in order to keep it as the default I need to not set x or y, but rather use the input directly with the function contains. I do need to keep setRadius though.
Now for part B. Here is a sample piece of code I made...am I at least on the right track?
int main()
{
Circle2D myCircle; // Instantiate myCircle object
Circle2D myCircle2; // for the first circle
Circle2D myCircle3; // for the second circle
double radius;
double x;
double y;
char again;
do
{
cout << "This program will display whether or not the point and radius you specify are inside, outside, or upon the circle.\n";
// The radius of default circle and a point (x,y)..For part A
cout << "Enter the radius: ";
cin >> radius;
myCircle.setRadius(radius);
cout << "Enter the x point: ";
cin >> x;
cout << "Enter the y point: ";
cin >> y;
// Display the results of the function contain
if (myCircle.contains(x, y)) // No need to put true as the it is the default
cout << "Your point is inside the circle.";
else
cout << "Your point is outside the circle.";
// The first circle--For part B
cout << "\nEnter the radius: ";
cin >> radius;
myCircle2.setRadius(radius);
cout << "Enter the x coordinate for the center of circle 1: ";
cin >> x;
myCircle2.setx(x);
cout << "Enter the y coordinate for the center of circle 1: ";
cin >> y;
myCircle2.sety(y);
// The second circle--For part B
cout << "\nEnter the radius: ";
cin >> radius;
myCircle3.setRadius(radius);
cout << "Enter the x coordinate for the center of circle 2: ";
cin >> x;
myCircle3.setx(x);
cout << "Enter the y coordinate for the center of circle 2: ";
cin >> y;
myCircle3.sety(y);
// Display the results of the function contain
if (myCircle2.contains(myCircle3))
cout << "Circle 2 is inside of circle 1.";
else
cout << "Circle 2 is outside of circle 1.";
cout << "\n\nWant to try again? (Y/N) ";
cin >> again;
}while(toupper(again) == 'Y');
return 0;
}
Yes, that code looks good. Now for the method Circle2D::contains(const Circle2D& otherCircle) method. I'm not sure if the class you're taking has covered const-correctness yet, but it's a good practice. If you haven't gotten to that yet, Circle2D::contains(Circle2D& otherCircle) works just as well in this academic case. Let me know if you need some help with the logic behind the circle-inside-circle part.
Yes, we did go over const but I'm not sure which class objects to use them on. Also since I use setx(x) and sety(y) for both objects (myCircle2 and myCircle3) will myCircle3 overwrite myCircle2?
How do I get the values for both objects (myCircle2 and myCircle3)? I can see how to get one...but both without overwriting I'm not sure.
Alright, we'll have 6 pieces of information for finding if a circle is inside another one; the radius, x, and y of each circle. The math is nearly the same as bool Circle2D::contains(double x, double y)
1 2 3 4 5 6 7 8 9 10 11 12 13
bool Circle2D::contains(Circle2D& otherCircle)
{
//otherCircle.getradius() - Info 1
//otherCircle.getx() - Info 2
//otherCircle.gety() - Info 3
//this->getradius() - Info 4
//this->getx() - Info 5
//this->gety() - Info 6
/*
The distance between the centers of the circles plus the radius of 'otherCircle'
must be less than the radius of `this` circle for 'otherCircle' to be inside `this` circle.
*/
}
What is otherCircle, and what does this-> point to? I would think that it would be...
1 2 3 4 5 6 7 8 9 10 11 12 13
bool Circle2D::contains(Circle2D& otherCircle)
{
//myCircle2.getradius() - Info 1
//myCircle2.getx() - Info 2
//myCircle2.gety() - Info 3
//myCircle3.getradius() - Info 4
//myCircle3.getx() - Info 5
//myCircle3.gety() - Info 6
/*
The distance between the centers of the circles plus the radius of 'otherCircle'
must be less than the radius of `this` circle for 'otherCircle' to be inside `this` circle.
*/
}
Circle2D myCircle2;
Circle2D myCircle3;
myCircle2.contains(myCircle3); //Inside the method, myCircle2 is pointed at by `this`, and myCircle3 is 'otherCircle'
//Think of it as this method being 'inside' of myCircle2, and referring to itself as `this`
//Now this code
Circle2D myCircle2
Circle2D myCircle3
myCircle3.contains(myCircle2); //Inside the method, myCircle3 is pointed at by `this`, and myCircle2 is 'otherCircle'
Ah I see...so 'this' points to the object that is being used rather than the parameters of the function it is performing. And the parameters are passed like any other...
Do I need to perform the 'sets' for these objects, or just do it inside of the member function section? For instance:
// The first circle--For part B
cout << "\nEnter the radius: ";
cin >> radius;
myCircle2.setRadius(radius);
cout << "Enter the x coordinate for the center of circle 1: ";
cin >> x;
myCircle2.setx(x);
cout << "Enter the y coordinate for the center of circle 1: ";
cin >> y;
myCircle2.sety(y);
// The second circle--For part B
cout << "\nEnter the radius: ";
cin >> radius;
myCircle3.setRadius(radius);
cout << "Enter the x coordinate for the center of circle 2: ";
cin >> x;
myCircle3.setx(x);
cout << "Enter the y coordinate for the center of circle 2: ";
cin >> y;
myCircle3.sety(y);