Kurs:KTurtle/Code-Rubrik/Stern
Erscheinungsbild
Mit diesem Programmcode zeichnet die Schildkröte einen Stern
Einen Stern 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
star 5, 150, 75, 0