2014-01-25 36 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