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

  • 相关阅读:
    容斥原理学习(Hdu 4135,Hdu 1796)
    ACdream 1112
    CodeChef--Cards, bags and coins
    ACdream 1108(莫队)
    Hdu 2586(LCA)
    CodeChef--EQUAKE
    Hackerrank--Mixing proteins(Math)
    Clash Credenz 2014 Wild Card Round题解
    Codeforces 463D
    CodeChef August Lunchtime 2014 题解
  • 原文地址:https://www.cnblogs.com/starryxsky/p/7181850.html
Copyright © 2020-2023  润新知