Useless update
[hammerdown.git] / dethphysics / vector.c
bloba2f7d5e473f9e650fcd00e44642c9707523365e8
1 //headers
2 #include <SDL/SDL.h>
3 #include <SDL/SDL_opengl.h>
4 #include <stdio.h>
5 #include <math.h>
7 //Screen attributes
8 const int SCREEN_WIDTH = 640;
9 const int SCREEN_HEIGHT = 480;
10 const int SCREEN_BPP = 32;
12 //function prototypes
13 void draw_line(int, int, int, int vectores[][2]);
14 void draw_axis();
15 void draw_vectors(int, int vectores[][2]);
16 void dibuja_flecha(int, int);
18 int main(int argc, char *argv[])
20 int quit = 0;
22 //Initialize
23 if( init() == 0 )
25 return 1;
28 SDL_Surface *screen;
29 SDL_Event event; //The event structure
30 int cantidad = 0; // cantidad de vectores
31 int vectores[1000][2]; //arreglo de vectores
33 glBegin(GL_LINES);
34 draw_axis();
35 glEnd();
37 //escucha de eventos
38 //While the user hasn't quit
39 while( quit == 0 )
42 //If there's events to handle
43 if( SDL_PollEvent( &event ) )
45 //reset the viewport
46 // glViewport(0, 0, 600, 800);
48 //The mouse offsets
49 int x = 0, y = 0;
51 //If the mouse moved
52 if( event.type == SDL_MOUSEMOTION )
54 //Get the mouse offsets
55 x = event.motion.x;
56 y = event.motion.y;
57 draw_line(x, y, cantidad, vectores);
60 if (event.type == SDL_MOUSEBUTTONDOWN)
62 if( event.button.button == SDL_BUTTON_LEFT )
64 vectores[cantidad][0] = event.button.x;
65 vectores[cantidad][1] = event.button.y;
66 cantidad++;
70 SDL_GL_SwapBuffers();
71 //If the user has Xed out the window
72 if( event.type == SDL_QUIT )
74 //Quit the program
75 quit = 1;
80 SDL_Quit();
82 return 0;
85 int init_GL()
87 //Set clear color
88 glClearColor( 0, 0, 0, 0 );
90 //Set the viewport
91 glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
93 //Set projection
94 glMatrixMode( GL_PROJECTION );
95 glLoadIdentity();
96 glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
98 //Initialize modelview matrix
99 glMatrixMode( GL_MODELVIEW );
100 glLoadIdentity();
102 //If there was any errors
103 if( glGetError() != GL_NO_ERROR )
105 return 0;
108 //If everything initialized
109 return 1;
112 int init()
114 //Initialize SDL
115 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
117 return 0;
120 //Create Window
121 if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
123 return 0;
126 //Initialize OpenGL
127 if( init_GL() == 0 )
129 return 0;
132 //Set caption
133 SDL_WM_SetCaption( "OpenGL Test", NULL );
135 return 1;
138 void draw_line(int x, int y, int cantidad, int vectores [][2])
140 // Clear the screen before drawing:
141 glClear( GL_COLOR_BUFFER_BIT );
143 // draw the line
144 glBegin(GL_LINES);
146 draw_axis();
147 draw_vectors(cantidad, vectores);
149 //dibuja lineas
150 glVertex3f( SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0 );
151 glVertex3f( x, y, 0 );
152 dibuja_flecha(x,y);
154 glEnd();
157 void draw_vectors(int cantidad, int vectores[][2])
159 int i=0;
160 while(i < cantidad && (cantidad != 0))
162 glVertex3f( SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0 );
163 glVertex3f( vectores[i][0], vectores[i][1], 0 );
164 i++;
168 void draw_axis()
170 //eje x
171 glVertex3f( SCREEN_WIDTH/2, 20, 0 );
172 glVertex3f( SCREEN_WIDTH/2,SCREEN_HEIGHT - 20, 0 );
173 //eje y
174 glVertex3f( SCREEN_WIDTH - 20, SCREEN_HEIGHT/2, 0 );
175 glVertex3f( 20, SCREEN_HEIGHT/2,0 );
178 void dibuja_flecha(int x1, int y1){
179 int x = x1 - SCREEN_WIDTH/2;
180 int y = y1 - SCREEN_HEIGHT/2;
181 float theta = atan2(y, x); //angulo del vector
182 int magnitud = 20;//magnitud del vector
183 float a1=0, b1 = 0; //
184 float a2=0, b2 = 0;
186 //valores del vector flecha
187 //obtengo los angulos
188 float phi1 = theta + 210;
189 float phi2 = theta - 210;
191 //obtengo los puntos y le sumo el valor del punto P(x1,y1)
192 //para obtener una representacion particular
193 a1 = magnitud*cos( phi1 ) + x1; b1 = magnitud*sin( phi1 ) + y1;
194 a2 = magnitud*cos( phi2 ) + x1; b2 = magnitud*sin( phi2 ) + y1;
196 //dibuja la lineas de la flecha
197 glVertex3f ( x1, y1, 0);
198 glVertex3f ( a1, b1, 0);
199 glVertex3f ( x1, y1, 0);
200 glVertex3f ( a2, b2, 0);