Class object question

Pages: 12
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).
-(I'm looking at this part)A method contains(Circle2D circle) that returns true if the specified
circle is inside this circle. See Figure 9.11(b).

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?
}


My previous thread for the first part is here: (Code is there)
http://www.cplusplus.com/forum/general/35233/
well each circle has a centre point, and a radius. So if you think about it we can easily calculate the min and max x and y values for each circle, we could call them left, right, top and botom then:

1
2
3
4
if ( otherLeft > thisLeft && otherRight < thisRight && otherTop > thisTop && otherBottom < thisBottom )
    return true;
else
    return false;


this is presuming that the function should only return true if the circle is entirely inside

EDIT - You could probably also do it using the sqrt function and pythagoras theorum comparing the distance of their centre points and then using their radius to figure out whether it was inside. But sqrt is slow so I'm not sure which version will be faster.
Last edited on
Ok, I see that. How do I send an object to a function? Like:
 
contains(Circle2D &circle)


because the first object created used
 
contains(double x, double y)


And when I create the second object, I'm unsure on how to set that up in order to check if the circle is inside the other circle. Also I want to make sure that I am reading the question correctly. For part b I am checking to see if the first circle with radius whatever is inside the second with whatever radius the user specifies? So x and y will only be needed for the center of the circle for the second one correct?
I don't know how your constructor works but lets say it takes an x, y and radius.

1
2
3
4
Circle2D aCircle(10,10,50);
Circle3D anotherCircle(20,10,10);
if ( aCircle.contains(anotherCircle) )
    cout << "fuck yeah!";


I think you may be confused though because I just solved part B for you, not A. Have you already solved part A? Maybe I just didn't understand your question.
Yes I solved part A already. You can look at this post to see the code for it (http://www.cplusplus.com/forum/general/35233/ ). Here are my constructors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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);
}        


EDIT - You could probably also do it using the sqrt function and pythagoras theorum comparing the distance of their centre points and then using their radius to figure out whether it was inside. But sqrt is slow so I'm not sure which version will be faster.


You can omit the sqrt() if you square both sides of the equation. Remember that pythagorean's theorum is a*a + b*b = c*c... it isn't actually c = sqrt(a*a + b*b) -- you don't need to actually sqrt anything.

My confusion here is that "inside" could mean one of two different things.

1) Do you need to know whether one circle is completely enclosed inside another circle?

or

2) Do you need to know if two circles overlap?


#2 is definitely easier, and can be solved by applying pythagorean's theorum.

The difference in the center points can be used to find A and B. Whereas the sum of the radius' is C

if A*A + B*B is <= C*C, then the circles overlap


For #1 I'd have to think about it more...

EDIT:

Quirkyusername's solution would not work for #1. It's possible for a small circle to be outside of a larger circle, while still inside the larger circle's bounding box.

A pythagorean approach for this would probably be something like...

I = inner circle
O = outer circle

X = I.x - O.x
Y = I.y - O.y

D = (X*X + Y*Y) <- distance between center points squared

IR = i.radius * i.radius
OR = o.radius * o.radius

if IR + D <= OR, then I is inside O
Last edited on
Don't forget about const correctness...
Make sure the function is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Circle2D.h
class Circle2D
{
public:
...
bool contains(const Circle2D&) const;
...
};


// Circle2D.cpp
bool Circle2D::contains(const Circle2D& myCircle) const
{
//what would go here?
}


If the function should return true if the input circle is COMPLETELY inside 'this' circle, one condition would be that the diameter of the input circle must be less than the diameter of 'this' circle (unless the exact same circles is considered inside as well). There are obviously other conditions that must be met.
Here is an image of the problem. Part B wants a circle completely inside another circle.

http://i4.photobucket.com/albums/y116/socomnego/118.jpg

From the code I have I just don't understand how I can go from part A when I had the user enter in a point and a radius of a default circle; to a circle inside of a circle...makes no sense to me. Maybe I misread but the script I have asks the user for a radius and an x and y which are used to make a point on the 2D graph..not to be used as a center for that circle with radius (whatever). Then with the x and y that the user entered I check to see if the point is inside of the circle centered at (0,0) with radius (lets say 4)
http://www.cplusplus.com/forum/general/35233/#msg190416 (it missed an sqrt in the formula).
Distance between centers + radius2 < Radius1

@Disch: I don't think that you could simply square the things
Dist + r2 < R2
(Dist + r2)2 < R12
Dist2 + r22+ 2*Dist*r2 < R12


Edit: Using your nomenclature:
D + IR + 2*sqrt(D*IR) <= OR
Last edited on
But you'll notice I never actually use the distance in my formula, I only use the distance squared.

