三点顺序
1000 ms | 内存限制: 65535
3
现在给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,现在让你判断A,B,C是顺时针给出的还是逆时针给出的?
如:
图1:顺时针给出
图2:逆时针给出
<图1> <图2>
每行是一组测试数据,有6个整数x1,y1,x2,y2,x3,y3分别表示A,B,C三个点的横纵坐标。(坐标值都在0到10000之间)
输入0 0 0 0 0 0表示输入结束
测试数据不超过10000组
输出
如果这三个点是顺时针给出的,请输出1,逆时针给出则输出0
样例输入
0 0 1 1 1 3 0 1 1 0 0 0 0 0 0 0 0 0
样例输出
0 1
思路:
最简单:
如果AB*AC=0,则说明三点共线。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
double x1, y1, x2, y2, x3, y3;
scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);
while(x1 != 0 || y1 != 0 || x2 != 0 || y2 != 0 || x3 != 0 || y3 != 0) {
if((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1) > 0) cout << "0" << endl;
else cout << "1" << endl;
scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);
}
return 0;
}
AC2:
#include <iostream>
#include<cstdio>
using namespace std;
struct point
{
double x,y;
};
double multi(point p0, point p1, point p2)
{
if ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y)>=0)
return 0;
else return 1;
}
int main()
{
point a,b,c;double s;
while(cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y)
{
if(a.x==0&&a.y==0&&b.x==0&&b.y==0&&c.x==0&&c.y==0)
break;
else
s=multi(a,b,c);
cout<<s<<endl;
}
return 0;
}
可以用向量积,判断向量积的的第三个坐标的正负,可以判断是顺时针或者逆时针,不懂?百度向量积.....
#include<stdio.h>
#include<string.h>
struct zb
{
int x,y,z;
}fx[2];
int is_tri(zb a,zb b)//向量积的结果
{
int c=a.x*b.y+a.y*b.z+a.z*b.x;
int d=a.x*b.z+a.y*b.x+a.z*b.y;
return c-d;
}
int main()
{
int i,x[4],y[4],kase;
while(~scanf("%d%d",&x[0],&y[0]))
{
for(i=1;i<3;++i)
{
scanf("%d%d",&x[i],&y[i]);
}
for(i=1;i<3;++i)//计算向量
{
fx[i/2].x=x[i]-x[i-1];
fx[i/2].y=y[i]-y[i-1];
fx[i/2].z=0;
}
kase=is_tri(fx[0],fx[1]);
if(kase)//如果结果不为 0
{
if(kase>0)//为正,代表逆时针
{
printf("0\n");
continue;
}
printf("1\n");//否则,顺时针
continue;
}
return 0;//为零,则证明要退出了
}
return 0;
}
第二种:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
double x1,y1,x2,y2,x3,y3,k,b,con1,con2,con3;
while(cin>>x1>>y1>>x2>>y2>>x3>>y3)
{
if(x1==0 && y1==0 && x2==0 && y2==0 && x3==0 && y3==0) break;
if(x1==x2)
{
if(y1>y2) printf(x3>x1?"0\n":"1\n");
else printf(x3>x1?"1\n":"0\n");
continue;
}
k=(y1-y2)/(x1-x2);
b=y1-k*x1;
con1=x1<x2?1:0;
con2=(k*x3+b>y3)?1:0;
if(con1)
printf(con2?"1\n":"0\n");
else
printf(con2?"0\n":"1\n");
}
}