Wednesday 26 June 2013

#include <iostream>
using namespace std;
#define HEIGHT 25
#define WIDTH 80
char Matrix[HEIGHT][WIDTH];
double findSlope(int x1,int x2, int y1, int y2)
{
    if(x1 == x2)
        return 0;
    return (double(y2-y1)/double(x2-x1));
}
void initializeMatrix()
{
     for(int i=0; i<HEIGHT; i++)
     {
         for(int j=0; j<WIDTH; j++)
         {
             Matrix[i][j] = '.';  
         }
     }
}
int roundToInt(float val)
{
    if(val - int(val) < 0.5)
       return int(val);
    return int(val+1);
}
void setPoint(float slope,float constant,int x)
{
     int y = roundToInt(slope * x + constant);    
     if(y >=0 && y < HEIGHT)
     {
          Matrix[x][y] = '*';
     }
}
void fillArray(float slope,float constant,int x1,int x2, int y1, int y2)
{
     for(int x=0; x < WIDTH; x++)
     {
         setPoint(slope,constant,x);
     }
}
float findConstant(float slope,int x1,int y1)
{
    return (y1 - slope * x1);
}
void displayMatrix()
{
     for(int row=0; row<HEIGHT; row++)
     {
         cout<<"\n"<<row<<" = ";
         for(int col = 0; col<WIDTH; col++)
         {
             cout<<" "<<Matrix[row][col];
         }
     }
}
int main()
{
    int x1,x2,y1,y2;
    x1= 10;
    y1= 20;
    x2= 20;
    y2= 30;
    float slope= findSlope(x1,x2,y1,y2);
    float constant = findConstant(slope,x1,y1);
    initializeMatrix();
    fillArray(slope,constant,x1,x2,y1,y2);
    displayMatrix();
    system("pause");
}

No comments:

Post a Comment