C++ 用c+制作一颗心+;

C++ 用c+制作一颗心+;,c++,drawing,geometry,sfml,vertices,C++,Drawing,Geometry,Sfml,Vertices,我对做一颗心非常感兴趣。 我知道几何基本类型 我很好奇我将如何得到一条曲线。我是否必须使用cmath库,并以某种方式从两点连接它 我一直在浏览很多不同的网站,了解制作红心背后的数学原理 我在努力把这个数学移植到C++,而不是实际的数学;我才刚开始学这门语言 如果有人能为我提供一些示例代码和解释,我会很高兴,因为我在互联网上找不到。我还在这个项目中使用SFML框架。 谢谢大家! 下面是当前代码的一个示例 #include <SFML/Graphics.hpp> #include

我对做一颗心非常感兴趣。

我知道几何基本类型

我很好奇我将如何得到一条曲线。我是否必须使用cmath库,并以某种方式从两点连接它

我一直在浏览很多不同的网站,了解制作红心背后的数学原理

我在努力把这个数学移植到C++,而不是实际的数学;我才刚开始学这门语言

如果有人能为我提供一些示例代码和解释,我会很高兴,因为我在互联网上找不到。我还在这个项目中使用SFML框架。

谢谢大家!

下面是当前代码的一个示例

#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>



int main()
{

    sf::RenderWindow Window;
    Window.create(sf::VideoMode(800, 600), "My First Smfl Game");

    Window.setKeyRepeatEnabled(false);



    sf::Texture pTexture;

    while(Window.isOpen())
    {
        sf::Event Event;
        while(Window.pollEvent(Event))
        {
            switch(Event.type)
            {
                case sf::Event::Closed:
                    Window.close();
                    break;
            }


        }

        sf::VertexArray vArray(sf::Lines, 20);
        vArray[0].position = sf::Vector2f(82, 300);
        vArray[1].position = sf::Vector2f(82, 84);
        vArray[2].position = sf::Vector2f(82, 84);
        vArray[3].position = sf::Vector2f(200, 84);
        vArray[4].position = sf::Vector2f(200, 84);
        vArray[5].position = sf::Vector2f(200, 100);
        vArray[6].position = sf::Vector2f(200, 100);
        vArray[7].position = sf::Vector2f(99, 100);
        vArray[8].position = sf::Vector2f(99, 100);
        vArray[9].position = sf::Vector2f(99, 284);
        vArray[10].position = sf::Vector2f(99, 284);
        vArray[11].position = sf::Vector2f(200, 284);
        vArray[12].position = sf::Vector2f(200, 284);
        vArray[13].position = sf::Vector2f(200, 300);
        vArray[14].position = sf::Vector2f(200, 300);
        vArray[15].position = sf::Vector2f(82, 300);
        vArray[16].position = sf::Vector2f(250, 300);
        vArray[17].position = sf::Vector2f(300, 82);
        vArray[18].position = sf::Vector2f(380, 300);
        vArray[19].position = sf::Vector2f(320, 82);
        for(int k = 0; k < 20; k++)
        {
            int red = rand() % 255;
            int green = rand() % 255;
            int blue = rand() % 255;

            vArray[k].color = sf::Color(red, green, blue);
        }

        Window.draw(vArray);
        Window.display();
        Window.clear();
    }
}

#包括
#包括
#包括
#包括
#包括
int main()
{
sf::RenderWindow窗口;
创建(sf::VideoMode(800600),“我的第一个Smfl游戏”);
Window.setKeyRepeatEnabled(假);
sf::纹理和纹理;
while(Window.isOpen())
{
sf::事件;
while(Window.pollEvent(事件))
{
开关(事件类型)
{
案例sf::事件::已结束:
Window.close();
打破
}
}
sf::VertexArray vArray(sf::Lines,20);
vArray[0].位置=sf::Vector2f(82300);
vArray[1].位置=sf::Vector2f(82,84);
vArray[2].位置=sf::Vector2f(82,84);
vArray[3].位置=sf::Vector2f(200,84);
vArray[4].位置=sf::Vector2f(200,84);
vArray[5].位置=sf::Vector2f(200100);
vArray[6].位置=sf::Vector2f(200100);
vArray[7].位置=sf::Vector2f(99100);
vArray[8].位置=sf::Vector2f(99100);
vArray[9].位置=sf::Vector2f(99284);
vArray[10].位置=sf::Vector2f(99284);
vArray[11].位置=sf::Vector2f(200284);
vArray[12].位置=sf::Vector2f(200284);
vArray[13].位置=sf::Vector2f(200300);
vArray[14].位置=sf::Vector2f(200300);
vArray[15].位置=sf::Vector2f(82300);
vArray[16].位置=sf::Vector2f(250300);
vArray[17].位置=sf::Vector2f(300,82);
vArray[18].位置=sf::Vector2f(380300);
vArray[19].位置=sf::Vector2f(320,82);
对于(int k=0;k<20;k++)
{
int red=rand()%255;
int green=rand()%255;
int blue=rand()%255;
vArray[k].color=sf::color(红色、绿色、蓝色);
}
窗口绘制(vArray);
Window.display();
Window.clear();
}
}

用生成坐标的代码替换曲线的硬编码坐标(所有
vArray[.]位置
指定)。要生成这些坐标,只需从参考中对其中一条建议的曲线进行采样。下面是第二个链接中的一个可能的实现(这是一个带有四个正方形的链接,看起来很简单,可以实现):

#包括
#包括
#伊夫德夫·穆皮
#定义M_PI 3.14159265358979323846
#endif//M_PI
// ...
int x0=800/2;//心脏中心的坐标
int y0=600/2;
int size=400;//心脏大小
int r=大小/4;//曲线半径
int总曲线顶点计数=40;//每条曲线的最大顶点数
int总顶点计数=80;//顶点总数:30+10+10+30
struct CurveInfo//存储四条方形曲线的信息
{
整数顶点计数;
双t0;//角度原点
双s;//角符号:+1或-1
int cx,cy;//(相对)曲线中心坐标
}
曲线信息[4]=
{
//左上角
{3*总曲线\顶点\计数/4,0.0,-1.0,-r,-r},
//左下角
{total_curve_vertex_count/4,1.5*M_PI,1.0,-r,r},
//右下角
{total_curve_vertex_count/4,M_PI,1.0,r,r},
//右上角
{3*总曲线\顶点\计数/4,0.5*M \π,-1.0,r,-r},
};
std::向量顶点(总顶点计数);
int顶点_指数=0;
对于(int i=0;i<4;i++)
{
曲线信息&曲线信息=曲线信息[i];
int vertex_count=曲线_info.vertex_count;
双t0=曲线_info.t0;
双s=曲线信息s;
int cx=x0+曲线_info.cx;
int cy=y0+曲线信息cy;
对于(int j=0;j<顶点计数;j++)
{
双dt=s*2.0*M_PI*j/(总曲线顶点计数-1);
int x=cx+r*cos(t0+dt);
int y=cy+r*sin(t0+dt);
顶点[vertex_index++]=sf::Vector2f(x,y);
}
}
//生成线基本体的顶点
int total_line_count=total_vertex_count-1;
//不要复制第一个和最后一个顶点
int line_vertex_count=2*总计_vertex_count-2;
sf::VertexArray vArray(sf::线条,线条顶点计数);
int line_index=0;
顶点指数=0;
对于(int k=0;k<总行数;k++)
{
vArray[line_index++]。position=顶点[vertex_index++];
vArray[line_index++]。位置=顶点[vertex_index];
}
对于(int k=0;k
您可能想看看贝齐尔曲线和德卡斯特卢算法。这将使绘制平滑曲线更容易。我在上发布了一段视频,但谷歌搜索“de Casteljau”会带来很多点击率。很简单,试试看!当我运行代码时,它会给我以下错误:非POD元素类型'sf::Vector2f'(也称为'Vector2'的可变长度数组)@
#include <vector>
#include <math.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif // M_PI

// ...

    int x0 = 800 / 2; // Coordinates of the center of the heart
    int y0 = 600 / 2;

    int size = 400; // Size of the heart
    int r = size / 4; // Radius of the curves

    int total_curve_vertex_count = 40; // Maximum number of vertices per curve
    int total_vertex_count = 80; // Total number of vertices: 30 + 10 + 10 + 30

    struct CurveInfo // Store information for each of the four square curves
    {
        int vertex_count;
        double t0; // Angle origin
        double s; // Angle sign: +1 or -1
        int cx, cy; // (Relative) coordinates of the center of the curve
    }
    curve_infos[4] =
    {
        // Upper-left
        { 3 * total_curve_vertex_count / 4,        0.0, -1.0, -r, -r},
        // Lower-left
        {     total_curve_vertex_count / 4, 1.5 * M_PI,  1.0, -r,  r},
        // Lower-right
        {     total_curve_vertex_count / 4,       M_PI,  1.0,  r,  r},
        // Upper-right
        { 3 * total_curve_vertex_count / 4, 0.5 * M_PI, -1.0,  r, -r},
    };

    std::vector<sf::Vector2f> vertices(total_vertex_count);
    int vertex_index = 0;

    for(int i = 0; i < 4; i++)
    {
        CurveInfo& curve_info = curve_infos[i];
        int vertex_count = curve_info.vertex_count;
        double t0 = curve_info.t0;
        double s = curve_info.s;
        int cx = x0 + curve_info.cx;
        int cy = y0 + curve_info.cy;

        for(int j = 0; j < vertex_count; j++)
        {
            double dt = s * 2.0 * M_PI * j / (total_curve_vertex_count - 1);
            int x = cx + r * cos(t0 + dt);
            int y = cy + r * sin(t0 + dt);
            vertices[vertex_index++] = sf::Vector2f(x, y);
        }
    }

    // Generate the vertices of the lines primitives
    int total_line_count = total_vertex_count - 1;
    // Don't duplicate the first and last vertices
    int line_vertex_count = 2 * total_vertex_count - 2;

    sf::VertexArray vArray(sf::Lines, line_vertex_count);

    int line_index = 0;
    vertex_index = 0;

    for(int k = 0; k < total_line_count; k++)
    {
        vArray[line_index++].position = vertices[vertex_index++];
        vArray[line_index++].position = vertices[vertex_index];
    }

    for(int k = 0; k < line_vertex_count; k++)
    {
        int red = rand() % 255;
        int green = rand() % 255;
        int blue = rand() % 255;

        vArray[k].color = sf::Color(red, green, blue);
    }

// ...