c++ help. Recall, the equation for a straight line in cartesian coordinates (in
ID: 3671041 • Letter: C
Question
c++ help.
Recall, the equation for a straight line in cartesian coordinates (in 2 dimensions) is
y = ax + b
If we have two straight lines, with coefficients {a1, b1} and {a2, b2}, then the lines will either be parallel, or will intersect at some point
{xi = (b2 - b1) / (a1 - a2), yi = a1 * xi + b1}
Write a function called intersect that calculates these coordinates, using reference parameters for xi and yi, and value parameters for a1, b1, a2, b2.
The intersect function should also return an int value used to flag whether the lines are parallel (meaning that a1 == a2, which would cause a divide-by-zero error - make sure you avoid that!).
Use one of 2 global const variables for the return value:
const int OK = 0; // the values of {xi, yi} correctly represent the coordinates of the intersection of the two lines
const int DIV_ZERO = 1; // the two lines are parallel, and the values of {xi, yi} have not been altered.
Your function will NOT take user input, or output anything to screen - all that will be done in main.
The only code you will need to add to the main provided is the actual function invocation.
this is what I have so far but it says i am supposed to test that `intersect(xi, yi, a1, b1, a2, b2);` correctly sets xi, yi to intersection coordinates. i am also supposed to test that the intersect function returns correct flag value.
#include <iostream>
using namespace std;
// Global const values for intersect return value
const int OK = 0;
const int DIV_ZERO = 1;
//function prototype
int intersect(double a1, double b1, double a2, double b2, double &xi, double &y1);
int main()
{
int flag;
double a1 = 0.0, b1 = 0.0, a2 = 0.0, b2 = 0.0;
cout << "Enter coefficients for Line 1 (a1, b1): ";
cin >> a1 >> b1;
cout << endl;
cout << "Enter coefficients for Line 2 (a2, b2): ";
cin >> a2 >> b2;
cout << endl;
cout << "Line 1: Y = " << a1 << "X + " << b1 << endl;
cout << "Line 2: Y = " << a2 << "X + " << b2 << endl;
double xi, yi;
//calling intersect function
flag=intersect(a1,b1,a2,b2,xi,yi);
if (flag == DIV_ZERO)
{
cout << "Lines are parallel" << endl;
}
else
{
cout << " Lines 1 & 2 intersect at {" << xi << ", " << yi << '}' << endl;
cout << "Proof: yi on Line 1 at (xi = " << xi << ") is "
<< a1 * xi + b1 << endl;
cout << " yi on Line 2 at (xi = " << xi << ") is "
<< a2 * xi + b2 << endl;
}
//pause program output on console until user press a key
return 0;
}
/**The method intersect that takes two coorodinates points
and xi and yi as reference variables and finds if the two lines are parallel
and intersecting points. */
int intersect(double a1, double b1, double a2, double b2, double &xi, double &yi)
{
int flag;
//Check if a1 and a2 are equal then set DIV_ZERO
//for parallel lines
if(a1==a2)
{
flag= DIV_ZERO;
}
else
{
//Set flag OK
flag= OK;
//find xi and yi
xi=(b2-b1)/(a1-a2);
yi=a1*xi+b1;
}
//return flag value
return flag;
}
Explanation / Answer
Answer
#include <iostream>
using namespace std;
const int OK = 0;
const int DIV_ZERO = 1;
int intersect(double a1, double b1, double a2, double b2, double &xi, double &yi)
{
int flag;
if(a1==a2)
{
flag= DIV_ZERO;
}
else
{
flag= OK;
xi=(b2-b1)/(a1-a2);
yi=a1*xi+b1;
}
return flag;
}
int main()
{
int flag;
double a1 = 0.0, b1 = 0.0, a2 = 0.0, b2 = 0.0;
cout << "Enter coefficients for Line 1 (a1, b1): ";
cin >> a1 >> b1;
cout << endl;
cout << "Enter coefficients for Line 2 (a2, b2): ";
cin >> a2 >> b2;
cout << endl;
cout << "Line 1: Y = " << a1 << "X + " << b1 << endl;
cout << "Line 2: Y = " << a2 << "X + " << b2 << endl;
double xi, yi;
flag=intersect(a1,b1,a2,b2,xi,yi);
if (flag == DIV_ZERO)
{
cout <<" Both Lines have equal slope a1==a2, so";
cout<< " Lines are parallel" << endl;
}
else
{
cout << " Lines 1 & 2 intersect at {" << xi << ", " << yi << '}' << endl;
cout << "Proof: yi on Line 1 at (xi = " << xi << ") is "<< a1 * xi + b1 << endl;
cout << " yi on Line 2 at (xi = " << xi << ") is "<< a2 * xi + b2 << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.