#include <graphics.h>
#include <process.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>

#define Tab 100 //от 70 до 230 ( рекомендуем 100 )
#define koeff 1.01 //д.б. <= 1 (esli 1 to popikselno)

float func( float*);

void isoline(void)
{
  int 	gdriver = DETECT,
	gmode,
	errorcode;
  short nX, nY,
  mono = 2;
  int Color[] = { 15, 14, 12, 4, 13, 11, 9, 1, 7, 8,
		   0,  0,  0, 0,  0,  0, 0, 0, 0, 0 };
  float  deltaX, deltaY, deltaF,
	 dX, dY, dF,
	 F, _X_[2],
	 minX, maxX, minY, maxY, minF, maxF,
	 _X, _Y, _F;
  short	i, j, k, maxcolor,
	width, height,
	mX, mY, Dim,
	illustr;
	char S[40];
	char S1[40];
	FILE *in;

	in = fopen( "raboch.dat", "r");

	fscanf( in, "%d", &Dim);

	if ( Dim != 2) exit( 1);
	printf( "Проиллюстрировать:\nНет(n)\tДля принтера(p)\tВ цвете(c) :");
	illustr = getch();
	putch( illustr);
	printf( "\n");
	if (( illustr == 'n')||( illustr == 'N')) return;

	fscanf( in, "%f%f%f", &minX, &minY, &minF);

	maxX = minX;
	maxY = minY;
	maxF = minF;

	while ( fscanf( in, "%f%f%f", &_X, &_Y, &_F) == 3)
	{
	  if ( _X > maxX) maxX = _X;
	  if ( _X < minX) minX = _X;
	  if ( _Y > maxY) maxY = _Y;
	  if ( _Y < minY) minY = _Y;
	  if ( _F > maxF) maxF = _F;
	  if ( _F < minF) minF = _F;
	   }

	   deltaX = maxX - minX;
	   deltaY = maxY - minY;
	   deltaF = (maxF - minF)*0.7+0.001;

	   minX -= deltaX/10 + 0.0005;
	   maxX += deltaX/10 + 0.0006;
	   minY -= deltaY/10 + 0.0005;
	   maxY += deltaY/10 + 0.0006;

	   deltaX = maxX - minX;
	   deltaY = maxY - minY;

	   fclose( in);

	   initgraph(&gdriver, &gmode, "");

	   maxcolor = sizeof( Color)/sizeof( Color[0])/2;

	   if ( ( illustr == 'p')||( illustr == 'P'))
	   {
		maxcolor *= 2;
		mono = 5;
		for ( i = 0; i < maxcolor; i += 2 )
		 for ( j = 0; j < 2; j++)
			Color[ i+j] = j;
	   }

	   height = getmaxy()-2*Tab;
	   width  = getmaxx()-2*Tab;

	   nX = width*koeff;
	   nY = height*koeff;

	   dX = deltaX/nX;
	   dY = deltaY/nY;
	   dF = deltaF/maxcolor;

	   setfillstyle(CLOSE_DOT_FILL, DARKGRAY);
	   if (( illustr != 'p')&&( illustr != 'P'))
			bar( Tab,Tab,Tab+width,Tab+height);

	   setcolor( WHITE);
	   line( Tab-2, Tab/2+2,      Tab-2,         height+Tab+1);
	   line( Tab-2, height+Tab+1, width+2*Tab-2, height+Tab+1);
	   line( Tab+width+2, Tab-2, Tab+width+2, height+Tab+1);
	   line( Tab+width+2, Tab-3, Tab-2, Tab-3);

	   setcolor( RED);
	   outtextxy( width+Tab*1.5+5, height+Tab-10, "X1");
	   outtextxy( Tab+5, Tab/2, "X2");

	   setcolor( LIGHTBLUE);
	   sprintf( S, "%8.3f", minX);
	   outtextxy( Tab, height+Tab+5, S);

	   sprintf( S, "%8.3f", maxX);
	   outtextxy( width+Tab, height+Tab+5, S);

	   sprintf( S, "%8.3f", minY);
	   outtextxy( Tab/3, height+Tab-5, S);

	   sprintf( S, "%8.3f", maxY);
	   outtextxy( Tab/3, Tab, S);


	   for ( i = 1; i <= nX; i++)
		for ( j = 1; j <= nY; j++)
	    {
		_X_[0] = minX+i*dX;
		_X_[1] = minY+j*dY;
		F = func( _X_);

		for ( k = 0; k < maxcolor; k++)
		{
			if ( fabs( F-minF-k*dF) <= dF/mono)
			{

			   mX = width/deltaX*dX*i+Tab;
			   mY = height*(1-j*dY/deltaY)+Tab; //масштабирование

			   if ( koeff < 0.3 )
			   {
			       setcolor(Color[k]);
			       circle(mX,mY,1);
			       circle(mX,mY,2);
			   }
			   else  putpixel(mX, mY, Color[k]);
			   break;
			}//end if
		}//end for k
	    }//end for i,j

	    in = fopen( "raboch.dat", "r");

	    fscanf( in, "%d%f%f%f", &Dim, &_X, &_Y, &_F);

	    mX = width*(_X-minX)/deltaX+Tab;
	    mY = height*(1-(_Y-minY)/deltaY)+Tab;

	    moveto  ( mX, mY);
	    setcolor( GREEN);
	    circle  ( mX, mY, 5);
	    circle  ( mX, mY, 4);

	    setlinestyle( SOLID_LINE, 1, 2);

	    while ( fscanf( in, "%f%f%f", &_X, &_Y, &_F) == 3)
	    {
		mX = width*(_X-minX)/deltaX+Tab;
		mY = height*(1-(_Y-minY)/deltaY)+Tab;
		lineto( mX, mY);
	    }
	    setcolor( RED);
	    circle( mX, mY, 1);
	    circle( mX, mY, 2);
	    circle( mX, mY, 3);

	    fclose( in);
	    getch();
	    closegraph();
}
