C++实现Barnsley Fern

C++实现Barnsley Fern

1.安装EasyX_2014冬至版
https://www.easyx.cn/Files/downloads/EasyX_2014冬至版.zip
在这里插入图片描述双击安装即可
它会自动检索你电脑里的VS版本,没有的版本即是灰色的
在这里插入图片描述
2.打开Visual Studio 2012编写程序,头文件是graphics.h(此头文件是windows.h下的)
3.参考了其他博主的C语言编写的程序
在这里插入图片描述
此程序是可以运行的,但是和我需要的图片效果不一样,因为这个巴恩斯利蕨是倒过来的。

原因就在于windows平台下的窗口纵轴向下是正,此时需要将y换成-y。

还有一点是运行出的窗口一下子就消失了,原因不明。

4.改进的代码(包括巴恩斯利蕨和蕨叶)

#include <graphics.h>    
#include<time.h>
void main()
{
    HDC hdc = ::GetDC(NULL);
	double x=0.0,y=0.0;
	srand((unsigned)time(0));

	//巴恩斯利蕨
	for (int i=0; i<20000; i++){
		int n = rand()%100;
		double _x, _y;
        if (n < 1){
            _x = 0; _y = 0.16*y;
        }
        else if(n < 8){
            _x = 0.2*x - 0.26*y;_y = 0.23*x + 0.22*y + 1.6;
        }
        else if (n < 15){
            _x = -0.15*x + 0.28*y;_y = 0.26*x + 0.24*y + 0.44;
        }
        else{
            _x = 0.85*x + 0.04*y;_y = -0.04*x + 0.85*y + 1.6;
        }
        x = _x;y = _y;
		SetPixel(hdc,(int)((x*30)+600),(int)(500-(y*30)),RGB(0,255,0));
	}

	//蕨叶
	for(int i=0; i<20000; i++){
		int n = rand()%100;
		double _x, _y;
        if (n < 5){
            _x = 0; _y = 0.5*y;
        }
        else if(n < 45){
            _x = 0.12*x - 0.82*y;_y = 0.42*x + 0.42*y + 0.2;
        }
        else if (n < 85){
            _x = 0.12*x + 0.82*y;_y = -0.42*x + 0.42*y + 0.2;
        }
        else{
            _x = 0.1*x;_y = 0.1*y + 0.2;
        }
        x = _x;y = _y;
		SetPixel(hdc,(int)((x*200)+350),(int)(600-(y*200)),RGB(0,255,0));
	}
	system("pause");
}

效果图:
在这里插入图片描述
因为窗口会闪退,所以在代码段加上system(“pause”);

这样虽然不会出现闪退了,但是在鼠标点击了窗口范围内时图片依然会消失。

5.使用OpenGL实现巴恩斯利蕨

OpenGL配置

参考博客地址
https://www.cnblogs.com/chen-cai/p/7909737.html

配置文件下载地址:
http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip

下载完成解压后:会有glut.h glut.lib glut32.lib glut.dll glut32.dll这五个文件

(1)将头文件glut.h放在 C:\Program Files\Microsoft Visual Studio 11.0\VC\include\gl\下(即:自己的Visual Studio 2012的安装目录下)。找到\vc\include文件夹,若没有gl文件,自己新建一个文件夹,名为gl(大小写都可以),将glut.h放进去。

(2)将静态库文件glut.lib和glut32.lib放在 C:\Program Files\Microsoft Visual Studio 11.0\VC\lib\下

(3)将动态库文件glut.dll和glut32.dll放在 C:\Windows\System32\下。

通过以上3步,OpenGL的配置即完成,你可以找段程序进行测试。

可能会出现:

OpenGL程序无法启动此应用程序,因为计算机中丢失glut32.dll这种情况。

原因:自己的电脑是64位的,但是别急,有接口C:\Windows\SysWOW64\(文件SysWOW64是为了让32位的应用程序可以不经过任何修改就运行在64位的系统上所提供的接口)

此时把动态库文件glut32.dll复制到C:\Windows\SysWOW64\下即可。

6.代码实现

参考博客:
https://blog.csdn.net/qq_23912545/article/details/70233606

#include <GL/glut.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<time.h>
void fractalFern(void){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.4f, 0.8f, 0.4f);
    glBegin(GL_POINTS);
    GLfloat x=0,y=0;
    srand((unsigned)time(0));

    for (int i = 0; i < 50000;i++){

            int n = (int) 100*rand()/(RAND_MAX + 1);
            //printf("%d ", n);
            GLfloat _x, _y;
            if (n < 1){
                _x = 0; _y = 0.16*y;
            }
            else if(n < 8){
                _x = 0.2*x - 0.26*y;
                _y = 0.23*x + 0.22*y + 1.6;
            }
            else if (n < 15){
                _x = -0.15*x + 0.28*y;
                _y = 0.26*x + 0.24*y + 0.44;
            }
            else{
                _x = 0.85*x + 0.04*y;
                _y = -0.04*x + 0.85*y + 1.6;
            }
            x = _x;
            y = _y;
            glVertex2f(_x/10, _y/10-0.3);
            glFlush();
    }
    glEnd();
    glFlush();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(450, 450);
    glutCreateWindow("巴恩斯利蕨");
    glutDisplayFunc(&fractalFern);
    glutMainLoop();
    return 0;
}

效果图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值