Finding circle zero-points with C++

  • Context: Comp Sci 
  • Thread starter Thread starter noblepants
  • Start date Start date
  • Tags Tags
    C++ Circle
Click For Summary
SUMMARY

This discussion focuses on implementing a C++ function to determine the intersection points of a circle with the x-axis using the standard circle equation (x − h)² + (y − k)² = r². The user seeks to create a function called circleIntersections that utilizes a previously defined quadraticRoots function to find the x-values where the circle intersects the x-axis. Key insights include the necessity of calculating the discriminant to ascertain the number of real roots and refining variable usage for clarity and efficiency in the code.

PREREQUISITES
  • Understanding of the standard form of a circle equation
  • Proficiency in C++ programming, particularly with functions and pointers
  • Familiarity with the quadratic formula and its application
  • Knowledge of mathematical concepts such as discriminants and real roots
NEXT STEPS
  • Implement the circleIntersections function to calculate intersection points
  • Refine the quadraticRoots function by calculating the discriminant once
  • Explore error handling for invalid circle parameters (e.g., negative radius)
  • Research best practices for naming conventions in programming for clarity
USEFUL FOR

Beginner C++ programmers, mathematics enthusiasts, and anyone interested in computational geometry and function implementation in programming.

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 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K