Scratch: Turtle drawing (IX)

In this lesson leading part, we talk about how to do in order to become a responsible "program ape." I think the responsible programmer will never be any rough handling " function interface ."

 

For example, the "circle" function, the programmer will be to scrutiny "which parameters are necessary, and which parameters are superfluous," to think of ourselves and set "If I were a user, what kind of function is what I need most of . " Eventually, he will be starting from the "essence of the circle," it concluded: the user circle most need to consider is the "radius of the circle", "Radius" set, round also determined. Therefore, the "radius" as a parameter "circle" function, but is undoubtedly the most suitable.

 

If you draw a circle with this function to "radius" as a parameter, the user easier. As our program ape, it is necessary to think about other than "radius", when all the details of the circle - with the number of polygons to approximate simulation of a circle? Side length of each side is how much? Unfinished each side to turn the number of degrees? Etc., etc.

 

Although many problems, but this is a "program ape" bounden duty. We will not be intimidated by these problems, to "break all" it!

 

First to be considered is a regular polygon, "the number of sides." The following figure shows the same radius, the effect of different numbers of polygonal edges.

Scratch: Turtle drawing (IX)

From the figure it can be found: the number of sides too No, this is not enough to draw a circle "circle", "regular polygon" traces too obvious; increase the number of sides, but added more round "circle", but the program needs more time to "draw", efficiency is reduced.

 

30-sided polygon looks very like a "circle", in order to make the effect better, let's stipulate that the number of sides as "60," it seems to have been enough, and you will not have much impact on efficiency.

 

However, "a fixed number of sides" will cause another problem, as shown below:

 

Scratch: Turtle drawing (IX)

 

For the same number of sides, different radii of the polygon, if the user "radius" is set for a long, round would be great, then the side length of each side is bound to increase, which in turn will lead to "a regular polygon" too obvious traces ; if the user "radius" setting is very short, round will be very small, then the side length of each side are bound to decrease, even as small as only "one pixel", which is completely unnecessary, wasted run time and system resources. It seems "the number of sides" not so good OK, "fixed number of sides," the road like a dead end.

 

不要紧,我们再来试试“边长”。通过上面的分析我们知道,边长过长,会使“正多边形”的痕迹过于明显;边长过短,会浪费时间和资源。那么边长设为多少合适呢?前人经过大量的实验,得出的结论是:只要边长不大于“3个像素”,人眼就会把“正多边形”认作是“圆”。那么好,为了提高效率,我们就把边长设置为“3”好了。

 

“边长”确定了,“边数”就好办了。我们知道了圆的“半径”,小学数学老师曾经告诉我们,要计算圆的“周长”,只要使用公式“2πr”就好了。算出了“周长”,又知道“边长”,自然也就能算出“边数”了。

 

“边长”和“边数”都定下来了,再画起“圆”来就易如反掌了。您问“为什么”?因为我们有现成的“绘制边长为 length 的正 n 边形”这个函数啊!

 

至此,问题迎刃而解。理清了思路,代码写起来就容易多了。

 

Step1:制作新的积木,取名为“画半径为r的圆”

Scratch: Turtle drawing (IX)

 

Step2:定义变量“周长”,并使用公式“2πr”计算周长

Scratch: Turtle drawing (IX)

 

Step3:计算边数。由于边数必须为整数,所以这里我们使用系统提供的“取整函数”来得到整数边数

Scratch: Turtle drawing (IX)

注意:这里使用的是“向上取整”,即不管小数点后的第1位数是几,都向上“进一”。

 

Step4:计算边长

Scratch: Turtle drawing (IX)

 

可能您会问了:不都说好了边长是“3”吗,怎么还要计算边长呢?

To answer this question, we have to re-calculation process just stroked it: we will start with 2 [pi] R to calculate the perimeter, then "what if" side length of 3, with a side length of the circumference divided by 3 to calculate the number of sides. As we estimated the number of sides thus calculated is likely not an integer (such as a 20.65), and therefore it had a "rounded up" (20.65 is rounded up to 21). We use 2 pi] r exactly equal to the originally calculated perimeter "3✖️20.65", after rounding circumference becomes "3✖️21", the circumferential length! Although only a long distance in less than one side, but still long. Circumferential length will lead to increase in radius, we will draw a circle than the user wants it "a Diudiu" large circle painted.

 

Although this "Diudiu a" very small, but as a serious, responsible program ape, we do not allow any defects on the "algorithm." To this end, we then divide the number of perimeter edges, to calculate the exact length of the side (side length may be fractional). Our fully consistent round with the user in mind.

 

So far, we have opened up the last "knot", get out of our ultimate combination of function with the circle above building blocks it. The final outcome of the function as shown below:

 

Scratch: Turtle drawing (IX)

 

Next class, we are about to usher in the "turtle graphics" ultimate challenge part of the draw below the figure of "Flower":

Scratch: Turtle drawing (IX)

Tip: flower "flower" seems complicated, in fact, just look at each "petal", is composed of two symmetrical "arc" composition. So, to solve the problem of the arc, "flower" will complete most of them. Today's course where we have learned to draw "circle", then the arc how to paint it?

 

Project Summary:

 

In today's program, we have:

1. From the "essence of the circle," starting to write the "circle of radius r" parameter "Circling function";

2 designing "Circle function" in the process, become more familiar with, understand the "interface design" ideas, process and significance;

3. Know when defining a new function (blocks), we can be "reused" previously been defined function (blocks);

4. in "Circle function" design, has become a responsible, have to play "program ape."

Guess you like

Origin www.cnblogs.com/scratch3/p/10936612.html