Основы компьютерной графики. Программа построения сплайна, страница 2

glPointSize(3);

glBegin(GL_POINTS);

for (i=0; i<=n; i++)

glVertex2d(X[i],Y[i]);

glEnd();

}

//построить функцию на k-ом отрезке

void drow()

{int i;

double h,x;

// glPointSize(3); glBegin(GL_POINTS);

if(f1)

{glColor3d(0.0,1.0,0.0);

glLineWidth(1); glBegin(GL_LINES);

for(i=1; i<=n; i++)

{h=1.0/(100*((X[i]-X[i-1])*(X[i]-X[i-1])+(Y[i]-Y[i-1])*(Y[i]-Y[i-1])));

x=X[i-1];

do{glVertex2d(x,f(Y[i-1],b[i],c[i],d[i],x-X[i-1]));

x+=h;//0.0001;

glVertex2d(x,f(Y[i-1],b[i],c[i],d[i],x-X[i-1]));

}while(x<X[i]);

}

glEnd();

}

if(f2)

{glColor3d(0.0,0.0,1.0);

glLineWidth(1); glBegin(GL_LINES);

for(i=1; i<=n; i++)

{h=1.0/(100*((X[i]-X[i-1])*(X[i]-X[i-1])+(Y[i]-Y[i-1])*(Y[i]-Y[i-1])));

x=X[i-1];

do{glVertex2d(x,f_(b[i],c[i],d[i],x-X[i-1]));

x+=h;//0.0001;

glVertex2d(x,f_(b[i],c[i],d[i],x-X[i-1]));

}while(x<X[i]);

}

glEnd();

}

if(f3)

{glColor3d(1.0,0.0,0.0);

glLineWidth(1); glBegin(GL_LINES);

for(i=1; i<=n; i++)

{h=1.0/(100*((X[i]-X[i-1])*(X[i]-X[i-1])+(Y[i]-Y[i-1])*(Y[i]-Y[i-1])));

x=X[i-1];

do{glVertex2d(x,f__(c[i],d[i],x-X[i-1]));

x+=h;//0.0001;

glVertex2d(x,f__(c[i],d[i],x-X[i-1]));

}while(x<X[i]);

}

glEnd();

}

};

void sort(int n)

{int i,flag;

double x;

do{flag=0;

for(i=0; i<n; i++)

if(X[i]>X[i+1])

{x=X[i]; X[i]=X[i+1]; X[i+1]=x;

x=Y[i]; Y[i]=Y[i+1]; Y[i+1]=x;

flag=1;

}

}while(flag);

};

void display()

{glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

build_point(); // Изобразить точки

if(n>0)

{sort(n); // Сортировка y по x 

count(); //вычисление коэффициентов

drow();  // Отрисовать сплайн

}

glFinish();

SwapBuffers(wglGetCurrentDC());

}

LRESULT CALLBACK WindowFunc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)

{PAINTSTRUCT ps;

switch(msg)

{case WM_CREATE:

{hDC=GetDC(hWnd);

SetWindowPixelFormat();

hGLRC=wglCreateContext(hDC);

wglMakeCurrent(hDC, hGLRC);

glEnable(GL_ALPHA_TEST);

glEnable(GL_DEPTH_TEST);

glEnable(GL_COLOR_MATERIAL);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

return 0;

};

case WM_DESTROY:

{

if (hGLRC)

{wglMakeCurrent(NULL, NULL);

wglDeleteContext(hGLRC);

}

ReleaseDC(hWnd, hDC);

PostQuitMessage(0);

return 0;

};

case WM_PAINT:

{BeginPaint(hWnd, &ps);

display();

EndPaint(hWnd, &ps);

return 0;

};

case WM_KEYDOWN:

{switch(wParam)

{case 49: f1=!f1; break;

case 50: f2=!f2; break;

case 51: f3=!f3; break;

case 32: n=-1  ; break; 

}

display();

return 0;

}

case WM_LBUTTONDOWN:

{int stop=1,i;

gx=LOWORD(lParam);

gy=HIWORD(lParam);

tx=10*gx/scrx-5;

ty=5-10*gy/scry;          

for(i=0; stop&&i<=n; i++) if ( scrx*fabs(tx-X[i])<5.0 ) stop=0;

if (stop) {n++;

X[n]=tx;

Y[n]=ty;

display();         

}

else SendMessage(hWnd,WM_RBUTTONDOWN,1,1);

return 0 ;

};

/*case WM_RBUTTONDOWN:

n=-1;

display();        

return 0 ;

};*/

case WM_SIZE:

{

resize(LOWORD(lParam),HIWORD(lParam));

return 0;

};

default:

return DefWindowProc(hWnd,msg,wParam,lParam);

};

return 0;

};

int WINAPI WinMain(

HINSTANCE hThisInst,HINSTANCE hPrevInst,

LPSTR str,int nWinMode)

{ MSG msg;

WNDCLASS wcl;

X=new double[maxpoint];

Y=new double[maxpoint];

b=new double[maxpoint];

c=new double[maxpoint-1];

d=new double[maxpoint-1];

wcl.hInstance=hThisInst;

wcl.lpszClassName="OpenGLWinClass";

wcl.lpfnWndProc=WindowFunc;

wcl.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW;

wcl.hIcon=NULL;

wcl.hCursor=LoadCursor(NULL,IDC_ARROW);

wcl.lpszMenuName=NULL;

wcl.cbClsExtra=0;

wcl.cbWndExtra=0;

wcl.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

RegisterClass(&wcl);

hWnd=CreateWindow

(          "OpenGLWinClass",

"Лагранжев интерполяционный кусочно кубический сплайн",

WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,

1,

1,

400,

400,

HWND_DESKTOP,

NULL,

hThisInst,

NULL

);

ShowWindow(hWnd,nWinMode);

UpdateWindow(hWnd);

while (GetMessage(&msg,NULL,0,0))

{TranslateMessage(&msg);

DispatchMessage(&msg);

};

delete [] X;

delete [] Y;

delete [] b;

delete [] c;

delete [] d;

//delete [] gld;

return msg.wParam;

};