Learning computational Geometry… :)

by adrien

adrien lucca computation ex1 nov 7 2013

click on the picture to zoom, and compare with that:

https://adrienlucca.files.wordpress.com/2013/06/adrien-lucca-d65-5-8-june-13-coupe.jpg

………tired!

This thing (computationnal Geometry) will hopefully make my work much easier, faster, more precise, help avoiding lots of human mistakes, etc. However it’s a hell to computer-program when being almost totally ignorant, I’m just using the programmaing language as a linear calculator instead of being smart and properly code… I know, I’m a bullshit programmer, I know I should use classes since I use systematically similar objects such as lines, segments, intersections, colors…

And YES I know it’s an absolutely ugly code, sorry!

So, here’s the trash!

(that however fully works for triangular shapes now, after 4 full days of solving every mathematical detail:)

 #include <vector>
#include <cmath>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

#define PI 3.14159265

//___________________________________________________

/// variables from the *.srgb file

double xa = 0;
double ya = 0;
double Ta = 0;
double Ca = 0;
double Ma = 0;
double Da = 0;

double xb = 0;
double yb = 0;
double Tb = 0;
double Cb = 0;
double Mb = 0;
double Db = 0;

double xc = 0;
double yc = 0;
double Tc = 0;
double Cc = 0;
double Mc = 0;
double Dc = 0;

double xd = 0;
double yd = 0;
double Td = 0;
double Cd = 0;
double Md = 0;
double Dd = 0;

/// variables for calculation

double Dxa = 0; /// Deltas
double Dya = 0;
double Dxb = 0;
double Dyb = 0;

double DET = 0; /// Determinant

double Dx  = 0; /// Slopes
double Mx  = 0;
double Cx  = 0;
double Tx  = 0;

double Dy  = 0;
double My  = 0;
double Cy  = 0;
double Ty  = 0;

double points = 0; /// amount of points

//__________________________________________________

int trash    = 0;   // ifstream bug (?) stuff

//___________________________________

