610

### Calculer la distance entre deux points

1. #include <stdio.h>
2. #include <math.h>
3.
4. struct point {
5.     int x;
6.     int y;
7. };
8.
9. double dist( struct point pt1, struct point pt2 ) {
10.     int dx = pt2.x - pt1.x;
11.     int dy = pt2.y - pt1.y;
12.
13.     return sqrt( dx*dx + dy*dy );
14. }
15.
16. main( ) {
17.     struct point pt1 = { 0, 0 };
18.     struct point pt2 = { 3, 4 };
19.     struct point pt3 = { -3, -4 };
20.
21.     /* don't forget to include math.h */
22.     printf("[%d, %d] <-> [%d, %d] = %.1f\n", pt1.x, pt1.y, pt2.x, pt2.y, dist( pt1, pt2 ));
23.     printf("[%d, %d] <-> [%d, %d] = %.1f\n", pt2.x, pt2.y, pt1.x, pt1.y, dist( pt2, pt1 ));
24.     printf("[%d, %d] <-> [%d, %d] = %.1f\n", pt2.x, pt2.y, pt3.x, pt3.y, dist( pt2, pt3 ));
25.     printf("[%d, %d] <-> [%d, %d] = %.1f\n", pt3.x, pt3.y, pt2.x, pt2.y, dist( pt3, pt2 ));
26.     printf("[%d, %d] <-> [%d, %d] = %.1f\n", pt3.x, pt3.y, pt3.x, pt3.y, dist( pt3, pt3 ));
27.     printf("[%d, %d] <-> [%d, %d] = %.1f\n", pt1.x, pt1.y, pt1.x, pt1.y, dist( pt1, pt1 ));
28. }
``````\$ gcc -DSTANDALONE -o points -lm points.c
\$ ./points
[0, 0] <-> [3, 4] = 5.0
[3, 4] <-> [0, 0] = 5.0
[3, 4] <-> [-3, -4] = 10.0
[-3, -4] <-> [3, 4] = 10.0
[-3, -4] <-> [-3, -4] = 0.0
[0, 0] <-> [0, 0] = 0.0``````

#### Commentaires

###### Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].