Ural_1348. Goat in the Garden 2(计算几何)

  乱搞题,就是看仔细不仔细了。代码很水,分好几种情况。

c所在的位置分三种情况,分别计算;ab那条线段也可能是平行x轴或y轴,也分别计算。

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define pi 3.1415926535

using namespace std;

struct point{
double x;
double y;
};

int Abs(double x){
return x > 0 ? x : -x;
}

int main(){
//freopen("data.in", "r", stdin);

double l, A, B, C, k, t;
double d1, d2, y;
point a, b, c, tmp;

scanf("%lf%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y, &l);

if(a.y > b.y) {tmp = a; a = b; b = tmp;}

A = b.y - a.y; B = a.x - b.x;
C = a.y*(b.x - a.x) - a.x*(b.y - a.y);

d1 = sqrt((c.y - a.y)*(c.y - a.y) + (c.x - a.x)*(c.x - a.x));
d2 = sqrt((c.y - b.y)*(c.y - b.y) + (c.x - b.x)*(c.x - b.x));

if(a.y == b.y){
if(a.x < b.x) {tmp = a; a = b; b = tmp;}
if(c.x > a.x || c.x < b.x){
d1 -= l; d2 -= l;
if(d2 < d1) {t = d1; d1 = d2; d2 = t;}
printf("%.2lf\n%.2lf\n", d1>0?d1:0, d2>0?d2:0);
} else {
d1 -= l; d2 -= l; d2 = d2 > d1 ? d2 : d1;
d1 = Abs(c.y - a.y);
d1 -= l;
printf("%.2lf\n%.2lf\n", d1>0?d1:0, d2>0?d2:0);
}
}else if(a.x == b.x){
if(c.y < a.y || c.y > b.y){
d1 -= l; d2 -= l;
if(d2 < d1) {t = d1; d1 = d2; d2 = t;}
printf("%.2lf\n%.2lf\n", d1>0?d1:0, d2>0?d2:0);
}else{
d1 -= l; d2 -= l; d2 = d2 > d1 ? d2 : d1;
d1 = Abs(c.x - a.x);
d1 -= l;
printf("%.2lf\n%.2lf\n", d1>0?d1:0, d2>0?d2:0);
}
}else{
k = (a.y - b.y)/(a.x - b.x);
k = -1/k;
y = k*c.x + a.y - k*a.x;
if(c.y < y){
d1 -= l; d2 -= l;
if(d2 < d1) {t = d1; d1 = d2; d2 = t;}
printf("%.2lf\n%.2lf\n", d1>0?d1:0, d2>0?d2:0);
}else{
y = k*c.x - b.y - k*b.x;
if(c.y > y){
d1 -= l; d2 -= l;
if(d2 < d1) {t = d1; d1 = d2; d2 = t;}
printf("%.2lf\n%.2lf\n", d1>0?d1:0, d2>0?d2:0);
}else{
d1 -= l; d2 -= l; d2 = d2 > d1 ? d2 : d1;

d1 = Abs(A*c.x + B*c.y + C)/sqrt(A*A + B*B);
d1 -= l;
printf("%.2lf\n%.2lf\n", d1>0?d1:0, d2>0?d2:0);
}
}
}
return 0;
}



原文地址:https://www.cnblogs.com/vongang/p/2228184.html