Circle2D class question

Pages: 12
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;
}    
closed account (3hM2Nwbp)
It often helps to visually represent these kind of problems (MS-Paint being my choice application).

To find if a circle is enclosed within another:
1) Find the linear distance between centers
2) Add the linear distance with the argument circle's radius
3) Compare the result with the radius of the other circle.

Reference:

http://img707.imageshack.us/img707/1293/circleproblem.png

I'll re-edit with the other if you need it.
Last edited on
I see what you did...first off does it look like I did part a right? And then I'm not sure how to setup contains(Circle2D &circle) in the circle.cpp class file, which is the function for part b. Also is it possible to create two functions with the same name, but different parameters like "contains"?
Last edited on
closed account (3hM2Nwbp)
Ah sorry, I left out part of the distance formula. It's been a while since I've used formulas of any kind.

Distance = SQRT((x1-y1)^2+(x2-y2)^2)

It looks like you did part A correctly, except that you're using the wrong coordinates as the circle you're testing against* - Edited
It should be double result = pow( pow((pointX - this->x),2) + pow((pointY - this->y),2) , 0.5);

Creating overloaded functions is done like so:
1
2
3
4
5
//Contains another circle?
bool contains(Circle2D& circle);

//Contains a point?
bool contains(double x, double y);


* Edit 3 - You may find it convenient to set up a static method in your circle class to calculate the distance between two points so instead of writing double result = pow( pow((pointX - 0),2) + pow((pointY - 0),2) , 0.5);, you could write getDistance(pointX, 0, pointY, 0)
Last edited on
double result = pow( pow((pointX - this->x),2) + pow((pointY - this->y),2) , 0.5);

Blech. pow abuse.

pow is generalized to be able to use any exponent. When you're just squaring something, it's best to not use pow, but to just square it manually.

likewise, sqrt() is specialized to find the square root of something.

Instead of the above, you really should do this:

1
2
3
4
double difx = pointX - x;
double dify = pointY - y;

double result = sqrt( (difx*difx) + (dify*dify) );
pointX and this->x are the same thing though right? This chapter is on pointers and dynamic memory...do I need to create a dynamic object, like Circle2D *myCircle = new Circle2D(double x, double y, double radius) in order to have two circle objects created?

closed account (3hM2Nwbp)
pointX and this->x are the same thing though right?
1
2
3
4
bool Circle2D::contains(double x, double y)
{
	double pointX = x; //Which x? this->x or argument x?
	double pointY = y; //See above 

I'm pretty sure that the variable 'x' - local to that function scope, not this->x - is being set assigned to pointX.
Last edited on
I think the variable parameter of the function contains is being assigned to pointX and pointY. So where does this->x come from...the default constructor?
closed account (3hM2Nwbp)
this is a pointer to the object of whose member function you are calling. It's an implicit parameter to member functions. Refer to the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class C
{
    private:
        int x, y;
    public:
        C(int a, int b) : x(a), y(b) { }

        int getX(void) const
        {
            return this->x;
        }

        int getY(void) const
        {
            return this->y;
        }
};

int main()
{
    C c(1, 2);
    c.getX();  //A pointer to 'c' is passed to the method invisibly.  That pointer is 'this'.
    return 0; //*Edit* - Forgot my return again :-\
}


Is it more clear now?
Last edited on
Sorry, but I dont know what this is ": x(a), y(b) { }" means
Last edited on
closed account (3hM2Nwbp)
That is referred to as an initializer initialization list. It has a similar affect as:

1
2
3
4
5
C(int a, int b)
{
    x = a; //or this->x = a; if you want to be explicit about which x
    y = b; //or this->y = b;
}


An excellent article about them can be found here:
http://www.cprogramming.com/tutorial/initialization-lists-c++.html
Last edited on
Ok, so with your example the member function being called is getX. So when you say this->x you are pointing to the value at the address of x, correct?

I've read and read, and thought I understood pointers, but it seems that I don't.
closed account (3hM2Nwbp)
Yes
1
2
3
4
5
6
7
8
9
10
11
12

class C
{
    private:
        int x;
    public:
        C(void)
        {
            x = 12345;
            std::cout << this->x << std::endl; //Prints out '12345'
        }
};


Trust me, it eventually clicks and you say "ohhhhhhhhhhhhhh" - it happened to me about 8 years back while learning Java :)
Last edited on
I guess I just don't understand why use it? Because you could just say

