The assignment was to graph a line within 10 units of the origin
after the user inputs the slope and y-intercept.
Sounded much easier than it was, I gave it a shot but I can't
get it to print correctly. To be honest I just dont know if im
headed in remotely the right direction, any help would be greatly appreciated.
I think the problem is passing my counter i to the function printline() can that be done?

``````#include<iostream>
#include<iomanip>
using namespace std;
//
//
//
// Graph a line when given the linear equation.
//
void graph(double, double);
void printline(double, double, int);
//
int main()   {

cout << "Please enter the slope and y-intercept" << endl;
cout << "of the line you would like to graph." << endl;

//take input
double slope, intercept;
cout << "Slope: ";
cin >> slope;
cout << "Y-Intercept: ";
cin >> intercept;
cout << endl;

//graph the line
graph(slope, intercept);

cout << endl;

cout << "The full equation of your line is" << endl;
cout << "y = " << slope << "x + " << intercept;

cin.get();
cin.get();
return 0;
}

void graph(double slope, double intercept)
{
// tried to use printline() for lines not on grid
// and print big line where it should be on a
// cartesian plane, at 0
int i;
for(i=10; i>(-10); i--)
if (i=0)
cout << "---------------------" << endl;
else
printline(slope, intercept, i);
}

void printline(double slope, double intercept, int i)  {

double xcoord;

//xcoord is the distance from (-10) the asterisk should print
xcoord=(i-intercept)/slope;
if (xcoord<0)
// if its negative i flip it to positive and subtract it
// from ten to make it the proper distance
// from the y-axis
{	xcoord=xcoord*(-1);
cout << setw(10-xcoord) << "*";
cout << setw(xcoord+1) << "|";
cout << "          " << endl;
}

else
// otherwise i print the blankspace, the y axis,
// and then the proper distance fromthe y-axis
// once i have my asterisk in the right spot i fill in the rest with blanks
{	cout << setw(11) << "|";
cout << setw(xcoord) << "*";
cout << setw(10-xcoord) << " " << endl;
}
}
// note that my use of setw() is based on the
// assumption that it automatically alignstext within
// to the right. My comsc teach told me that though
// i could be doing it wrong.``````

If I understand you right, what you need to do is google for line drawing algos? In particular, Bresenham's line algorithm stands out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.