-
Notifications
You must be signed in to change notification settings - Fork 0
/
p1.cpp
284 lines (238 loc) · 6.22 KB
/
p1.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <GL/gl.h>
#include <GL/glut.h>
#include <cmath>
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
Vector4d pt_x1, pt_y1, pt_z1;
Vector4d pt_x2, pt_y2, pt_z2;
Vector4d pt_x3, pt_y3, pt_z3;
VectorXd t_x1, t_y1, t_z1;
VectorXd t_x2, t_y2, t_z2;
VectorXd t_x3, t_y3, t_z3;
VectorXd ts;
Vector3d sphere_coo;
Vector3d cube_coo;
float counter1=0;
float counter2=0;
float counter3 = 0;
bool start = false;
bool stop = false;
float angle_0=-90;
float angle_n=0;
float angle1 = -90;
float angle2 = 0;
float angle3 = 0;
long binomials ( long n, long k ){
// n && k >=0
long i;
long b;
if ( 0 == k || n == k )
{
return 1;
}
if ( k > n )
{
return 0;
}
if ( k > ( n - k ) )
{
k = n - k;
}
if ( 1 == k )
{
return n;
}
b = 1;
for ( i = 1; i <= k; ++i )
{
b *= ( n - ( k - i ) );
if ( b < 0 ) return -1; /* OVERFLOW */
b /= i;
}
return b;
}
double polyterm ( const int &n, const int &k, const double &t ){
return pow ( ( 1.-t ),n-k ) *pow ( t,k );
}
double getValue ( const double &t, const VectorXd &v ){
int order = v.size()-1;
double value = 0;
for ( int n=order, k=0; k<=n; k++ )
{
if ( v ( k ) ==0 ) continue;
value += binomials ( n,k ) * polyterm ( n,k,t ) * v ( k );
}
return value;
}
void changeSize ( int w, int h ){
if ( h == 0 )
{
h = 1;
}
float ratio = 1.0* w / h;
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
glViewport ( 0, 0, w, h );
gluPerspective ( 45,ratio,1,1000 ); // view angle u y, aspect, near, far
}
void keyboard (unsigned char key, int x, int y) {
switch(key){
case 27: exit(0); break;
case 's': start = true; break;
case 'x': stop = true; break;
default: break;
}
}
void generateBezierCurve1(){
ts = VectorXd::LinSpaced ( 21,0,1. );
t_x1.resize ( ts.size() );
t_y1.resize ( ts.size() );
t_z1.resize ( ts.size() );
for ( int idx=0; idx< ts.size(); ++idx )
{
t_x1 ( idx ) = getValue ( ts ( idx ), pt_x1 );
t_y1 ( idx ) = getValue ( ts ( idx ), pt_y1 );
t_z1 ( idx ) = getValue ( ts ( idx ), pt_z1 );
}
}
void generateBezierCurve2(){
ts = VectorXd::LinSpaced ( 21,0,1. );
t_x2.resize ( ts.size() );
t_y2.resize ( ts.size() );
t_z2.resize ( ts.size() );
for ( int idx=0; idx< ts.size(); ++idx )
{
t_x2 ( idx ) = getValue ( ts ( idx ), pt_x2 );
t_y2 ( idx ) = getValue ( ts ( idx ), pt_y2 );
t_z2 ( idx ) = getValue ( ts ( idx ), pt_z2 );
}
}
void generateBezierCurve3(){
ts = VectorXd::LinSpaced ( 21,0,1. );
t_x3.resize ( ts.size() );
t_y3.resize ( ts.size() );
t_z3.resize ( ts.size() );
for ( int idx=0; idx< ts.size(); ++idx )
{
t_x3 ( idx ) = getValue ( ts ( idx ), pt_x3 );
t_y3 ( idx ) = getValue ( ts ( idx ), pt_y3 );
t_z3 ( idx ) = getValue ( ts ( idx ), pt_z3 );
}
}
void drawBezierCurve1(){
glPushMatrix();
glColor3f ( 1.0f, 0.0f, 0.0f );
glBegin ( GL_LINE_STRIP );
for ( int i=0; i< (int)counter1; ++i ){
glVertex3f ( t_x1 ( i ), t_y1 ( i ), t_z1 ( i ) );
}
glEnd();
glPopMatrix();
}
void drawBezierCurve2(){
glPushMatrix();
glColor3f ( 1.0f, 0.0f, 0.0f );
glBegin ( GL_LINE_STRIP );
for ( int i=0; i< (int)counter2; ++i ){
glVertex3f ( t_x2 ( i ), t_y2 ( i ), t_z2 ( i ) );
}
glEnd();
glPopMatrix();
}
void drawBezierCurve3(){
glPushMatrix();
glColor3f ( 1.0f, 0.0f, 0.0f );
glBegin ( GL_LINE_STRIP );
for ( int i=0; i< (int)counter3; ++i ){
glVertex3f ( t_x3 ( i ), t_y3 ( i ), t_z3 ( i ) );
}
glEnd();
glPopMatrix();
}
void drawSphere(){
glPushMatrix();
glTranslatef(sphere_coo(0), sphere_coo(1), sphere_coo(2));
glColor3f(0,0,1);
glutSolidSphere(0.2, 16,16);
glPopMatrix();
}
void drawScene(){
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode ( GL_MODELVIEW ); // idemo u perspektivu
glLoadIdentity(); // resetiranje
gluLookAt ( 0.0,0.0,10.0, // camera
0.0,0.0,-1.0, // where
0.0f,1.0f,0.0f ); // up vector
glPushMatrix();
glTranslatef(-5.4, 3.9, 0);
glPushMatrix();
glTranslatef(-pt_x1(0), -pt_y1(0), -pt_z1(0));
drawBezierCurve1();
drawBezierCurve2();
drawBezierCurve3();
drawSphere();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void update ( int ){
if( (start) && !(stop) ){
glutPostRedisplay();
if(angle1 <0)
{
counter1 += 0.238;
double t = (angle1 - angle_0)/(angle_n - angle_0);
sphere_coo(0) = getValue(t, pt_x1);
sphere_coo(1) = getValue(t, pt_y1);
sphere_coo(2) = getValue(t, pt_z1);
angle1 +=1;
if (angle1 == 0){angle2=-90;}
} else if(angle2 < 0){
counter2 += 0.238;
double t = (angle2 - angle_0)/(angle_n - angle_0);
sphere_coo(0) = getValue(t, pt_x2);
sphere_coo(1) = getValue(t, pt_y2);
sphere_coo(2) = getValue(t, pt_z2);
angle2 +=1;
if (angle2 == 0){angle3=-90;}
}else if(angle3 < 0){
counter3 += 0.238;
double t = (angle3 - angle_0)/(angle_n - angle_0);
sphere_coo(0) = getValue(t, pt_x3);
sphere_coo(1) = getValue(t, pt_y3);
sphere_coo(2) = getValue(t, pt_z3);
angle3 +=1;
}
if ( (int)counter1 > ts.size() ) counter1= ts.size();
if ( (int)counter2 > ts.size() ) counter2= ts.size();
if ( (int)counter3 > ts.size() ) counter3= ts.size();
}
glutTimerFunc ( 25, update, 0 );
}
int main ( int argc, char **argv ){
pt_x1 << -0.9, 0.4, 1.7, 2;
pt_y1 << 5, 4.8, 3, -3;
pt_z1 << 0, 0, 0, 0;
pt_x2 << 2, 2.3, 6.2, 6.5;
pt_y2 << -3, 3, 3, -3;
pt_z2 << 0, 0, 0, 0;
pt_x3 << 6.5, 7.67, 8.84, 10;
pt_y3 << -3, 1, 1, -3;
pt_z3 << 0, 0, 0, 0;
sphere_coo << pt_x1(0), pt_y1(0), pt_z1(0);
generateBezierCurve1();
generateBezierCurve2();
generateBezierCurve3();
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize ( 800, 600 );
glutCreateWindow ( "Zadaca - zadatak1" );
glutReshapeFunc ( changeSize );
glutDisplayFunc ( drawScene );
glutKeyboardFunc(keyboard);
glutTimerFunc ( 25, update, 0 );
glutMainLoop();
return 0;
}