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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| #include <stdio.h>
#include <stdlib.h>
#include <math.h> // juste pour abs
// la tolerance
const double epsilon=0.001; // par exemple
// Pour représenter les points
typedef struct
{
double x;
double y;
}
Point;
// initialisation d'un point avec ses coordonnées
void InitPoint (double x0, double y0, Point * pP)
{
pP->x=x0;
pP->y=y0;
}
// Représentation des droites
typedef struct
{
double a;
double b;
double c;
} Droite;
// initialisation des droites par les coefficients
void InitDroite(double u, double v, double w, Droite * pD)
{
pD->a=u;
pD->b=v;
pD->c=w;
}
// représentation des polygones
typedef struct
{
int n ; // nombre de sommets
Point * Sommets; // tableau des sommets
} Polygone;
// initialisation d'un polygone
// à partir d'un tableau de points
void InitPolygone ( int m, Point S[], Polygone * pP)
{
int i;
pP->n=m;
pP->Sommets=malloc(m*sizeof(Point));
for (i=0;i<m;i++)
InitPoint(S[i].x,S[i].y,pP->Sommets+i);
}
// teste si un Point A est egal à un point B a epsilon près
int Egal(Point A, Point B)
{
double deltax=abs(A.x-B.x);
double deltay=abs(A.y-B.y);
return (deltax<epsilon)&&(deltay<epsilon);
}
// teste si un point M appartient à un polygone P
int EstSommet(Point M, Polygone P)
{
int i;
for (i=0;i<P.n;i++)
if (Egal (M, P.Sommets[i])) return 1;
return 0;
}
// Calcule la médiatrice d'un segment
Droite Mediatrice (Point A, Point B)
{
Droite MD;
double u= 2*(B.x-A.x);
double v= 2*(B.y-A.y);
double w= A.x*A.x+A.y*A.y-B.x*B.x-B.y*B.y;
InitDroite(u,v,w,&MD);
return MD;
}
// symétrique du point A par rapport à la droite D
Point Symetrique (Point A, Droite D)
{
double k= (D.a*A.x+D.b*A.y+D.c)/(D.a*D.a+D.b*D.b);
double X=A.x-D.a*k;
double Y=A.y-D.b*k;
Point * S=malloc(sizeof(Point));
InitPoint(X,Y,S);
return *S;
}
// teste si la droite D est axe de symétrie du polygone P
int Axe (Droite D, Polygone P)
{
int i;
Point S;
// vérifie si tous les symétriques des sommets sont des sommets
for (i=0;i<P.n;i++)
{
S=Symetrique(P.Sommets[i],D);
if (!EstSommet(S,P))
return 0;
}
return 1;
}
// Teste si le polygone P admet un axe de symétrie
int Test(Polygone P)
{
int i,j;
Droite D;
for (i=0;i<P.n;i++)
for (j=i+1;j<P.n;j++)
{
D=Mediatrice(P.Sommets[i],P.Sommets[j]);
if (Axe(D,P)) return 1; // Gagné
}
return 0; // perdu
}
int main()
{
return 0;
} |
Partager