int main() {

/// FAIRE MIEUX L'IMPORTATION POUR ACCEPTER UN TABLEAU .CSV SEPARE PAR TAB ///


ifstream ifs ("mat.srgb"); ///LOADING
if (!ifs)
// process error
ifs >>  trash;
ifs >>  xa ;
ifs >>  ya ;
ifs >>  Da ;
ifs >>  Ma ;
ifs >>  Ca ;
ifs >>  Ta ;
ifs >>  xb ;
ifs >>  yb ;
ifs >>  Db ;
ifs >>  Mb ;
ifs >>  Cb ;
ifs >>  Tb ;
ifs >>  xc ;
ifs >>  yc ;
ifs >>  Dc ;
ifs >>  Mc ;
ifs >>  Cc ;
ifs >>  Tc ;
ifs >>  xd ;
ifs >>  yd ;
ifs >>  Dd ;
ifs >>  Md ;
ifs >>  Cd ;
ifs >>  Td ;

if (xd == 0, yd == 0) {
points = 3;
}
else {
points = 4;
}

cout << "point A:" << endl;
cout << "x: " << xa << endl << "y: " << ya << endl << "TOP: " << Ta << "   CHEESE: "  << Ca << "   MEAT: " <<  Ma <<  "   DOWN: " << Da << endl;
cout << "point B:" << endl;
cout << "x: " << xb << endl << "y: " << yb << endl << "TOP: " << Tb << "   CHEESE: "  << Cb << "   MEAT: " <<  Mb <<  "   DOWN: " << Db << endl;
cout << "point C:" << endl;
cout << "x: " << xc << endl << "y: " << yc << endl << "TOP: " << Tc << "   CHEESE: "  << Cc << "   MEAT: " <<  Mc <<  "   DOWN: " << Dc << endl;

if (points == 4) {
cout << "point D:" << endl;
cout << "x: " << xd << endl << "y: " << yd << endl << "TOP: " << Td << "   CHEESE: "  << Cd << "   MEAT: " <<  Md <<  "   DOWN: " << Dd << endl;
}

//_________________________________________________________________________________________________________________________________________________

/// calculations

// deltas

Dxa = xa - xc;
Dya = ya - yc;
Dxb = xb - xc;
Dyb = yb - yc;

// determinant

DET = Dxa*Dyb - Dxb*Dya;

// slopes

Dx  =  ((Da - Dc) * Dyb - (Db - Dc) * Dya) / DET;
Mx  =  ((Ma - Mc) * Dyb - (Mb - Mc) * Dya) / DET;
Cx  =  ((Ca - Cc) * Dyb - (Cb - Cc) * Dya) / DET;
Tx  =  ((Ta - Tc) * Dyb - (Tb - Tc) * Dya) / DET;

Dy  =  ((Db - Dc) * Dxa - (Da - Dc) * Dxb) / DET;
My  =  ((Mb - Mc) * Dxa - (Ma - Mc) * Dxb) / DET;
Cy  =  ((Cb - Cc) * Dxa - (Ca - Cc) * Dxb) / DET;
Ty  =  ((Tb - Tc) * Dxa - (Ta - Tc) * Dxb) / DET;

// ___________________________________________________

/// frame

double MinX = 0;
double MaxX = 0;
double MinZ = 0;
double MaxZ = 0;

MinX = min(xa,xb);
MinX = min(xc,MinX);
MaxX = max(xa,xb);
MaxX = max(xc,MaxX);

MinZ = min(ya,yb);
MinZ = min(yc,MinZ);
MaxZ = max(ya,yb);
MaxZ = max(yc,MaxZ);


if (points == 4) {
MinX = min(xd,MinX);
MaxX = max(xd,MaxX);
MinZ = min(yd,MinZ);
MaxZ = max(yd,MaxZ);
}

cout << "minX, maxX, minZ, maxZ" << endl;
cout << MinX  << endl;
cout << MaxX  << endl;
cout << MinZ  << endl;
cout << MaxZ  << endl;

//_________________________________________________

/// Min & Max Densities

double MeatDmax = 0;
double MeatDmin = 0;
double CheeseDmax = 0;
double CheeseDmin = 0;

MeatDmin = min(Ma,Mb);
MeatDmin = min(Mc,MeatDmin);
MeatDmax = max(Ma,Mb);
MeatDmax = max(Mc,MeatDmax);

CheeseDmin = min(Ca,Cb);
CheeseDmin = min(Cc,CheeseDmin);
CheeseDmax = max(Ca,Cb);
CheeseDmax = max(Cc,CheeseDmax);


if (points == 4) {
MeatDmin = min(Md,MeatDmin);
MeatDmax = max(Md,MeatDmax);
CheeseDmin = min(Cd,CheeseDmin);
CheeseDmax = max(Cd,CheeseDmax);
}

bool isA  = false;
bool isB  = false;
bool isC  = false;
bool isD  = false;
bool cisA = false;
bool cisB = false;
bool cisC = false;
bool cisD = false;
bool ISA  = false;
bool ISB  = false;
bool ISC  = false;
bool ISD  = false;
bool cISA = false;
bool cISB = false;
bool cISC = false;
bool cISD = false;


/// MAX
// MEAT
if (MeatDmax == Ma) {
ISA = true;
cout << "MAX MEAT is point A" << endl;
}

else if (MeatDmax == Mb) {
ISB = true;
cout << "MAX MEAT is point B" << endl;
}

else if (MeatDmax == Mc) {
ISC = true;
cout << "MAX MEAT is point C" << endl;
}

else {
ISD = true;
cout << "MAX MEAT is point D" << endl;
}

// CHEESE
if (CheeseDmax == Ca) {
cISA = true;
cout << "MAX CHEESE is point A" << endl;
}

else if (CheeseDmax == Cb) {
cISB = true;
cout << "MAX CHEESE is point B" << endl;
}

else if (CheeseDmax == Cc) {
cISC = true;
cout << "MAX CHEESE is point C" << endl;
}

else {
cISD = true;
cout << "MAX CHEESE is point D" << endl;
}

/// MIN
// MEAT

if (ISA == true) {
if (Mb == Mc) {
isB = true;
isC = true;
}
else if (Mb == Md) {
isB = true;
isD = true;
}
else if (Mc == Md) {
isC = true;
isD = true;
}
else if (MeatDmin == Mb) {
isB = true;
}
else if (MeatDmin == Mc) {
isC = true;
}
else if (MeatDmin == Md) {
isD = true;
}
}

if (ISB == true) {
if (Ma == Mc) {
isA = true;
isC = true;
}
else if (Ma == Md) {
isA = true;
isD = true;
}
else if (Mc == Md) {
isC = true;
isD = true;
}
else if (MeatDmin == Ma) {
isA = true;
}
else if (MeatDmin == Mc) {
isC = true;
}
else if (MeatDmin == Md) {
isD = true;
}
}

if (ISC == true) {
if (Mb == Ma) {
isB = true;
isA = true;
}
else if (Mb == Md) {
isB = true;
isD = true;
}
else if (Ma == Md) {
isA = true;
isD = true;
}
else if (MeatDmin == Mb) {
isB = true;
}
else if (MeatDmin == Ma) {
isA = true;
}
else if (MeatDmin == Md) {
isD = true;
}
}

if (ISD == true) {
if (Mb == Mc) {
isB = true;
isC = true;
}
else if (Mb == Ma) {
isB = true;
isA = true;
}
else if (Mc == Ma) {
isC = true;
isA = true;
}
else if (MeatDmin == Mb) {
isB = true;
}
else if (MeatDmin == Mc) {
isC = true;
}
else if (MeatDmin == Ma) {
isA = true;
}
}

// CHEESE

if (cISA == true) {
if (Cb == Cc) {
cisB = true;
cisC = true;
}
else if (Cb == Cd) {
cisB = true;
cisD = true;
}
else if (Cc == Cd) {
cisC = true;
cisD = true;
}
else if (CheeseDmin == Cb) {
cisB = true;
}
else if (CheeseDmin == Cc) {
cisC = true;
}
else if (CheeseDmin == Cd) {
cisD = true;
}
}

if (cISB == true) {
if (Ca == Cc) {
cisA = true;
cisC = true;
}
else if (Ca == Cd) {
cisA = true;
cisD = true;
}
else if (Cc == Cd) {
cisC = true;
cisD = true;
}
else if (CheeseDmin == Ca) {
cisA = true;
}
else if (CheeseDmin == Cc) {
cisC = true;
}
else if (CheeseDmin == Cd) {
cisD = true;
}
}

if (cISC == true) {
if (Cb == Ca) {
cisB = true;
cisA = true;
}
else if (Cb == Cd) {
cisB = true;
cisD = true;
}
else if (Ca == Cd) {
cisA = true;
cisD = true;
}
else if (CheeseDmin == Cb) {
cisB = true;
}
else if (CheeseDmin == Ca) {
cisA = true;
}
else if (CheeseDmin == Cd) {
cisD = true;
}
}

if (cISD == true) {
if (Cb == Cc) {
cisB = true;
cisC = true;
}
else if (Cb == Ca) {
cisB = true;
cisA = true;
}
else if (Cc == Ca) {
cisC = true;
cisA = true;
}
else if (CheeseDmin == Cb) {
cisB = true;
}
else if (CheeseDmin == Cc) {
cisC = true;
}
else if (CheeseDmin == Ca) {
cisA = true;
}
}

/// RECAPITULATION

cout << endl << "recapitulation:" << endl;
cout << "ISA:  " << ISA  << "   ISB: " << ISB  << "   ISC: " << ISC  << "   ISD: " << ISD  << endl;
cout << "isA:  " << isA  << "   isB: " << isB  << "   isC: " << isC  << "   isD: " << isD  << endl;
cout << "cISA: " << cISA << "  cISB: " << cISB << "  cISC: " << cISC << "  cISD: " << cISD << endl;
cout << "cisA: " << cisA << "  cisB: " << cisB << "  cisC: " << cisC << "  cisD: " << cisD << endl;


//___________________________________________________________________________________________________
// solutions variables

double X = 0;
double Z = 0;
double D = 0;
double M = 0;
double C = 0;
double T = 0;

double A_RealMeatMax   = 0;
double B_RealMeatMax   = 0;
double A_RealMeatMin   = 0;
double B_RealMeatMin   = 0;
double A_RealCheeseMax = 0;
double B_RealCheeseMax = 0;
double A_RealCheeseMin = 0;
double B_RealCheeseMin = 0;

/// MAKE THE FIGURE BORDER'S EQUATIONS

/// equations

double EligneA_m = 0;
double EligneA_E = 0;
double EligneB_m = 0;
double EligneB_E = 0;
double EligneC_m = 0;
double EligneC_E = 0;

double MinX_linA = 0;
double MaxX_linA = 0;
double MinX_linB = 0;
double MaxX_linB = 0;
double MinX_linC = 0;
double MaxX_linC = 0;


double EligneD_m = 0;
double EligneD_E = 0;
double MinX_linD = 0;
double MaxX_linD = 0;


// m & E

if (points == 3) {

double m_AB = (yb-ya)/(xb-xa);
double m_AC = (yc-ya)/(xc-xa);
double m_BC = (yc-yb)/(xc-xb);

double E_AB = yb - m_AB * xb;
double E_AC = yc - m_AC * xc;
double E_BC = yc - m_BC * xc;

cout << "m, E"   << endl;
cout << "AB  "   << m_AB << " " << E_AB << endl;
cout << "AC  "   << m_AC << " " << E_AC << endl;
cout << "BC  "   << m_BC << " " << E_BC << endl << endl;

EligneA_m = m_AB;
EligneA_E = E_AB;
MinX_linA = min (xa,xb);
MaxX_linA = max (xa,xb);

EligneB_m = m_AC;
EligneB_E = E_AC;
MinX_linB = min (xa,xc);
MaxX_linB = max (xa,xc);

EligneC_m = m_BC;
EligneC_E = E_BC;
MinX_linC = min (xb,xc);
MaxX_linC = max (xb,xc);

}

else {

double m_AD = (yd-ya)/(xd-xa);
double m_BD = (yd-yb)/(xd-xb);
double m_CD = (yd-yc)/(xd-xc);
double m_AB = (yb-ya)/(xb-xa);
double m_AC = (yc-ya)/(xc-xa);
double m_BC = (yc-yb)/(xc-xb);

double E_AD = yd - m_AD * xd;
double E_BD = yd - m_BD * xd;
double E_CD = yd - m_CD * xd;
double E_AB = yb - m_AB * xb;
double E_AC = yc - m_AC * xc;
double E_BC = yc - m_BC * xc;

cout << "AB"   << m_AB << " " << E_AB << endl;
cout << "AC"   << m_AC << " " << E_AC << endl;
cout << "AD"   << m_AD << " " << E_AD << endl;
cout << "BC"   << m_BC << " " << E_BC << endl;
cout << "BD"   << m_BD << " " << E_BD << endl;
cout << "CD"   << m_CD << " " << E_CD << endl << endl;

/// test of order, ab, ac, ad, bc, bd, cd (2 possibilities should be excluded!)

/// find angles BAD, CAB and CAD

// vectors AB, AC, AD

///AB

double Vab_x  = xb-xa;
double Vab_y  = yb-ya;
double Vab_L  = sqrt(Vab_x*Vab_x+Vab_y*Vab_y);
double Nab_x  = Vab_x / Vab_L;
double Nab_y  = Vab_y / Vab_L;

///AC

double Vac_x  = xc-xa;
double Vac_y  = yc-ya;
double Vac_L  = sqrt(Vac_x*Vac_x+Vac_y*Vac_y);
double Nac_x  = Vac_x / Vac_L;
double Nac_y  = Vac_y / Vac_L;

///AD

double Vad_x  = xd-xa;
double Vad_y  = yd-ya;
double Vad_L  = sqrt(Vad_x*Vad_x+Vad_y*Vad_y);
double Nad_x  = Vad_x / Vad_L;
double Nad_y  = Vad_y / Vad_L;

/// dot product, angles

double D_BAD  = acos (Nab_x * Nad_x + Nab_y * Nad_y) * 180.0 / PI;
double D_CAB  = acos (Nab_x * Nac_x + Nab_y * Nac_y) * 180.0 / PI;
double D_CAD  = acos (Nac_x * Nad_x + Nac_y * Nad_y) * 180.0 / PI;

cout << "BAC = " << D_CAB << "°" << endl;
cout << "BAD = " << D_BAD << "°" << endl;
cout << "CAD = " << D_CAD << "°" << endl;

if (D_CAB > D_BAD) {
if (D_CAB > D_CAD) {
/// ACDB
// ordered values

EligneA_m = m_AC;
EligneA_E = E_AC;
MinX_linA = min (xa,xc);
MaxX_linA = max (xa,xc);

EligneB_m = m_CD;
EligneB_E = E_CD;
MinX_linB = min (xc,xd);
MaxX_linB = max (xc,xd);

EligneC_m = m_BD;
EligneC_E = E_BD;
MinX_linC = min (xd,xb);
MaxX_linC = max (xd,xb);

EligneD_m = m_AB;
EligneD_E = E_AB;
MinX_linD = min (xa,xb);
MaxX_linD = max (xa,xb);

}
else {
/// ACBD
// ordered values

EligneA_m = m_AC;
EligneA_E = E_AC;
MinX_linA = min (xa,xc);
MaxX_linA = max (xa,xc);

EligneB_m = m_BC;
EligneB_E = E_BC;
MinX_linB = min (xc,xb);
MaxX_linB = max (xc,xb);

EligneC_m = m_BD;
EligneC_E = E_BD;
MinX_linC = min (xd,xb);
MaxX_linC = max (xd,xb);

EligneD_m = m_AD;
EligneD_E = E_AD;
MinX_linD = min (xa,xd);
MaxX_linD = max (xa,xd);

}
}
else if (D_BAD > D_CAD) {
/// ABCD        // ordered values

EligneA_m = m_AB;
EligneA_E = E_AB;
MinX_linA = min (xa,xb);
MaxX_linA = max (xa,xb);

EligneB_m = m_BC;
EligneB_E = E_BC;
MinX_linB = min (xc,xb);
MaxX_linB = max (xc,xb);

EligneC_m = m_CD;
EligneC_E = E_CD;
MinX_linC = min (xc,xd);
MaxX_linC = max (xc,xd);

EligneD_m = m_AD;
EligneD_E = E_AD;
MinX_linD = min (xa,xd);
MaxX_linD = max (xa,xd);

}
else if (D_BAD < D_CAD) {
/// ACBD        // ordered values

EligneA_m = m_AC;
EligneA_E = E_AC;
MinX_linA = min (xa,xc);
MaxX_linA = max (xa,xc);

EligneB_m = m_BC;
EligneB_E = E_BC;
MinX_linB = min (xc,xb);
MaxX_linB = max (xc,xb);

EligneC_m = m_BD;
EligneC_E = E_BD;
MinX_linC = min (xd,xb);
MaxX_linC = max (xd,xb);

EligneD_m = m_AD;
EligneD_E = E_AD;
MinX_linD = min (xa,xd);
MaxX_linD = max (xa,xd);


}

}

/// SOLUTION 1 (other solution n°2 just change X fixed to Y fixed)

double inchUN = 0;
double UNIT   = 0;
double DPI    = 600;

cout << endl << "UNIT (600 DPI)?" << endl;
cin  >> inchUN;

UNIT = 2.54 * inchUN / 600;

cout << endl << "UNIT = " << UNIT << endl;

cout << endl << "| | *solution 1, all vertical" << endl;

// MEAT

/// left

X = MinX;

Z = ((yc*Ty+yc*Cy-Tc-Cc+(xc-X)*Tx+(xc-X)*Cx+1)*MeatDmax+yc*My-Mc+(xc-X)*Mx)/((Ty+Cy)*MeatDmax+My);
A_RealMeatMax   = Z;

Z = ((yc*Ty+yc*Cy-Tc-Cc+(xc-X)*Tx+(xc-X)*Cx+1)*MeatDmin+yc*My-Mc+(xc-X)*Mx)/((Ty+Cy)*MeatDmin+My);
A_RealMeatMin   = Z;

/// right

X = MaxX;

Z = ((yc*Ty+yc*Cy-Tc-Cc+(xc-X)*Tx+(xc-X)*Cx+1)*MeatDmax+yc*My-Mc+(xc-X)*Mx)/((Ty+Cy)*MeatDmax+My);
B_RealMeatMax   = Z;

Z = ((yc*Ty+yc*Cy-Tc-Cc+(xc-X)*Tx+(xc-X)*Cx+1)*MeatDmin+yc*My-Mc+(xc-X)*Mx)/((Ty+Cy)*MeatDmin+My);
B_RealMeatMin   = Z;

// test

cout << "MEAT" << endl;
cout << "A MAX (Z): " << A_RealMeatMax << endl;
cout << "A MIN (Z): " << A_RealMeatMin << endl;
cout << "B MAX (Z): " << B_RealMeatMax << endl;
cout << "B MIN (Z): " << B_RealMeatMin << endl;


// Cheese

/// left

X = MinX;

Z = ((yc*Ty-Tc+(xc-X)*Tx+1)*CheeseDmax+yc*Cy-Cc+(xc-X)*Cx)/(Ty*CheeseDmax+Cy);
A_RealCheeseMax   = Z;

Z = ((yc*Ty-Tc+(xc-X)*Tx+1)*CheeseDmin+yc*Cy-Cc+(xc-X)*Cx)/(Ty*CheeseDmin+Cy);
A_RealCheeseMin   = Z;

/// right

X = MaxX;

Z = ((yc*Ty-Tc+(xc-X)*Tx+1)*CheeseDmax+yc*Cy-Cc+(xc-X)*Cx)/(Ty*CheeseDmax+Cy);
B_RealCheeseMax   = Z;

Z = ((yc*Ty-Tc+(xc-X)*Tx+1)*CheeseDmin+yc*Cy-Cc+(xc-X)*Cx)/(Ty*CheeseDmin+Cy);
B_RealCheeseMin   = Z;

// test

cout << "CHEESE" << endl;
cout << "A MAX (Z): " << A_RealCheeseMax << endl;
cout << "A MIN (Z): " << A_RealCheeseMin << endl;
cout << "B MAX (Z): " << B_RealCheeseMax << endl;
cout << "B MIN (Z): " << B_RealCheeseMin << endl;

/// KEEP/MAKE THE DATA & FORMULAE

/// Meat

double a_m_AB    = abs(MaxX - MinX);
double a_m_AstaX = MinX;
double a_m_AstaZ = A_RealMeatMax;
double a_m_AendX = MinX;
double a_m_AendZ = A_RealMeatMin;
double a_m_BstaX = MaxX;
double a_m_BstaZ = B_RealMeatMax;
double a_m_BendX = MaxX;
double a_m_BendZ = B_RealMeatMin;

double a_m_Ai    = (Mc+Mx*(a_m_AstaX-xc)+My*(a_m_AstaZ-yc));
double a_m_Ao    = (Mc+Mx*(a_m_AendX-xc)+My*(a_m_AendZ-yc));
double a_m_Atopi = (Tc+Tx*(a_m_AstaX-xc)+Ty*(a_m_AstaZ-yc))+(Cc+Cx*(a_m_AstaX-xc)+Cy*(a_m_AstaZ-yc));
double a_m_Atopo = (Tc+Tx*(a_m_AendX-xc)+Ty*(a_m_AendZ-yc))+(Cc+Cx*(a_m_AendX-xc)+Cy*(a_m_AendZ-yc));

double a_m_Bi    = (Mc+Mx*(a_m_BstaX-xc)+My*(a_m_BstaZ-yc));
double a_m_Bo    = (Mc+Mx*(a_m_BendX-xc)+My*(a_m_BendZ-yc));
double a_m_Btopi = (Tc+Tx*(a_m_BstaX-xc)+Ty*(a_m_BstaZ-yc))+(Cc+Cx*(a_m_BstaX-xc)+Cy*(a_m_BstaZ-yc));
double a_m_Btopo = (Tc+Tx*(a_m_BendX-xc)+Ty*(a_m_BendZ-yc))+(Cc+Cx*(a_m_BendX-xc)+Cy*(a_m_BendZ-yc));


/// Cheese

double a_c_AB    = abs(MaxX - MinX);
double a_c_AstaX = MinX;
double a_c_AstaZ = A_RealCheeseMax;
double a_c_AendX = MinX;
double a_c_AendZ = A_RealCheeseMin;
double a_c_BstaX = MaxX;
double a_c_BstaZ = B_RealCheeseMax;
double a_c_BendX = MaxX;
double a_c_BendZ = B_RealCheeseMin;

double a_c_Ai    = (Cc+Cx*(a_c_AstaX-xc)+Cy*(a_c_AstaZ-yc));
double a_c_Ao    = (Cc+Cx*(a_c_AendX-xc)+Cy*(a_c_AendZ-yc));
double a_c_Atopi = (Tc+Tx*(a_c_AstaX-xc)+Ty*(a_c_AstaZ-yc));
double a_c_Atopo = (Tc+Tx*(a_c_AendX-xc)+Ty*(a_c_AendZ-yc));

double a_c_Bi    = (Cc+Cx*(a_c_BstaX-xc)+Cy*(a_c_BstaZ-yc));
double a_c_Bo    = (Cc+Cx*(a_c_BendX-xc)+Cy*(a_c_BendZ-yc));
double a_c_Btopi = (Tc+Tx*(a_c_BstaX-xc)+Ty*(a_c_BstaZ-yc));
double a_c_Btopo = (Tc+Tx*(a_c_BendX-xc)+Ty*(a_c_BendZ-yc));

//_____________________________________________________________________________________

cout <<  endl << "meat:" << endl;
cout <<  a_m_AB    << endl;
cout <<  a_m_AstaX << endl;
cout <<  a_m_AstaZ << endl;
cout <<  a_m_AendX << endl;
cout <<  a_m_AendZ << endl;
cout <<  a_m_BstaX << endl;
cout <<  a_m_BstaZ << endl;
cout <<  a_m_BendX << endl;
cout <<  a_m_BendZ << endl;
cout <<  a_m_Ai    << endl;
cout <<  a_m_Ao    << endl;
cout <<  a_m_Atopi << endl;
cout <<  a_m_Atopo << endl;
cout <<  a_m_Bi    << endl;
cout <<  a_m_Bo    << endl;
cout <<  a_m_Btopi << endl;
cout <<  a_m_Btopo << endl;

cout << "verifications, values sould be very near equality"                   << endl;
cout << "A start REAL = " << a_m_Ai/(1-a_m_Atopi)                             << endl;
cout << "B start REAL = " << a_m_Bi/(1-a_m_Btopi)                             << endl;
cout << "diff = "         << abs(a_m_Ai/(1-a_m_Atopi) - a_m_Bi/(1-a_m_Btopi)) << endl;
cout << "A end REAL = "   << a_m_Ao/(1-a_m_Atopo)                             << endl;
cout << "B end REAL = "   << a_m_Bo/(1-a_m_Btopo)                             << endl;
cout << "diff = "         << abs(a_m_Ao/(1-a_m_Atopo) - a_m_Bo/(1-a_m_Btopo)) << endl;

cout <<  endl << "cheese:" << endl;
cout <<  a_c_AB    << endl;
cout <<  a_c_AstaX << endl;
cout <<  a_c_AstaZ << endl;
cout <<  a_c_AendX << endl;
cout <<  a_c_AendZ << endl;
cout <<  a_c_BstaX << endl;
cout <<  a_c_BstaZ << endl;
cout <<  a_c_BendX << endl;
cout <<  a_c_BendZ << endl;
cout <<  a_c_Ai    << endl;
cout <<  a_c_Ao    << endl;
cout <<  a_c_Atopi << endl;
cout <<  a_c_Atopo << endl;
cout <<  a_c_Bi    << endl;
cout <<  a_c_Bo    << endl;
cout <<  a_c_Btopi << endl;
cout <<  a_c_Btopo << endl;

cout << "verifications, values sould be very near equality" << endl;
cout << "A start REAL = " << a_c_Ai/(1-a_c_Atopi) << endl;
cout << "B start REAL = " << a_c_Bi/(1-a_c_Btopi) << endl;
cout << "diff = " << abs(a_c_Ai/(1-a_c_Atopi) - a_c_Bi/(1-a_c_Btopi)) << endl;
cout << "A end REAL = " << a_c_Ao/(1-a_c_Atopo) << endl;
cout << "B end REAL = " << a_c_Bo/(1-a_c_Btopo) << endl;
cout << "diff = " << abs(a_c_Ao/(1-a_c_Atopo) - a_c_Bo/(1-a_c_Btopo)) << endl;

/// _____________________________________________________________________________

/// containers

vector <double> sideA; // intersections pour SVG
vector <double> sideB;
vector <double> sideC;
vector <double> sideD;

vector <double> fileA; // fichiers TXT
vector <double> fileB;
vector <double> fileC;
vector <double> fileD;

// processing variables
/// !!! DONT FORGET TO RESET THEM !!!

double lengthA = 0;
double lengthB = 0;
double amplifi = 0;

double SIZE_A  = 0;
double SIZE_B  = 0;
double D_START = 0;
double D_TOP_S = 0;
double D_END   = 0;
double D_TOP_E = 0;

double A_X     = 0;
double B_X     = 0;

int iteration  = 0;

double VAR_Z   = 0;
double SUM     = 0;
double percent = 0;
double POS_A   = 0;
double POS_B   = 0;
double POS_B2  = 0;
double unitBis = 0;
double unitTer = 0;
double PilotD  = 0;
double TOP_D   = 0;
double D_REAL  = 0;

double slope_a = 0;
double add_a   = 0;
double slope_b = 0;
double add_b   = 0;

double AB_x    = 0;
double AB_y    = 0;
bool   testAB  = false;

double AD_x    = 0;
double AD_y    = 0;
bool   testAD  = false;

double BC_x    = 0;
double BC_y    = 0;
bool   testBC  = false;

double CD_x    = 0;
double CD_y    = 0;
bool   testCD  = false;


/// MEAT

lengthA = abs(a_m_AendZ-a_m_AstaZ); // a_c for cheese solution 1, b_c = cheese sol 2, etc.
lengthB = abs(a_m_BendZ-a_m_BstaZ);

// test

if (lengthA>lengthB) {

amplifi = lengthA/lengthB;

cout << "Pilot is B" << endl;
cout << "amp = "     << amplifi << endl;

SIZE_A  = lengthB;
SIZE_B  = lengthA;

POS_A   = a_m_BstaZ;
POS_B   = a_m_AstaZ;
A_X     = a_m_BstaX;
B_X     = a_m_AstaX;

D_START = a_m_Bi;
D_TOP_S = a_m_Btopi;
D_END   = a_m_Bo;
D_TOP_E = a_m_Btopo;
}

else {

amplifi = lengthB/lengthA;

cout << "Pilot is A" << endl;
cout << "amp = "     << amplifi << endl;

SIZE_A  = lengthA;
SIZE_B  = lengthB;

POS_A   = a_m_AstaZ;
POS_B   = a_m_BstaZ;
A_X     = a_m_AstaX;
B_X     = a_m_BstaX;

D_START = a_m_Ai;
D_TOP_S = a_m_Atopi;
D_END   = a_m_Ao;
D_TOP_E = a_m_Atopo;
}

/// processing

/// ATTENTION, BEFORE THE FIRST VALUE (JUST A POINT, A MARK)
/// SHOULD BE PUSHED IN THE VECTORS !!!!

/// CAREFUL WITH SVGs, 90 DPI!

vector <double> log;

while (percent < 1) {

/// intersections sides,

/// slopes ba0 and ba1 // slope is m, add is E

slope_a = (POS_B  - POS_A )/(B_X-A_X);
add_a   =  POS_B  - slope_a * B_X;
slope_b = (POS_A  - POS_B2)/(A_X-B_X);
add_b   =  POS_A  - slope_b * A_X;

cout << "Ax " << A_X << "  Bx " << B_X << "  mba0 " << slope_a << "  eba0 " << add_a <<"  mab1 " << slope_b << "  eab1 " << add_b << endl;

/// intersection & test in segment via X

// AB

AB_x   = -(add_a - EligneA_E) / (slope_a - EligneA_m);
AB_y   = EligneA_m * AB_x + EligneA_E;

cout << endl << "ABx  " << AB_x << "   ABy  " << AB_y << endl;

if ((AB_x-MaxX_linA)/(MinX_linA-MaxX_linA)>=0){
if ((AB_x-MaxX_linA)/(MinX_linA-MaxX_linA)<=1) {
testAB = true;
cout << "intersection on Line 1: " << endl << "x = " << AB_x << " | y = " << AB_y << endl << endl;
log.push_back(AB_x);
log.push_back(AB_y);

}}

AB_x   = -(add_b - EligneA_E) / (slope_b - EligneA_m);
AB_y   = EligneA_m * AB_x + EligneA_E;

cout << endl << "ABx2 " << AB_x << "   ABy2 " << AB_y << endl;

if ((AB_x-MaxX_linA)/(MinX_linA-MaxX_linA)>=0){
if ((AB_x-MaxX_linA)/(MinX_linA-MaxX_linA)<=1) {
testAB = true;
cout << "2nd intersection on Line 1: " << endl << "x = " << AB_x << " | y = " << AB_y << endl << endl;
log.push_back(AB_x);
log.push_back(AB_y);

}}

// BC

BC_x   = -(add_a - EligneB_E) / (slope_a - EligneB_m);
BC_y   = EligneB_m * BC_x + EligneB_E;

cout << endl << "BCx  " << BC_x << "   BCy  " << BC_y << endl;

if ((BC_x-MaxX_linB)/(MinX_linB-MaxX_linB)>=0) {
if ((BC_x-MaxX_linB)/(MinX_linB-MaxX_linB)<=1) {
testBC = true;
cout << "intersection on Line 2: " << endl << "x = " << BC_x << " | y = " << BC_y << endl << endl;
log.push_back(BC_x);
log.push_back(BC_y);

}}

BC_x   = -(add_b - EligneB_E) / (slope_b - EligneB_m);
BC_y   = EligneB_m * BC_x + EligneB_E;

cout << endl << "BCx2 " << BC_x << "   BCy2 " << BC_y << endl;

if ((BC_x-MaxX_linB)/(MinX_linB-MaxX_linB)>=0) {
if ((BC_x-MaxX_linB)/(MinX_linB-MaxX_linB)<=1) {
testBC = true;
cout << "2nd intersection on Line 2: " << endl << "x = " << BC_x << " | y = " << BC_y << endl << endl;
log.push_back(BC_x);
log.push_back(BC_y);

}}

// CD

CD_x   = -(add_a - EligneC_E) / (slope_a - EligneC_m);
CD_y   = EligneC_m * CD_x + EligneC_E;

cout << endl << "CDx  " << CD_x << "   CDy  " << CD_y << endl;

if ((CD_x-MaxX_linC)/(MinX_linC-MaxX_linC)>=0) {
if ((CD_x-MaxX_linC)/(MinX_linC-MaxX_linC)<=1) {
testCD = true;
cout << "intersection on Line 3: " << endl << "x = " << CD_x << " | y = " << CD_y << endl << endl;
log.push_back(CD_x);
log.push_back(CD_y);

}}


CD_x   = -(add_b - EligneC_E) / (slope_b - EligneC_m);
CD_y   = EligneC_m * CD_x + EligneC_E;

cout << endl << "CDx2 " << CD_x << "   CDy2 " << CD_y << endl;

if ((CD_x-MaxX_linC)/(MinX_linC-MaxX_linC)>=0) {
if ((CD_x-MaxX_linC)/(MinX_linC-MaxX_linC)<=1) {
testCD = true;
cout << "2nd intersection on Line 3: " << endl << "x = " << CD_x << " | y = " << CD_y << endl << endl;
log.push_back(CD_x);
log.push_back(CD_y);

}}

// BC

AD_x   = -(add_a - EligneD_E) / (slope_a - EligneD_m);
AD_y   = EligneD_m * AD_x + EligneD_E;

cout << endl << "ADx  " << AD_x << "   ADy  " << AD_y << endl;

if ((AD_x-MaxX_linD)/(MinX_linD-MaxX_linD)>=0) {
if ((AD_x-MaxX_linD)/(MinX_linD-MaxX_linD)<=1) {
testAD = true;
cout << "intersection on Line 4: " << endl << "x = " << AD_x << " | y = " << AD_y << endl << endl;
log.push_back(AD_x);
log.push_back(AD_y);

}}

AD_x   = -(add_b - EligneD_E) / (slope_b - EligneD_m);
AD_y   = EligneD_m * AD_x + EligneD_E;

cout << endl << "ADx2 " << AD_x << "   ADy2 " << AD_y << endl;

if ((AD_x-MaxX_linD)/(MinX_linD-MaxX_linD)>=0) {
if ((AD_x-MaxX_linD)/(MinX_linD-MaxX_linD)<=1) {
testAD = true;
cout << "2nd intersection on Line 4: " << endl << "x = " << AD_x << " | y = " << AD_y << endl << endl;
log.push_back(AD_x);
log.push_back(AD_y);

}}




unitBis = sqrt((-tan(atan((POS_A-POS_B)/a_m_AB))*UNIT)*(-tan(atan((POS_A-POS_B)/a_m_AB))*UNIT)+UNIT*UNIT);
unitTer = unitBis * amplifi - unitBis;
percent = SUM / SIZE_A;
PilotD  = D_START - (D_START - D_END)   * percent;
TOP_D   = D_TOP_S - (D_TOP_S - D_TOP_E) * percent;
D_REAL  = PilotD / (1-TOP_D);
VAR_Z   = unitBis * 1 / D_REAL;
SUM     = SUM + VAR_Z;
POS_A   = POS_A + VAR_Z;
POS_B   = POS_B + VAR_Z * amplifi;
POS_B2  = POS_B + unitTer;

iteration = iteration + 1;

cout << endl << iteration    << endl;
cout << "unitBis "<< unitBis << endl;
cout << "unitTer "<< unitTer << endl;
cout << "POS_B2  "<< POS_B2  << endl;
cout << "percent "<< percent << endl;
cout << "PilotD  "<< PilotD  << endl;
cout << "TOP_D   "<< TOP_D   << endl;
cout << "D_REAL  "<< D_REAL  << endl;
cout << "VAR_Z   "<< VAR_Z   << endl;
cout << "SUM     "<< SUM     << endl;
cout << "POS_A   "<< POS_A   << endl;
cout << "POS_B   "<< POS_B   << endl << endl;

/// OKAY JUSQUE ICI

}

/// _____________________WRITE_LOG FILE___________________________

ofstream output_file("./log.csv");

while (!log.empty()) {
// write file
output_file << "\n" << log.front() <<  ";" ;
log.erase(log.begin());
output_file << log.front() <<  ";" ;
log.erase(log.begin());
}

output_file.close();


/// RESET VARIABLES (pas oublier amplifi)


return(0);
}