std::cout << x << std::endl; //Prints out '12345'

And you get the same thing
closed account (3hM2Nwbp)
consider this
1
2
3
4
5
6
7
8
9
10
11
12
13
class C
{
    private:
        int x;
    public:
        C(void)
        {
            this->x = 12345; //Sets 'x' on line 4
            int x = 4;
            std::cout << x << std::endl; //Prints out '4' (x from line 9)
            std::cout << this->x << std::endl; //Prints out '12345' (x from line 4)
        }
};


In normal cases, you can get away without using 'this', but for a bunch of cases, the 'this' pointer is invaluable. This was the case for your solution in part 'A', where you had class members 'x', and 'y', as well as local method parameters 'x', and 'y'.
Last edited on
I just printed out the value of this->x in the code

double result = pow( pow((pointX - this->x),2) + pow((pointY - this->y),2) , 0.5);

and the value was 5, because when asked for the value of x I put 5...so basically I am taking (5-5)^2...I thought it was supposed to be (5-0)^2. I'm probably wrong though
Last edited on
closed account (3hM2Nwbp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Check to see if the point (x, y) is inside this circle
bool Circle2D::contains(double x, double y)
{
        //The point we're checking minus the x coordinate center of this circle
        int xDelta = x - this->x;
        //The point we're checking minus the y coordinate center of this circle
        int yDelta = y - this->y;
        //The distance between the center of this circle and the point we're checking
	double distance = pow( (xDelta*xDelta) + (yDelta*tDelta) , 0.5);
	
	// Checking if the distance from the center to the radius is larger than center to the point
        // If the distance is less than the radius of this circle, then the point is inside
	if (distance< this->radius)
        {
            return true;
        }
        else
        {
             return false;
        }
}


Let me know if that explanation helps.

I'm going to sleep now, but I'll be sure to check this topic before I leave tomorrow. If you need help sooner than that, I'm sure that the other members will be able to help.

Kind regards,
Luc
Last edited on
Ok, the code makes sense...why it is setup this way does not. I put this snippet of code in my file to test what the output would be.

1
2
3
4
int xDelta = x - this->x;
	cout << x << endl;
	cout <<this->x  << endl;
	cout <<xDelta << endl;


My input for the radius was 3, x was 6, and y was 7.

The output for that little piece of code was

This program will display whether or not the point and radius you specify are in
side, outside, or upon the circle.
Enter the radius: 3

Enter the x point: 6

Enter the y point: 7
6
7
-1
Your point is outside the circle.


Why is it taking 6-7? this->x should not be the value of y...

Last edited on
Also how do I go about part b of this problem? I have to ask the user for the radius and points of two circle objects (right?). Then I have to check and see if one is inside the other. I don't get how and why the second function is setup the way it is though.

contains(Circle2D &circle)

I didn't know you could put a class and an object inside of a function like that..the reference of the object "&circle" does it mean the memory address of the previous created object?

So..
1
2
3
4
bool Circle2D::contains(Circle2D &myCircle)
{
//what would go here?
}
closed account (3hM2Nwbp)
Sorry for the delayed response. From what you posted 2 posts up, you've entered a radius and two coordinates, X and Y. Were the X and Y coordinates for the circle that you're making, or the coordinates for the point to check for? To make a circle you need the radius, and coordinate of the center. You'll also need to get the coordinates of the point to check for. My main question is, where is the second coordinate entry?

1
2
3
4
5
6
7
8
9
10
11
12
13
This program will display whether or not the point and radius you specify are in
side, outside, or upon the circle.
Enter the radius: 3

Enter the x coordinate of the circle: 6

Enter the y coordinate of the circle: 7

Enter the x coordinate of the point to check for: ?

Enter the y coordinate of the point to check for: ?

Do calculation now in the form of circle.contains(the x coordinate of the point to check for, the y coordinate of the point to check for)
Pages: 12