0

我正在使用 SFML 和 C++ 编码。我正在编写的程序必须是递归实现。

我的目标是创建一个函数,根据先前绘制的正方形,在不同的位置和旋转中递归地在屏幕上绘制一个正方形。

每个后续的正方形都应该小于前一个函数调用,并且向左旋转 45 度(从前一个正方形的左角)或前一个正方形的右侧旋转 45 度。

每个新方块都会产生另外两个方块等等。

我的想法是将正方形的左上点和右上点传递给两个不同的递归函数调用,并将这些点用作后续正方形的起点。

虽然生成的正方形也会将左上角和右上角传递给递归函数调用等。

我开发的代码没有显示应该从递归函数调用生成的两个方块。只显示了一侧。

我开发了以下代码(请原谅我的代码..我已经很久没有用 C++ 编码了..)

程序驱动程序 (main.cpp)

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "PTree.hpp"

using namespace std;
using namespace sf;

int main( int argc, char* argv[ ] )
{
  double L = 0.0; // Length of square sides
  int N = 0; // Number of times to call recursive function           

  L = atol( argv[ 1 ] );
  N = atoi( argv[ 2 ] );
  Vector2f vPoint;
  vPoint.x = 0;
  vPoint.y = 0;

  // Create and Display Window
  PTree tree( L, N );
  return 0;
}  

(PTree.hpp)

#ifndef PTREE_H
#define PTREE_H

using namespace std;
using namespace sf;

class PTree /*:public sf::Drawable, public sf::Transformable*/{
public:
  // Constructor
  PTree( double L, int N );
  // Destructor
  ~PTree();

  // Recursive function to draw Pythagorias Tree
  void pTree( double L, int N, Vector2f vPoint, Vector2f vOrigin, float rotation );

private:
  float width = 0;
  float height = 0;
  int originX = 0;
  int originY = 0;
  float rotation = 0;
  RenderWindow window;
  int angle1 = 0;
  int angle2 = 0;

};

#endif // PTREE_H included

(PTree.cpp)

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <math.h>
#include "PTree.hpp"
#include <iostream>

using namespace std;
using namespace sf;

// Constructor
PTree::PTree( double L, int N )
{ 
  width = ( 6 * L );
  height = ( 4 * L );
  Vector2f vPoint = { width/2, height - 1 };
  Vector2f vOrigin;
  vOrigin.x = L/2;
  vOrigin.y = L;
  /* vPoint.x = width/2;
  vPoint.y = height - 1;
 */
  window.create( VideoMode( width, height ), "Pythagoras Fractal Tree" );
  pTree( L, N, vPoint, vOrigin, 0 );
}

// Destructor
PTree::~PTree(){}
/*###########################################################################*/

