Simple Program to Calculate Distance in 3 Dimensional Plane

In 2-space if two points are p1(x1, y1) and p2(x2, y2) then the distance between them is,

d = \sqrt[]{(x_2 - x_1)^2 + (y_2 - y_1)^2}

Similarly in 3-space distance between two points p1(x1, y1, z1) and p2(x2, y2, z2) is,

d = \sqrt[]{(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2}

 

Example:

Distance between point (1, 1, 2) and point (3, -2, 4) is,

d = \sqrt[]{(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2}
d = \sqrt[]{(3 - 1)^2 + (-2 - 1)^2 + (4 - 2)^2}
d = \sqrt[]{4 + 9 + 4}
d = 4.12


Code:

/**
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 */

#include<cstdio>
#include<cmath>
#include<algorithm>

struct point{
    float x, y, z;
};

// descending comparison x
bool point_sorter_x_desc(point const &p0, point const &p1){
    return p0.x > p1.x;
}

// ascending comparison x
bool point_sorter_x_asc(point const &p0, point const &p1){
    return p0.x < p1.x;
}

// descending comparison y
bool point_sorter_y(point const &p0, point const &p1){
    return p0.y > p1.y;
}

// descending comparison y
bool point_sorter_z(point const &p0, point const &p1){
    return p0.z > p1.z;
}

// descending comparison priority x then y then z
bool point_sorter_greatest(point const &p0, point const &p1){
    if(p0.x > p1.x)
        return p0.x > p1.x;
    if(p0.y > p1.y)
        return p0.y > p1.y;
    return p0.z > p1.z;
}

int main(){

    int N;
    printf("Enter number of points:\n");
    scanf("%d", &N);

    /*
     * Comment this section to hard code points
     * Uncomment to take user input
     */

    //struct point *p = new point[N];

    //for(int i = 0; i < N; ++i)
      //  scanf("%f %f %f", &p[i].x, &p[i].y, &p[i].z);

    /*
     * Comment this section to input points
     * Uncomment the struct memory allocation and scan above
     */
    struct point p[6] = {
        {1,1,2},
        {3,-2,4},
        {10,10,12},
        {6,2,7},
        {5,6,9},
        {8,1,1}
    };

    // Sort based on the point in x axis
    std::sort(p, p + N, &point_sorter_x_asc);

    // show the sorted points
    printf("\nSorted Points:\n\n");
    for(int i = 0; i < N; ++i)
        printf("P%d: (%.2f , %.2f, %.2f)\n", i, p[i].x, p[i].y, p[i].z);
    printf("\n\n");

    // Finding distance in 3-Dimensional Plane
    float total_distance = 0;
    for(int i = 1; i < N; ++i){
        float distance = sqrt( pow( p[i].x - p[i - 1].x, 2 ) + pow( p[i].y - p[i - 1].y, 2 ) + pow( p[i].z - p[i - 1].z, 2 ) );
        printf("\nDistance between points (%.2f,%.2f,%.2f) and (%.2f,%.2f,%.2f) is: %.2f\n", p[i - 1].x, p[i - 1].y, p[i - 1].z, p[i].x, p[i].y, p[i].z, distance);
        total_distance += distance;
    }

    printf("\n\n");
    printf("Total Distance: %.2f\n", total_distance);

	return 0;
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s