大佬们帮忙看看为什么 纯萌新 代码照网上敲的

#include <graphics.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
using namespace std;

//
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
//
struct Point
{
    int x = 0;
    int y = 0;
};
//
//
//
//
struct Atom
{
    static const int width = 2;
    static const int height = 4;
    int x = 0;
    int y = 0;
    COLORREF color;
};

//
vector<Atom> _atomGroup;  //
vector<Atom> _wordGroup;  //

double _angle_x = 0;  //
double _angle_y = 0;  //

ExMessage _lastMsg;  //
ExMessage _nowMsg;  //

//

void InitQuietHeart();  //
void BeatHeart();  //
void InitBlessingWord();  //
void Rotate();  //
void Draw();  //
Point GetConvertPoint(int x, int y);  //

int main()
{
    initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
    setorigin(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
    SetWindowText(GetHWnd(), _T("致你的七夕礼物 —— 3D Heart"));

    BeatHeart();  //
    InitBlessingWord();  //
    Draw();  //
    bool beating = true;  //

    while (true)  //
    {
        ExMessage msg;
        while (peekmessage(&msg, EX_MOUSE))  //
        {
            //
            if (msg.message == WM_LBUTTONDOWN)
            {
                _lastMsg = msg;
            }
            //
            if (msg.lbutton && msg.message == WM_MOUSEMOVE)
            {
                beating = false;  //
                _nowMsg = msg;  //
                Rotate();  //
                _lastMsg = _nowMsg;  //
                Draw();  //
                circle(0, 0, 2);  //
            }
            else if (msg.rbutton)
            {
                beating = false;  //
            }
            else
            {
                beating = true;  //
            }
        }
        //
        if (beating)
        {
            BeatHeart();
            Draw();
        }
        //
        Sleep(16);
    }
    //
    closegraph();
    return 0;
}

//
float f(float x, float y, float z)
{
    float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
    return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}

//
float h(float x, float z)
{
    for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
    {
        if (f(x, y, z) <= 0.0f)
        {
            return y;
        }
    }
    return 0.0f;
}

void InitQuietHeart()
{
    //
    int _x = 0;
    int _y = -30;
    for (float z = 1.5f; z > -1.5f; z -= 0.05f)
    {
        _x = -60;
        for (float x = -1.5f; x < 1.5f; x += 0.025f)
        {
            float v = f(x, 0.0f, z);
            if (v <= 0.0f)
            {
                //
                Atom a;
                //
                a.x = _x;
                a.y = _y;
                //
                float y0 = h(x, z);
                float ny = 0.01f;
                float nx = h(x + ny, z) - y0;
                float nz = h(x, z + ny) - y0;
                float nd = 1.0f / sqrtf(nx * nx + ny * ny * ny + nz * nz);
                float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
                switch ((int)(d * 5.0f))
                {
                case 0: a.color = 0x111133; break;
                case 1: a.color = 0x111144; break;
                case 2: a.color = 0x111155; break;
                case 3: a.color = 0x111177; break;
                case 4: a.color = 0x111199; break;
                case 5: a.color = 0x1111aa; break;
                case 6: a.color = 0x2222cc; break;
                case 7: a.color = 0x2222ee; break;
                case 8: a.color = 0x3333ff; break;
                }
                _atomGroup.push_back(a);
            }
            _x++;
        }
        _y++;
    }
}

void BeatHeart()
{
    //
    static float t = 0.0f;
    t += (t > 100) ? -100 : 0.1f;
    //
    float s = sinf(t);
    float a = s * s * s * s * 0.2f;
    //
    _atomGroup.clear();
    //
    int _x = 0;
    int _y = -20;
    for (float z = 1.3f; z > -1.2f; z -= 0.05f)
    {
        float tz = z * (1.2f - a);
        _x = -60;
        for (float x = -1.5f; x < 1.5f; x += 0.025f)
        {
            float tx = x * (1.2f + a);
            float v = f(tx, 0.0f, tz);
            if (v <= 0.0f)
            {
                //
                Atom atom;
                //
                atom.x = _x;
                atom.y = _y;
                //
                //
                float y0 = h(tx, tz);
                float ny = 0.01f;
                float nx = h(tx + ny, tz) - y0;
                float nz = h(tx, tz + ny) - y0;
                float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
                float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
                switch ((int)(d * 5.0f))
                {
                case 0: a.color = 0x111133; break;
                case 1: a.color = 0x111144; break;
                case 2: a.color = 0x111155; break;
                case 3: a.color = 0x111177; break;
                case 4: a.color = 0x111199; break;
                case 5: a.color = 0x1111aa; break;
                case 6: a.color = 0x2222cc; break;
                case 7: a.color = 0x2222ee; break;
                case 8: a.color = 0x3333ff; break;
                }
                //
                _atomGroup.push_back(atom);
            }
            _x++;
        }
        _y++;
    }
}

void InitBlessingWord()
{
    char blessing[7][34] = {  //
        { 0,0,0,0,0,0,0, 0,0, 0,0,0,1,0,0,0, 0,0, 0,1,0,0,1,0,0, 0,0, 0,0,0,0,1,0,0 },
        { 0,0,1,0,0,0,1, 0,0, 0,0,1,1,1,0,0, 0,0, 0,1,0,1,1,1,1, 0,0, 0,1,1,1,0,0,0 },
        { 0,0,1,0,0,1,0, 0,0, 0,1,1,0,0,1,0, 0,0, 0,1,0,0,1,0,1, 0,0, 1,0,0,1,0,0,0 },
        { 0,0,1,1,1,0,0, 0,0, 1,0,0,1,0,1,0, 0,0, 1,1,1,1,1,1,1, 0,0, 1,1,1,1,1,1,1 },
        { 0,1,1,0,0,0,1, 0,0, 0,0,0,0,1,0,0, 0,0, 0,1,0,0,1,0,0, 0,0, 0,0,0,1,0,0,0 },
        { 1,0,0,1,1,1,0, 0,0, 0,0,0,1,0,0,0, 0,0, 0,1,0,1,0,1,0, 0,0, 0,1,0,1,0,1,0 },
        { 0,0,0,0,0,0,0, 0,0, 0,0,1,0,0,0,0, 0,0, 0,1,1,0,0,0,1, 0,0, 1,0,0,1,0,0,1 },
    };
    //
    for (int i = 0; i < 7; i++)
    {
        for (int j = -17; j < 17; j++)
        {
            if (blessing[i][j + 17] == 1)
            {
                Atom a;
                a.x = j;
                a.y = 30 + i;
                a.color = WHITE;
                _wordGroup.push_back(a);
            }
        }
    }
}

void Rotate()
{
    //
    _angle_x += (_nowMsg.x - _lastMsg.x) / 100.0;
    _angle_y += (_nowMsg.y - _lastMsg.y) / 100.0;
    //
    if (fabs(_angle_x) > 2 * M_PI)
    {
        _angle_x > 0 ? _angle_x -= 2 * M_PI : _angle_x += 2 * M_PI;
    }
    if (fabs(_angle_y) > 2 * M_PI)
    {
        _angle_y > 0 ? _angle_y -= 2 * M_PI : _angle_y += 2 * M_PI;
    }
}

void Draw()
{
    static Point p;
    //
    BeginBatchDraw();
    //
    cleardevice();
    //
    for (Atom a : _atomGroup)
    {
        //
        setfillcolor(a.color);
        //
        p = GetConvertPoint(a.x, a.y);
        //
        solidrectangle(p.x - Atom::width / 2, p.y - Atom::
            height / 2, p.x + Atom::width / 2, p.y + Atom::
            height / 2);
    }
    //
    setfillcolor(WHITE);
    for (Atom a : _wordGroup)
    {
        //
        p = GetConvertPoint(a.x, a.y);
        //
        solidrectangle(p.x - 1, p.y - 1, p.x + 1, p.y + 1);
    }
    //
    EndBatchDraw();
}
Point GetConvertPoint(int x, int y)
{
    static int x0, y0;
    //
    x *= Atom::width;
    y *= Atom::height;
    //
    x0 = int(x * cos(_angle_x) + y * sin(_angle_y) * sin(_angle_x));
    y0 = int(y * cos(_angle_y) + x * sin(_angle_x) * sin(_angle_y));
    return Point({ x0, y0 });
}

  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值