Скачиваний:
8
Добавлен:
01.05.2014
Размер:
4.84 Кб
Скачать
#ifndef CPP002
#define CPP002

#include "cTriangle.h"

cTriangle::cTriangle()
{

	 double p1=0.0,p2=1.0;
	 Top[0].SetXY(p1,p1);
	 Top[1].SetXY(p2,p1);
	 Top[2].SetXY(p1,p2);

};

cTriangle::cTriangle(cPoint P1,cPoint P2,cPoint P3)
{
 Top[0].SetXY(P1.GetX(),P1.GetY());
 Top[1].SetXY(P2.GetX(),P2.GetY());
 Top[2].SetXY(P3.GetX(),P3.GetY());
 if (!(rb() && correct()))
  {
   Top[0].SetXY(0,0);
   Top[1].SetXY(1,0);
   Top[2].SetXY(0,1);
  };
};

void cTriangle::GetTopXY(int i, double &x, double &y)
{
 Top[i].GetXY(x,y);
}


double cTriangle::GetX(int i)
{
 return Top[i].GetX();
}

double cTriangle::GetY(int i)
{
 return Top[i].GetY();
}



void cTriangle::SetTopXY(int i, double x, double y)
{
 Top[i].SetXY(x,y);
}


int cTriangle::rb()
{
 double x1,y1;
 GetTopXY(0,x1,y1);
 if (fabs(Top[1].Distance(x1,y1) - Top[2].Distance(x1,y1)) <= 0.01) return 1;
 GetTopXY(1,x1,y1);
 if (fabs(Top[0].Distance(x1,y1) - Top[2].Distance(x1,y1)) <= 0.01) return 1;
 GetTopXY(2,x1,y1);
 if (fabs(Top[0].Distance(x1,y1) - Top[1].Distance(x1,y1)) <= 0.01) return 1;
 else return 0;
}


int cTriangle::correct()
{
 double x1,y1,x2,y2,l1,l2,l3;
 Top[1].GetXY(x1,y1);
 Top[2].GetXY(x2,y2);
 l1=Top[0].Distance(x1,y1);
 l2=Top[0].Distance(x2,y2);
 l3=Top[1].Distance(x2,y2);
 if ((l1+l2>l3)&&(l1+l3>l2)&&(l2+l3>l1)) return 1;
 else return 0;
}

void cTriangle::Move(double dx,double dy)
{
 if (!((rb())&&(correct()))) exit(1);
 Top[0].Move(dx,dy);
 Top[1].Move(dx,dy);
 Top[2].Move(dx,dy);
 if (!((rb())&&(correct()))) exit(1);
}

void cTriangle::Rotate(double x0,double y0,double a)
{
 if (!((rb())&&(correct()))) exit(1);
 Top[0].Rotate(x0,y0,a);
 Top[1].Rotate(x0,y0,a);
 Top[2].Rotate(x0,y0,a);
 if (!((rb())&&(correct()))) exit(1);
}

void cTriangle::Mirror(double x0,double y0)
{
 if (!((rb())&&(correct()))) exit(1);
 Top[0].Mirror(x0,y0);
 Top[1].Mirror(x0,y0);
 Top[2].Mirror(x0,y0);
 if (!((rb())&&(correct()))) exit(1);
}


void cTriangle::Scale(double coef)
{
 double x,y;
 if (!((rb())&&(correct())&&(coef<=MAXFLOAT)&&(coef>=-MAXFLOAT))) exit(1);
 Top[0].GetXY(x,y);
 x*=coef;
 y*=coef;
 Top[0].SetXY(x,y);
 Top[1].GetXY(x,y);
 x*=coef;
 y*=coef;
 Top[1].SetXY(x,y);
 Top[2].GetXY(x,y);
 x*=coef;
 y*=coef;
 Top[2].SetXY(x,y);
 if (!((rb())&&(correct()))) exit(1);
}

cPoint cTriangle::GetTop(int i)
{
 cPoint P(GetX(i),GetY(i));
 return P;
}

