#include "Complex.h"

Complex InitComplex(double a, double b){
  return (Complex){a,b};
}

Complex ReadComplex(){
  Complex z;
  printf("Enter complex number a+ib :\n  a=");
  scanf("%lf",&z.Re);
  printf("  b=");
  scanf("%lf",&z.Im);
  return z;
}

void PrintComplex(Complex z){
  printf("(%.6f,%.6f)",z.Re,z.Im);
}

void permut(double *x, double *y){
  double tmp;
  tmp=*x;*x=*y;*y=tmp;
}

void PermutComplex(Complex *z1,Complex *z2){
  permut(&(z1->Re),&(z2->Re));
  permut(&(z1->Im),&(z2->Im));
}

Complex axpbyComplex(double lambda,Complex z1,double mu, Complex z2){
  Complex z;
  z.Re=lambda*z1.Re+mu*z2.Re;
  z.Im=lambda*z1.Im+mu*z2.Im;
  return z;
}

double modulusComplex(Complex z){
  return sqrt( z.Re*z.Re + z.Im*z.Im);
}
 
double areaComplexT(Complex z1, Complex z2, Complex z3){
   return 0.5*fabs( (z2.Re-z1.Re)*(z3.Im-z1.Im) - (z2.Im-z1.Im)*(z3.Re-z1.Re) );
}

double maxLengthComplexT(Complex z1, Complex z2, Complex z3){
  double h=0.;
  Complex Edge;
  h=MAX(h,fabs(modulusComplex(axpbyComplex(1.,z1,-1.,z2))));
  h=MAX(h,fabs(modulusComplex(axpbyComplex(1.,z1,-1.,z3))));
  h=MAX(h,fabs(modulusComplex(axpbyComplex(1.,z2,-1.,z3))));
  return h;
}
  
  
void solve(double A, double B, double C, Complex *z1, Complex *z2){
  double Delta;
  Delta=B*B-4.*A*C;
  if (Delta >=0){
    z1->Re=(-B-sqrt(Delta))/(2.*A);z1->Im=0.;
    z2->Re=(-B+sqrt(Delta))/(2.*A);z1->Im=0.;
  }else{
    z1->Re=z2->Re=-B/(2.*A);
    z2->Im=sqrt(-Delta)/(2.*A);
    z1->Im=-z2->Im;
  }
}