Cod sursa(job #2342559)

Utilizator BotzkiBotzki Botzki Data 12 februarie 2019 21:50:42
Problema Patrate 3 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("patrate3.in");
ofstream fout("patrate3.out");
const int NMAX=1000;
const double INF=2.0e9;
const double eps=1.0e-14;
struct point
{
    double x, y;
};
struct segment
{
    point p1, p2;
    double panta_seg;
    double distanta;
};
double dist(point p1, point p2)
{
   return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
bool vertical(point p1, point p2)
{
    if(fabs(p1.x-p2.x)<eps)
        return 1;
    return 0;
}
double panta(point p1, point p2)
{
    if(vertical(p1, p2)==1)
        return INF;
    return (p2.y-p1.y)/(p2.x-p1.x);
}
bool cmp(segment a, segment b)
{
    return a.distanta-b.distanta<=-eps;
}
double cp(point p1, point p2, point p3)
{
    return (p2.x-p1.x)*(p3.y-p2.y)-(p2.y-p1.y)*(p3.x-p2.x);
}
bool same_point(point p1, point p2)
{
    return fabs(p1.x-p2.x)<eps and fabs(p1.y-p2.y)<eps;
}
point mijloc(point p1, point p2)
{
    point mij;
    mij.x=(p1.x+p2.x)*0.5;
    mij.y=(p1.y+p2.y)*0.5;
    return mij;
}
point v[NMAX+5];
vector <segment>s;
int main()
{
    int i, j, n;
    fin>>n;
    double a, b;
    segment aux;
    for(i=1;i<=n;i++)
    {
        fin>>a>>b;
        v[i].x=a;
        v[i].y=b;
    }
    for(i=1;i<n-1;i++)
    {
       for(j=i+1;j<=n;j++)
       {
          aux.p1=v[i];
          aux.p2=v[j];
          aux.panta_seg=panta(v[i], v[j]);
          s.push_back(aux);
       }
    }
    int sol=0;
    double p;
    sort(s.begin(), s.end(), cmp);

    for(i=0;i<s.size()-1;i++)
    {
        j=i+1;
        while(fabs(s[i].distanta-s[j].distanta)<eps and j<s.size())
       {
          p=s[i].panta_seg*s[j].panta_seg;
          if(p>=-1.0-eps and p<=-1.0+eps and same_point(mijloc(s[i].p1, s[i].p2), mijloc(s[j].p1, s[j].p2)))
              sol++;
          if(p==0 and (s[i].panta_seg==INF or s[j].panta_seg==INF) and same_point(mijloc(s[i].p1, s[i].p2), mijloc(s[j].p1, s[j].p2)))
            sol++;
          j++;
       }
    }
    fout<<sol<<"\n";
    return 0;
}