用c ++打造一颗心

时间:2014-01-25 03:37:17

标签: c++ drawing geometry sfml vertices

我对创造一颗心非常感兴趣。

我知道几何基元类型。

http://www.opentk.com/doc/chapter/2/opengl/geometry/primitives

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

我一直在寻找许多不同的网站,关于制作心灵背后的数学。

http://www16.ocn.ne.jp/~akiko-y/heart2/index_heart2_E.html

http://www.mathematische-basteleien.de/heart.htm

我正在努力将这个数学移植到c ++,而不是实际的数学;我刚刚开始学习这门语言。

我很乐意,如果有人可以请我提供一些示例代码和解释,因为我无法在互联网上找到它。此外,我正在为此项目使用SFML框架。

谢谢!

以下是当前代码的示例。

enter image description here

#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();
    }
}

1 个答案:

答案 0 :(得分:1)

用生成坐标的代码替换曲线的硬编码坐标(所有vArray[.].position赋值)。要生成这些坐标,您只需从引用中对其中一条建议的曲线进行采样。接下来是第二个链接中method 3的可能实现(它是带有四个方块的那个,看起来很简单,可以实现):

#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);
    }

// ...