Here is what I have so far...it seems like I am doing it right, but I'm not sure how to even start the last two functions. Which are below.
I did part a, I think...but b and c I'm not sure on how to do. Any help would be greatly appreciated. My code is below the question.
The question is:
9.11* (Geometry: The Circle2D class) Define the Circle2D class that contains:
-Two double data fields named x and y that specify the center of the circle with get methods.
-A data field radius with a get method.
-A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius.
-A constructor that creates a circle with the specified x, y, and radius.
-A method getArea() that returns the area of the circle.
-A method getPerimeter() that returns the perimeter of the circle.
-A method contains(double x, double y) that returns true if the specified
point (x, y) is inside this circle. See Figure 9.11(a).
-A method contains(Circle2D circle) that returns true if the specified
circle is inside this circle. See Figure 9.11(b).
-A method overlaps(Circle2D circle) that returns true if the specified
circle overlaps with this circle. See Figure 9.11(c).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
// circle.h
// Specification file for the Circle2D Class
#ifndef CIRCLE2D_H
#define CIRCLE2D_H
class Circle2D
{
private:
// Private member variables
double x;
double y;
double radius;
// Private member function
public:
// These accessors are inline member functions
double getx() const;
double gety() const;
double getArea() const;
double getRadius() const;
double getPerimeter() const;
// Default and overloaded constructors
Circle2D();
Circle2D(double, double, double);
// Mutator
void setRadius(double);
void setx(double);
void sety(double);
// Member function
bool contains(double x, double y);
bool contains(Circle2D &circle);
bool overlaps(Circle2D &circle);
};
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
// circle.cpp
// Class implementation file for the Circle2D class
#include "circle.h"
#include<iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// Default constructor
Circle2D::Circle2D()
{
x = 0;
y = 0;
radius = 1;
}
// Overloaded constructor
Circle2D::Circle2D(double x, double y, double radius)
{
setx(x);
sety(y);
setRadius(radius);
}
// Mutator member functions
void Circle2D::setRadius(double rad)
{
if(rad >= 0)
radius = rad;
else
{
cout << "Invalid radius\n";
exit(EXIT_FAILURE);
}
}
void Circle2D::setx(double pX)
{
x = pX;
}
void Circle2D::sety(double pY)
{
x = pY;
}
// Member functions
double Circle2D::getRadius() const
{
return radius;
}
double Circle2D::getx() const
{
return x;
}
double Circle2D::gety() const
{
return y;
}
double Circle2D::getArea() const
{
// Area = pi * radius * radius
return 3.14 * radius * radius;
}
double Circle2D::getPerimeter() const
{
// Perimeter = 2* pi * r
return radius * 3.14 * 2;
}
bool Circle2D::contains(double x, double y)
{
double pointX = x;
double pointY = y;
double circleRadius = radius;
/* Equation for center of circle!!
(x - h)^2 + (y - k)^2 = r^2 -- In this case h and k are the center of the circle
Use this to find Distance from center to the specified point
*/
double result = pow( pow((pointX - 0),2) + pow((pointY - 0),2) , 0.5);
// Checking if the distance from the center to the radius is larger than center to the point
if (result < circleRadius)
return true;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
// Circle2D.cpp
// Written by Kraig Blamires
// S[ring 2011
// Circle2D class
#include <iostream>
#include <string>
#include "circle.h"
#include <cmath>
#include <iomanip>
using namespace 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);
// 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;
}
|