y=4λx(1-x) y=x 逻辑图 源代码

来源:互联网 发布:淘宝学生手表 编辑:程序博客网 时间:2024/06/05 06:25
#include<windows.h>
#include<math.h>
#include <gl/Glut.h>
#include <iostream>

using namespace std;



struct GLPoint{
    GLdouble x, y;
};

GLPoint pt[2]={{0,0},{0,0}};

const GLint num = 50;            //循环最大限制

const GLint screenWidth = 600;
const GLint screenHeight = 600;
GLdouble A, B, C, D;

static GLdouble lambda = 0.7;        //λ = 0.7
static GLdouble startNum = 0.1;        // x的初始值

void myDisplay(void);
void myInit(void);

void setWindow(GLdouble left, GLdouble right , GLdouble botton, GLdouble top);
void setViewport(GLdouble left, GLdouble right , GLdouble botton, GLdouble top);

void drawLineToCurve(GLint i);
void drawLineToLine(GLint i);


int main(int argc, char ** argv){
    //cout<<"输入x起始值(0~)";

    pt[0].x = startNum;
    pt[0].y = 0.0;


    cout<<pt[0].x<<pt[0].y<<endl;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(screenWidth,screenHeight);
    glutInitWindowPosition(100,100);
    glutCreateWindow("y=4λx(1-x) y=x 逻辑图");

    setWindow(0,1,0,1);
    setViewport(0,screenWidth,0,screenHeight);

    glutDisplayFunc(myDisplay);

    myInit();
    glutMainLoop();

    return 0;
}

void setViewport(GLdouble left, GLdouble right , GLdouble botton, GLdouble top){
    glViewport(left,right,right-left,top-botton);
}


void setWindow(GLdouble left, GLdouble right , GLdouble botton, GLdouble top){
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(left,right,botton,top);
}

void drawLineToCurve(GLint i){
    
    pt[1].x = pt[0].x;
    pt[1].y = 4*lambda*pt[1].x*(1-pt[1].x);

    cout<<pt[1].x<<","<<pt[1].y<<"  ";
    glBegin(GL_LINES);
    glVertex2d(pt[0].x,pt[0].y);
    glVertex2d(pt[1].x,pt[1].y);
    glEnd();
    glFlush();
}

void drawLineToLine(GLint i){
    pt[0].y = pt[1].y;
    pt[0].x = pt[0].y;

    cout<<pt[0].x<<","<<pt[0].y<<"  ";
    glBegin(GL_LINES);
    glVertex2d(pt[1].x,pt[1].y);
    glVertex2d(pt[0].x,pt[0].y);
    glEnd();
    glFlush();
}


void myDisplay(void){
    glClear(GL_COLOR_BUFFER_BIT);
    // y = x ; 直线方程
    
    glBegin(GL_LINES);
    glVertex2d(0.0,0.0);
    glVertex2d(1.0,1.0);
    glEnd();
    glFlush();

    //曲线方程

    glBegin(GL_LINE_STRIP);
    for (GLdouble x = 0 ; x < 1 ; x += 0.01)
    {
        glVertex2d(x, 4*lambda*x*(1-x));
    }
    glEnd();
    glFlush();

    //曲线==》折线==》曲线==》折线... 焦点
    for (int i= 0 ; i < num; i ++)
    {
        if (i%2 == 0 )
        {
            drawLineToCurve(i);
        }
        else{
            drawLineToLine(i);
        }
    }
}

void myInit(void){
    glClearColor(1.0,1.0,1.0,0.0);
    glColor3f(0.0f, 0.0f, 0.0f);        

    glPointSize(1.0);
    glLineWidth(1.0);

}