--Originally published at World's Fanciest Cursors
Howdy. Today I’m sharing a cute little program that calculates the distance between two points in a Cartesian plane. Not much new to see. There’s only a lonely function and the use of squares and roots in a Pythagorean equation.
Without further ado here’s the link to GitHub and here’s the holy noodles:
#include <iostream> #include <cmath> float distance (float, float, float, float); int main () { float x1 = 0, y1 = 0, x2 = 0, y2 = 0; std::cout << "X uno, puh-leeze" << std::endl; std::cin >> x1; std::cout << "Y uno, puh-leeze" << std::endl; std::cin >> y1; std::cout << "X dos, puh-leeze" << std::endl; std::cin >> x2; std::cout << "Y dos, puh-leeze" << std::endl; std::cin >> y2; std::cout << "\nThe distance is " << distance (x1, y1, x2, y2) << std::endl; std::cout << "\nBuh-bye" << std::endl; } float distance (float x1, float y1, float x2, float y2) { float delta_x = 0, delta_y = 0, rdistance = 0; delta_x = x2 - x1; delta_y = y2 - y1; rdistance = sqrt(pow(delta_x, 2) + pow(delta_y, 2)); return rdistance; }