Thread: how to do euclidian distance?

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    11

    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. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    when i say text file i just mean put it into my program haha

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    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.
    Last edited by Malcolm McLean; 05-23-2013 at 01:21 AM.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Why does it keep showing Distance = 0.00?
    By toobee in forum C Programming
    Replies: 4
    Last Post: 02-19-2012, 11:38 PM
  2. distance formula help
    By Arex Bawrin in forum C Programming
    Replies: 15
    Last Post: 02-25-2011, 03:38 PM
  3. Hamming Distance?
    By rs07 in forum C Programming
    Replies: 3
    Last Post: 09-12-2008, 04:02 PM
  4. Hex distance
    By waterst in forum C Programming
    Replies: 7
    Last Post: 11-02-2002, 01:59 PM
  5. love and distance
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-19-2002, 01:51 PM