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就变为“画线图”

  • 相关阅读:
    maven 仓库配置 pom中repositories属性
    Spring Boot集成持久化Quartz定时任务管理和界面展示
    gradle使用总结
    sqlserver 分页
    MyBatis特殊字符转义
    Mybatis中#{}和${}传参的区别及#和$的区别小结
    Markdown 手册
    Spring boot——logback.xml 配置详解(四)<filter>
    Spring boot——logback.xml 配置详解(三)<appender>
    Spring boot——logback.xml 配置详解(二)
  • 原文地址:https://www.cnblogs.com/starryxsky/p/7181850.html
Copyright © 2011-2022 走看看