-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAnswer8.cpp
176 lines (142 loc) · 3.25 KB
/
Answer8.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
#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX_CPTS 25
#define PI 3.14
GLfloat cpts[MAX_CPTS][3];
int ncpts = 0;
static int width = 500, height = 500;
float l, b;
int maxWidth, maxHeight;
int count = 0;
float scale = 1;
void addPoint(float l, float b) {
cpts[ncpts][0] = (l * scale)/maxHeight;
cpts[ncpts][1] = (b*scale)/maxWidth;
cpts[ncpts][2] = 0.0;
ncpts++;
}
void addIntitialPoints() {
addPoint(0, 0);
addPoint(b + b/2, l/4);
addPoint(b + b/2, 3*(l/4));
addPoint(0, l);
}
void drawCurves()
{
int i;
for(i=0; i<ncpts-3; i +=3)
{
/* draw the curve using OpenGL evaluators */
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, cpts[i]);
glMapGrid1f(30, 0.0, 1.0);
glEvalMesh1(GL_LINE, 0, 30);
}
glFlush();
}
void drawPetal() {
glPushMatrix();
glScalef(-1, 1, 1);
drawCurves();
glPopMatrix();
drawCurves();
}
float getRadians(float degree) {
return (degree * PI)/180;
}
void drawPetalAtAngele(float angle) {
//float angle = getRadians(degree);
glPushMatrix();
glRotatef(angle, 0, 0, 1);
drawPetal();
glPopMatrix();
}
void drawFlower(int n) {
int angle = 0;
int delta_angle = (360.0)/n;
for(int i = 0; i < n; i++) {
drawPetalAtAngele(angle + i * delta_angle);
}
}
void display()
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
for (i = 0; i < ncpts; i++)
glVertex3fv(cpts[i]);
glEnd();
drawFlower(6);
glFlush();
}
void reshape(int w, int h)
{
width = w;
height = h;
maxWidth = w > h ? w : h;
maxHeight = maxWidth;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, w, h);
ncpts = 0;
addIntitialPoints();
glutPostRedisplay();
}
void addReflection() {
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'q': case 'Q':
exit(0);
break;
case 'c': case 'C':
ncpts = 0;
glutPostRedisplay();
break;
case 'r': case 'R':
addIntitialPoints();
addReflection();
glutPostRedisplay();
break;
case 'd': case 'D':
drawCurves();
break;
case 'i': case 'I':
scale = scale * 2;
ncpts = 0;
addIntitialPoints();
addReflection();
glutPostRedisplay();
drawCurves();
break;
case 'o': case 'O':
scale = scale / 2;
ncpts = 0;
addIntitialPoints();
addReflection();
glutPostRedisplay();
drawCurves();
break;
}
}
int main(int argc, char **argv)
{
printf("Enter the value of L and B");
scanf("%f %f", &l, &b);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("curves");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(5.0);
glEnable(GL_MAP1_VERTEX_3);
glutMainLoop();
}