Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************************/
- /* DUT Informatique 2e Annee - Introduction a l'Infographie */
- /* Exemple 00 : le squelette minimal */
- /*******************************************************************************************/
- #include <g2x.h>
- /*-------------------------------------------------*/
- /* limites de la zone reelle associee a la fenetre */
- /*-------------------------------------------------*/
- static double wxmin=-2.,wymin=-2.,wxmax=+2.,wymax=+2.;
- G2Xpoint A,B,C,D,I;
- bool InterSegment(G2Xpoint A,G2Xpoint B,G2Xpoint C,G2Xpoint D,G2Xpoint *I) {
- G2Xvector AB = G2Xdefvector(A,B);
- G2Xvector AC = G2Xdefvector(A,C);
- G2Xvector CA = G2Xdefvector(C,A);
- G2Xvector CD = G2Xdefvector(C,D);
- G2Xvector AD = G2Xdefvector(A,D);
- double ta = G2Xprodvect(AC,CD) / G2Xprodvect(AB,CD);
- if (ta<0 || ta>1) {
- return false;
- }
- double tc = G2Xprodvect(CA,AB) / G2Xprodvect(CD,AB);
- if (tc<0 || tc>1) {
- return false;
- }
- I->x = A.x+ta*AB.x;
- I->y = A.y+ta*AB.y;
- return true;
- }
- /*------------------------------*/
- /* la fonction d'initialisation */
- /*------------------------------*/
- static void init(void)
- {
- A.x=-1.0; A.y=-1.0;
- B.x=-1.0; B.y=+1.0;
- C.x=+1.0; C.y=+1.0;
- D.x=+1.0; D.y=-1.0;
- g2x_SetControlPoint(&A,9);
- g2x_SetControlPoint(&B,9);
- g2x_SetControlPoint(&C,9);
- g2x_SetControlPoint(&D,9);
- }
- /*------------------------------*/
- /* la fonction de dessin */
- /*------------------------------*/
- static void draw(void)
- {
- g2x_Plot(A.x,A.y,G2Xy,4);
- g2x_Plot(B.x,B.y,G2Xg,4);
- g2x_Plot(C.x,C.y,G2Xc,4);
- g2x_Plot(D.x,D.y,G2Xr,4);
- g2x_Line(A.x,A.y,B.x,B.y,G2Xk,2);
- g2x_Line(C.x,C.y,D.x,D.y,G2Xk,2);
- if(InterSegment(A,B,C,D,&I))
- g2x_Plot(I.x,I.y,G2Xo,8);
- }
- /*------------------------------*/
- /* la fonction d'animation */
- /*------------------------------*/
- static void anim(void)
- {
- fprintf(stderr,"\e[33m 3- fonction d'animation\n");
- }
- /*------------------------------*/
- /* la fonction de sortie */
- /*------------------------------*/
- static void quit(void)
- {
- fprintf(stderr,"\e[34m 4- fonction de sortie\n\e[0m ");
- }
- /***************************************************************************/
- /* */
- /***************************************************************************/
- int main(int argc, char **argv)
- {
- /* creation de la fenetre - titre et tailles (pixels) */
- g2x_InitWindow(*argv,512,512);
- /* zone graphique reelle associee a la fenetre */
- g2x_SetWindowCoord(wxmin,wymin,wxmax,wymax);
- g2x_SetInitFunction(init); /* fonction d'initialisation */
- g2x_SetDrawFunction(draw); /* fonction de dessin */
- g2x_SetAnimFunction(anim); /* fonction d'animation */
- g2x_SetExitFunction(quit); /* fonction de sortie */
- /* lancement de la boucle principale */
- return g2x_MainStart();
- /* RIEN APRES CA */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement