Advertisement
plantbae

Mangelatina

Nov 16th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. #include <windows.h>
  2. #include <GL/glut.h>
  3.  
  4.  
  5. typedef struct{
  6. float x;
  7. float y;
  8. } punto;
  9.  
  10.  
  11. typedef struct{
  12. punto P0;
  13. punto Pf;
  14. float dx;
  15. float dy;
  16. } arista;
  17.  
  18. void poligono(punto pt1, punto pt2, punto pt3);
  19. void linea(punto A, punto B);
  20.  
  21.  
  22. int W = 700;
  23. int H = 700;
  24. int click = 0;
  25.  
  26. punto p1, p2, p3;
  27.  
  28. void display()
  29. {
  30. glClearColor(0.0, 0.0, 0.0, 0.0);
  31. glClear(GL_COLOR_BUFFER_BIT);
  32.  
  33. glMatrixMode(GL_PROJECTION);
  34. glLoadIdentity();
  35. gluOrtho2D(0.0, (double)W, 0.0, (double)H);
  36. glViewport(0.0, 0.0, W, H);
  37.  
  38. poligono(p1, p2, p3);
  39. glColor3f(1.0, 0.0, 0.0);
  40. linea(p1, p2);
  41. linea(p2, p3);
  42. linea(p3, p1);
  43. glutSwapBuffers();
  44. }
  45.  
  46. void poligono(punto pt1, punto pt2, punto pt3)
  47. {
  48. glMatrixMode(GL_MODELVIEW);
  49. glLoadIdentity();
  50.  
  51. glColor4f(0.10f, 0.69f, 0.40f, 0.0f);
  52. glBegin(GL_POINTS);
  53. glVertex2i(pt1.x, pt1.y);
  54. glVertex2i(pt2.x, pt2.y);
  55. glVertex2i(pt3.x, pt3.y);
  56. glEnd();
  57. glFlush();
  58. }
  59.  
  60.  
  61. /* ojo*/
  62.  
  63. void linea(punto A, punto B)
  64. {
  65. glMatrixMode(GL_MODELVIEW);
  66. glLoadIdentity();
  67.  
  68. glColor4f(0.10f, 0.69f, 0.40f, 0.0f);
  69. glBegin(GL_POINTS);
  70. /*Recta forma cartesiana y=mx+b
  71. m=dy/dx
  72. Recta forma general f(x,y)=Ax+By+C=0
  73. donde m= -A/B y b= -C/B*/
  74. int dx, dy, dE, dNE, De, Dne, d, D, x, y, f, mv;
  75. dx = labs(B.x - A.x);
  76. dy = labs(B.y - A.y);
  77. //Si (dx > dy) entonces se toma que el negativo será B en lugar de A por ende A= dy, B= -dx y C= -bdx
  78. //Si (dx < dy) entonces se toma que el negativo será A en lugar de B por ende A= -dy, B= dx y C= bdx
  79. d = 2 * dy - dx; // Es el dk cuando (dx > dy) = (dy -1/2 dx) se multiplicó x2 para quitar los racionales.
  80. D = 2 * dx - dy; // Es el dk cuando (dx < dy) = (dx -1/2 dy) se multiplicó x2 para queitar los racionales
  81. dE = 2 * dy; // dE= dk+1 cuando d<0 y cuando (dx > dy)
  82. De = 2 * dx; // De= dk+1 cuando d<0 y cuando (dx < dy)
  83. dNE = 2 * (dy - dx); // dNe = dk+1 cuando d>=0 y cuando (dx > dy)
  84. Dne = 2 * (dx - dy); //Dne = dk+1 cuando d>=0 y cuando (dx < dy)
  85. x = A.x;
  86. y = A.y;
  87. mv = 1; // Dependiendo del octante en que se encuentre indica si la variable que lleva el punto medio aumenta o disminuye.
  88. if (dx > dy){ // Se ubica desde (pi/4 , 7pi/4) u (3pi/4 , 5pi/4 )
  89. if (A.x > B.x){
  90. x = B.x;
  91. y = B.y;
  92. f = A.x; // El punto f siempre se ubicará hacia la derecha por organización y cumplimiento de la condición
  93. //while dentro de un determinado octante
  94. if (B.y>A.y)
  95. mv = -1;
  96. }
  97. else{
  98. x = A.x;
  99. y = A.y;
  100. f = B.x;
  101. if (A.y>B.y)
  102. mv = -1;
  103. }
  104. glVertex2i(x, y);
  105. while (x < f){
  106. x++;
  107. if (d < 0)
  108. d = d + dE;
  109. else{
  110. y = y + mv;
  111. d = d + dNE;
  112. }
  113.  
  114. glVertex2i(x, y);
  115. }
  116. } // Desde aquí se refleja en los octantes faltantes desde (pi/4 , 3pi/4) u (5pi/4 , 7pi/4)
  117. else{
  118. if (A.y > B.y){
  119. x = B.x;
  120. y = B.y;
  121. f = A.y;
  122. if (B.x>A.x)
  123. mv = -1;
  124. }
  125. else{
  126. x = A.x;
  127. y = A.y;
  128. f = B.y;
  129. if (A.x>B.x)
  130. mv = -1;
  131. }
  132. glVertex2i(x, y);
  133. while (y < f){
  134. y++;
  135. if (D < 0)
  136. D = D + De;
  137. else{
  138. x = x + mv;
  139. D = D + Dne;
  140. }
  141. glVertex2i(x, y);
  142. }
  143. }
  144. glEnd();
  145. glFlush();
  146. }
  147.  
  148. void mouse(int btn, int state, int x, int y)
  149. {
  150. if (btn == GLUT_LEFT_BUTTON)
  151. if (state == GLUT_DOWN)
  152. {
  153. switch (click)
  154. {
  155. case 0:
  156. p1.x = x;
  157. p1.y = H - y;
  158. click = 1;
  159. break;
  160.  
  161. case 1:
  162. p2.x = x;
  163. p2.y = H - y;
  164. click = 2;
  165. break;
  166. case 2:
  167. p3.x = x;
  168. p3.y = H - y;
  169. click = 0;
  170. break;
  171. }
  172. glutPostRedisplay();
  173.  
  174. }
  175. }
  176.  
  177. int ordenar()
  178. {
  179.  
  180. }
  181. int main()
  182. {
  183. glutInitWindowSize(W, H);
  184. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  185. glutCreateWindow("Triangulo");
  186. glutDisplayFunc(display);
  187. glutMouseFunc(mouse);
  188. glutMainLoop();
  189. return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement