-
Notifications
You must be signed in to change notification settings - Fork 0
/
BandeiraDoBrasil.c
127 lines (117 loc) · 3.55 KB
/
BandeiraDoBrasil.c
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
#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>
#define MAXIMUM 1.80
#define MINIMUM -1.80
#define MIDDLE 0.0
#define MAXIMUM_RADIUS 0.90
#define MINIMUM_RADIUS -0.90
#define TOTAL_SEGMENTS 360.0
void toLimitValue(GLfloat *value) {
if (*value > MAXIMUM) {
*value = MAXIMUM;
} else if (*value < MINIMUM) {
*value = MINIMUM;
}
}
void toLimitRadius(GLfloat *radius) {
if (*radius > MAXIMUM_RADIUS) {
*radius = MAXIMUM_RADIUS;
} else if(*radius < MINIMUM_RADIUS) {
*radius = MINIMUM_RADIUS;
}
}
void calculateCurrentPointsXYUsingAngle(GLfloat *x, GLfloat *y, GLfloat angle) {
GLfloat height = GLfloat(glutGet(GLUT_WINDOW_HEIGHT));
GLfloat width = GLfloat(glutGet(GLUT_WINDOW_WIDTH));
GLfloat windowProportion;
if (height == width) {
*x *= cosf(angle);
*y *= sinf(angle);
} else if(height < width) {
windowProportion = height / width;
*x *= windowProportion * cosf(angle);
*y *= sinf(angle);
} else {
windowProportion = width / height;
*x *= cosf(angle);
*y *= windowProportion * sinf(angle);
}
}
void calculateWidthAndHeightProportion(GLfloat *widthProportion, GLfloat *heightProportion) {
GLfloat height = GLfloat(glutGet(GLUT_WINDOW_HEIGHT));
GLfloat width = GLfloat(glutGet(GLUT_WINDOW_WIDTH));
*widthProportion = *heightProportion = 1;
if(height < width) {
*widthProportion = height / width;
} else if(width < height) {
*heightProportion = width / height;
}
}
void drawRectangle(GLfloat width, GLfloat height) {
glColor3f( 0.0, 0.8, 0.0 );
toLimitValue(&width);
toLimitValue(&height);
GLfloat upperY = height/2, bottomY = -height/2;
GLfloat rightX = width/2, leftX = -width/2;
glBegin(GL_QUADS);
glVertex2f(leftX, bottomY);
glVertex2f(leftX, upperY);
glVertex2f(rightX, upperY);
glVertex2f(rightX, bottomY);
glEnd();
glFinish();
}
void drawRhombus(GLfloat width, GLfloat height) {
glColor3f( 1.0, 1.0, 0.0 );
toLimitValue(&width);
toLimitValue(&height);
GLfloat upperY = height/2, bottomY = -height/2;
GLfloat rightX = width/2, leftX = -width/2;
upperY = upperY/1.2;
rightX = rightX/1.2;
bottomY = bottomY/1.2;
leftX = leftX/1.2;
glBegin(GL_QUADS);
glVertex2f(leftX, MIDDLE);
glVertex2f(MIDDLE, upperY);
glVertex2f(rightX, MIDDLE);
glVertex2f(MIDDLE, bottomY);
glEnd();
glFinish();
}
void drawCircle(GLfloat radius) {
glColor3f( 0.0, 0.0, 0.8 );
toLimitRadius(&radius);
glBegin(GL_POLYGON);
for(GLint currentSegment = 0; currentSegment < TOTAL_SEGMENTS; currentSegment++) {
GLfloat currentAngle = 2.0 * M_PI * GLfloat(currentSegment) / TOTAL_SEGMENTS;
GLfloat x = radius/2, y = radius/2;
calculateCurrentPointsXYUsingAngle(&x, &y, currentAngle);
glVertex2f(x, y);
}
glEnd();
glFinish();
}
void displayMe(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
drawRectangle(100.0, 100.0);
drawRhombus(5.0, 5.0);
drawCircle(5.0);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(400, 300);
glutInitWindowPosition(100, 100);
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glutCreateWindow("Hello world!");
glutDisplayFunc(displayMe);
glutMainLoop();
return 0;
}