Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cstdio>
- #include <vector>
- #include <cmath>
- using namespace std;
- struct point {
- long double x;
- long double y;
- long double len() {
- return sqrt(x * x + y * y);
- }
- };
- point operator-(point a, point b) {
- point res;
- res.x = a.x - b.x;
- res.y = a.y - b.y;
- return res;
- }
- long double height(point a, point b, point c) {
- point base = a - c;
- long double s = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
- if (base.len() == 0) {
- return 0;
- }
- return (abs(s) / base.len());
- }
- int main() {
- point p, a, b;
- long double x, y, z;
- scanf("%Lf %Lf %Lf %Lf %Lf %Lf", &p.x, &p.y, &a.x, &a.y, &b.x, &b.y);
- if (p.x == a.x && p.x == b.x) {
- if ((a.y < p.y && b.y > p.y) || (b.y < p.y && a.y > p.y)) {
- printf("0");
- } else {
- printf("%.5Lf", min(abs(p.y - a.y), abs(p.y - b.y)));
- }
- return 0;
- }
- if (p.y == a.y && p.y == b.y) {
- if ((a.x < p.x && b.x > p.x) || (b.x < p.x && a.x > p.x)) {
- printf("0");
- } else {
- printf("%.5Lf", min(abs(p.x - a.x), abs(p.x - b.x)));
- }
- return 0;
- }
- x = (p - b).len() * (p - b).len();
- y = (p - a).len() * (p - a).len();
- z = (a - b).len() * (a - b).len();
- if (x <= y + z && y <= x + z) {
- printf("%.5Lf", height(a, p, b));
- } else {
- printf("%.5Lf", sqrt(min(x, y)));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement