在 c++ 中使用递归放大和缩小图片 (sfml)

Zooming in and out a picture (sfml) using recursion in c++

本文关键字:缩小 sfml 放大 c++ 递归      更新时间:2023-10-16

使用以下代码,我将添加一个递归函数,而无需更改代码中的任何内容以使其工作:

 sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), TITLE);
 int stop = 800;
 sf::Color colour1(sf::Color::Black), colour2(sf::Color::Red);
 // Start the main loop
 while (window.isOpen()) {          
     // Process events
     sf::Event event;
     while (window.pollEvent(event)) {
         // check the type of the event...
         switch (event.type) {
             // window closed
             case sf::Event::Closed:
                 window.close();
                 break;
             case sf::Event::MouseMoved: //mouse moved
                 stop = mapXtoMinSize(event.mouseMove.x); // convert x coordinate of mouse to minimum size of square to draw
                 break;    
             // we don't process other types of events
             default:
                break;
          } //end switch     
      } //End-Process events
      window.clear(colour2);
 }

我只是想知道如何去做。

归函数通常是这样的:

int recursive(int x) {
    if (x == 0) {
        return 0;
    } else {
        return (x + recursive(x - 1));
    }
}

这将对从 0 到 x 的所有整数求和,方法是重复调用自身来计算(x - 1)的总和并将其添加到结果中。如果遇到基本情况 x == 0 ,则递归结束,调用展开,直到它们到达原始状态,此时总和已计算完毕。请更具体。

在不知道你想用递归函数实现什么的情况下,我不能给你更多的建议。我不知道你评论"不更改代码中的任何内容"是什么意思。