-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangle.cpp
235 lines (203 loc) · 6.45 KB
/
Triangle.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
/**
* Triangle.cpp
* Project UID 8b3bcc444eb500121e420f7e2e359014
*
* EECS 183, Fall 2019
* Project 4: CoolPics
*
* <Tin Long Rex Fung, Isaac Lok-Tin Li>
* <rexfung, isaliac>
*
* <This file contains the class "Triangle">
*/
#include "Triangle.h"
#include "Graphics.h"
#include "Shape.h"
#include <cmath>
using namespace std;
//default constructor
Triangle::Triangle() {}
//constructor for triangle with one color
Triangle::Triangle(Point pt1, Point pt2, Point pt3, Color color) {
vertexOne = pt1;
vertexTwo = pt2;
vertexThree = pt3;
vertexOneColor = color;
vertexTwoColor = color;
vertexThreeColor = color;
}
//construtor for triangle with 3 colors
Triangle::Triangle(Point pt1, Color color1,
Point pt2, Color color2,
Point pt3, Color color3) {
vertexOne = pt1;
vertexTwo = pt2;
vertexThree = pt3;
vertexOneColor = color1;
vertexTwoColor = color2;
vertexThreeColor = color3;
}
//one color for the whole triangle
void Triangle::setColor(Color color) {
vertexOneColor = color;
vertexTwoColor = color;
vertexThreeColor = color;
}
void Triangle::setVertexOne(Point pt) {
vertexOne = pt;
}
Point Triangle::getVertexOne() {
return vertexOne;
}
void Triangle::setVertexOneColor(Color color) {
vertexOneColor = color;
}
Color Triangle::getVertexOneColor() {
return vertexOneColor;
}
void Triangle::setVertexTwo(Point pt) {
vertexTwo = pt;
}
Point Triangle::getVertexTwo() {
return vertexTwo;
}
void Triangle::setVertexTwoColor(Color color) {
vertexTwoColor = color;
}
Color Triangle::getVertexTwoColor() {
return vertexTwoColor;
}
void Triangle::setVertexThree(Point pt) {
vertexThree = pt;
}
Point Triangle::getVertexThree() {
return vertexThree;
}
void Triangle::setVertexThreeColor(Color color) {
vertexThreeColor = color;
}
Color Triangle::getVertexThreeColor() {
return vertexThreeColor;
}
void Triangle::read(istream& ins) {
ins >> vertexOne;
ins >> vertexOneColor;
//checks if triangle has one color or three colors
if (ins.fail() == true) {
ins.clear();
ins >> vertexTwo >> vertexThree >> vertexOneColor;
vertexTwoColor = vertexOneColor;
vertexThreeColor = vertexOneColor;
}
else {
ins >> vertexTwo >> vertexTwoColor >> vertexThree >> vertexThreeColor;
}
}
void Triangle::write(ostream& outs) {
outs << vertexOne << " " << vertexOneColor << " " << vertexTwo << " "
<< vertexTwoColor << " " << vertexThree << " " << vertexThreeColor;
}
// Your code goes above this line.
// Don't change the implementations below!
istream& operator >> (istream& ins, Triangle& tri)
{
tri.read(ins);
return ins;
}
ostream& operator << (ostream& outs, Triangle tri)
{
tri.write(outs);
return outs;
}
void Triangle::draw (Graphics& drawer) {
int xa = checkRange(getVertexOne().getX());
int ya = checkRange(getVertexOne().getY());
int xb = checkRange(getVertexTwo().getX());
int yb = checkRange(getVertexTwo().getY());
int xc = checkRange(getVertexThree().getX());
int yc = checkRange(getVertexThree().getY());
float red1 = (float)(getVertexOneColor().getRed()/255.0);
float green1 = (float)(getVertexOneColor().getGreen()/255.0);
float blue1 = (float)(getVertexOneColor().getBlue()/255.0);
float red2 = (float)(getVertexTwoColor().getRed()/255.0);
float green2 = (float)(getVertexTwoColor().getGreen()/255.0);
float blue2 = (float)(getVertexTwoColor().getBlue()/255.0);
float red3 = (float)(getVertexThreeColor().getRed()/255.0);
float green3 = (float)(getVertexThreeColor().getGreen()/255.0);
float blue3 = (float)(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 = (float)(ya - yb);
a02 = (float)(ya - yc);
a03 = (float)(yb - yc);
b01 = (float)(xb - xa);
b02 = (float)(xc - xa);
b03 = (float)(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, (int)a01, (int)b01, (int)c01) < 0) {
a01 = a01 * (-1);
b01 = b01 * (-1);
c01 = c01 * (-1);
}
if (evalFunc(xb,yb, (int)a02, (int)b02, (int)c02) < 0) {
a02 = a02 * (-1);
b02 = b02 * (-1);
c02 = c02 * (-1);
}
if (evalFunc(xa,ya, (int)a03, (int)b03, (int)c03) < 0) {
a03 = a03 * (-1);
b03 = b03 * (-1);
c03 = c03 * (-1);
}
if (xa > xb) { highX = (float)xa; } else { highX = (float)xb; }
if (xc > highX) { highX = (float)xc; }
if (ya > yb) { highY = (float)ya; } else { highY = (float)yb; }
if (yc > highY) { highY = (float)yc; }
if (xa < xb) { lowX = (float)xa; } else { lowX = (float)xb; }
if (xc < lowX) { lowX = (float)xc; }
if (ya < yb) { lowY = (float)ya; } else { lowY = (float)yb; }
if (yc < lowY) { lowY = (float)yc; }
triangleArea = triArea(xa, ya, xb, yb, xc, yc);
for (i = lowX; i < (highX+1); i++) {
for (j = lowY; j < (highY+1); j++) {
t1 = (float)evalFunc((int)i,(int)j, (int)a01, (int)b01, (int)c01);
t2 = (float)evalFunc((int)i,(int)j, (int)a02, (int)b02, (int)c02);
t3 = (float)evalFunc((int)i,(int)j, (int)a03, (int)b03, (int)c03);
if ((t1 >= 0)&&(t2 >= 0)&&(t3 >= 0)){
t1 = triArea(xb, yb, xc, yc, (int)i, (int)j);
t2 = triArea(xc, yc, xa, ya, (int)i, (int)j);
t3 = triArea(xa, ya, xb, yb, (int)i, (int)j);
beta1 = triArea(xb, yb, xc, yc, (int)i, (int)j)/triangleArea;
beta2 = triArea(xc, yc, xa, ya, (int)i, (int)j)/triangleArea;
beta3 = triArea(xa, ya, xb, yb, (int)i, (int)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;
drawer.setPixel((int)i,(int)j,Color((int)R,(int)G,int(B)));
}
}
}
}
int Triangle::checkRange(int val)
{
if (val < 0)
return 0;
else if (val > DIMENSION)
return DIMENSION;
else
return val;
}
int Triangle::evalFunc(int x, int y, int a, int b, int c) {
int e = a*x+b*y+c;
return(e);
}
float Triangle::triArea(int xa, int ya, int xb, int yb, int xref, int yref) {
xa -= xref;
xb -= xref;
ya -= yref;
yb -= yref;
return((float)(sqrt(pow((double)(yb*xa)-(xb*ya), (double)2))*0.5));
}