double cTriangle::Square()
{
 double x1,y1,a,c;
 GetTopXY(0,x1,y1);
 if (fabs(Top[1].Distance(x1,y1) - Top[2].Distance(x1,y1)) <= 0.01)
 {
	a=Top[1].Distance(x1,y1);
	GetTopXY(1,x1,y1);
	c=Top[2].Distance(x1,y1);
 }
 else
 {
	GetTopXY(1,x1,y1);
	if (fabs(Top[0].Distance(x1,y1) - Top[2].Distance(x1,y1)) <= 0.01)
	{
	 a=Top[0].Distance(x1,y1);
	 GetTopXY(2,x1,y1);
	 c=Top[0].Distance(x1,y1);
	}
	else
	{
	 a=Top[2].Distance(x1,y1);
	 c=Top[0].Distance(x1,y1);
	}
 }
 return (c*sqrt(a*a-c*c/4)/2);
};

void cTriangle::Set(cTriangle F)
{
 SetTopXY(0,F.GetX(0),F.GetY(0));
 SetTopXY(1,F.GetX(1),F.GetY(1));
 SetTopXY(2,F.GetX(2),F.GetY(2));
}

void cTriangle::out()
{
 int i;
 for (i=0;i<3;i++)
   (Top[i].Get()).out();
 cout<<endl;
}

/*void cTriangle::Center(double &X,double &Y)
{
 double x0,y0,x1,y1,x2,y2,xh,yh;
 GetTopXY(0,x0,y0);
 GetTopXY(1,x1,y1);
 GetTopXY(2,x2,y2);
 if (fabs(Top[1].Distance(x0,y0)-Top[2].Distance(x0,y0)) <= 0.01)
  {
   xh = (x1 + x2) / 2;
   yh = (y1 + y2) / 2;
   X = (x0 + 2 * xh) / 3;
   Y = (y0 + 2 * yh) / 3;
  }
 else
  {
   if (fabs(Top[0].Distance(x1,y1)-Top[2].Distance(x1,y1)) <= 0.01)
    {
     xh = (x0 + x2) / 2;
     yh = (y0 + y2) / 2;
     X = (x1 + 2 * xh) / 3;
     Y = (y1 + 2 * yh) / 3;
    }
   else
    {
     xh = (x0 + x1) / 2;
     yh = (y0 + y1) / 2;
     X = (x2 + 2 * xh) / 3;
     Y = (y2 + 2 * yh) / 3;
    }
  }
}

void cTriangle::Draw()
{
 int gdriver = DETECT, gmode;
 initgraph(&gdriver, &gmode, "");
 double Xm,Ym;
 Xm = 320;
 Ym = 240;
 double x0,y0,scc;
 Center(x0,y0);
 scc = Xm / (4 * sqrt(x0*x0+y0*y0));
 cTriangle bTr;
 int i;
 for (i=0;i<3;i++)
  {
   bTr.SetTopXY(i,GetX(i)*scc+Xm,Ym-GetY(i)*scc);
  };
 setcolor(BLUE);
 line(bTr.GetX(0),bTr.GetY(0),bTr.GetX(1),bTr.GetY(1));
 line(bTr.GetX(1),bTr.GetY(1),bTr.GetX(2),bTr.GetY(2));
 line(bTr.GetX(0),bTr.GetY(0),bTr.GetX(2),bTr.GetY(2));
 setcolor(WHITE);
 line(Xm-10,Ym,Xm+10,Ym);
 line(Xm,Ym-10,Xm,Ym+10);
 cout<<"("<<GetX(0)<<";"<<GetY(0)<<")"<<endl
     <<"("<<GetX(1)<<";"<<GetY(1)<<")"<<endl
     <<"("<<GetX(2)<<";"<<GetY(2)<<")";
 setcolor(RED);
 line(Xm-scc,Ym-scc,Xm-scc,Ym+scc);
 line(Xm+scc,Ym-scc,Xm+scc,Ym+scc);
 line(Xm-scc,Ym-scc,Xm+scc,Ym-scc);
 line(Xm-scc,Ym+scc,Xm+scc,Ym+scc);
 getch();
 closegraph();
}
*/



#endif
Соседние файлы в папке Лабораторная работа 32