Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <cmath> using namespace std; // a structure to repr

ID: 3836359 • Letter: #

Question

#include <iostream>
#include <cmath>
using namespace std;

// a structure to represent a point in 2D , the x and y co-ordinates
class Point
{
   double x;
   double y;
   public:
       Point() //default constructor initiializing to (0,0)
       {
           x=y=0;
       }
       Point(double x1,double y1)
       {
           x=x1;
           y=y1;
       }
       double getX()
       {
           return x;
          
       }
       double getY()
       {
           return y;
       }
       void display()
       {
           cout<<"("<<x<<","<<y<<")";
       }
};
/*class representing circle with center (a,b) and radius r*/
class Circle
{
   Point center;
   double radius;  
   public:
       Circle(Point cent,double rad)
       {
           center=cent;
           radius=rad;
       }
      
       void setCenter(double x,double y)
       {
           center=Point(x,y);
       }
       void setRadius(double r)
       {
           radius=r;
       }
       //display the circle equation
       void displayCircleEq()
       {
           cout<<" (x-"<<center.getX()<<")^2 + (y-"<<center.getY()<<")^2 = "<<radius<<"^2";
       }
      
       void display()
       {
           cout<<" circle with center ";
           center.display();
           cout<<" radius "<<radius;
       }
       Point getCenter() const
       {
           return center;
       }
       double getRadius()
       {
           return radius;
       }
  
};

//class representing a line with equation y=mx+c
class Line
{
   double m;
   double c;
   public:
       Line(int m1,int c1)
       {
           m=m1;
           c=c1;
       }  
      
       double getM()
       {
           return m;
       }
       double getC()
       {
           return c;
       }
       void display()
       {
           cout<<" line y= "<<m<<" * x + " <<c;
       }
      
};

Point * calculateIntersection(Circle &cir,Line& lin)
{
   double A,B,C;
   double m=lin.getM(),c=lin.getC(),a=cir.getCenter().getX(),b=cir.getCenter().getY();
   double r=cir.getRadius();
   double x1,y1,x2,y2, d;
      
   A=pow(m,2)+1; // A=m^2 + 1
   B=2*(m*c-m*b-a); //B=2(mc-mb-a)
   C= pow(a,2)+pow(b,2)-pow(r,2)+pow(c,2)-2*b*c;//a^2+b^2-r^2+c^2-2bc
  
   d=sqrt(pow(B,2)-4*A*C); //calculate sqrt(B^2-4AC)
  
   //first root x=(-B+sqrt(B^2-4AC)) / 2A
   x1=(-B+d)/(2*A);
  
   //second root x=(-B-sqrt(B^2-4AC)) / 2A  
   x2=(-B+d)/(2*A);
  
   //using line equation to get y values
   y1=m*x1+c;
   y2=m*x2+c;
  
   Point *intersection=new Point[2];
   intersection[0] =Point(x1,y1);
   intersection[1] =Point(x2,y2); //the 2 intersection points stored in array and returned
  
     
   return intersection;
}

int main()
{

   Circle circle(Point(0,0),5); //circle with origin as center and radius 5
   Line line(1,0); // line y=x
   Point *inter=calculateIntersection(circle,line);
   circle.display();
   circle.displayCircleEq();
   line.display();
   cout<<" intersection points are ";
   cout<<" Point 1: ";
   inter[0].display();
   cout<<" Point 2: ";
   inter[1].display();
   return 0;
}

output:


********************** dont understand

Point * calculateIntersection(Circle &cir,Line& lin)
{
   double A,B,C;
   double m=lin.getM(),c=lin.getC(),a=cir.getCenter().getX(),b=cir.getCenter().getY();
   double r=cir.getRadius

why do you use point * calcuateIntersection? dont understand * why use pointer/ explain lin.get statements / can simplier code be used for this entire function/ thanks c++

Explanation / Answer

Hi Friend, We can ONLY return ONE value from function.

So, if you want to return more than one value, then you can return pointer to Array from function
because we can store any number of value in an array of similar data type.


In you scenario,

YOu have to retorn Point of Intersections between Line and Circle from function :
                   Point * calculateIntersection(Circle &cir,Line& lin)

Since, more than one Point of Intersections can be possible between Lines and a Circle, so you need to return
more than one Point Objects from method calculateIntersection.

Thats why we are returnng pointer to Point obiects array.