Skip to content

Instantly share code, notes, and snippets.

@frp
Created February 4, 2017 18:44
Show Gist options
  • Select an option

  • Save frp/6457a149e8cd12e885a79b56f8255584 to your computer and use it in GitHub Desktop.

Select an option

Save frp/6457a149e8cd12e885a79b56f8255584 to your computer and use it in GitHub Desktop.
#include <fstream>
#include <cmath>
using namespace std;
double sqr(double x){return x*x;}
double len(double x1,double y1,double x2,double y2)
{
return sqrt(sqr(x2-x1)+sqr(y2-y1));
}
#define eps 1e-4
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/*double sq_rect(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
return len(x1,y1,x2,y2)*len(x2,y2,x3,y3);
}
double sq_tri(double x1,double y1,double x2,double y2,double x3,double y3)
{
double a=len(x1,y1,x2,y2),b=len(x2,y2,x3,y3),c=len(x3,y3,x1,y1),p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}*/
bool intersect(double x01,double y01,double x02,double y02,double x11,double y11,double x12,double y12)
{
double A1=y01-y02,B1=x02-x01,A2=y11-y12,B2=x12-x11;
double C1=-(A1*x01+B1*y01),C2=-(A2*x11+B2*y11);
double d=A1*B2-A2*B1,dx=C1*B2-C2*B1,dy=A1*C2-A2*C1;
if(abs(d)<eps)
if(abs(dy)<eps && abs(B1*C2-B2*C1)<eps)return true;
else return false;
double x=-dx/d,y=-dy/d;
if(x>x11)return false;
if(abs(len(x01,y01,x,y)+len(x,y,x02,y02)-len(x01,y01,x02,y02))<eps)return true;
return false;
}
bool online(double x,double y,double x1,double y1,double x2,double y2)
{
return abs(len(x,y,x1,y1)+len(x,y,x2,y2)-len(x1,y1,x2,y2))<eps;
}
bool check(double x,double y,double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
/*return abs(sq_tri(x,y,x1,y1,x2,y2)+sq_tri(x,y,x2,y2,x3,y3)+sq_tri(x,y,x3,y3,x4,y4)+sq_tri(x,y,x4,y4,x1,y1)-
sq_rect(x1,y1,x2,y2,x3,y3,x4,y4))<=1e-9;*/
if(online(x,y,x1,y1,x2,y2) || online(x,y,x2,y2,x3,y3) || online(x,y,x3,y3,x4,y4) || online(x,y,x4,y4,x1,y1))return true;
/*int k=0;
if(intersect(x1,y1,x2,y2,x,y,x-1,y))k++;
if(intersect(x2,y2,x3,y3,x,y,x-1,y))k++;
if(intersect(x3,y3,x4,y4,x,y,x-1,y))k++;
if(intersect(x4,y4,x1,y1,x,y,x-1,y))k++;
if(y1==y && x1<=x)k--;
if(y2==y && x2<=x)k--;
if(y3==y && x3<=x)k--;
if(y4==y && x4<=x)k--;
return k%2!=0;*/
double a=len(x1,y1,x2,y2),b=len(x2,y2,x3,y3),c=len(x3,y3,x4,y4),d=len(x4,y4,x1,y1);
double cosa1=(sqr(a)-sqr(len(x1,y1,x,y))-sqr(len(x2,y2,x,y)))/(-2*len(x1,y1,x,y)*len(x2,y2,x,y));
double cosa2=(sqr(b)-sqr(len(x2,y2,x,y))-sqr(len(x3,y3,x,y)))/(-2*len(x2,y2,x,y)*len(x3,y3,x,y));
double cosa3=(sqr(c)-sqr(len(x3,y3,x,y))-sqr(len(x4,y4,x,y)))/(-2*len(x3,y3,x,y)*len(x4,y4,x,y));
double cosa4=(sqr(d)-sqr(len(x4,y4,x,y))-sqr(len(x1,y1,x,y)))/(-2*len(x4,y4,x,y)*len(x1,y1,x,y));
double ang=(acos(cosa1)+acos(cosa2)+acos(cosa3)+acos(cosa4))*180/M_PI;
return abs((acos(cosa1)+acos(cosa2)+acos(cosa3)+acos(cosa4))*180/M_PI-360)<eps;
}
int main()
{
ifstream in("input.txt");
int x,y,x1,y1,x2,y2,x3,y3,x4,y4,n,k=0;
in>>n;
for(int i=0;i<n;i++)
{
in>>x>>y>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
if(check(x,y,x1,y1,x2,y2,x3,y3,x4,y4))k++;
}
in.close();
ofstream out("output.txt");
out<<k<<'\n';
out.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment