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
|
#include<allegro.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#define blanc makecol(255,255,255)
//structures
struct point {double x,y;};
struct ligne {double a,b;}; //y=ax+b
//routines
double intersection_droite(struct point M1,struct point M2, struct point M3,struct point M4, struct point *adresseM );
int main()
{ allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,1920,1080,0,0);
//
struct point A,B,C,D,E ;
A.x=100;A.y=100; B.x=200;B.y=200;
C.x=300;C.y=0;D.x=400;D.y=1000;
E.x=0;E.y=0;
intersection_droite(A,B,C,D,&E) ;
textprintf(screen,font,0,0,blanc,"x=%lf y=%lf ",(E.x),(E.y));
while(key[KEY_ESC]==0);
return 0;
}END_OF_MAIN();
//routines
double intersection_droite(struct point M1,struct point M2, struct point M3,struct point M4, struct point *adresseM )
{ double k1,k2 ;
if(M2.x-M1.x!=0) k1=(M2.y-M1.y)/(M2.x-M1.x) ; // y=k1(x-xm1)+ym1
if(M4.x-M3.x!=0) k2=(M4.y-M3.y)/(M4.x-M3.x) ; // y=k2(x-xm3)+ym3
if(M1.x!=M2.x && M3.x!=M4.x)
{ if(k1!=k2) // 0=x(k1-k2)-k1.xm1+k2.xm3+ym1-ym3
{adresseM->x=(k1*M1.x-k2*M3.x-M1.y+M3.y)/(k1-k2) ; //*(adresseM.x)
adresseM->y= k1*((adresseM->x)-M1.x)+M1.y ;
}
if(k1==k2) adresseM=0 ;
}
return 0;
} |
Partager