1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <iostream>
#include <fstream>
/*This project was made by Talha Asif
ID:EE-131097 CLASS: 1-B*/
using namespace std;
const int sz=250;
int x[sz][sz];
string fname="abc.pgm";
void drawinfile(string fname)
{
ofstream out(fname.c_str());
// out.open();
out << "P2 \n" << sz << " " << sz << " \n255 \n";
for(int i=0;i<sz; i++)
{
for(int j=0;j<sz; j++)
out << x[i][j] << " ";
out<< endl;
}
out.close();
}
void line(int x1, int y1, int x2, int y2, int c)
{
double deltax, deltay, m12, m, c12;
deltax = x2 - x1;
deltay = y2 - y1;
m12 = (deltay / deltax);
c12 = y2 - (m12 * x2);
int *smallx, *smally, *bigx, *bigy;
if(x1 > x2)
{
smallx = &x2;
bigx = &x1;
}
else
{
smallx = &x1;
bigx = &x2;
}
if(y1 > y2)
{
smally = &y2;
bigy = &y1;
}
else
{
smally = &y1;
bigy = &y2;
}
if(m12 != 0)
{
for(int y = *smallx;y <= *bigx;y++)
{
for(int z = *smally;z <= *bigy;z++)
{
int a = ((m * y) + c12);
x[a][z] = c;
}
}
}
else
{
for(int y = *smallx;y <= *bigx;y++)
{
for(int z = *smally;z <= *bigy;z++)
{
int a = c12;
x[a][z] = c;
}
}
}
}
void init()
{
for(int i=0;i<sz; i++)
for(int j=0;j<sz; j++)
x[i][j]=0;
}
int main()
{
int choice, x1, y1, x2, y2, c;
init();
cout << "Enter what you want as output\n" << "1) Single line\n" << "2) Triangle" << endl;
cout << "Enter your desired choice" << endl;
cin >> choice;
while(!(choice == 1 || choice == 2))
{
cout << "Please enter a valid choice again" << endl;
cin >> choice;
}
switch(choice)
{
case 1:
{
cout << "Enter the coordinate of the starting point in the sequence x y seperating them by a space" << endl;
cin >> x1 >> y1;
cout << "Enter the coordinate of the ending point in the sequence x y seperating them by a space" << endl;
cin >> x2 >> y2;
cout << "Enter the c" << endl;
cin >> c;
line(x1, y1, x2, y2, c);
}
break;
default:
{
for(int a = 0;a != 3;a++)
{
cout << "Enter the coordinate of the starting point in the sequence x y seperating them by a space" << endl;
cin >> x1 >> y1;
cout << "Enter the coordinate of the ending point in the sequence x y seperating them by a space" << endl;
cin >> x2 >> y2;
cout << "Enter the c" << endl;
cin >> c;
line(x1, y1, x2, y2, c);
}
}
}
drawinfile("abc.pgm");
return 0;
}
|