判断三个点共线,只需要其两个乘积相等即可。
两个向量必须是有同一个点,比如ab = bc
若ab=(2,3),bc=(3,1);
if(2 * 1 == 3 * 3)
则是a,b,c三点共线
若有错误,欢迎评论
int res1 = x[a] - x[b];
int res2 = x[b] - x[c];
int res3 = x[c] - x[a];
int yes1 = y[a] - y[b];
int yes2 = y[b] - y[c];
int yes3 = y[c] - y[a];
int p = res1 * res2 + yes1 * yes2;
int q = res1 * res3 + yes1 * yes3;
int t = res2 * res3 + yes2 * yes3;
if( res1 * yes2 == res2 * yes1)
return 1;
#include<iostream>
using namespace std;
int x[600];
int y[600];
int Angle(int a,int b,int c)
{
int res1 = x[a] - x[b];
int res2 = x[b] - x[c];
int res3 = x[c] - x[a];
int yes1 = y[a] - y[b];
int yes2 = y[b] - y[c];
int yes3 = y[c] - y[a];
int p = res1 * res2 + yes1 * yes2;
int q = res1 * res3 + yes1 * yes3;
int t = res2 * res3 + yes2 * yes3;//皆为外角
if( res1 * yes2 == res2 * yes1)
return 1;
if( p > 0 || q > 0 || t > 0)
return -1;
return 1;
}
int main()
{
int n;
int count =0;
cin >> n;
for(int i=0;i<n;i++) cin >> x[i] >> y[i];
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if(Angle(i,j,k) < 0 )
count++;
}
}
}
cout << count;
return 0;
}