三點順序
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
樣例輸出
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");
}
}