-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipleSnowflakes.cpp
197 lines (183 loc) · 4.61 KB
/
multipleSnowflakes.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
#include <windows.h>
#include "graphics2dpartb.h"
Canvas:: Canvas(int width, int height) {
char* argv[1];
char dummyString[8];
argv[0] = dummyString;
int argc = 1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutInitWindowPosition(200, 200);
setWindow(0, (float)width, 0, (float)height);
setViewport(0, width, 0, height);
CP.set(0.0f, 0.0f);
}
void Canvas :: setWindow(float l, float r, float b, float t) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D((GLdouble)l, (GLdouble)r, (GLdouble)b, (GLdouble)t);
if(t == b) return;
windowAspect = (r - 1)/(t - b);
}
void Canvas :: setViewport(int l, int r, int b, int t) {
glViewport((GLint)l, (GLint)b, (GLint)(r-l), (GLint)(t-b));
}
void Canvas :: lineTo(float x, float y) {
glBegin(GL_LINES);
glVertex2f((GLfloat)CP.x, (GLfloat)CP.y);
CP.x = x; CP.y = y;
glVertex2f((GLfloat)CP.x, (GLfloat)CP.y);
glEnd();
glFlush();
}
void Canvas :: forward(float dist, int vis) {
#define RadPerDeg 0.017453393
float x = CP.x + dist * cos(RadPerDeg * CD);
float y = CP.y + dist * sin(RadPerDeg * CD);
if(vis) lineTo(x, y);
else moveTo(x, y);
CP.x = x; CP.y = y;
}
void Canvas::ngon(int n,float cx, float cy, float radius) {
#define RadPerDeg 0.017453393
if(n < 3)
return;
double angle = 0, angleInc = 2 * 3.14159265 /n;
moveTo(cx + radius, cy);
for(int k = 1; k <= n; k++) {
angle += angleInc;
lineTo(radius * cos(angle) + cx, radius*sin(angle) + cy);
}
}
void Canvas::moveRel(float dx, float dy) {
CP.set(CP.x+dx, CP.y+dy);
}
void Canvas::lineRel(float dx, float dy) {
float x=CP.x+dx, y=CP.y+dy;
lineTo(x,y);
CP.set(x, y);
}
#define TWOPI 2*3.14159265
const int SCREENWIDTH = 680;
const int SCREENHEIGHT = 480;
Point2 CP1;
Canvas cvs(SCREENWIDTH,SCREENHEIGHT);
void snowflakemotiff(float cx,float cy, float size) {
cvs.moveTo(cx,cy);
cvs.turn(30);
cvs.forward(size*.5,1);
cvs.turn(40);
cvs.forward(size*.75,1);
cvs.turn(290);
cvs.forward(size*.25,1);
cvs.turn(260);
cvs.forward(size*.5,1);
cvs.turn(130);
cvs.forward(size*.6,1);
cvs.turn(270);
cvs.forward(size*.125,1);
}
void snowflake(float cx,float cy, float size) {
cvs.moveTo(cx,cy);
cvs.initCT();
cvs.pushCT();
snowflakemotiff(cx,cy,size);
cvs.translate2D (cx,cy);
cvs.scale2D(-1,1);
cvs.rotate2D(190);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.popCT();
cvs.pushCT();
cvs.translate2D (cx,cy);
cvs.rotate2D(60);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.translate2D (cx,cy);
cvs.scale2D(-1,1);
cvs.rotate2D(70);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.popCT();
cvs.pushCT();
cvs.translate2D (cx,cy);
cvs.rotate2D(120);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.translate2D (cx,cy);
cvs.scale2D(-1,1);
cvs.rotate2D(310);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.popCT();
cvs.pushCT();
cvs.translate2D (cx,cy);
cvs.rotate2D(180);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.translate2D (cx,cy);
cvs.scale2D(-1,1);
cvs.rotate2D(190);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.popCT();
cvs.pushCT();
cvs.translate2D (cx,cy);
cvs.rotate2D(240);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.translate2D (cx,cy);
cvs.scale2D(-1,1);
cvs.rotate2D(70);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.popCT();
cvs.pushCT();
cvs.translate2D (cx,cy);
cvs.rotate2D(300);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.translate2D (cx,cy);
cvs.scale2D(-1,1);
cvs.rotate2D(310);
cvs.translate2D (-cx,-cy);
snowflakemotiff(cx,cy,size);
cvs.popCT();
cvs.popCT();
}
void myInit(void) {
glClearColor(0,0,0,0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
cvs.setWindow(0.0, SCREENWIDTH, 0.0, SCREENHEIGHT);
cvs.setViewport(0,SCREENWIDTH,0,SCREENHEIGHT);
glColor3f(1,1,1);
snowflake(400,200,25);
glColor3f(.321431,.1246663,9123131);
snowflake(300,220,25);
glColor3f(.3,1,.3);
snowflake(650,420,50);
glColor3f(1,.2,1);
snowflake(400,110,40);
glColor3f(1,0,.2);
snowflake(240,300,70);
glColor3f(.4,.4,.4);
snowflake(50,50,50);
glColor3f(0,0,1);
snowflake(350,250,150);
glColor3f(1,1,1);
snowflake(240,240,240);
glFlush();
}
void main(int argc, char** argv) {
glutCreateWindow("Multiple Snowflakes");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}