# Thread: how to do euclidian distance?

1. ## how to do euclidian distance?

how would i go about writing up an equation for euclidian distance using c language. I am quite unfamiliar with the syntax's involved using squared and square roots.

the equation for euclidian distance is:
square root((Xv -Xu)^2 + (Yv-Yu)^2 +(Zv-Zu)^2)

hopefully that made sense. The variables Xv, Yv and Zv represent the equation of vector 1 and then variables Xu, Yu and Zu represent the equation of vector 2. I have already put these into my program and i just need to know how to write that equation up there into the text file so that it works.

Any help is greatly appreciated.

2. when i say text file i just mean put it into my program haha

3. Writing the function is trivial. Just calculate the sum of squares, then pass the result to sqrt().
There's no exponentiation operator in C. So just do a square by manually saying (ux - vx)*(ux -vx).
It's conventional to pass points about as structures or arrays of three elements, not to have a separate x, y and z variable for each one. But that doesn't matter too much for now.

4. If you read existing C programs, you often see code similar to
Code:
```/* 3D point (or 3D vector), data type. */
typedef struct {
double  x;
double  y;
double  z;
} point_t;

/* "constructor" for simple assignments. */
point_t point(const double x, const double y, const double z)
{
point_t  p;
p.x = x;
p.y = y;
p.z = z;
return p;
}

/* Return the dot product of two 3D vectors. */
double dot_product(const point_t v1, const point_t v2)
{
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}

/* Return the cross product of two vectors. */
point_t cross_product(const point_t v1, const point_t v2)
{
point_t p;
p.x = v1.y * v2.z - v1.z * v2.y;
p.y = v1.z * v2.x - v1.x * v2.z;
p.z = v1.x * v2.y - v1.y * v2.x;
return p;
}```
For square root, you'll need to include the <math.h> header, link against the math library (add -lm to your compiling command), and use the sqrt() function. (Or sqrtf(), if you are using single-percision floats instead of doubles.)

A side note:

An often overlooked feature of C is that structures can be copied and assigned. Given
Code:
`    point_t p1, p2;`
many programmers still write (the equivalent of)
Code:
```    p1.x = p2.x;
p1.y = p2.y;
p1.z = p2.z;```
even though
Code:
`    p1 = p2;`
is perfectly valid and works like you'd expect it to. Thus, with the point() function above, you can write code like
Code:
```    point_t p;
p = cross_product(point(1.0, 2.0, 3.0), point(-1.0, 0.0, -1.0));```
If you use C99 (instead of ANSI C, C89/C90), you can use
Code:
```#define _POSIX_C_SOURCE 200809L
#include <stdio.h>

/* 3D point (or 3D vector), data type.
*/
typedef struct {
double  x;
double  y;
double  z;
} point_t;

/* Point "constructor" for easier coding. */
static inline point_t point(const double x, const double y, const double z)
{
return (point_t){ x, y, z };
}

/* Return the dot product of two 3D vectors. */
static inline double dot_product(const point_t v1, const point_t v2)
{
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}

/* Return the cross product of two vectors. */
static inline point_t cross_product(const point_t v1, const point_t v2)
{
return (point_t){ v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x };
}```
which tends to be faster code (since the basic operations are inlined, not called as functions, and the compiler can often optimize more complex math operations than when it could were they separate functions).