-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRender.java
More file actions
163 lines (131 loc) · 4.14 KB
/
Copy pathRender.java
File metadata and controls
163 lines (131 loc) · 4.14 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
/* @author Sidney Durant
*
* This class can rasterize basic geometric primitives onto a 1D array of size
* WIDTH*HEIGHT
* implemented: points, lines, and triangles with vertex colors defined
*/
public class Render {
private static int WIDTH, HEIGHT;
private static int[] raster;
public Render(int width, int height, int[] raster) {
WIDTH = width;
HEIGHT = height;
this.raster = raster;
}
// clears all of the pixels to be transparent black
public void clear() {
for (int i = 0; i < raster.length; i++){
raster[i] = 0xffffffff;
}
}
// draws a point if its inside the frame
public void drawColoredPoint(int x, int y, Color c) {
if (x < WIDTH && x >= 0 && y < HEIGHT && y >= 0) {
raster[x + WIDTH * y] = c.getColor();
}
}
// attempts to draw a point whether or not it's inside the frame. Will
// throw arrayIndexOutOfBoundsException, drawPoint() is a safe alternative
public void drawUncheckedColoredPoint(int x, int y, Color c) {
raster[x + WIDTH * y] = c.getColor();
}
// rasterizes a colored line segment from (x0, y0) to (x1, y1) with a smooth
// gradient from (c0, c1)
public void drawColoredLine(int x0, int y0, Color c0, int x1, int y1, Color c1){
// if the entire line is on the screen, drawUnchecked point
if (x0 < WIDTH && x0 >= 0 && y0 < HEIGHT && y0 >= 0 && x1 < WIDTH
&& x1 >= 0 && y1 < HEIGHT && y1 >= 0) {
float dx = x1 - x0;
float dy = y1 - y0;
Color dc = c1.minus(c0);
// compare dx and dy to see if slope is steep ( slope > .5 )
if ( ( dy > 0 ? dy : -dy) > (dx > 0 ? dx : -dx ) ){ // slope steep
float xChangePerStep = dx/dy;
Color cChangePerStep = dc.times(1/dy);
if( y0 < y1 ){ // iterate from y0 to y1
float x = x0;
Color c = new Color(c0);
for( int y = y0; y <= y1; y++ ){
drawUncheckedColoredPoint( (int) x, y, c );
x += xChangePerStep;
c.add(cChangePerStep);
}
}else{ // iterate from y1 to y0
float x = x1;
Color c = new Color(c1);
for( int y = y1; y <= y0; y++ ){
drawUncheckedColoredPoint( (int) x, y, c );
x += xChangePerStep;
c.add(cChangePerStep);
}
}
} else { // not steep
float yChangePerStep = dy/dx;
Color cChangePerStep = dc.times(1/dx);
if( x0 < x1){ // iterate from x0 to x1
float y = y0;
Color c = new Color(c0);
for ( int x = x0; x <= x1; x++ ){
drawUncheckedColoredPoint( x, (int)y, c );
y += yChangePerStep;
c.add(cChangePerStep);
}
}else{ // iterate from x1 to x0
float y = y1;
Color c = new Color(c1);
for ( int x = x1; x <= x0; x++ ){
drawUncheckedColoredPoint( x, (int)y, c );
y += yChangePerStep;
c.add(cChangePerStep);
}
}
}
} else { // line segment goes off screen, call drawPoint()
float dx = x1 - x0;
float dy = y1 - y0;
Color dc = c1.minus(c0);
// compare dx and dy to see if slope is steep ( slope > .5 )
if ( ( dy > 0 ? dy : -dy) > (dx > 0 ? dx : -dx ) ){ // slope steep
float xChangePerStep = dx/dy;
Color cChangePerStep = dc.times(1/dy);
if( y0 < y1 ){ // iterate from y0 to y1
float x = x0;
Color c = new Color(c0);
for( int y = y0; y <= y1; y++ ){
drawColoredPoint( (int) x, y, c );
x += xChangePerStep;
c.add(cChangePerStep);
}
}else{ // iterate from y1 to y0
float x = x1;
Color c = new Color(c1);
for( int y = y1; y <= y0; y++ ){
drawColoredPoint( (int) x, y, c );
x += xChangePerStep;
c.add(cChangePerStep);
}
}
} else { // not steep
float yChangePerStep = dy/dx;
Color cChangePerStep = dc.times(1/dx);
if( x0 < x1){ // iterate from x0 to x1
float y = y0;
Color c = new Color(c0);
for ( int x = x0; x <= x1; x++ ){
drawColoredPoint( x, (int)y, c );
y += yChangePerStep;
c.add(cChangePerStep);
}
}else{ // iterate from x1 to x0
float y = y1;
Color c = new Color(c1);
for ( int x = x1; x <= x0; x++ ){
drawColoredPoint( x, (int)y, c );
y += yChangePerStep;
c.add(cChangePerStep);
}
}
}
}
}
}