Finding circle zero-points with C++

  • Comp Sci
  • Thread starter noblepants
  • Start date
  • Tags
    C++ Circle
In summary: If quadratic roots returns 2, there are two roots of the quadratic equation, and both are x-values of points of intersection.
  • #1
noblepants
7
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
  • #2
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;
    
 }
 
  • #3
: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.
 

Related to Finding circle zero-points with C++

1. What is the purpose of finding circle zero-points with C++?

The purpose of finding circle zero-points with C++ is to determine the coordinates of points where a circle intersects with the x and y axes, also known as the zero-points. This information is useful in various mathematical and scientific calculations involving circles.

2. How does C++ help in finding circle zero-points?

C++ provides a programming platform for implementing algorithms and equations that can efficiently calculate the zero-points of a circle. Its built-in mathematical functions and efficient memory management make it a popular choice for scientific computations.

3. What are the steps involved in finding circle zero-points with C++?

The steps for finding circle zero-points with C++ include defining the circle's radius and center coordinates, writing an equation to represent the circle, setting up a system of equations to find the zero-points, and implementing the algorithm in C++ code.

4. Can C++ accurately find the zero-points of any circle?

Yes, C++ can accurately find the zero-points of any circle as long as the circle's equation is properly defined and the algorithm is correctly implemented. However, the precision of the results may depend on the data types and mathematical functions used in the code.

5. Are there any limitations to using C++ for finding circle zero-points?

Using C++ for finding circle zero-points may require a good understanding of mathematical concepts and programming skills. Additionally, the accuracy of the results may be affected by rounding errors and the limitations of floating-point arithmetic in C++.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
764
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
896
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top