Kurs:KTurtle/Code-Rubrik/Nachthimmel
Dieser Code zeichnet beliebig viele Sterne auf blauen Hintergrund.
Nachthimmel 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:
$csx = 500
$csy = 500
$b = 10
reset
canvassize $csx, $csy
canvascolor 0, 0, 99
pencolor 246, 213, 0
$num = ask "Number of stars (~10)?"
penwidth (50 / $num)
for $i = 1 to $num {
$edges = round (random 3.51, 6.49)
$radius = random 10, (($csx * $csy) / (500 * $num))
$posx = random ($radius + $b), ($csx - $radius - $b)
$posy = random ($radius + $b), ($csy - $radius - $b)
go $posx, $posy
star $edges, $radius, ($radius / 2), 0
}
spritehide