Print the distance between two points - C++ Data Structure

C++ examples for Data Structure:Algorithm

Description

Print the distance between two points

Demo Code

#include <cmath>
#include <iostream>

struct Point {//from   w ww  .j ava 2s  .c o  m
    Point(double X, double Y) {
        x = X;
        y = Y;
    }

    double x;
    double y;
};

double distance(Point, Point);

int main(int argc, const char *argv[]) {
    Point pointA(1, 5);
    Point pointB(2, 6);

    std::cout << "Distance between (" << pointA.x << "," << pointA.y << ")("<< pointB.x << "," << pointB.y
              << ") = " << distance(pointA, pointB) << std::endl;

    return 0;
}
// calculate and return the distance between two points
double distance(Point pointA, Point pointB) {
    double diffX = pointA.x - pointB.x;
    double diffY = pointA.y - pointB.y;

    return sqrt((diffY * diffY) + (diffX * diffX));
}

Result


Related Tutorials