Разработка функций, рисующих на экране незакрашенный и закрашенный параллелограммы, страница 2

void savetofile(char *filename);

};

//---konstruktor---------------

parallelogramm::parallelogramm()

{

for(int i=0;i<4;i++)

      {

      point[i].x=0;

      point[i].y=0;

      }

fillcolor=15;

linecolor=15;

tolshina=1;

linestyle=0;

fillstyle=1;

}

//---proverjaem vhodit li v predely ekrana,

//menjaem nevernie parametry vida------

int parallelogramm::ok()

{

for (int i=0;i<4;i++)

      {

      if (fillcolor<0 || fillcolor>15)fillcolor=15;

      if (linecolor<0 || linecolor>15)linecolor=15;

      if (tolshina!=1 && tolshina!=3)tolshina=1;

      if (linestyle<0 || linestyle>3)linestyle=0;

      if (fillstyle<0 || fillstyle>11) fillstyle=1;

      //--proverjaem geometricheskie parametry-----------

      if (point[i].x<0||point[i].x>639)return 0;

      if (point[i].y<0||point[i].y>479)return 0;

      }

return 1;

}

//---vi4islenie nedostauwih verwin---------

void parallelogramm::count()

{

dx=center.x-point[0].x;

dy=center.y-point[0].y;

point[2].x=center.x+dx;

point[2].y=center.y+dy;

dx=center.x-point[1].x;

dy=center.y-point[1].y;

point[3].x=center.x+dx;

point[3].y=center.y+dy;

}

//---opredelenie koordinat------------

void parallelogramm::set_coordinates(int x1,int y1,int x2,int y2,int x3,int y3)

{

point[0].x=x1;

point[0].y=y1;

point[1].x=x2;

point[1].y=y2;

center.x=x3;

center.y=y3;

count();

}

//---risyem ---------

void parallelogramm::draw()

{

setcolor(linecolor);

setlinestyle(linestyle,0,tolshina);

moveto(point[0].x,point[0].y);

for (int i=1;i<4;i++)

      {

      lineto(point[i].x,point[i].y);

      }

lineto(point[0].x,point[0].y);

}

//---zakrawivaem-----------

void parallelogramm::drawfill()

{

int fill[8];

draw();

setfillstyle(fillstyle,fillcolor);

fill[0]=point[0].x;fill[4]=point[2].x;

fill[1]=point[0].y;fill[5]=point[2].y;

fill[2]=point[1].x;fill[6]=point[3].x;

fill[3]=point[1].y;fill[7]=point[3].y;

fillpoly(4,fill);

}

int parallelogramm::findinside(parallelogramm *obj)

{

double S,s,dlina1,dlina2,dlina3,p,od1,od2,od3;

dlina1=sqrt(sqr(point[1].x-point[0].x)+sqr(point[1].y-point[0].y));

dlina2=sqrt(sqr(point[3].x-point[0].x)+sqr(point[3].y-point[0].y));

dlina3=sqrt(sqr(point[1].x-point[3].x)+sqr(point[1].y-point[3].y));

p=(dlina1+dlina2+dlina3)*0.5;

S=sqrt(sqr(p-dlina1)*sqr(p-dlina2)*sqr(p-dlina3))*2;

for (int i=0;i<4;i++)

      {

      od1=sqrt(sqr(point[0].x- obj->point[i].x)+sqr(point[0].y- obj->point[i].y));

      od2=sqrt(sqr(point[1].x- obj->point[i].x)+sqr(point[1].y- obj->point[i].y));

      p=(dlina1+od1+od2)*0.5;

      s=sqrt(sqr(p-dlina1)*sqr(p-od1)*sqr(p-od2));

      od3=sqrt(sqr(point[3].x- obj->point[i].x)+sqr(point[3].y-obj->point[i].y));

      p=(dlina2+od1+od3)*0.5;

      s=s+sqrt(sqr(p-dlina2)*sqr(p-od1)*sqr(p-od3));

      od1=sqrt(sqr(point[2].x- obj->point[i].x)+sqr(point[2].y- obj->point[i].y));

      p=(dlina2+od1+od2)*0.5;

      s=s+sqrt(sqr(p-dlina2)*sqr(p-od1)*sqr(p-od2));

      p=(dlina1+od1+od3)*0.5;

      s=s+sqrt(sqr(p-dlina1)*sqr(p-od1)*sqr(p-od3));

      if (S-s<0) {i=4;return 0;}

      }

return 1;

}

//---chitaem parametri iz faila--------

int parallelogramm::readfromfile(char *filename)

{

FILE *f;

if ((f=fopen(filename,"r"))!=0)

      {

      fscanf(f,"%i %i",&point[0].x,&point[0].y);

      fscanf(f,"%i %i",&point[1].x,&point[1].y);

      fscanf(f,"%i %i",&center.x,&center.y);

      fscanf(f,"\nlinecolor=%i",&linecolor);

      fscanf(f,"\nlinestyle=%i",&linestyle);

      fscanf(f,"\nfillcolor=%i",&fillcolor);

      fscanf(f,"\nfillstyle=%i",&fillstyle);

      fscanf(f,"\nlinewidth=%i",&tolshina);

      fclose(f);

      count();

      }

else return 0;

return 1;

}

void parallelogramm::savetofile(char *filename)

{

FILE *f;

f=fopen(filename,"w");

fprintf(f,"%i %i",point[0].x,point[0].y);

fprintf(f,"\n%i %i",point[1].x,point[1].y);

fprintf(f,"\n%i %i",center.x,center.y);

fprintf(f,"\nlinecolor=%i",linecolor);

fprintf(f,"\nlinestyle=%i",linestyle);

fprintf(f,"\nfillcolor=%i",fillcolor);

fprintf(f,"\nfillstyle=%i",fillstyle);

fprintf(f,"\nlinewidth=%i",tolshina);

fclose(f);

}