Drawing for PPM

What I need to do is write a program which can read in a data file, process the
data, and create and write a PPM file for the corresponding image.The menu (with three options) should look like this:
1. Load file
2. Write file
3. Quit
Enter menu selection:

The datafile will contain a list of lines and/or triangles that are to be "drawn". A line will start
with an "L" and followed by the start point, the end point, and the color.
L x1 y1 x2 y2 r g b
A triangle will start with a "T" and followed by a point, color, point, color, point, color.
T x1 y1 r1 g1 b1 x2 y2 r2 g1 b2 x3 y3 r3 g3 b3

Load File will request an input file name.
Write File program should request the name of the output file, e.g. data1.ppm.
Open the file and write the image out to this file.
Quit just quits the program.

Files I will need:
Point.h
Point.cpp
Color.h
Line.h
Triangle.h
ShapeDrawer.h
ShapeDrawer.cpp
Color.cpp
Line.cpp
Triangle.cpp
pics.cpp

These are what I have...I obviously need help.
color.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef COLOR_H
#define COLOR_H
class Color
{
public:
	Color();           //default constructor
	Color(int, int, int);//constructor taking rgb
	void setRed(int);    // set red value
	void setGreen(int);  // set green value
	void setBlue(int);   // set blue value
	
	int getRed();        // return red value
	int getGreen();      // return green value
	int getBlue();       // return blue value
private:
	int red, green, blue;
	
};
#endif /*COLOR_H_*/ 


line.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef LINE_H
#define LINE_H
#include "Color.h"
#include "Point.h"
class Line
{
public:
   Line();                 // default constructor
	Line(Point, Point, Color);// constructor
	void setStart(Point);  // set the starting point of line
	void setEnd(Point);    // set the ending point of line
   void setColor(Color);  // set the color of the line
	Point getStart();      // return the starting point of line
	Point getEnd();        // return the ending point of line
	Color getColor();      // return the color of line
private:
	Point start, end;
	Color lineColor;
};
#endif /*LINE_H_*/ 


point.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef POINT_H
#define POINT_H
class Point
{
public:
	Point();
	Point(int, int);  
	void setX(int);  // set the x coordinate
	void setY(int);  // set the y coordinate

	int getX();      // return the x coordinate
	int getY();      // return the y coordinate
private:
	int x, y;	
};
#endif /*POINT_H_*/ 


shapedrawer.h
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
#ifndef SD_H
#define SD_H
#include<string>
using namespace std;
#include"Line.h"
#include"Point.h"
#include"Triangle.h"
#include"Color.h"
class ShapeDrawer
{
public:
	ShapeDrawer();
	void clear();
	void drawLine(Line);
	void drawTriangle(Triangle);
	void writeFile(string fileName);
	const static int N_ROWS = 100;
	const static int N_COLS = 100;
	float triArea(int xa, int ya, int xb, int yb, int xref, int yref);
private:
	void initArray();
	int pixeldata[3 * N_ROWS * N_COLS];
	void setPixel(int X, int Y, char color, int data);
	void setOnePixel(int X, int Y, int data);
	int evalFunc(int x, int y, int a, int b, int c);	
};
#endif 


triangle.h
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
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "Point.h"
#include "Color.h"
class Triangle
{
public:
	Triangle();
	Triangle(Point, Color, Point, Color, Point, Color);

	void setVertexOne(Point);
	void setVertexOneColor(Color);

	void setVertexTwo(Point);
	void setVertexTwoColor(Color);

	void setVertexThree(Point);
	void setVertexThreeColor(Color);

	Point getVertexOne();
	Color getVertexOneColor();

	Point getVertexTwo();
	Color getVertexTwoColor();

	Point getVertexThree();
	Color getVertexThreeColor();
private:
	Point vertexOne, vertexTwo, vertexThree;
	Color vertexOneColor, vertexTwoColor, vertexThreeColor;	
};
#endif 


AND..

point.cpp
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
#include <iostream>
#include "Point.h"
using namespace std;

Point::Point()
{
	x = 0;
	y = 0;
}
Point::Point(int xVal, int yVal)
{
	x = xVal;
	y = yVal;
}
void Point::setX(int xVal)
{
	x = xVal;
}
void Point::setY(int yVal)
{
	y = yVal;
}
int Point::getX()
{
	return x;
}
int Point::getY()
{
	return y;
}


shapedrawer.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

#include"ShapeDrawer.h"
#include"Line.h"
#include"Point.h"
#include"Triangle.h"
#include"Color.h"

using namespace std;

ShapeDrawer::ShapeDrawer()
{
	initArray();
}

void ShapeDrawer::initArray()
{
	for (int i = 0; i < (3*N_ROWS*N_COLS); i++) {
		pixeldata[i] = 0;
	}
}

