Kurs:KTurtle/Code-Rubrik/Strichlinien

Dieser Code zeichnet 2 Sterne mit gestrichelten Linien

Gestrichelte Linien (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
}

# dasehdlineto:
# - turns the turtle towards point ($x, $y) and then moves towards that point, drawing a dashed line
# - returns nothing
learn dashedlineto $x, $y, $l {
  face $x, $y
  $dist = distance $x, $y
  while $dist > 2 * $l {
    pendown
    forward $l
    penup
    forward $l
    $dist = ($dist - (2 * $l))
  }
  pendown
  forward $l
  $dist = $dist - $l
  if $dist > 0 {
    penup
    forward $dist
    pendown
  }
}

# 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 dashedstar $n, $r1, $r2, $o, $l {
  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)
    dashedlineto ($r2 * (sin ($angle + $o)) + $x0), ($r2 * (0 - cos ($angle + $o)) + $y0), $l
    $angle = $angle + ($step / 2)
    dashedlineto ($r1 * (sin $angle) + $x0), ($r1 * (0 - cos $angle) + $y0) , $l
  }

  direction $angle
  go $x0, $y0
}

#########

# example picture:
reset
canvascolor 250, 250, 250
pencolor 128, 0, 0
penwidth 4
dashedstar 5, 150, 75, 0, 10
turnright 36
penwidth 2
dashedstar 5, 70, 40, 0, 5
turnright 144