Junior — Middle
How can you determine the distance between two points on a display screen?
sobes.tech AI
Answer from AI
To determine the distance between two points on a screen, given their coordinates (x1, y1) and (x2, y2), the Euclidean distance formula is used:
[ \text{distance} = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} ]
Example in C++:
#include <cmath>
#include <iostream>
struct Point {
int x;
int y;
};
double distance(const Point& p1, const Point& p2) {
int dx = p2.x - p1.x;
int dy = p2.y - p1.y;
return std::sqrt(dx * dx + dy * dy);
}
int main() {
Point a{10, 20};
Point b{30, 40};
std::cout << "Distance: " << distance(a, b) << std::endl;
return 0;
}
This distance is in pixels or units of screen coordinates.