#include #include using namespace std; struct position { float x; float y; }; // sqrt(a^2 + b^2) float distance_from_origin(position p){ return sqrtf((p.x * p.x) + (p.y * p.y)); } float distance_between(position a, position b){ float dx = a.x - b.x; float dy = a.y - b.y; return sqrtf((dx * dx) + (dy * dy)); } position add_positions(position a, position b){ position sum; sum.x = a.x + b.x; sum.y = a.y + b.y; return sum; } ostream& operator<<(ostream& os, position a){ os << "(" << a.x << ", " << a.y << ")"; return os; } position operator+(position a, position b){ position sum; sum.x = a.x + b.x; sum.y = a.y + b.y; return sum; } int main(){ position here; here.x = 100.2; here.y = 98.3; // another way to create these position there = {-72.1, 1.63}; cout << "here is " << distance_from_origin(here) << " from the origin\n"; cout << "It's " << distance_between(here, there) << " from here to there\n"; position together = add_positions(here, there); cout << "Added together, these are " << together.x << ", " << together.y << "\n"; cout << "Here is: " << here << "\n"; cout << "Here plus there is: " << here + there << "\n"; return 0; }