HDU-Rectangle and Circle
2012-05-09 22:48
381 查看
点击打开链接
这个是一个模板题,谢谢岩儿提供的模板。
疑问:直线到点的距离模板 和 线段的到点距离的模板有什么区别?应该怎么写?
Trick:我自己使用的一个for循环中每一个边必须是矩形的边而不应该是对角线,所以pp数组的顺序是固定的。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const double eps = 1e-10;
typedef struct
{
double x, y;
}point;
double dist(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double xmult(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
//点到直线的距离
double disptoline(point p,point l1,point l2)
{
return fabs(xmult(p,l1,l2))/dist(l1,l2);
}
//相交返回1,不想交返回0
int intersect_seg_circle(point c,double r, point l1,point l2)
{
double t1=dist(c,l1)-r,t2=dist(c,l2)-r;
point t=c;
if (t1<eps||t2<eps)
return t1>-eps||t2>-eps;
t.x+=l1.y-l2.y;
t.y+=l2.x-l1.x;
return xmult(l1,c,t)*xmult(l2,c,t)<eps&&disptoline(c,l1,l2)-r<eps;
}
point pp[4];
point cc;
int main()
{
int Case;
double X, Y, R, X1, Y1, X2, Y2;
cin>>Case;
while( Case-- )
{
cin>>X>>Y>>R>>X1>>Y1>>X2>>Y2;
pp[0].x = X1;
pp[0].y = Y1;
pp[1].x = X1;
pp[1].y = Y2;
pp[2].x = X2;
pp[2].y = Y2;
pp[3].x = X2;
pp[3].y = Y1;
cc.x = X;
cc.y = Y;
bool flag = false;
for(int i = 0; i < 4; i++)
{
if( intersect_seg_circle(cc, R, pp[i % 4], pp[(i+1) % 4]) ) { flag = true; break; }
}
if( flag ) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
这个是一个模板题,谢谢岩儿提供的模板。
疑问:直线到点的距离模板 和 线段的到点距离的模板有什么区别?应该怎么写?
Trick:我自己使用的一个for循环中每一个边必须是矩形的边而不应该是对角线,所以pp数组的顺序是固定的。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const double eps = 1e-10;
typedef struct
{
double x, y;
}point;
double dist(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double xmult(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
//点到直线的距离
double disptoline(point p,point l1,point l2)
{
return fabs(xmult(p,l1,l2))/dist(l1,l2);
}
//相交返回1,不想交返回0
int intersect_seg_circle(point c,double r, point l1,point l2)
{
double t1=dist(c,l1)-r,t2=dist(c,l2)-r;
point t=c;
if (t1<eps||t2<eps)
return t1>-eps||t2>-eps;
t.x+=l1.y-l2.y;
t.y+=l2.x-l1.x;
return xmult(l1,c,t)*xmult(l2,c,t)<eps&&disptoline(c,l1,l2)-r<eps;
}
point pp[4];
point cc;
int main()
{
int Case;
double X, Y, R, X1, Y1, X2, Y2;
cin>>Case;
while( Case-- )
{
cin>>X>>Y>>R>>X1>>Y1>>X2>>Y2;
pp[0].x = X1;
pp[0].y = Y1;
pp[1].x = X1;
pp[1].y = Y2;
pp[2].x = X2;
pp[2].y = Y2;
pp[3].x = X2;
pp[3].y = Y1;
cc.x = X;
cc.y = Y;
bool flag = false;
for(int i = 0; i < 4; i++)
{
if( intersect_seg_circle(cc, R, pp[i % 4], pp[(i+1) % 4]) ) { flag = true; break; }
}
if( flag ) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
相关文章推荐
- HDU1221---Rectangle and Circle HDU(94)
- HDU 1221 Rectangle and Circle(判定圆与矩形是否相交)
- HDU 1221 Rectangle and Circle
- hdu 1221 Rectangle and Circle(矩形与圆焦点问题)
- HDU 1221 Rectangle and Circle
- HDU 1221 Rectangle and Circle(判定圆与矩形是否相交)
- HDU 1221 Rectangle and Circle(判断圆与矩形是否相交)
- Rectangle and Circle_hdu_1221(数学几何).java
- 【数学】hdu 1221 Rectangle and Circle
- hdu-1221--Rectangle and Circle(计算几何)
- LightOJ 1130 Intersection between Circle and Rectangle
- 有史以来最奇葩的事情发生了!!lightOJ 1130 Intersection between Circle and Rectangle
- 判断圆和矩形是否相交C - Rectangle and Circle
- hdoj 1221 Rectangle and Circle(判点的位置)
- 计算几何-hdoj-1221-Rectangle and Circle
- hdu 1028 Ignatius and the Princess III
- HDU 5226 Tom and matrix
- hdu Monkey and Banana(DP)
- Hdu1312 Red and Black 【简单dfs】
- hdu-1312-Red and Black