Comp Sci Finding circle zero-points with C++

  • Thread starter Thread starter noblepants
  • Start date Start date
  • Tags Tags
    C++ Circle
AI Thread Summary
The discussion focuses on implementing a C++ function to determine how many times a circle intersects the x-axis based on its center and radius. The standard circle equation is transformed into a quadratic form to facilitate finding intersection points. Participants emphasize the importance of using the quadratic formula to derive real roots, which indicate intersection points. Suggestions include refining variable usage and structuring the code for clarity, particularly in the quadraticRoots function. The conversation also touches on improving problem-solving skills in programming by breaking down tasks into manageable parts.
noblepants
Messages
7
Reaction score
0
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:
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<<quadraticRoots(a,b,c,x1,x2)<<"\n";
    
    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;
    cout<<"And please enter the circle's radius: " ;
        cin>>r;
    
    circleIntersections (h,k,x1,x2);
    
    return 0;
    
 }
 
Physics news on Phys.org
noblepants said:
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.

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:
*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:
{
   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.
thatguythere said:
Code:
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<<quadraticRoots(a,b,c,x1,x2)<<"\n";
    
    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;
    cout<<"And please enter the circle's radius: " ;
        cin>>r;
    
    circleIntersections (h,k,x1,x2);
    
    return 0;
    
 }
 
:smile:
Sorry about the notation I was being lazy :redface: 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.
 

Similar threads

Replies
2
Views
3K
Replies
1
Views
2K
Replies
0
Views
148
Replies
3
Views
1K
Replies
5
Views
2K
Replies
8
Views
1K
Replies
7
Views
1K
Back
Top