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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值