In pythagorean, every unit is squared, so there's not need to sqrt any of them if they're all squares.

I wrote:
if IR + D <= OR, then I is inside O


Where:
IR = inside circle's radius squared
D = distance between center points squared
OR = outside circle's radius squared
Alright, I'm not really worrying about the formula but really the code and if I am even doing it right thus far. So I have step A done...did I do it right? And as far as part B goes how do I get the input for two circles in order to compare them with the class object? Do I have to add more user inputs in the main(), or am I supposed to reuse the same input variables by using pointers or memory allocation? I ask this because this chapter is about Pointers and Dynamic Memory...so you would think that the homework for it would be to implement that, right?
@Disch: Yeah, but I think that you are forgetting an 2*sqrt(D*IR) term.
However:
Dist + r1 < R2
Dist < R2-r1
Now square both sides
Dist2 < (R2-r1)2


@kraigballa: I don't see any pointer in the code.
I don't understand your question. Just test your functions.

Off topic: Is there a way to depth copy a post. I mean with all the tags visible.
Last edited on
I know, and I think that is because part A didn't need any...however do the other parts?

My question is what circles am I using for part B? Is it the same circle from part A plus another one? Or do I have to create two new objects in the main function to hold the user input?

For example: Is this how it should be, or is there an easier way with pointer,etc?
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
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 << "\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.";

	 // The first circle--For part B
	 cout << "\nEnter the radius: ";
	 cin >> radius;
	 myCircle2.setRadius(radius);
	 cout << "\nEnter the x point: ";
	 cin >> x;
	 myCircle2.setx(x);
	 cout << "\nEnter the y point: ";
	 cin >> y;
	 myCircle2.setx(y);

	 // The second circle--For part B
	 cout << "\nEnter the radius: ";
	 cin >> radius;
	 myCircle3.setRadius(radius);
	 cout << "\nEnter the x point: ";
	 cin >> x;
	 myCircle3.setx(x);
	 cout << "\nEnter the y point: ";
	 cin >> y;
	 myCircle3.setx(y);

	 // Display the results of the function contain

	 if (myCircle2.contains(myCircle3) == true)
		 cout << "Your circle is inside the circle.";
	 else
		 cout << "Your circle is outside the circle.";


     cout << "\n\nWant to try again? (Y/N) ";
     cin >> again;
 
  }while(toupper(again) == 'Y');

  return 0;
}  
Last edited on
I think that that looks fine (it could be zipped, though)
However, why are you testing if the centre is inside the circle?
1
2
3
4
5
6
7
8
9
	 cin >> x;
	 myCircle.setx(x);
	 cout << "\nEnter the y point: ";
	 cin >> y;
	 myCircle.setx(y);
/*add an point input here*/

	 // Display the results of the function contain
	 if (myCircle.contains(x, y) == true) //myCircle.centre is x,y 


Also, there is no need to do if(var == true), just if( var ) will work.
In part A I am testing if the point the user input is inside the circle with the radius they input. And then for part B I am testing if circle two is inside circle 1. What do you mean by "add a point input here?" Should I start myCircle2 and myCircle3 right there, or do you mean something else?
I thought that
1
2
myCircle.setx(x);
myCircle.sety(y);
set the centre of the circle.

So I don't see where do you ask the user to input the point to test. You are using the same x,y as the centre.
Well for part A it represents the point inside of the circle. The default center is (0,0) which is shown in the formula that I have in the script. Then for part B and C they will be used as the center of the circle instead of a point inside of a circle. Does that make sense?
I don't understand you. Could you please post Circle2D::contains(const Circle2D&) and Circle2D::contains(double x, double y)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 - this->x),2) + pow((pointY - this->y),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;
}

bool Circle2D::contains(Circle2D &myCircle)
{
	// Not sure what to put here
}
I am working on part C now, and wanted to see if my overlap of the two circles function is correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool Circle2D::overlaps(Circle2D &otherCircle)
{
	// 'this' points to the object in which calls the function
       double thirdCircleRadius = otherCircle.getRadius();
       double thirdCircleX = otherCircle.getx();      
       double thirdCircleY = otherCircle.gety();   
       double secondCircleRadius = this->getRadius();         
       double secondCircleX =	this->getx();               
       double secondCircleY = this->gety();   
       double X = thirdCircleX - secondCircleX;
       double Y = thirdCircleY - secondCircleY;
       double D = (X*X + Y*Y);   // distance between centers squared
       double IR = thirdCircleRadius*thirdCircleRadius;
       double OR = secondCircleRadius*secondCircleRadius;

	// Testing for overlap
	if ((IR + D) < OR && (IR + D) > OR)
		return true;
	else
		return false;
}
Pages: 12