您的位置:首页 > 其它

向量的叉积 凸包

2013-08-07 20:38 155 查看
向量的叉积(凸包)1. 向量的叉积的模表示这两个向量围成的平行四边形的面积。 设矢量P = ( x1, y1 ),Q = ( x2, y2 ),则矢量叉积定义为由(0,0)、p1、p2和p1+p2所组成的平行四边形的带符号的面积,即:P×Q = x1*y2 - x2*y1,其结果是一个伪矢量。 显然有性质 P × Q = - ( Q × P ) 和 P × ( - Q ) = - ( P × Q )。
2. 叉积的一个非常重要性质是可以通过它的符号判断两矢量相互之间的顺逆时针关系: 若 P × Q > 0 , 则P在Q的顺时针方向。 若 P × Q < 0 , 则P在Q的逆时针方向。 若 P × Q = 0 , 则P与Q共线,但可能同向也可能反向。
3. 叉积的方向与进行叉积的两个向量都垂直,所以叉积向量即为这两个向量构成平面的法向量。
4. 如果向量叉积为零向量,那么这两个向量是平行关系。 因为向量叉积是这两个向量平面的法向量,如果两个向量平行无法形成一个平面,其对应也没有平面法向量。所以,两个向量平行时,其向量叉积为零。
例子:
给定一个点集,求点积中离得最远的两个点的距离!
#include<iostream>
#include<cmath>
#include<cstdio>
#include<stack>
using namespace std;
#define exp 0.0000000001
typedef struct point
{
int x,y;
double degree;
}rr;
point a[50005];
int n;
int b[50005],Max;
int cmp(const void *a,const void *b)
{
point *c=(point *)a;
point *d=(point *)b;
if(fabs(c->degree-d->degree)>exp)
return c->degree>d->degree?1:-1;
else//相等的情况
return c->x-d->x;//升序排列
}
void Sort()
{
int i,j,x=INT_MAX,y=INT_MAX;
for(i=0; i<n; i++)
{
if(a[i].y<y)
{
y=a[i].y;j=i;x=a[i].x;
}
else if(a[i].y==y && a[i].x<x)
{
y=a[i].y;j=i;x=a[i].x;
}
}//找到最小的
a[j].x=a[0].x;a[j].y=a[0].y;
a[0].x=x;a[0].y=y;//j和0的位置进行互换的
for(i=1; i<n; i++)
{
a[i].degree=acos(((double)(a[i].x-x))/(sqrt(pow((double)(a[i].x-x),2)+pow((double)(a[i].y-y),2))));
}
qsort(&a[1],n-1,sizeof(point),cmp);//对其进行升序排列的
// for(i=0; i<n; i++)
// printf("%d %d %lf\n",a[i].x,a[i].y,a[i].degree);
}
double judge(point s,point t,point i)
{
return (t.x-s.x)*(i.y-s.y)-(t.y-s.y)*(i.x-s.x);
}
void fun()
{
int i,s,t;
stack<int>q;
q.push(0);
q.push(1);//先进去两个的
for(i=2; i<n; i++)
{
t=q.top();
q.pop();
if(!q.empty())
{
while(!q.empty())
{
s=q.top();
if(judge(a[s],a[t],a[i])<-exp)//将t删除
{
q.pop();
t=s;
if(q.empty())//为空
{
q.push(t);//将t放入
break;
}
s=q.top();
}
else
{
q.push(t);//将原来的还放入的
break;
}
}
}
else
{
q.push(t);
}
q.push(i);
}
i=0;
while(!q.empty())
{
b[i++]=q.top();
q.pop();
}
n=i;//返回的是啊
return ;
}
void len()
{
int i,j;
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
int s=(a[b[j]].x-a[b[i]].x)*(a[b[j]].x-a[b[i]].x)+(a[b[j]].y-a[b[i]].y)*(a[b[j]].y-a[b[i]].y);
if(s>Max)
Max=s;
}
}
}
int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
for(i=0; i<n; i++)
scanf("%d%d",&a[i].x,&a[i].y);
Sort();
fun();
Max=0;
len();
printf("%d\n",Max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: