-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.cc
168 lines (154 loc) · 3.55 KB
/
draw.cc
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
/* draw.cc */
ipoint point_to_ipoint(point P){ // project 3d point to 2d ipoint
ipoint p;
p.x = (int) ((double) (SCREEN/2) + P[0]*200.0); // perspective: P[0]*(200.0+P[2]*50.0)
p.y = (int) ((double) (SCREEN/2) + P[1]*200.0); // perspective: P[1]*(200.0+P[2]*50.0)
return(p);
};
void draw_edge(point P, point Q){
ipoint p,q;
p=point_to_ipoint(P);
q=point_to_ipoint(Q);
draw_line(p,q,0);
};
void draw_thin_edge(point P, point Q){
ipoint p,q;
p=point_to_ipoint(P);
q=point_to_ipoint(Q);
draw_faint_line(p,q,0);
};
void draw_triangle(point P, point Q, point R, point incolor, point outcolor){
ipoint p,q,r;
point N;
int RR,G,B;
long col;
N=(Q-P)*(R-P);
N=N/norm(N);
if(N[2]>0){ // use outcolor; gives numbers between 50 and 200 for each of R G B
RR=(int) 50.0+(150.0*N[2]*outcolor[0]);
G=(int) 50.0+(150.0*N[2]*outcolor[1]);
B=(int) 50.0+(150.0*N[2]*outcolor[2]);
} else { // use incolor; gives numbers between 50 and 200 for each of R G B
RR=(int) 50.0-(150.0*N[2]*incolor[0]);
G=(int) 50.0-(150.0*N[2]*incolor[1]);
B=(int) 50.0-(150.0*N[2]*incolor[2]);
};
col = (long) ((RR*65536)+(G*256)+B);
p=point_to_ipoint(P);
q=point_to_ipoint(Q);
r=point_to_ipoint(R);
draw_triangle(p,q,r,col);
};
void draw_stripy_triangle(point P, point Q, point R, double iheight){
ipoint p,q,r;
point N,M;
int x,z,i;
double I,s,t;
p=point_to_ipoint(P);
q=point_to_ipoint(Q);
r=point_to_ipoint(R);
draw_triangle(p,q,r,16777215); // draw white triangle
// first, sort points
if(P[2]<Q[2]){
N=P;
P=Q;
Q=N;
};
if(P[2]<R[2]){
N=P;
P=R;
R=N;
};
if(Q[2]<R[2]){
N=Q;
Q=R;
R=N;
};
// now P[2]>=Q[2]>=R[2]
assert(P[2]>=Q[2]);
assert(Q[2]>=R[2]);
if(P[2]>=0.0){
x=(int) (P[2]/iheight);
} else {
x=(int) (P[2]/iheight)-1;
};
if(R[2]>=0.0){
z=(int) (R[2]/iheight);
} else {
z=(int) (R[2]/iheight)-1;
};
for(i=z+1;i<=x;i++){
I= ((double) i)*iheight;
assert(R[2]<=I);
assert(I<=P[2]);
if(I<=Q[2]){
s=(I-R[2])/(P[2]-R[2]); // proportion of the way from R to P
t=(I-R[2])/(Q[2]-R[2]); // proportion of the way from R to Q
N=((1.0-s)*R)+(s*P);
M=((1.0-t)*R)+(t*Q);
draw_edge(N,M);
} else {
s=(I-R[2])/(P[2]-R[2]); // proportion of the way from R to P
t=(I-Q[2])/(P[2]-Q[2]); // proportion of the way from Q to P
N=((1.0-s)*R)+(s*P);
M=((1.0-t)*Q)+(t*P);
draw_edge(N,M);
};
};
};
void draw_stripy_eps_triangle(point P, point Q, point R, double iheight, ofstream &output_file){
ipoint p,q,r;
point N,M;
int x,z,i;
double I,s,t;
p=point_to_ipoint(P);
q=point_to_ipoint(Q);
r=point_to_ipoint(R);
// first, sort points
if(P[2]<Q[2]){
N=P;
P=Q;
Q=N;
};
if(P[2]<R[2]){
N=P;
P=R;
R=N;
};
if(Q[2]<R[2]){
N=Q;
Q=R;
R=N;
};
// now P[2]>=Q[2]>=R[2]
assert(P[2]>=Q[2]);
assert(Q[2]>=R[2]);
if(P[2]>=0.0){
x=(int) (P[2]/iheight);
} else {
x=(int) (P[2]/iheight)-1;
};
if(R[2]>=0.0){
z=(int) (R[2]/iheight);
} else {
z=(int) (R[2]/iheight)-1;
};
for(i=z+1;i<=x;i++){
I= ((double) i)*iheight;
assert(R[2]<=I);
assert(I<=P[2]);
if(I<=Q[2]){
s=(I-R[2])/(P[2]-R[2]); // proportion of the way from R to P
t=(I-R[2])/(Q[2]-R[2]); // proportion of the way from R to Q
N=((1.0-s)*R)+(s*P);
M=((1.0-t)*R)+(t*Q);
output_file << N[0] << " " << -N[1] << " " << M[0] << " " << -M[1] << " l\n";
} else {
s=(I-R[2])/(P[2]-R[2]); // proportion of the way from R to P
t=(I-Q[2])/(P[2]-Q[2]); // proportion of the way from Q to P
N=((1.0-s)*R)+(s*P);
M=((1.0-t)*Q)+(t*P);
output_file << N[0] << " " << -N[1] << " " << M[0] << " " << -M[1] << " l\n";
};
};
};