2014-01-25 3 views
0

나는 마음을 만드는 데 매우 흥미가있다.C++로 마음 만들기

나는 기하학적 원시 타입을 알고있다.

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

은 내가 곡선을 받고 가겠어요 방법에 대해 궁금합니다. 어떻게해도 cmath 라이브러리를 사용해야하고 두 점에서 연결해야합니까?

나는 마음을 만드는 수학에 대해 많은 다른 사이트를보고있었습니다. 내가 아닌 실제 수학 C++이 수학을 포팅과 사투를 벌인거야

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

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

; 나는 방금 언어를 배우기 시작했다.

인터넷에서 찾을 수없는 일부 예제 코드와 설명을 제공해 주시면 좋겠습니다. 또한이 프로젝트에 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(); 
    } 
} 
+0

당신은 베 지어 곡선에서와 Casteljau 알고리즘 드 볼 수도 있습니다 : 무엇 다음 것은 두 번째 링크에서 method 3의 가능한 구현이다 (이것은 구현하기에 충분 간단한 듯 네 개의 사각형과 하나). 이렇게하면 부드러운 곡선 그리기가 훨씬 쉬워집니다. https://www.youtube.com/watch?v=YATikPP2q70에서 동영상을 올렸지 만 "de Casteljau"를 검색하면 많은 조회수가 발생할 것입니다. 쉽습니다. 한번 사용해보십시오! –

답변

1

좌표를 생성하는 코드를하여 곡선 (모든 vArray[.].position 과제)에 대한 하드 코딩 된 좌표를 교체

enter image description here

. 이 좌표를 생성하려면 참조에서 제안 된 곡선 중 하나를 샘플링하기 만하면됩니다.

#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'(일명 'Vector2 ')의 가변 길이 배열 – AEGIS

+0

@AEGIS 코드가 비표준 C++ 확장을 사용하여 코드가 변경되었습니다. 대신 std :: vector를 사용하십시오. – user3146587

+0

정말 고마워요! – AEGIS

관련 문제