Cod sursa(job #2478062)

Utilizator lucianistratiIstrati Lucian lucianistrati Data 21 octombrie 2019 16:38:01
Problema Aria Scor 0
Compilator cpp-32 Status done
Runda Arhiva educationala Marime 3.12 kb
#include <iostream>
#include <math.h>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
    double x,y;
}punct;
const int inf=1e9;
vector<punct> v,I,J;
double det;
double a1,b1,c1,a2,b2,c2,X,Y;

double dist(punct a,punct b)
{
    return (sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y)));
}
 double arie(punct a,punct b,punct c)
{
    double p,arie,len_a,len_b,len_c;
    len_a=dist(a,b);
    len_b=dist(b,c);
    len_c=dist(a,c);
    p=(len_a+len_b+len_c)/2;
    arie=sqrt(p*(p-len_a)*(p-len_b)*(p-len_c));
    return arie;
}
int apartine(double X,double Y)
{
    return (((a1*X+b1*Y+c1)==0) && ((a2*X+b2*Y+c2)==0));
}
int main()
{
    ifstream fin("maxsubsum.in");
    punct t;
    int i;
    for(i=1;i<=4;i++)
    {
        fin>>t.x>>t.y;
        v.push_back(t);
    }
    //Ecuatia A1A2
    a1=v[0].y-v[1].y;
    b1=v[1].x-v[0].x;
    c1=v[0].x*v[1].y-v[1].x*v[0].y;
    //Ecuatia A3A4
    a2=v[2].y-v[3].y;
    b2=v[3].x-v[2].x;
    c2=v[2].x*v[3].y-v[3].x*v[2].y;
    //Calculam det
    det=a1*b2-a2*b1;
    if(det!=0)
    {
        double detX,detY;
        detX=(-c1)*b2+c2*b1;
        detY=(-c2)*a1+a2*c1;
        X=detX/det;
        Y=detY/det;
        if(apartine(X,Y)==1)
        {
        cout<<"Intersectia este "<<X<<" "<<Y<<"\n";
        }
    }
    else if(det==0)
    {
       double rang12,rang13,rang23;
       rang12=a1*b2-a2*b1;
       rang23=b1*c2-b2*c1;
       rang13=a1*c2-a2*c1;
       if(rang13!=0 || rang23!=0)
          {
              cout<<"Intersectia e vida\n";
          }
          else
          {
               cout<<"Punctele sunt coliniare\n";
               vector<pair <int,int> > new_v;
               for(i=0;i<=3;i++)
               {
                   new_v.push_back({v[i].x,v[i].y});
               }
               sort(new_v.begin(),new_v.end());
               if( (new_v[2].first==v[1].x && new_v[2].second==v[1].y)  && (new_v[1].first==v[2].x && new_v[1].second==v[2].y))
               {
                   cout<<"Intersectia este segmentul A2A3: [("<<v[2].x<<", "<<v[2].y<<"),("<<v[1].x<<", "<<v[1].y<<")]\n";
               }
               else if ((new_v[0].first==v[0].x && new_v[0].second==v[0].y)  && (new_v[3].first==v[1].x && new_v[3].second==v[1].y))
               {
                   cout<<"Intersectia este segmentul A3A4: [("<<v[2].x<<", "<<v[2].y<<"),("<<v[3].x<<", "<<v[3].y<<")]\n";
               }
               else if( (new_v[2].first==v[3].x && new_v[2].second==v[3].y)  && (new_v[1].first==v[0].x && new_v[1].second==v[0].y))
               {
                   cout<<"Intersectia este segmentul A4A1: [("<<v[3].x<<", "<<v[3].y<<"),("<<v[0].x<<", "<<v[0].y<<")]\n";
               }
               else if ((new_v[0].first==v[2].x && new_v[0].second==v[2].y)  && (new_v[3].first==v[3].x && new_v[3].second==v[3].y))
               {
                   cout<<"Intersectia este segmentul A1A2 :[("<<v[0].x<<", "<<v[0].y<<"),("<<v[1].x<<", "<<v[1].y<<")]\n";
               }
               else{ cout<<"Intersectia este totusi vida";}
          }
    }
    fin.close();
    return 0;
}