-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolygon.h
More file actions
170 lines (146 loc) · 4 KB
/
polygon.h
File metadata and controls
170 lines (146 loc) · 4 KB
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
#ifndef POLYGON_H_
#define POLYGON_H_
#include <vector>
#include <string>
#include <iostream>
#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "edge.h"
#include "color.h"
#include "point.h"
#include "config.h"
#include "edge_table.h"
#include "clipping_window.h"
using namespace std;
class Polygon {
public:
Polygon() {
y_lowest = HEIGHT;
y_highest = 0;
is_constructed = false;
}
int size() {
return points.size();
}
void add_point( int x, int y ) {
if ( points.size() >= (MAX_POINTS - 1) ) {
cerr << "This polygon already has nine points, add a final "
<< "tenth point by right-clicking the screen." << endl;
return;
}
Point p( x, y );
verify_y_bounds( p );
points.push_back( p );
}
void add_final_point( int x, int y ) {
// add final point to poly
Point p( x, y );
verify_y_bounds( p );
points.push_back( p );
// construct edges
vector<Edge> edges;
for ( int i = 0; i < points.size() - 1; ++i ) {
Edge e( points[i], points[i + 1] );
edges.push_back( e );
}
Edge e( points[0], points[points.size() - 1] );
edges.push_back( e );
// create edge table
EdgeTable et( edges );
edge_table = et;
is_constructed = true;
}
void draw_verticies( float (&framebuffer)[HEIGHT][WIDTH][3] ) {
if(!is_constructed){
for ( auto &point : points ) {
color.draw( point.get_x()-1, point.get_y()-1, framebuffer);
color.draw( point.get_x()-1, point.get_y(), framebuffer);
color.draw( point.get_x(), point.get_y()-1, framebuffer);
color.draw( point.get_x()+1, point.get_y()+1, framebuffer);
color.draw( point.get_x()+1, point.get_y(), framebuffer);
color.draw( point.get_x(), point.get_y()+1, framebuffer);
color.draw( point.get_x()-1, point.get_y()+1, framebuffer);
color.draw( point.get_x()+1, point.get_y()-1, framebuffer);
color.draw( point, framebuffer );
}
}
}
void clear_active_edge_list() {
active_edge_list.clear();
}
void draw_fill( int y, float (&framebuffer)[HEIGHT][WIDTH][3],
ClippingWindow &cw ) {
// check for any active edge additions
edge_table.push_active_edges( y, active_edge_list );
// clear any edges no longer within the y
for ( int i = active_edge_list.size() - 1; i >= 0; --i ) {
if ( active_edge_list[i].get_max_y() <= y ) {
active_edge_list.erase( active_edge_list.begin() + i );
}
}
for ( int i = 0; i < active_edge_list.size(); i += 2 ) {
int x_1 = active_edge_list[i].get_x_increment( y );
int x_2 = active_edge_list[i + 1].get_x_increment( y );
int max_x = (x_1 >= x_2) ? x_1 : x_2;
int min_x = (x_1 <= x_2) ? x_1 : x_2;
if ( max_x > WIDTH || max_x < 0 || min_x > WIDTH || min_x < 0 ) {
if ( IS_DEBUG ) {
cerr << "Out of window drawing bounds [x-min/x-max]("
<< min_x << ", " << max_x << ")" << endl;
}
continue;
}
// nothing to draw (left side of clip)
if ( min_x < cw.get_min_x() && max_x < cw.get_min_x() ) {
continue;
}
// nothing to draw (right side of clip)
if ( min_x > cw.get_max_x() && max_x > cw.get_max_x() ) {
continue;
}
// trim left side of line (if needed)
if ( min_x < cw.get_min_x()) {
min_x = cw.get_min_x();
}
// trim right side of line (if needed)
if ( max_x > cw.get_max_x()) {
max_x = cw.get_max_x();
}
for ( int x = min_x; x < max_x; ++x ) {
if ( cw.get_min_y() <= y && y <= cw.get_max_y() ) {
color.draw( x, y, framebuffer );
}
}
}
cw.draw( framebuffer );
}
friend ostream& operator<<( ostream& os, const Polygon& poly ) {
for ( int i = 0; i < poly.points.size(); ++i ) {
if ( i < poly.points.size() - 1 ) {
os << poly.points[i] << ", ";
} else {
os << poly.points[i];
}
}
return os;
}
private:
Color color;
vector<Point> points;
EdgeTable edge_table;
vector<Edge> active_edge_list;
int y_lowest, y_highest;
bool is_constructed;
void verify_y_bounds( Point p ) {
if ( p.get_y() < y_lowest ) {
y_lowest = p.get_y();
}
if ( p.get_y() > y_highest ) {
y_highest = p.get_y();
}
}
};
#endif