Kurs:KTurtle/Code-Rubrik/Sterne
Dieser Programmcode zeichnet mehrere Sterne
Sterne zeichnen (en)
Bearbeiten# distance:
# - draws nothing
# - returns distance from current position to point ($x, $y)
learn distance $x, $y {
$dx = $x - getx
$dy = $y - gety
return sqrt ($dx * $dx + $dy * $dy)
}
# degreeto:
# - draws nothing
# - returns degree between direction zero and the line through current position and point ($x, $y)
learn degreeto $x, $y {
$dx = $x - getx
$dy = $y - gety
if ($dx == 0) and ($dy == 0){
return
}
$deg = (arccos ((0 - $dy) / (distance $x, $y)))
if $x < getx {
$deg = 0 - $deg
}
return $deg
}
# face:
# - turns the turtle towards point ($x, $y)
# - returns nothing
learn face $x, $y {
direction degreeto $x, $y
}
# lineto:
# - turns the turtle towards point ($x, $y) and then moves towards that point
# - returns nothing
learn lineto $x, $y {
face $x, $y
forward distance $x, $y
}
# star:
# - draws a $n-edged star with outer/inner radius of $r1/$r2 respectively, around the current position. Inner edges are offset to outer edges by $o degrees. Pen is down afterwards.
# - returns nothing
learn star $n, $r1, $r2, $o {
penup
$step = 360 / $n
$x0 = getx
$y0 = gety
backward 10
$angle = degreeto $x0, $y0
forward 10
forward $r1
pendown
repeat $n {
$angle = $angle + ($step / 2)
lineto ($r2 * (sin ($angle + $o)) + $x0), ($r2 * (0 - cos ($angle + $o)) + $y0)
$angle = $angle + ($step / 2)
lineto ($r1 * (sin $angle) + $x0), ($r1 * (0 - cos $angle) + $y0)
}
direction $angle
go $x0, $y0
}
#########
# example picture:
reset
penup
for $i = 1 to 12 {
turnright 30
forward 100
star ($i + 1), 30, 15, 0
penup
backward 30
print $i + 1
backward 70
}