/
parabola.cpp
205 lines (171 loc) · 4.29 KB
/
parabola.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
198
199
200
201
202
203
204
205
/*
This will draw a parabola using bresenham
adjusting the vertex and a point to pass
*/
#include <iostream>
#include <valarray>
#include <GL/glut.h>
using namespace std;
int w,h;
int pointsize=10;
float
white[] = {1,1,1},
black[] = {0,0,0},
blue[] = {0,0,1},
green[] = {0,1,0},
red[] = {1,0,0};
float *fondo=white;
void initialize();
template<class T>
T square(T x){
return x*x;
}
template<class T>
T norm1(const std::valarray<T> &v){
return v.max();
}
namespace bresenham{
typedef std::valarray<int> point2d;
void parabola(const point2d &end);
}
namespace recursive{
typedef std::valarray<double> point2d;
void parabola(double a, const point2d &begin, const point2d &end);
}
int main(int argc, char **argv){
glutInit(&argc,argv);// inicializa glut
initialize(); // condiciones iniciales de la ventana y OpenGL
glutMainLoop(); // entra en loop de reconocimiento de eventos
return 0;
}
void Keyboard_cb(unsigned char key,int x=0,int y=0);
void Mouse_cb(int button, int state, int x, int y);
void Motion_cb(int x, int y);
void Reshape_cb(int width, int height);
void Display_cb();
void initialize() {
// pide color RGB y double buffering
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(w,h);
glutCreateWindow("Parabola Bresenham"); // crea el main window
//declara los callbacks
//los que no se usan no se declaran
glutDisplayFunc(Display_cb); // redisplays
glutReshapeFunc(Reshape_cb);
glutKeyboardFunc(Keyboard_cb);
//glutMouseFunc(Mouse_cb);
// ========================
// estado normal del OpenGL
// ========================
glClearColor(fondo[0],fondo[1],fondo[2],1); // color de fondo
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // constante
}
void Keyboard_cb(unsigned char key,int x,int y) {
if (tolower(key)=='q')
exit(EXIT_SUCCESS);
}
// Clicks del mouse
void Mouse_cb(int button, int state, int x, int y){
}
// Movimientos del mouse
void Motion_cb(int x, int y){
}
void Reshape_cb(int width, int height){
if (!width||!height) return; // minimizado ==> nada
w=width; h=height;
glViewport(0,0,w,h); // región donde se dibuja (toda la ventana)
// rehace la matriz de proyección (la porcion de espacio visible)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w/pointsize,0,h/pointsize,-1,1);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void Display_cb(){
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(pointsize);
glColor3fv(green);
glBegin(GL_POINTS);{
std::valarray<int> end(2);
end[0] = w/pointsize; end[1] = h/pointsize;
bresenham::parabola(end);
}glEnd();
glPointSize(1);
glColor3fv(black);
glBegin(GL_POINTS);{
std::valarray<double> begin(2),end(2);
end[0] = w/pointsize; end[1] = h/pointsize;
recursive::parabola(end[1]/square(end[0]),begin,end);
}glEnd();
glutSwapBuffers();
}
namespace bresenham{
void travel_x(const point2d &end){
int
dx2 = square(end[0]),
dy = end[1],
discriminant = -2*dy + dx2,
south = -6*dy,
north = -6*dy + 2*dx2,
increment = -4*dy;
for(size_t x=0, y=0; x<=end[0]; ++x){
glVertex2i(x,y);
if(discriminant>0){ //south (same y)
discriminant += south;
}
else{
discriminant += north;
++y;
}
south += increment;
north += increment;
}
}
void travel_y(const point2d &begin, const point2d &end){
int
dx2 = square(end[0]),
dy = end[1],
discriminant = 4*dx2*(begin[1]+1) - dy*square(2*begin[0]+1),
east = 4*dx2 - 8*dy*(begin[0]+1),
west = 4*dx2;
for(size_t x=begin[0], y=begin[1]; y<=end[1]; ++y){
glVertex2i(x,y);
if(discriminant>0){ //east (increase x)
discriminant += east;
east += -8*dy;
++x;
}
else{
discriminant += west;
}
}
}
void parabola(const point2d &end){
//y-ax^2 = 0
//a > 0
int
dx2 = square(end[0]),
dy = end[1];
point2d change(2);
change[0] = dx2 / (2*dy);
change[1] = square(change[0])*dy/dx2;
//0 <= y' <= 1
travel_x(change);
//1 < y'
travel_y(change, end);
glColor3fv(red);
glVertex2i(change[0], change[1]);
}
}
namespace recursive{
void parabola(double a, const point2d &begin, const point2d &end){
glVertex2d(begin[0], begin[1]);
point2d delta = end-begin;
if(norm1(delta) <= 1.0/pointsize) return;
point2d midpoint(2);
midpoint[0] = begin[0]+delta[0]/2;
midpoint[1] = a*square(midpoint[0]);
parabola(a, begin, midpoint);
parabola(a, midpoint, end);
}
}