// Recursive function to draw Pythagorias Tree
void PTree::pTree( double L, int N, Vector2f vPoint, Vector2f vOrigin, float rotation  )
{
  Vector2f vPointR;

  if( N < 1 )
  {
    return;
  }

  // Define a convex shape called convexSquare
  ConvexShape convexSquare( 4 );
  convexSquare.setPoint( 0, Vector2f( 0, 0 ));
  convexSquare.setPoint( 1, Vector2f( 0, L ));
  convexSquare.setPoint( 2, Vector2f( L, L ));
  convexSquare.setPoint( 3, Vector2f( L, 0 ));

  convexSquare.setOutlineThickness( 1.f );
  convexSquare.setFillColor( Color::Black );
  convexSquare.setOutlineColor( Color::White );

  convexSquare.setPosition( vPoint );
  convexSquare.setOrigin( vOrigin );
  convexSquare.setRotation( rotation );

  while( window.isOpen( ))
  {
    Event event;
    while( window.pollEvent( event ))
    {
      if( event.type == Event::Closed )
      {
        window.close( );
      }
    }
    if( N >= 0 )
    {
    window.draw( convexSquare );
    window.display( );
    L = ( L * ( sqrt(2)/2 ));
    N = N - 1;
    rotation = rotation - 135;
    cout << "LOOPS:" << N << endl;

//left
    vPoint = convexSquare.getTransform( ).transformPoint(   convexSquare.getPoint( 0 ));
    vOrigin = convexSquare.getPoint( (angle1) );
    pTree( L, N, vPoint, vOrigin, rotation );
    angle1 = (( angle1 + 1 ) % 4 );

//right
    vPointR = convexSquare.getTransform( ).transformPoint( convexSquare.getPoint( 3 ));
    vOrigin = convexSquare.getPoint( 2 );
    pTree( L, N, vPointR, vOrigin, rotation-90 ); 

    }
  }
cout << "X value =" << vPoint.x << "     Y value = " << vPoint.y << endl;

到目前为止,我已经尝试为函数 pTree 的第二次递归调用返回凸形的各个点。这也没有显示任何东西。

最初我只使用 Vector2f vPoint 并在每次递归调用之前对其进行修改,但在用尽我的知识库以获得解决方案后,我专门为右侧方块创建了一个名为 Vector2f vPointR 的新变量。

SFML 文档没有为像我这样的菜鸟提供足够的示例。API 本质上是一个选项列表,每个函数都有最少的示例(如果有的话)。我已经尽我所能搜索互联网,看看我是否通过了错误的点,但找不到答案。

确实有效的一件事(虽然不完全正确)是当我切换递归调用时......这意味着我在调用左侧方块之前移动了右侧方块的调用,但问题是左侧s 方块没有显示。

在这一点上,我也在尝试为每个正方形计算出正确的旋转,但这是我遇到的最少的问题。

我尝试递归显示这些方块的方式有问题吗?

除了 Stack Overflow 寻求帮助之外,我不知道从这里去哪里。

感谢您的时间和专业知识。

4

1 回答 1

1

不要递归调用整个while循环。只递归调用绘图部分

// Initialize window...

while (window.isOpen())
{
    sf::Event event;
    // Handle events...

    window.clear();

    // call the recursive function here

    window.display();
}

此外,您可能想使用sf::RectangleShape绘图而不是sf::ConvexShape

这是一个有效的“示例”:

#include <SFML/Graphics.hpp>
#include <cmath>

void drawPythagoreanTree(sf::RenderTarget&, const float, const int);

int main()
{
    const float L = 150;
    const int N = 14;

    const unsigned width = static_cast<unsigned>(6 * L);
    const unsigned height = static_cast<unsigned>(4 * L);
    sf::RenderWindow window{{width, height}, "Pythagorean Tree"};
    while (window.isOpen())
    {
        for (sf::Event event; window.pollEvent(event);)
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::White);
        drawPythagoreanTree(window, L, N);
        window.display();
    }
}

void drawPythagoreanTree(sf::RenderTarget& target, const int N,
                         const sf::RectangleShape& parent)
{
    static const float halfSqrt2 = sqrt(2.f) / 2;

    if (N < 1) return;
    target.draw(parent);
    auto const& sz = parent.getSize();
    auto const& tf = parent.getTransform();

    auto childL = parent;                    // copy parent's color and rotation
    childL.setSize(sz * halfSqrt2);          // resize
    childL.setOrigin(0, childL.getSize().y); // bottom left corner
    childL.setPosition(tf.transformPoint({0, 0})); // reposition
    childL.rotate(-45);
    drawPythagoreanTree(target, N - 1, childL);

    auto childR = parent;               // copy parent's color and rotation
    childR.setSize(sz * halfSqrt2);     // resize
    childR.setOrigin(childR.getSize()); // bottom right corner
    childR.setPosition(tf.transformPoint({sz.x, 0})); // reposition
    childR.rotate(45);
    drawPythagoreanTree(target, N - 1, childR);
}

void drawPythagoreanTree(sf::RenderTarget& target, const float L, const int N)
{
    sf::RectangleShape rect{{L, L}};
    // set origin to center of the rect, easier to center position on screen
    rect.setOrigin(rect.getSize() / 2.f);
    rect.setPosition(target.getSize().x / 2.f, target.getSize().y - L / 2.f);
    rect.setFillColor(sf::Color::Black);
    drawPythagoreanTree(target, N, rect);
}
于 2019-02-16T20:31:17.647 回答