In: Computer Science
Define a structure Point. A point has an x- and a y-coordinate. Write a function double distance(Point a, Point b) that computes the distance from a to b. Write a program that reads the coordinates of the points, calls your function, and displays the result. IN C++
if possible please give //comments
// do comment if any problem arises
// code
#include <iostream>
#include <cmath>
using namespace std;
struct Point
{
int x, y;
};
// this function calculates
double distance(Point a, Point b)
{
// (x1-x2)^2 + (y1-y2)^2
double d = pow(a.x - b.x, 2) + pow(a.y - b.y, 2);
// squareroot of above
return sqrt(d);
}
int main()
{
Point a, b;
// 1,1
a.x = 1;
a.y = 1;
// 3,3
b.x = 4;
b.y = 5;
cout << "Distance between (" << a.x << "," << a.y << ") and (" << b.x << "," << b.y << ") is: " << distance(a, b);
}
Output: