how to do euclidian distance?

This is a discussion on how to do euclidian distance? within the C Programming forums, part of the General Programming Boards category; how would i go about writing up an equation for euclidian distance using c language. I am quite unfamiliar with ...

  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
    2,553
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    333
    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.
    http://www.malcolmmclean.site11.com/www

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    838
    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, 10:38 PM
  2. distance formula help
    By Arex Bawrin in forum C Programming
    Replies: 15
    Last Post: 02-25-2011, 02: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, 12:59 PM
  5. love and distance
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-19-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21