Advertisement
Guest User

LouisMaBiatch

a guest
Apr 23rd, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. /*******************************************************************************************/
  2. /* DUT Informatique 2e Annee - Introduction a l'Infographie */
  3. /* Exemple 00 : le squelette minimal */
  4. /*******************************************************************************************/
  5. #include <g2x.h>
  6.  
  7. /*-------------------------------------------------*/
  8. /* limites de la zone reelle associee a la fenetre */
  9. /*-------------------------------------------------*/
  10. static double wxmin=-2.,wymin=-2.,wxmax=+2.,wymax=+2.;
  11.  
  12. G2Xpoint A,B,C,D,I;
  13.  
  14. bool InterSegment(G2Xpoint A,G2Xpoint B,G2Xpoint C,G2Xpoint D,G2Xpoint *I) {
  15. G2Xvector AB = G2Xdefvector(A,B);
  16. G2Xvector AC = G2Xdefvector(A,C);
  17. G2Xvector CA = G2Xdefvector(C,A);
  18. G2Xvector CD = G2Xdefvector(C,D);
  19. G2Xvector AD = G2Xdefvector(A,D);
  20.  
  21. double ta = G2Xprodvect(AC,CD) / G2Xprodvect(AB,CD);
  22. if (ta<0 || ta>1) {
  23. return false;
  24. }
  25.  
  26. double tc = G2Xprodvect(CA,AB) / G2Xprodvect(CD,AB);
  27. if (tc<0 || tc>1) {
  28. return false;
  29. }
  30.  
  31. I->x = A.x+ta*AB.x;
  32. I->y = A.y+ta*AB.y;
  33. return true;
  34. }
  35.  
  36. /*------------------------------*/
  37. /* la fonction d'initialisation */
  38. /*------------------------------*/
  39. static void init(void)
  40. {
  41. A.x=-1.0; A.y=-1.0;
  42. B.x=-1.0; B.y=+1.0;
  43. C.x=+1.0; C.y=+1.0;
  44. D.x=+1.0; D.y=-1.0;
  45.  
  46. g2x_SetControlPoint(&A,9);
  47. g2x_SetControlPoint(&B,9);
  48. g2x_SetControlPoint(&C,9);
  49. g2x_SetControlPoint(&D,9);
  50. }
  51.  
  52. /*------------------------------*/
  53. /* la fonction de dessin */
  54. /*------------------------------*/
  55. static void draw(void)
  56. {
  57. g2x_Plot(A.x,A.y,G2Xy,4);
  58.  
  59. g2x_Plot(B.x,B.y,G2Xg,4);
  60. g2x_Plot(C.x,C.y,G2Xc,4);
  61. g2x_Plot(D.x,D.y,G2Xr,4);
  62.  
  63. g2x_Line(A.x,A.y,B.x,B.y,G2Xk,2);
  64. g2x_Line(C.x,C.y,D.x,D.y,G2Xk,2);
  65.  
  66. if(InterSegment(A,B,C,D,&I))
  67. g2x_Plot(I.x,I.y,G2Xo,8);
  68.  
  69. }
  70.  
  71. /*------------------------------*/
  72. /* la fonction d'animation */
  73. /*------------------------------*/
  74. static void anim(void)
  75. {
  76. fprintf(stderr,"\e[33m 3- fonction d'animation\n");
  77. }
  78.  
  79. /*------------------------------*/
  80. /* la fonction de sortie */
  81. /*------------------------------*/
  82. static void quit(void)
  83. {
  84. fprintf(stderr,"\e[34m 4- fonction de sortie\n\e[0m ");
  85. }
  86.  
  87. /***************************************************************************/
  88. /* */
  89. /***************************************************************************/
  90. int main(int argc, char **argv)
  91. {
  92. /* creation de la fenetre - titre et tailles (pixels) */
  93. g2x_InitWindow(*argv,512,512);
  94. /* zone graphique reelle associee a la fenetre */
  95. g2x_SetWindowCoord(wxmin,wymin,wxmax,wymax);
  96.  
  97. g2x_SetInitFunction(init); /* fonction d'initialisation */
  98. g2x_SetDrawFunction(draw); /* fonction de dessin */
  99. g2x_SetAnimFunction(anim); /* fonction d'animation */
  100. g2x_SetExitFunction(quit); /* fonction de sortie */
  101.  
  102. /* lancement de la boucle principale */
  103. return g2x_MainStart();
  104. /* RIEN APRES CA */
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement