-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.cpp
187 lines (160 loc) · 4.69 KB
/
4.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
/**
* Rectangle.cpp
*
* EECS 183, Winter 2016
* Project 4: CoolPics
*
* Ezra Lesser, Michelle Gurevich
* elesser, mgur
*
* Represents a rectangle.
*/
#include "Rectangle.h"
#include "Line.h"
#include "Triangle.h"
#include "Graphics.h"
#include <algorithm>
using namespace std;
Rectangle::Rectangle() {
return;
}
Rectangle::Rectangle(Point pt1, Point pt2, Color color) {
start = pt1;
end = pt2;
colorTopLeft = color;
colorBottomLeft = color;
colorTopRight = color;
colorBottomRight = color;
return;
}
Rectangle::Rectangle(Point pt1, Point pt2, Color cTopLeft, Color cTopRight,
Color cBottomRight, Color cBottomLeft) {
start = pt1;
end = pt2;
// sets four colors
colorTopLeft = cTopLeft;
colorTopRight = cTopRight;
colorBottomRight = cBottomRight;
colorBottomLeft = cBottomLeft;
return;
}
void Rectangle::setStart(Point pt) {
start = pt;
return;
}
Point Rectangle::getStart() {
return start;
}
void Rectangle::setEnd(Point pt) {
end = pt;
return;
}
Point Rectangle::getEnd() {
return end;
}
void Rectangle::setColor(Color color) {
colorTopLeft = color;
colorTopRight = color;
colorBottomRight = color;
colorBottomLeft = color;
return;
}
void Rectangle::setColorTopLeft(Color color) {
colorTopLeft = color;
return;
}
Color Rectangle::getColorTopLeft() {
return colorTopLeft;
}
void Rectangle::setColorTopRight(Color color) {
colorTopRight = color;
return;
}
Color Rectangle::getColorTopRight() {
return colorTopRight;
}
void Rectangle::setColorBottomRight(Color color) {
colorBottomRight = color;
return;
}
Color Rectangle::getColorBottomRight() {
return colorBottomRight;
}
void Rectangle::setColorBottomLeft(Color c) {
colorBottomLeft = c;
return;
}
Color Rectangle::getColorBottomLeft() {
return colorBottomLeft;
}
void Rectangle::read(istream& ins) {
// reads in start, end, and first color
ins >> start >> end >> colorTopLeft;
// reads in second color
ins >> colorTopRight;
/*
* reads in colors using two forms: 1) If second color read in fails, fail
* is cleared, and colors read manually 2) If second color read in works,
* next two colors are read in
*/
if (ins.fail()) {
ins.clear();
colorTopRight = colorTopLeft;
colorBottomRight = colorTopLeft;
colorBottomLeft = colorTopLeft;
}
else {
ins >> colorBottomRight >> colorBottomLeft;
}
return;
}
void Rectangle::write(ostream& outs) {
outs << start << ' ' << end << ' ' << colorTopLeft << ' ' << colorTopRight
<< ' ' << colorBottomRight << ' ' << colorBottomLeft;
return;
}
// Your code goes above this line.
// Don't change the implementations below!
istream& operator >> (istream& ins, Rectangle& rectangle)
{
rectangle.read(ins);
return ins;
}
ostream& operator << (ostream& outs, Rectangle rectangle)
{
rectangle.write(outs);
return outs;
}
void Rectangle::draw(Graphics & drawer)
{
// four points of rectangles
Point topLeft = start;
Point topRight(end.getX(), start.getY());
Point bottomRight = end;
Point bottomLeft(start.getX(), end.getY());
// center point and color by averaging
Point center((start.getX() + end.getX()) / 2,
(start.getY() + end.getY()) / 2);
Color colorCenter((colorTopLeft.getRed() + colorTopRight.getRed() +
colorBottomRight.getRed() +
colorBottomLeft.getRed()) / 4,
(colorTopLeft.getGreen() + colorTopRight.getGreen() +
colorBottomRight.getGreen() +
colorBottomLeft.getGreen()) / 4,
(colorTopLeft.getBlue() + colorTopRight.getBlue() +
colorBottomRight.getBlue() +
colorBottomLeft.getBlue()) / 4);
// four triangles to represent rectangle
Triangle top(topLeft, colorTopLeft, topRight, colorTopRight,
center, colorCenter);
Triangle bottom(bottomLeft, colorBottomLeft, bottomRight, colorBottomRight,
center, colorCenter);
Triangle left(topLeft, colorTopLeft, bottomLeft, colorBottomLeft,
center, colorCenter);
Triangle right(topRight, colorTopRight, bottomRight, colorBottomRight,
center, colorCenter);
top.draw(drawer);
bottom.draw(drawer);
left.draw(drawer);
right.draw(drawer);
}