luogu1355 神秘大三角

题解:

计算几何入门题

按逆时针方向访问三角形的边

然后作叉积判断点是否在边的顺时针方向

叉积和点积都有分配率 但不满足结合律

代码:

#include <bits/stdc++.h>
using namespace std;
#define rint register int
#define IL inline
#define rep(i,h,t) for (int i=h;i<=t;i++)
#define dep(i,t,h) for (int i=t;i>=h;i--)
#define me(x) memset(x,0,sizeof(x))
#define mid ((h+t)>>1)
namespace IO{
  char ss[1<<24],*A=ss,*B=ss;
  IL char gc()
  {
    return A==B&&(B=(A=ss)+fread(ss,1,1<<24,stdin),A==B)?EOF:*A++;
  }
  template<class T>void read(T &x)
  {
    rint f=1,c; while (c=gc(),c<48||c>57) if (c=='-') f=-1; x=(c^48);
    while (c=gc(),c>47&&c<58) x=(x<<3)+(x<<1)+(c^48); x*=f; 
  }
};
using namespace IO;
struct Point{
  int x,y;
  Point(){};
  Point(int x1,int y1)
  {
    x=x1,y=y1;
  }
  Point operator +(const Point b)const
  {
    return Point(x+b.x,y+b.y);
  }
  Point operator -(const Point b)const
  {
    return Point(x-b.x,y-b.y);
  }
  int operator *(const Point b)const
  {
    return b.x*x+b.y*y; 
  }
  int operator ^(const Point b)const
  {
    return x*b.y-y*b.x;
  }
  bool operator ==(const Point b)const
  {
    if (y==b.y&&x==b.x) return(1); else return(0);
  }
};
struct Line{
  Point x,y;
  Line() {};
  Line(Point x1,Point y1)
  {
    x=x1,y=y1;
  }
};
int main()
{
  int x1,y1,x2,y2,x3,y3,x,y;
  Point p1,p2,p3,p;
  read(x1); read(y1); p1=Point(x1,y1); 
  read(x2); read(y2); p2=Point(x2,y2);
  read(x3); read(y3); p3=Point(x3,y3);
  read(x); read(y); p=Point(x,y); 
  if (((p3-p1)^(p2-p1))>0) swap(p3,p1); 
  if (p==p1||p==p2||p==p3)
  {
    cout<<4<<endl;
    exit(0);
  }
  int tt=1;
  if (((p3-p1)^(p-p1))>0) tt=2;
  if (((p3-p1)^(p-p1))==0) tt=3;
  if (((p1-p2)^(p-p2))>0) tt=2;
  if (((p1-p2)^(p-p2))==0) tt=3;
  if (((p2-p3)^(p-p3))>0) tt=2;
  if (((p2-p3)^(p-p3))==0) tt=3; 
  cout<<tt<<endl;
  return 0; 
}

 

posted @ 2018-11-19 22:01  尹吴潇  阅读(148)  评论(0编辑  收藏  举报