#include "stdafx.h"
#include <GL/glut.h>
#include <math.h>
const int screenWidth = 640; //屏幕窗口的宽度为640像素;
const int screenHeight = 480; //屏幕窗口的高度为480像素;
GLdouble A, B, C, D; //比例变换和平移值;
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); //背景颜色为白;
glColor3f(0.0f, 0.0f, 0.0f); //画图颜色为黑;
glPointSize(2.0); //笔触大小为2像素;
glMatrixMode(GL_PROJECTION); //设置合适的矩阵;
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
A = screenWidth / 4.0; //设置比例变换和平移;
B = 0.0;
C = D = screenHeight / 2.0;
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); //清屏;
glBegin(GL_POINTS); for (GLdouble x = 0; x < 4.0; x += 0.005)
{
GLdouble func = exp(-x)*cos(2 * 3.14159265*x);
glVertex2d(A*x + B, C*func + D);
}
glEnd();
glFlush(); //送所有数据到显示;
}
void main(int argc, char** argv)
{
glutInit(&argc, argv); //初始工具包;
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //设置显示模式;
glutInitWindowSize(screenWidth, screenHeight); //设置窗口大小;
glutInitWindowPosition(100, 150); //设置窗口在屏幕上的位置;
glutCreateWindow("Dot Plot of a Function"); //打开屏幕窗口;
glutDisplayFunc(myDisplay); //注册重画函数;
myInit();
glutMainLoop(); //进入永久循环;
}