#include <iostream>
#include <process.h>
#include "stdafx.h"
#include <GL/glew.h>
#include <GL/glut.h>
#pragma comment(lib, "glew32.lib")
using namespace std;
void lagrange()
{
float p[10][2];
p[0][0] = 5, p[0][1] = 31.2;
p[1][0] = 7, p[1][1] = 43.7;
p[2][0] = 13, p[2][1] = 59.1;
p[3][0] = 16, p[3][1] = 72;
p[4][0] = 20, p[4][1] = 94.5;
p[5][0] = 25, p[5][1] = 122;
p[6][0] = 27, p[6][1] = 129.2;
p[7][0] = 34, p[7][1] = 153;
p[8][0] = 52, p[8][1] = 183;
p[9][0] = 60, p[9][1] = 197.8;
//得到数据点
int nmax = 10;
int n = 4;
float x, y;
int i, j, t;
float temp;
glColor3f(0.0, 0.0, 1.0);
for (i = 0; i <= (nmax - 1); i += (n - 1))
{
glBegin(GL_LINE_STRIP);
for (x = p[i][0]; x <= p[i + n - 1][0]; x += 1.0)
{
y = 0.0;
for (j = 0; j<n; j++)
{
temp = 1.0;
for (t = 0; t<n; t++)
{
if (t == j)
continue;
temp *= (x - p[i + t][0]) / (p[i + j][0] - p[i + t][0]);
}
y += temp*p[i + j][1];
}
glVertex2f(x/nmax, y/200);
}
glEnd();
}
glFlush();
}
//拉格朗日插值法&画图
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(600, 600);
glutCreateWindow("The first OpenGL Application");
glClearColor(0.0, 0.0, 0.0, 0.0);
glutDisplayFunc(&lagrange);
glutMainLoop();
return 0;
//lagrange(p, 4, 10);
}
如图,透明的,我咋调glclearcolor的透明度都这样