C++/SFML:使用两次递归调用将凸形打印到屏幕上仅显示第一次递归调用的形状,而不显示第二次递归调用的形状

C++ / SFML: Printing convex shapes to the screen using two recursive calls only displays the shapes from the first recursive call and not the second

我正在使用 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;

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

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

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

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

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

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

除了 Stack Overflow 之外,我不知道从哪里可以得到帮助。

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

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

// Initialize window...

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

    window.clear();

    // call the recursive function here

    window.display();
}

您可能还想使用 sf::RectangleShape 来绘制而不是 sf::ConvexShape

这是一个工作 "example":

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