使用C++编写乌龟画图程序

该博客介绍了如何使用C++模拟乌龟图形绘制,通过一个20*20的数组作为画板,并记录乌龟的位置和笔的状态。博客包含了乌龟的命令表和示例输入输出,展示了一个12*12正方形的绘制过程。
摘要由CSDN通过智能技术生成

C++乌龟画图

  • 输入命令并进行绘图

题目:Turtle Graphics

The Logo language, which is popular among elementary school children,made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the roomunder the control of a C++ program. The turtle holds a pen in one of two positions, up or down.While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this problem, you’ll simulate the operation of the turtle and create a computerized sketchpad as well.Use a 20-by-20 array floor that is initialized to false. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its pen up. The set of turtle commands your program must process are shown in Fig.

简单翻译过来,大意是一个拿着笔的乌龟,笔放下时,画出移动的路径;当笔向上时,乌龟自由地四处移动,不画任何东西。记录乌龟的操作命令和创建一个画板(使用一个20*20的数组),并随时记录海龟的当前位置,以及笔是否处于上升或下降状态。假设乌龟总是用它的笔从(0, 0)开始。

命令表

这里写图片描述

例如:

假设乌龟在靠近地板中心的某个地方。下面的命令将绘制和打印12*12的方形路径并且最后笔尖向上。

输入

2
5,12
3
5,12
3
5,12
3
5,12
1
6
9

输出

这里写图片描述


代码块

#include <iostream>
using namespace std;
const int COMMANDS = 100, SIZE = 40;  //这里SIZE为展示画板大小,由于原本行列间隔不一样,为美观我将行
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值