zoukankan      html  css  js  c++  java
  • OpenGL编程 基础篇(三)用点集绘制函数

    绘制函数 f(x) = e^(-x) * cos(2pi*x) 和 f(x) = e^|x| * cos(2pi * x)

    #include "stdafx.h"
    #include <windows.h>
    #include <math.h>
    #include <glGL.h>
    #include <glglut.h>
    const int screenWidth = 640;
    const int screenHeight = 480;
    GLdouble A,B,C,D;
    //myInit
    int abs(int x){ return x > 0 ? x : -x; }
    void myInit(){
        glClearColor(1.0, 1.0, 1.0, 0.0);
        glColor3f(0.0f, 0.0f, 0.0f);
        glPointSize(2.0);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
        A = screenWidth / 4.0;
        B = 0.0;
        //C = screenHeight / 2.0; //f(x) = e^(-x) * cos(2pi*x)的坐标变换 
       C
    = screenHeight / 32.0; //f(x) = e^|x| * cos(2pi * x)的坐标变换 D = screenHeight / 2.0; } void myDisplay(){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); for (GLdouble x = 0.0; x < 4.0; x += 0.01) {
         //GLdouble func = exp(-x) * cos(2 * 33.1415927 * x); GLdouble func
    = exp(abs(-x)) * cos(2 * 3.1415926 * 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(); }

    运行截图:

       

                               f(x) = e^(-x) * cos(2pi*x)                                

                               f(x) = e^|x| * cos(2pi * x)   

    说明:

    1.这里采用的坐标是强制转换的,需要调整ABCD的值来使函数在显示窗口中央。其中fun1的A= 1/4.0 B = 0.0 C = 1/2.0 D = 1/2,0 ;func2 A = 1/4 B = 0.0 C = 1/32.0 D = 1/2.0

    sx = A * x + B

    sy = C * y + D

     2.将GL_POINTS 改为 GL_LINE_STRIP就变为“画线图”

  • 相关阅读:
    VC.【转】采用_beginthread/_beginthreadex函数创建多线程
    Qt532.【转】Qt在pro中设置运行时库MT、MTd、MD、MDd,只适合VS版本的Qt
    Qt532界面.ZC测试
    Qt532.【转】Qt创建鼠标右键菜单
    VC.【转】窗口置于前台并激活的方法
    VS2010.STL::list的一个bug
    STL_map.VC6简单使用例子
    LeetCode题解-----Maximum Gap
    CEPH块设备创建及快照
    Ubuntu 14.04 部署 CEPH集群
  • 原文地址:https://www.cnblogs.com/starryxsky/p/7181850.html
Copyright © 2011-2022 走看看