# Finding circle zero-points with C++

1. Oct 6, 2012

### noblepants

Hi everyone! My primary question is below the problem.

My problem:
Circles
The standard form of an equation for a circle is (x − h)2 + (y − k)2 = r2 where (h,k) represents the center of the circle and r is the radius. The y-value of the equation becomes zero at the point of intersection with the x-axis. When the value of 0 is substituted for y, the equation can be simplified to a quadratic equation in standard form.
(x − h)2 + (0 − k)2=r2
(x − h)2 + k2 = r2
x2 − 2hx + (h2 + k2 − r2)=0
From here, the value(s) of x can be resolved using the quadratic formula.
Write a function that accepts the center point and radius of a circle and returns how many times the circle crosses the x-axis, if at all. If an intersection occurs, the function should return the x-value(s) as well. This new function should call a quadraticRoots function you wrote(which should return only the real roots and the number thereof) to determine if the circle crosses the x-axis and if so, where.

******************************************************************************
My question is how do I break the circle equation down so that I can incorporate it into the new function (circleIntersections) to be operated on by the previous function quadraticRoots.

Code (Text):

int quadraticRoots (double a, double b, double c, double& x1, double& x2);
int circleIntersections (double h, double k, double r, double& x1, double& x2);

int quadraticRoots (double a, double b, double c, double& x1, double& x2)
{
int numberOfroots; //real roots
double findRoots1;
double findRoots2;

if( (b*b)-(4*a*c) < 0) // determinant of quadratic formula
{

numberOfroots=0;
}

else if( (b*b)-(4*a*c) == 0)
{
findRoots1= (-b + ( sqrt( (b*b)-(4*a*c) ) ) ) /(2*a); // quadratic formula
findRoots2= (-b - ( sqrt( (b*b)-(4*a*c) ) ) ) /(2*a);
numberOfroots=1 ;

if (findRoots1 == findRoots2)
{
x1=findRoots1;
}
}

else
{
numberOfroots=2;
x1= (-b + ( sqrt( (b*b)-(4*a*c) ) ) ) /(2*a);
x2= (-b - ( sqrt( (b*b)-(4*a*c) ) ) ) /(2*a);

}
return numberOfroots;

}

//int circleIntersections (double h, double k, double r, double& x1, double& x2);
//{

//}
int main()
{
double a=3, b=2,c=3,x1,x2,h,k,r;
cout<<"The number of real roots are: ";

cout<<"Real roots: "<<x1<<" & "<<x2<<"\n\n";
cout<<"To determine the zero-points of a circle....";
<<"Please enter '(h,k)' coordinates that are the circle's centerpoint: ";
cin>>h>>k;
cin>>r;

circleIntersections (h,k,x1,x2);

return 0;

}

2. Oct 6, 2012

### Staff: Mentor

I'm not sure I understand what you're asking, but I'll do my best. Your quadraticRoots function returns 0, 1, or 2, depending on how many real roots there are to the equation (x - h)2 + k2 = r2.

Note that I am using HTML tags to make the exponents. You can do this by clicking the Go Advanced button, which opens a menu across the top with the advanced capabilities. At a minimum, you can represent exponents using the ^ character, some notation that has survived from Basic, and is in common usage.

Your quadraticRoots function is already set up to give you what you need, with a few small changes. This function has two parameters that are pointers to the value or values that are the roots to the equation above.

All you need is some logic to know when it's reasonable to access those values.

If quadraticRoots returns 0, there are no roots, hence the circle does not intersect the x-axis.
If quadraticRoots returns 1, there is one root, meaning one point of intersection with the x-axis.
If quadraticRoots returns 2, there are two roots - two intersection points.

As already mentioned, your code needs some refinement. First off, you don't need findRoots1 and findRoots2. Also, these names suggest that they do something rather than hold something. Names of things should nouns; names of functions should be verbs.

In the second clause of your if block (else if (b * b - 4 * a * c) == 0), remember that there is only one root so there is no need to set two variables, and then later set yet another variable. Instead of using intermediate variables (which you don't need), just set one of the pointer variables, like this:

Code (Text):

*x1= (-b + ( sqrt( (b*b)-(4*a*c) ) ) ) /(2*a);

Important! Notice that I am not setting x1 - I am setting the location that x1 points to.

An even nicer way to do things is to calculate the discriminant once, above the start if the if block, like so:
Code (Text):

{
double discriminant = b * b - 4.0 * a * c;

if (discriminant < 0)
{
...
}
else if (discriminant == 0)
{
// Set *x1
}
else
{
// Set *x1 and *x2
}
...
In main, use the logic I described above regarding the value (0, 1, or 2) returned by quadraticRoots to know whether there are values in x1 and x2.

3. Oct 6, 2012

### noblepants

Sorry about the notation I was being lazy And thank you your notes helped clarify a lot. I released that I had the basic concept down but a feeling that some pits and pieces were not structurally best.

I have been running into this problem a-lot as a noob programer, at parsing up the pieces of my problem or project so that I have an idea of how to ask myself. "What do I need to do next or What piece should work on first?" Might you have any suggestions as to train my mind how to do this? I do it well in other areas but not here.

Again thanks for the help I'm a bit embarrassed at having difficulty with things that seem so basic.