HDU - 2671(计算几何)
2017-09-02 20:17
211 查看
问题描述:
I'm sure this problem will fit you as long as you didn't sleep in your High School Math classes.
Yes,it just need a little math knowledge and I know girls are always smarter than we expeted.
So don't hesitate any more,come and AC it!
Tell you three point A,B,C in a 2-D plain,then a line L with a slope K pass through C,you are going to find
a point P on L,that makes |AP| + |PB| minimal.
Input
The first line contain a t.
Then t cases followed,each case has two parts,the first part is a real number K,indicating the slope,and the second
part are three pairs of integers Ax,Ay,Bx,By,Cx,Cy(0 <=|Ax|,|Ay|,|Bx|,|By|,|Cx|,|Cy| <= 10000 ).
Output
Just out put the minimal |AP| + |PB|(accurate to two places of decimals ).
Sample Input
Sample Output
问题描述:题目给我们一个斜率K,三个点(A,B,C)的坐标,让我们在一条直线(斜率为K,过C点),找到一点P使得|AP|+|BP|最小。
题目分析:我们先求出直线L的方程,然后根据直线方程来判断A与B是否同侧(判断方法就是:将点A,B代入直线方程中算它们的结果的积如果小于0,异测,等于0,至少有一点在直线上,大于0,同侧,判断原理就是:点在直线上方结果大于0,下方小于0,中等于0);
如果异侧:则直接求A与B的距离
如果同侧:则求出A点(B点也可)关于直线L对称的点D,求出B与D 的距离。(求D点时,可以先求出B与D的中点mid(联立方程即可))
代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
struct Point
{
double x,y;
};
double get_dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
int t;
scanf("%d",&t);
while (t--) {
double k;
Point a,b,c;
scanf("%lf",&k);
scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
double A=k*a.x+c.y-k*c.x-a.y;
double B=k*b.x+c.y-k*c.x-b.y;
if (A*B<=0) {
printf("%.2f\n",get_dis(a,b));
}
else {
if (k==0) {//当k等于0的时候,中点公式
Point d;
d.x=a.x;
d.y=2*c.y-a.y;
printf("%.2f\n",get_dis(b,d));
}
else {
double k1,k2,b1,b2;
Point mid,d;
k1=k,k2=-1.0/k;
b1=c.y-k*c.x;
b2=a.y+a.x/k;
mid.x=(b2-b1)/(k1-k2);//中点
mid.y=k2*mid.x+b2;
d.x=2*mid.x-a.x;
d.y=2*mid.y-a.y;
printf("%.2f\n",get_dis(b,d));
}
}
}
return 0;
}
I'm sure this problem will fit you as long as you didn't sleep in your High School Math classes.
Yes,it just need a little math knowledge and I know girls are always smarter than we expeted.
So don't hesitate any more,come and AC it!
Tell you three point A,B,C in a 2-D plain,then a line L with a slope K pass through C,you are going to find
a point P on L,that makes |AP| + |PB| minimal.
Input
The first line contain a t.
Then t cases followed,each case has two parts,the first part is a real number K,indicating the slope,and the second
part are three pairs of integers Ax,Ay,Bx,By,Cx,Cy(0 <=|Ax|,|Ay|,|Bx|,|By|,|Cx|,|Cy| <= 10000 ).
Output
Just out put the minimal |AP| + |PB|(accurate to two places of decimals ).
Sample Input
1 2.55 8467 6334 6500 9169 5724 1478
Sample Output
3450.55
问题描述:题目给我们一个斜率K,三个点(A,B,C)的坐标,让我们在一条直线(斜率为K,过C点),找到一点P使得|AP|+|BP|最小。
题目分析:我们先求出直线L的方程,然后根据直线方程来判断A与B是否同侧(判断方法就是:将点A,B代入直线方程中算它们的结果的积如果小于0,异测,等于0,至少有一点在直线上,大于0,同侧,判断原理就是:点在直线上方结果大于0,下方小于0,中等于0);
如果异侧:则直接求A与B的距离
如果同侧:则求出A点(B点也可)关于直线L对称的点D,求出B与D 的距离。(求D点时,可以先求出B与D的中点mid(联立方程即可))
代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
struct Point
{
double x,y;
};
double get_dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
int t;
scanf("%d",&t);
while (t--) {
double k;
Point a,b,c;
scanf("%lf",&k);
scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
double A=k*a.x+c.y-k*c.x-a.y;
double B=k*b.x+c.y-k*c.x-b.y;
if (A*B<=0) {
printf("%.2f\n",get_dis(a,b));
}
else {
if (k==0) {//当k等于0的时候,中点公式
Point d;
d.x=a.x;
d.y=2*c.y-a.y;
printf("%.2f\n",get_dis(b,d));
}
else {
double k1,k2,b1,b2;
Point mid,d;
k1=k,k2=-1.0/k;
b1=c.y-k*c.x;
b2=a.y+a.x/k;
mid.x=(b2-b1)/(k1-k2);//中点
mid.y=k2*mid.x+b2;
d.x=2*mid.x-a.x;
d.y=2*mid.y-a.y;
printf("%.2f\n",get_dis(b,d));
}
}
}
return 0;
}
相关文章推荐
- HDU 3532 Max Angle(计算几何——极角排序)
- hdu 2036 计算几何求面积
- HDU 4643 GSM (2013多校5 1001题 计算几何)
- HDU 4720 Naive and Silly Muggles (简单计算几何)
- HDU 1115 Lifting the Stone(计算几何)
- hdu 1966 /poj 2066 Minimax Triangulation 计算几何
- hdu-1221--Rectangle and Circle(计算几何)
- HDU 5572 An Easy Physics Problem (计算几何 点类 线类 向量类 线段与圆相交)
- HDU 4449 Building Design 第37届ACM/ICPC 金华赛区H题 (计算几何,三维凸包+空间坐标旋转+二维凸包)
- [HDU 4741]Save Labman No.004[计算几何][精度]
- (2017多校2)1011/hdu-6055 Regular polygon(计算几何)
- hdu6206—Apple(计算几何+高精度)
- hdu 1077 Catching Fish(计算几何)
- HDU 1174 爆头(三维计算几何)
- 计算几何 凸包 hdoj2108 Shape of HDU
- HDU 1086 You can Solve a Geometry Problem too(计算几何)
- HDU-4823(计算几何)
- HDU 6158 The Designer【计算几何+笛卡尔定理+韦达定理】
- hdu 3471 (tju 3554) England vs Germany (计算几何)
- HDU 5572 2015 上海区域赛 A题 计算几何(碰撞反弹、注意精度)