Quarter Circle using the ARC Command:
Refer to the earlier chapter "Polygons and Circles". In that chapter, we saw how to draw a full circle using the ARC command. That procedure is reproduced below:
ERASE "dcircle
TO dcircle :d
PU LT 90 BK :d/2 PD ARC 360 :d/2
PU FD :d/2 RT 90 PD
END
To draw a quarter circle, the ARC command's first input will now be 90. To mimic the behavior of our earlier q.dcircle procedure, we first have to shift the Turtle to the center of our circle, use ARC to draw the quarter circle, and then shift the Turtle back to its original position.
ERASE "q.dcircle
TO q.dcircle :d
PU LT 90 BK :d/2 PD ARC 90 :d/2
PU RT 90 FD :d/2 RT 90 PD
END
In this design, we have to draw 7 thick
semi-circles each with a different color. Each semi-circle would be smaller
than the previous one. If we use the semi.dcircle procedure, we just have to reduce the diameter input by twice the thickness
of the brush.
Try the following instructions to verify that this
approach will work.
CS SETPENSIZE 15
; Semi-circle of diameter 300
SETPC 1 semi.dcircle 300
; Return to original position by completing the
circle
PU semi.dcircle 300 PD
; Jump inside to draw another semi-circle smaller by
30.
PU RT 90 FD 15 LT 90 PD
; Semi-circle of diameter 270
SETPC 2 semi.dcircle 270
PU semi.dcircle 270 PD
Our approach seems to work. Now, we just have to
repeat the process 7 times:
CS SETPENSIZE 15
; Red semi-circle of diameter 300.
SETPC 4 semi.dcircle
300
PU semi.dcircle 300 PD
; Smaller (radius less by 15) amber semi-circle.
PU RT 90 FD 15 LT 90 PD
SETPC 14 semi.dcircle
270
PU semi.dcircle 270 PD
; Smaller yellow semi-circle.
PU RT 90 FD 15 LT 90 PD
SETPC 6 semi.dcircle
240
PU semi.dcircle 240 PD
; Smaller green semi-circle.
PU RT 90 FD 15 LT 90 PD
SETPC 2 semi.dcircle
210
PU semi.dcircle 210 PD
; Smaller blue semi-circle.
PU RT 90 FD 15 LT 90 PD
SETPC 1 semi.dcircle
180
PU semi.dcircle 180 PD
; Smaller gray semi-circle.
PU RT 90 FD 15 LT 90 PD
SETPC 15 semi.dcircle
150
PU semi.dcircle 150 PD
; Smaller purple semi-circle.
PU RT 90 FD 15 LT 90 PD
SETPC 13 semi.dcircle
120
PU semi.dcircle 120 PD
These instructions will draw the rainbow.
Rainbow procedure: Can you now write a procedure that takes two inputs:
initial diameter and thickness of each semi-circle?
The important thing to understand is: the diameter of each successive
semi-circle reduces by 2*thickness. The procedure below shows how the inputs
are used.
; Inputs:
; d is the diameter of the outermost semi-circle
; t is the thickness
ERASE "rainbow
TO rainbow :d :t
SETPENSIZE :t
; Red
semi-circle of diameter :d.
SETPC 4
semi.dcircle :d
PU semi.dcircle :d
PD
; Smaller (by :t) amber semi-circle.
PU RT 90 FD :t LT 90 PD
SETPC 14
semi.dcircle :d-(2*:t)
PU semi.dcircle :d-(2*:t)
PD
; Smaller yellow semi-circle.
pu rt 90 fd :t lt 90 pd
SETPC 6 semi.dcircle
:d-(4*:t)
pu semi.dcircle :d-(4*:t) pd
; Smaller green semi-circle.
PU RT 90 FD :t LT 90 PD
SETPC 2
semi.dcircle :d-(6*:t)
PU semi.dcircle :d-(6*:t) PD
; Smaller
blue semi-circle.
PU RT 90 FD :t LT 90 PD
SETPC 1
semi.dcircle :d-(8*:t)
PU semi.dcircle :d-(8*:t) PD
; Smaller
gray semi-circle.
PU RT 90 FD :t LT 90 PD
SETPC 15
semi.dcircle :d-(10*:t)
PU semi.dcircle :d-(10*:t) PD
; Smaller
purple semi-circle.
PU RT 90 FD :t LT 90 PD
SETPC 13
semi.dcircle :d-(12*:t)
PU semi.dcircle :d-(12*:t) PD
END
Test: Using our favorite jump procedure (defined earlier) we will draw 3
different rainbows:
Jump -350 -50 Rainbow 100 5 jump 150 50 Rainbow 150 8
Jump 150 50 Rainbow 250 12
Applications of quarter circle:
Semi-circle:
REPEAT 2 [q.circle 400]
Circle:
REPEAT 4 [q.circle 400]
ERASE "flower
TO flower :size
REPEAT 10 [Petal :size Right 360/10]
END
flower 150
; Version 1: always in full bloom, i.e. the petals
are
; evenly spread
horizontally left to right.
; Looks weird for small # of petals.
; n must be >1
ERASE "lotus1
TO lotus1 :size :n
LT 90 REPEAT :n [petal2 :size RT 180/(:n-1)]
END
CS lotus1 200 4
; Version 2: spread depends on # of petals, that is,
; we draw petals
symmetrically around the vertical line.
; The initial left turn is calculated thus: for 1
petal
; there is no need because
the 'petal' procedure draws
; the petal symmetrically.
For each additional petal the
; total turn is 2*gap, e.g.
for 2 petals the total turn
; is 1*gap, for 3 petals it is 2*gap, and so on.
; So to divide it equally on both sides, the left
turn
; should be (n-1)*gap.
ERASE "lotus2
TO lotus2 :size :n :gap
LT :gap*(:n-1)
REPEAT :n [petal2 :size RT 2*:gap]
END
CS lotus2 200 6 10
Size is the bird’s wing span. The bird will be drawn flying at 45 degrees
clockwise to current orientation. For example, if the Turtle is pointing north,
the bird will appear banking at northeast.
ERASE "bird
TO bird :size
RT 45
q.dcircle :size/2
LT 90
q.dcircle :size/2
LT 45
END
bird 200
REPEAT 4 [bird 300] ; try this just for fun
For the petal, we draw a q-circle, take a 90 degree right turn, and return
to the original point. If instead, we take a larger turn, we would arrive at a
different point, giving a fish-like shape.
;size is diameter of the circle ERASE "fish TO fish :size Q.dcircle :size RT 120 Q.dcircle :size END fish 100 |
|
REPEAT 6 [fish 100] ; creates pattern as in figure 11-19.
The median of the fish would be at half the angle of the right turn (e.g.
60 in our procedure above). So, in order to get a horizontal fish, we should
tilt the Turtle to the right by 30.
;Version 2:
horizontal fish
ERASE "fish
TO fish :size ; size is
diameter of circle
RT 30
Q.dcircle :size
RT 120
Q.dcircle :size
RT 30 ; To get original
orientation
END
fish 100
Finally, it would be nice to close the tail to make it look more realistic.
The width of the tail (0.36 * size) is found by experimentation.
;Version 3: With a proper tail ERASE "fish TO fish :size ;size: circle diameter RT 30 Q.dcircle :size RT 120 Q.dcircle :size RT 30 BK
:size*0.36 END fish 100 HT |
|
The pattern in Figure
11-22 shows the Turtle taking an anti-clockwise round trip (i.e. turning
leftwards in general). So, obviously it can be drawn by something like this:
REPEAT
4 [Q.dcircle 100 LT X]
Our task is to find out
the value of X.
According to the TRT
principle, the total angle of the round trip would be equal to
-360 (since we count turning left as negative).
Each quarter circle makes the Turtle turn right
through 90. So, we get the following equation:
4 *
(90 + X) = -360
X =
-180 (minus means it's a left turn)
REPEAT
4 [Q.dcircle 100 LT 180]
To figure out the design
in Figure 11-23, we will use the same mathematical approach:
REPEAT
6 [Q.dcircle 100 LT X]
Our task is to work out
the value of X.
According to TRT, the
total angle of the round trip should be -360. Each quarter circle makes the
Turtle turn through 90. So, we get the following equation:
6 *
(90 + X) = -360
X =
-150 (minus means it's a left turn)
REPEAT
6 [Q.dcircle 100 LT 150]
ERASE "wgrass
TO wgrass :n :size
REPEAT :n [
REPEAT :size [FD 1 RT 1]
REPEAT :size [FD 1 LT
1]
PU REPEAT :size [RT
1 BK 1]
REPEAT :size [LT 1 BK
1] PD
PU RT 90 FD :size/5 LT
90 PD
]
END
wgrass 10 50
ERASE "waves
TO waves :n :size
REPEAT :n [
REPEAT :size [FD 1 RT 1]
REPEAT :size [FD 1 LT 1]
]
END
The following instructions draw 2 parallel waves.
RT 60 waves 3 60 LT 60 PU BK 10 RT 180 PD
RT 60 waves 3 60 LT 60 PU FD 10 RT 180 PD
Using REPEAT, we can draw as many pairs as we want:
REPEAT 3 [
RT 60 waves 3 60 LT 60 PU BK
10 RT 180 PD
RT 60 waves 3 60 LT 60 PU FD
10 RT 180 PD
]