2014-01-25 12 views
0

我对制作一颗心非常感兴趣。用C++制作一颗心

我意识到几何原始类型。

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

你可能想看看贝塞尔曲线和德卡斯特里奥算法。这将使平滑的曲线绘制更容易。我已经在https://www.youtube.com/watch?v=YATikPP2q70发布了视频,但是对“de Casteljau”的谷歌搜索会带来很多点击。这很容易,试试吧! –

回答

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

// ... 
+0

当我运行你的代码时,它给了我这个错误:非POD元素类型'sf :: Vector2f'(aka'Vector2 ')的可变长度数组 – AEGIS

+0

@AEGIS代码使用了非标准的C++扩展,我改变了它使用std :: vector来代替。 – user3146587

+0

非常感谢! – AEGIS