locked
Why does this turtle program crash drawing further shapes? RRS feed

  • Question

  • Fairly new to Small Basic. Tried to write a small program to draw various regular polygons. I've stripped a lot of the code and this light version is still crashing. It crashes when it draws a second shape to the GraphicsWindow (although sometimes, on the third of fourth). Hopefully I've posted this in the right place, and included my code in the right way. Let me know if I've done anything wrong.

    Any help greatly appreciated.

    ' REGULAR POLYGON CREATOR
    TextWindow.Show()
    
    start:
    TextWindow.Write("Enter number of sides (more than 2, but fewer than 150): ")
    user_num = TextWindow.ReadNumber()
    
    TextWindow.Write("Enter side length (a number between 10 and 200): ")
    user_len = TextWindow.ReadNumber()
    
    'Create the graphics window and set up the colours
    GraphicsWindow.Show()
    GraphicsWindow.Width = 600
    GraphicsWindow.Height = 600
    GraphicsWindow.BackgroundColor = "Blue"
    GraphicsWindow.PenColor = "LightBlue"
    GraphicsWindow.PenWidth = 3
    
    'Work out the Exterior angle of the polygon so we can turn the Turtle
    ext_angle = 360 / user_num
    'Work out the distance to the middle of an edge of the polygon to let us move the Turtle at the start
    apothem = user_len / (2 * (Math.Tan(Math.Pi/user_num)))
    
    Turtle.Speed = 9
    Turtle.Show()
    
    ' move the turtle so that the polygon will centred in the window
    Turtle.PenUp()
    Turtle.MoveTo((GraphicsWindow.Width/2)-(user_len/2), (GraphicsWindow.Height/2)+apothem)
    Turtle.Angle = 0
    Turtle.Turn(-90 + ext_angle)
    Turtle.PenDown()
    
    'Draw the polygon
    For i = 1 To user_num
      Turtle.Move(user_len)
      Turtle.Turn(ext_angle)
    EndFor
    
    Goto start
    

    Wednesday, January 18, 2017 9:54 AM

Answers

  • The error occurs when Turtle.Show() is executed a second time. If you insert a Turtle.Hide just before the Goto Start or before EndWhile, there is no crash.

    [[ error found with the litdev extension LDdebug ]]


    Jan [ WhTurner ] The Netherlands

    • Proposed as answer by Nonki Takahashi Wednesday, January 18, 2017 1:27 PM
    • Marked as answer by Glenn Broadway Wednesday, January 18, 2017 1:54 PM
    Wednesday, January 18, 2017 12:16 PM
    Answerer

All replies

  • The code I stripped out was just a bit of input validation (to keep the numbers within sensible range) and a kepress detector to clear the canvas. The code above does have one slightly complicated section, where I work out the 'apothem'. it's just the 'radius' of the polygon, and it enables me to move the turtle at the start and face it in the right direction to draw the polygon right in the middle of the canvas.

    I did try removing the GOTO at the end and instead putting the whole thing inside a

    While "True"
    But this didn't stop it from crashing when it tries to draw further shapes.

    Wednesday, January 18, 2017 9:58 AM
  • The error occurs when Turtle.Show() is executed a second time. If you insert a Turtle.Hide just before the Goto Start or before EndWhile, there is no crash.

    [[ error found with the litdev extension LDdebug ]]


    Jan [ WhTurner ] The Netherlands

    • Proposed as answer by Nonki Takahashi Wednesday, January 18, 2017 1:27 PM
    • Marked as answer by Glenn Broadway Wednesday, January 18, 2017 1:54 PM
    Wednesday, January 18, 2017 12:16 PM
    Answerer
  • Excellent! Many thanks WhTurner. Was that something you just knew or was there a clue in the error message?

     
    Wednesday, January 18, 2017 2:04 PM
  • With LDDebug you can step through the program and see which line is in error. In this case the error occurred on the second time Turtle.Show, so I concluded that two times Turtle.Show gives an error.

    See the following testprogram

    GraphicsWindow.Show()
    Turtle.Show()
    Turtle.Show()
    
    CRASH !!!


    Jan [ WhTurner ] The Netherlands

    Wednesday, January 18, 2017 2:20 PM
    Answerer
  • Aha! Many thanks - I'll look into getting the debugger working when I get home (can't access the download within my school network).

    I'm a little surprised that two Turtle.Show() commands is quite so fatal!

    Wednesday, January 18, 2017 2:35 PM
  • With LDDebug you can step through the program and see which line is in error. In this case the error occurred on the second time Turtle.Show, so I concluded that two times Turtle.Show gives an error.

    See the following testprogram

    GraphicsWindow.Show()
    Turtle.Show()
    Turtle.Show()
    CRASH !!!


    Jan [ WhTurner ] The Netherlands


    Well diagnosed - I reported it - thanks.

    • Edited by litdev Wednesday, January 18, 2017 9:58 PM
    Wednesday, January 18, 2017 9:57 PM