point2D

point2D.c

#include `<stdio.h>`

#include `<stdlib.h>`
#include `<stdbool.h>`

#include `<math.h>` /* pour la fonction sqrt */

struct Point2D
{
    double x;
    double y;
};

typedef struct Point2D point2D;


double lire_x(point2D P)
{
    return P.x;
}

double lire_y(point2D P)
{
    return P.y;
}

point2D nouveau_point2D()
{
    point2D P;
    P.x = 0.0;
    P.y = 0.0;
    return P;
}

point2D ecrire_x(double x, point2D P)
{
    P.x = x;  /* attention, le premier 'x' correspond au champ du point2D, 
               le deuxieme 'x' correspond a l'argument de la fonction */
    return P;
}

point2D ecrire_y(double valeury, point2D P)
{
    P.y = valeury;
    return P;
}

double distance(point2D A, point2D B)
{
    double xA, yA;
    double xB, yB;
    double dx, dy, d;

    xA = lire_x(A);
    yA = lire_y(A);
    xB = lire_x(B);
    yB = lire_y(B);

    dx = xB-xA;
    dy = yB-yA;
    d = sqrt(dx*dx+dy*dy);
    return d;
}

point2D milieu(point2D A, point2D B)
{
    double xA, yA;
    double xB, yB;
    double xM, yM;
    point2D M;

    xA = lire_x(A);
    yA = lire_y(A);
    xB = lire_x(B);
    yB = lire_y(B);

    xM = 0.5*(xA+xB);
    yM = 0.5*(yA+yB);
    M = ecrire_x(xM, M);
    M = ecrire_y(yM, M);
    return M;
}

void affiche_point2D(point2D P)
{
    printf("(%f,%f)", lire_x(P), lire_y(P));
}

int main()
{
    point2D A;
    point2D B;
    point2D C;
    point2D M;
    double distAB;
    double distAC;
    double distBC;

    A = ecrire_x(2., ecrire_y(3., nouveau_point2D()));
    B = ecrire_x(-1., ecrire_y(-1., nouveau_point2D()));
    C = ecrire_x(2., ecrire_y(-1., nouveau_point2D()));

    M = milieu(A, B);
    distAB = distance(A, B);
    distAC = distance(A, C);
    distBC = distance(B, C);

    printf("point2D A = "); affiche_point2D(A); printf("\n");
    printf("point2D B = "); affiche_point2D(B); printf("\n");
    printf("point2D C = "); affiche_point2D(C); printf("\n");

    printf("le mileu de AB vaut "); affiche_point2D(M); printf("\n");

    printf("la distance AB vaut: %f\n", distAB);
    printf("la distance AC vaut: %f\n", distAC);
    printf("la distance BC vaut: %f\n", distBC);

    return EXIT_SUCCESS;
}