void ShapeDrawer::writeFile(string fileName)
{
	ofstream outFile;
	outFile.open(fileName.c_str());
	outFile << "P3" << endl << "100\n100\n255\n" << endl;
	for (int j = 0; j < N_ROWS; j++)
		for (int i = 0; i < N_COLS; i++)
			outFile << pixeldata[3*(j*N_COLS+i)] << " " << pixeldata[3*(j*N_COLS+i)+1] << " " << pixeldata[3*(j*N_COLS+i)+2] << endl;
	outFile.close();
}

void ShapeDrawer::drawLine(Line l) {
	int xa = l.getStart().getX();
	int ya = l.getStart().getY();
	int xb = l.getEnd().getX();
	int yb = l.getEnd().getY();
	int red = l.getColor().getRed();
	int blue = l.getColor().getBlue();
	int green = l.getColor().getGreen();
	int negSlope = 0;
	int bigSlope = 0;
	int dx = abs(xb-xa), dy = abs(yb-ya);
	int p, twoDx, twoDy, twoDyDx;
	int x, y, xEnd, yEnd;

	if ((((float) dy / (float) dx)) > 1) {
		bigSlope = 1;
	}

	// -1 > Slope > 1
	if (bigSlope == 0) {
		twoDy = 2 * dy;
		twoDyDx = 2 * (dy - dx);
		p = 2 * dy - dx;

		if (xa > xb) {
			x = xb;
			y = yb;
			xEnd = xa;
			if ((ya-yb) < 0) {
				negSlope = 1;
			}
		}
		else {
			x = xa;
			y = ya;
			xEnd = xb;
			if ((yb-ya) < 0) {
				negSlope = 1;
			}
		}
		setPixel(x, y, 'R', red);
		setPixel(x, y, 'G', green);
		setPixel(x, y, 'B', blue);
		
		// Slope > 0
		if (negSlope == 0) {
		    while (x < xEnd) {
				x++;
				if (p < 0)
					p += twoDy;
				else {
					y++;
					p += twoDyDx;
				}
				setPixel(x, y, 'R', red);
				setPixel(x, y, 'G', green);
				setPixel(x, y, 'B', blue);
			}
		}
		// Slope < 0
		else {
			while (x < xEnd) {
				x++;
				if (p < 0)
					p += twoDy;
				else {
					y--;
					p += twoDyDx;
				}
				setPixel(x, y, 'R', red);
				setPixel(x, y, 'G', green);
				setPixel(x, y, 'B', blue);
			}
		}
	}
	// -1 > Slope > 1
	else {	
		twoDx = 2 * dx;
		twoDyDx = 2 * (dx - dy);
		p = 2 * dx - dy;

		if (xa > xb) {
			x = xb;
			y = yb;
			yEnd = ya;
			if ((ya-yb) < 0) {
				negSlope = 1;
			}
		}
		else {
			x = xa;
			y = ya;
			yEnd = yb;
			if ((yb-ya) < 0) {
				negSlope = 1;
			}
		}
		setPixel(x, y, 'R', red);
		setPixel(x, y, 'G', green);
		setPixel(x, y, 'B', blue);
		// Slope > 1
		if (negSlope == 0) {
			while (y < yEnd) {
				y++;
				if (p < 0)
					p += twoDx;
				else {
					x++;
					p += twoDyDx;
				}
				setPixel(x, y, 'R', red);
				setPixel(x, y, 'G', green);
				setPixel(x, y, 'B', blue);
			}	
		}
		// Slope < -1
		else {
			while (y > yEnd) {
				y--;
				if (p <= 0)
					p += twoDx;
				else {
					x++;
					p += twoDyDx;
				}
				setPixel(x, y, 'R', red);
				setPixel(x, y, 'G', green);
				setPixel(x, y, 'B', blue);
			}
		}	
	}
}


void ShapeDrawer::setPixel(int X, int Y, char color, int data) {
	int outtieX;
	int outtieY;

	Y = (N_ROWS-1) - Y;

	outtieX = X;
	outtieY = Y;
	if ((X < 0)||(X > N_COLS)||(Y < 0)||(Y > N_ROWS)) {
		return;
	}

	switch (color) {
		case 'R': pixeldata[(3*((outtieY * (N_COLS)) + outtieX))] = data;
			break;
		case 'G': pixeldata[(3*((outtieY * (N_COLS)) + outtieX))+1] = data;
			break;
		case 'B': pixeldata[(3*((outtieY * (N_COLS)) + outtieX))+2] = data;
			break;
	}
}

void ShapeDrawer::clear() {
	initArray();
}

