#include #include using namespace std; // a structure to represent a point in 2D
ID: 3838137 • Letter: #
Question
#include
#include
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<<"("< }
};
/*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-"<
//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= "<
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;
}
*************************** we have not covered pointers to objects for classes yet/ revise without the use of the pointers to objects c++ / this needs to be simplified /cin input center and radius thanks c++ this should be a basic class program to determine intersection points for a straight line through a circle
Explanation / Answer
When we analyze the program the code written for the problem statement is correct and i have modifyed the program.
Find it below and commented on the same line of the code
#
include < stdio.h > #include < iostream >
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;
}
//MODIFYED IN THE DISPLAY METHOD
void display() {
cout << "Value of X:" + x + " " + Value of Y + " " + y << endl;
}
};
/*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
//MODIFYED THE BELOW METHOD BY ADDING THE BELOW EQUATION
void displayCircleEq() {
cout << " (x-center)^2+(y - center)^2=(radius)^2" << endl;
}
};
//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;
}
//MODIFYED BELOW METHOD
void display() {
int y;
y = m * x + c;
cout << "y = " << y << "when m = " << m << "; " << "b = " << b << "; x = " << x << endl;
}
};
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.