LOGO 海龟作画 系列三 递归画一个国际象棋棋盘 Logo Turtle Graphics - Series 3 - Recursively drawing a chess board

in #cn7 years ago (edited)

本系列:

Logo Tutorial Series:

今天我们要来讲一讲递归。递归就是函数自己调用自己,我们可以定义一个过程,然后这只海龟不停的画,结束的时候再调用自身再继续画。再次调用的时候参数变化了,至到参数满足一定的条件则停止。

A recursion is a function calling itself. In Logo, we can define a procedure, asking the turtle to draw something, then ask the turtle to repeat itself but with slightly different parameters, until a set of conditions are met.

比如 下面定义的这个过程可以用来画一个实现的正方 形。
For example, the following procedure can be used to draw a filled rectangle.

TO FK :B
  IF :B>15 [STOP]  ; 如果边长大于15就停止
  REPEAT 4 [FD :B RT 90] ; Draw a square with side B 画一个长度为B的正方形
  FK :B+1  ;  Draw a square with side B+1 递归调用自己画一个长度为B+1的正方形
END

然后我们可以通过 调用 FK 15 来画一个空心的正方形,这样的话,通过不停的画空心实心(不断交错)则可以构成一个棋盘:
The FK 15 draws a empty square. By such, we can draw the interleaving squares.

TO QP :Y
  IF :Y>4 [STOP]   
  REPEAT 4 [FK 15 FD 15 FK 1 FD 15] 
  RT 90 FD 30 RT 90
  REPEAT 4 [FK 15 FD 15 FK 1 FD 15]
  RT 180
  QP :Y+1
END

您可以使用我写的这个PHP-LOGO解释器来验证这段LOGO代码。
You could use this PHP-LOGO Interpreter to verify the LOGO program as stated above.

Originally published at https://steemit.com Thank you for reading my post, feel free to Follow, Upvote, Reply, ReSteem (repost) @justyy which motivates me to create more quality posts.

原创 https://Steemit.com 首发。感谢阅读,如有可能,欢迎Follow, Upvote, Reply, ReSteem (repost) @justyy 激励我创作更多更好的内容。

// Later, it will be reposted to my blogs: justyy.com, helloacm.com and codingforspeed.com 稍后同步到我的中文博客和英文计算机博客

近期热贴

Recent Popular Posts


Tags: #cn #cn-programming #logo-turtle #recursion #chessboard

Sort:  

矢量图!

Wow! Hi @justyy :)
You're post is very informative. Nice and clean code :) Thanks for sharing
I hope you can read some of my posts too @tiffanyrej

@justyy got you a $8.06 @minnowbooster upgoat, nice!
@justyy got you a $8.06 @minnowbooster upgoat, nice! (Image: pixabay.com)


Want a boost? Click here to read more!

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.029
BTC 57971.70
ETH 2448.51
USDT 1.00
SBD 2.34