void ShapeDrawer::setOnePixel(int X, int Y, int data) {
	setPixel(X, Y, 'R', data);
	setPixel(X, Y, 'G', data);
	setPixel(X, Y, 'B', data);
}

void ShapeDrawer::drawTriangle(Triangle t) {
	
	int xa = t.getVertexOne().getX();
	int ya = t.getVertexOne().getY();
	int xb = t.getVertexTwo().getX();
	int yb = t.getVertexTwo().getY();
	int xc = t.getVertexThree().getX();
	int yc = t.getVertexThree().getY();
	float red1 = t.getVertexOneColor().getRed()/255.0;
	float green1 = t.getVertexOneColor().getGreen()/255.0;
	float blue1 = t.getVertexOneColor().getBlue()/255.0;
	float red2 = t.getVertexTwoColor().getRed()/255.0;
	float green2 = t.getVertexTwoColor().getGreen()/255.0;
	float blue2 = t.getVertexTwoColor().getBlue()/255.0;
	float red3 = t.getVertexThreeColor().getRed()/255.0;
	float green3 = t.getVertexThreeColor().getGreen()/255.0;
	float blue3 = t.getVertexThreeColor().getBlue()/255.0;
	
	float a01, a02, a03, b01, b02, b03, c01, c02, c03, beta1, beta2, beta3;
	float lowX = 0, lowY = 0, highX = 0, highY = 0, i, j, R, G, B, t1, t2, t3;
	float triangleArea = 0;

	a01 = ya - yb;
	a02 = ya - yc;
	a03 = yb - yc;
	b01 = xb - xa;
	b02 = xc - xa;
	b03 = xc - xb;
	c01 = -(a01*(xa+xb)+b01*(ya+yb))/2;
	c02 = -(a02*(xa+xc)+b02*(ya+yc))/2;
	c03 = -(a03*(xb+xc)+b03*(yb+yc))/2;

	if (evalFunc(xc,yc, a01, b01, c01) < 0) {
        a01 = a01 * (-1);
		b01 = b01 * (-1);
		c01 = c01 * (-1);
	}
	if (evalFunc(xb,yb, a02, b02, c02) < 0) {
        a02 = a02 * (-1);
		b02 = b02 * (-1);
		c02 = c02 * (-1);
	}
	if (evalFunc(xa,ya, a03, b03, c03) < 0) {
        a03 = a03 * (-1);
		b03 = b03 * (-1);
		c03 = c03 * (-1);
	}
	if (xa > xb) { highX = xa;  } else { highX = xb; }
	if (xc > highX) { highX = xc; }
	if (ya > yb) { highY = ya; } else { highY = yb; }
	if (yc > highY) { highY = yc; }
	if (xa < xb) { lowX = xa; } else { lowX = xb; }
	if (xc < lowX) { lowX = xc; }
	if (ya < yb) { lowY = ya; } else { lowY = yb; }
	if (yc < lowY) { lowY = yc; }

	triangleArea = triArea(xa, ya, xb, yb, xc, yc);

	for (i = lowX; i < (highX+1); i++) {
		for (j = lowY; j < (highY+1); j++) {
			t1 = evalFunc(i,j, a01, b01, c01);
			t2 = evalFunc(i,j, a02, b02, c02);
			t3 = evalFunc(i,j, a03, b03, c03);
			if ((t1 >= 0)&&(t2 >= 0)&&(t3 >= 0)){ 
				t1 = triArea(xb, yb, xc, yc, i, j);
				t2 = triArea(xc, yc, xa, ya, i, j);
				t3 = triArea(xa, ya, xb, yb, i, j);
				beta1 = triArea(xb, yb, xc, yc, i, j)/triangleArea;
				beta2 = triArea(xc, yc, xa, ya, i, j)/triangleArea;
				beta3 = triArea(xa, ya, xb, yb, i, j)/triangleArea;
				R = (beta1*red1 + beta2*red2 + beta3*red3) * 255;
				G = (beta1*green1 + beta2*green2 + beta3*green3) * 255;
				B = (beta1*blue1 + beta2*blue2 + beta3*blue3) * 255;
				setPixel(i,j,'R',R);
				setPixel(i,j,'G',G);
				setPixel(i,j,'B',B);
			}
		}
	}
}
int ShapeDrawer::evalFunc(int x, int y, int a, int b, int c) {
	int e = a*x+b*y+c;
	return(e);
}
float ShapeDrawer::triArea(int xa, int ya, int xb, int yb, int xref, int yref) {
	xa -= xref;
	xb -= xref;
	ya -= yref;
	yb -= yref;
	return(sqrt(pow((double)((yb*xa)-(xb*ya)),2.0))*0.5);
}



I need the other .cpp files and don't really know where to start. Thanks for ANY help!
Topic archived. No new replies allowed.