http://blog.csdn.net/pzler/article/details/21123479
计算几何是一门几何学,研究图形几何性质的学科
计算几何也是一门计算科学,研究几何的算法性质引入计算几何,让计算机学会处理几何问题
向量代数:
叉积的二维意义:有向面积:外积大小为U,V这两个向量围成的平行四边形有向面积
叉积实现:
定义point结构体
struct point{double x,y;}point;
double det(double x1,double y1,doublex2,double y2)
{
return x1*y2-x2*y1;
}
灰太狼拯救大白菜
话说,由于灰太狼抓羊无术,为了和老婆填饱肚子,于是他决定种大白菜吃- -!
可是,冬天就快要到了,如果不采取什么措施,白菜就会冻死···
为此,聪明的灰太狼,发明了一种神奇的东西----“半圆形大棚”!
这种神奇的东西可以让大白菜在冬天也能够健康成长,可是灰太狼为了防止羊群们来偷吃,将大白菜种的都很分散。
经过仔细测量,灰太狼统计出了所有大白菜的坐标,并且经过三天三夜的计算,灰太狼确定出圆心的坐标和“半圆形大棚”的半径,但是他却不知道最多能拯救多少棵大白菜。
基本思路:
1.到圆心的距离大于半径的点直接排除。
2.以圆心和任意一点确定一 有向线段作为半径位置,分别计数该有向线段左边点的个数(nl)和右边点的个数(nr)。
3.重复步骤2直到所有点都被枚举 过。
4.枚举过程中出现的最大的nl或
nr就是所求的结果。
struct Point{
double x , y;
Point(double x=0,double y=0):x(x),y(y) {}//构造函数,方便代码编写
};
typedef Point Vector ;//从程序实现上,Vector只是Point的别名
//向量+向量=向量,点+向量=点
Vector operator + (Vector A , Vector B){
return Vector(A.x + B.x , A.y + B.y);
}
//点-点=向量
Vector operator - (Point A , Point B){
return Vector(A.x - B.x , A.y - B.y);
}
//向量*数=向量
Vector operator * (Vector A , double p){
return Vector(A.x * p, A.y * p);
}
//向量/数=向量
Vector operator / (Vector A , double p){
return Vector(A.x / p, A.y / p);
}
bool operator < (const Point& a, const Point& b){
return a.x<b.x||(a.x==b.x && a.y<b.y);
}
const double eps = 1e-10;
int dcmp(double x){
if(fabs(x) < eps) return 0;else return x<0 ?-1:1;
}
bool operator == (const Point& a, const Point& b){
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
向量(x,y)极角=atan2(y,x); //单位弧度
//两向量点积
double Dot(Vector A , Vector B){
return A.x*B.x+A.y*B.y;
}
//向量长度
double Length(Vector A){
return sqrt(Dot(A,A));
}
//两向量转角
double Angle(Vector A , Vector B){
return acos(Dot(A, B) / Length(A) / Length(B));
}
//向量叉积
double Cross(Vector A , Vector B){
return A.x*B.y - A.y*B.x;
}
//三角形有向面积两倍
double Area2(Point A ,Point B ,Point C){
return Cross(B-A, C-A);
}
//向量绕起点旋转 rad是弧度
Vector Rotate(Vector A, double rad){
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
//向量单位法向量
Vector Normal(Vector A){
double L = Length(A);
return Vector(-A.y/L , A.x/L);
}
//二直线交点(参数式)
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
Vector u = P-Q;
double t = Cross(w,u)/Cross(v,w);
return P+v*t;
}
//点到直线距离
double DistanceToLine(Point P, Point A, Point B){
Vector v1 = B-A, v2 = P - A;
return fabs(Cross(v1,v2) / Length(v1));
}
//点到线段距离
double DistanceToSegment(Point P, Point A, Point B){
if(A==B) return Length(P-A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if(dcmp(Dot(v1,v2)) < 0) return Length(v2);
else if if(dcmp(Dot(v1,v3)) > 0) return Length(v3);
else return fabs(Cross(v1,v2)) / Length(v1);
}
//多边形面积
double ConvexPolygonArea(Point* p, int n){
double area = 0;
for(int i = 1;i < n-1; i++)
area +=Cross(p[i]-p[0],p[i+1]-p[0]);
return area/2;
}
线段相交
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef double PointType;
struct point
{
PointType x,y;
};
PointType Direction(point pi,point pj,point pk) //判断向量PiPj在向量PiPk的顺逆时针方向 +顺-逆0共线
{
return (pj.x-pi.x)*(pk.y-pi.y)-(pk.x-pi.x)*(pj.y-pi.y);
}
bool On_Segment(point pi,point pj,point pk)
{
if(pk.x>=min(pi.x,pj.x)&&pk.x<=max(pi.x,pj.x)&&pk.y>=min(pi.y,pj.y)&&pk.y<=max(pi.y,pj.y))
return 1;
return 0;
}
bool Segment_Intersect(point p1,point p2,point p3,point p4)
{
PointType d1=Direction(p3,p4,p1),d2=Direction(p3,p4,p2),d3=Direction(p1,p2,p3),d4=Direction(p1,p2,p4);
if(((d1>0&&d2<0)||(d1<0&&d2>0))&&((d3>0&&d4<0)||(d3<0&&d4>0)))
return 1;
if(d1==0&&On_Segment(p3,p4,p1))
return 1;
if(d2==0&&On_Segment(p3,p4,p2))
return 1;
if(d3==0&&On_Segment(p1,p2,p3))
return 1;
if(d4==0&&On_Segment(p1,p2,p4))
return 1;
return 0;
}
凸包
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef double PointType;
struct point
{
PointType x,y;
int num;
};
point data[1005],stack[1005],MinA;
int top;
PointType Direction(point pi,point pj,point pk) //判断向量PiPj在向量PiPk的顺逆时针方向 +顺-逆0共线
{
return (pj.x-pi.x)*(pk.y-pi.y)-(pk.x-pi.x)*(pj.y-pi.y);
}
PointType Dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(point a,point b)
{
PointType k=Direction(MinA,a,b);
if(k>0) return 1;
if(k<0) return 0;
return Dis(MinA,a)>Dis(MinA,b);
}
void Graham_Scan(point *a,int numa)
{
for(int i=0; i<numa; i++)
if(a[i].y<a[0].y||(a[i].y==a[0].y&&a[i].x<a[0].x))
swap(a[i],a[0]);
MinA=a[0],top=0;
sort(a+1,a+numa,cmp);
stack[top++]=a[0],stack[top++]=a[1],stack[top++]=a[2];
for(int i=3; i<numa; i++)
{
while(Direction(stack[top-2],stack[top-1],a[i])<0&&top>=2)
top--;
stack[top++]=a[i];
}
}
int main()
{
int n;
double r,pi=3.141592654;
while(~scanf("%d%lf",&n,&r))
{
double sum=0;
for(int i=0; i<n; i++)
scanf("%lf%lf",&data[i].x,&data[i].y);
Graham_Scan(data,n);
for(int i=1; i<top; i++)
sum+=Dis(stack[i],stack[i-1]);
sum+=Dis(stack[0],stack[top-1]);
sum+=pi*r*2;
printf("%.0fn",sum);
}
return 0;
}