首页 > 编程语言 >C++绘制玫瑰花

C++绘制玫瑰花

时间:2022-10-07 15:00:08浏览次数:54  
标签:玫瑰花 cos int double rosesize C++ pow 绘制 dot

C++绘制玫瑰花

源码

#include <stdio.h>
#include <windows.h>
#include <math.h>
#include <graphics.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")

//定义全局变量
int rosesize = 500;
int h = -250;

//定义结构体
struct DOT {
    double x;
    double y;
    double z;
    double r;
    double g;
};

bool calc(double a, double b, double c, DOT& d) 
{
    double j, n, o, w, z;

    if (c > 60) 
    {
        // 花柄
        d.x = sin(a * 7) * (13 + 5 / (0.2 + pow(b * 4, 4))) - sin(b) * 50;
        d.y = b * rosesize + 50;
        d.z = 625 + cos(a * 7) * (13 + 5 / (0.2 + pow(b * 4, 4))) + b * 400;
        d.r = a * 1 - b / 2;
        d.g = a;
        return true;
    }
    double A = a * 2 - 1;
    double B = b * 2 - 1;
    if (A * A + B * B < 1) 
    {
        // 绿叶
        if (c > 37) {
            j = (int(c) & 1);
            n = j ? 6 : 4;
            o = 0.5 / (a + 0.01) + cos(b * 125) * 3 - a * 300;
            w = b * h;
            d.x = o * cos(n) + w * sin(n) + j * 610 - 390;
            d.y = o * sin(n) - w * cos(n) + 550 - j * 350;
            d.z = 1180 + cos(B + A) * 99 - j * 300;
            d.r = 0.4 - a * 0.1 + pow(1 - B * B, -h * 6) * 0.15 - a * b * 0.4 + cos(a + b) / 5 +
                pow(cos((o * (a + 1) + (B > 0 ? w : -w)) / 25), 30) * 0.1 * (1 - B * B);
            d.g = o / 1000 + 0.7 - o * w * 0.000003;
            return true;
        }

        if (c > 32) 
        { 
            // 花萼
            c = c * 1.16 - 0.15;
            o = a * 45 - 20;
            w = b * b * h;
            z = o * sin(c) + w * cos(c) + 620;
            d.x = o * cos(c) - w * sin(c);
            d.y = 28 + cos(B * 0.5) * 99 - b * b * b * 60 - z / 2 - h;
            d.z = z;
            d.r = (b * b * 0.3 + pow((1 - (A * A)), 7) * 0.15 + 0.3) * b;
            d.g = b * 0.7;
            return true;
        }
        // 花朵
        o = A * (2 - b) * (80 - c * 2);
        w = 99 - cos(A) * 120 - cos(b) * (-h - c * 4.9) + cos(pow(1 - b, 7)) * 50 + c * 2;
        z = o * sin(c) + w * cos(c) + 700;
        d.x = o * cos(c) - w * sin(c);
        d.y = B * 99 - cos(pow(b, 7)) * 50 - c / 3 - z / 1.35 + 450;
        d.z = z;
        d.r = (1 - b / 1.2) * 0.9 + a * 0.1;
        d.g = pow((1 - b), 20) / 4 + 0.05;
        return true;
    }

    return false;
}

int main()
{
    short *zBuffer;
    int x, y, z, zBufferIndex;
    DOT dot;
    initgraph(640, 480);//创建绘图窗口

    /*添加音乐*/
    //mciSendString("open BadApple.mp3 alias BadApple", NULL, 0, NULL);
    //mciSendString("play BAdApple", NULL, 0, NULL);

    setbkcolor(BLACK);
    cleardevice();

    zBuffer = new short[rosesize * rosesize];
    memset(zBuffer, 0, sizeof(short) * rosesize * rosesize);
    for (int j = 0; j < 2000; j++)
    {
        for (int i = 0; i < 10000; i++)
        {
            if (calc(double(rand()) / RAND_MAX, double(rand()) / RAND_MAX, rand() % 46 / 0.74, dot))
            {
                z = int(dot.z + 0.5);
                x = int(dot.x * rosesize / z - h + 0.5);
                y = int(dot.y * rosesize / z - h + 0.5);
                if (y > rosesize) continue;
                zBufferIndex = y * rosesize + x;
                if (!zBuffer[zBufferIndex] || zBuffer[zBufferIndex] > z)
                {
                    zBuffer[zBufferIndex] = z;
                    // 画点
                    int r = ~int((dot.r * h));
          
                    if (r < 0) r = 0;
                    
                    if (r > 255) r = 255;

                    int g = ~int((dot.g * h));
                    if (g < 0) g = 0;
                    if (g > 255) g = 255;

                    int b = ~int((dot.r * dot.r * -80));
                    if (b < 0) b = 0;

                    if (b > 255) b = 255;

                    putpixel(x + 50, y - 20, RGB(r, g, b));
                }
            }
        }
        Sleep(100);
    }
    delete[]zBuffer;
    getchar();
    closegraph();
    return 0;
}

效果

无法显示

标签:玫瑰花,cos,int,double,rosesize,C++,pow,绘制,dot
From: https://www.cnblogs.com/veis/p/16759746.html

相关文章

  • C/C++基于朋友圈的商品推荐系统
    C/C++基于朋友圈的商品推荐系统基于朋友圈的商品推荐【课题内容和要求】模拟基于朋友圈的商品推荐,可以用于任何电商,如淘宝、京东等。假设目前用户有100人,每人均有自己......
  • 模板基类与正确的派生类函数调用--Effective C++ Item 43
    问题描述假设我们有这样一个业务场景,我们管理着许多公司,每个公司都有一个自己的许多日志信息需要处理,于是为了方便,我们写了一个模板类用来处理这些公司的信息,并且将这些公......
  • C/C++语言 MD5例子
    之前研究了一下在C中进行MD5加密,由于找了很久没有找到现成的库文件,所以所幸自己去写了一下。个人感觉C的便捷性没有Python好的原因就是这里。下面是我写的一个例子。mian......
  • C/C++大学生考勤系统
    C/C++大学生考勤系统程序设计题:大学生考勤系统出题人:付竟芝面向专业:人文难度等级:31问题描述该系统要求实现一个简单、实用的学生考勤系统程序,主要功能包括考勤数据......
  • WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
    前言接着上周写的截图控件继续更新 绘制方框与椭圆。​​1.WPF实现截屏「仿微信」​​​​2.WPF实现截屏控件之移动(二)「仿微信」​​​​3.WPF截图控件之伸缩(三)「仿微......
  • 【C++】实现一个有理数类
    有理数:指所有可以用两个整数的商来表示的数Rational.hRational类的接口#ifndef_RATIONAL_H#define_RATIONAL_H#include<iostream>#include<string>classRa......
  • More Effective C++中文版 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1XpmUcUW-3tCqGlmZUqNVMg点击这里获取提取码 ......
  • C++ 条件变量(condition_variable)
    生产者消费者模式的代码(以下代码参考链接):#include<iostream>#include<thread>#include<mutex>#include<queue>#include<windows.h>#include<condition_variabl......
  • C++里的花括号{},块,作用域
    在C/C++中大括号指明了变量的作用域,在大括号内声明的局部变量其作用域自变量声明开始,到大括号之后终结,举例如下:intmain(){inta=0;{intb=0;......
  • c++小游戏
    #include<bits/stdc++.h>#include<windows.h>#include<stdio.h>#include<conio.h>#include<time.h>usingnamespacestd;intmain(){MessageBox(NULL,TEXT("......