none
Problems with GetLeft and GetTop for AddLine? RRS feed

  • Question

  • I spent hours trying to figure out why some code wasn't working.  It looks like there could be an issue with the aforementioned functions.  I simply have a few lines drawn on the display using Addline, move them by getting their GetTop and GetLeft and adding a constant then using Move to finish it.  It works, however I needed the GetLeft value also to do some display bounds checking, and it would never be what's expected.  It would be always zero referenced, i.e. even though I made a short line near the middle of the screen, the 'debugged' GetTop and GetLeft would report zeros, and when I Move the object, the GetLeft would then change from zero.

    I also made the terrible realization that all variables are global.  I sure hope that's changed!

    Lastly, I realized that code can be totally prempted by keypress event processing.  That really makes it hard to deal with having variables potentially changing throughout execution of a function (unless you have a Delay call which is understandable).

    Lastly lastly, a single-stepping debugger would make this a very easy language to develop in.  Putting in trace code or printing all over the screen isn't much fun.

    Thanks for any help!
    Thursday, May 14, 2009 5:49 AM

Answers

  • This is a known problem.
    When you add lines, be sure adding them at the origin, and then moving them.

    GraphicsWindow.Show()
    L = Shapes.AddLine(5,5,55,55)
    Dialogs.ShowMessage("Left: " + Shapes.GetLeft(L) + " (5 is the hoped value)")

    ==>

    GraphicsWindow.Show()
    L = Shapes.AddLine(0,0,50,50)
    Shapes.Move(L, 5, 5)
    Dialogs.ShowMessage("Left: " + Shapes.GetLeft(L) + " (5 is the hoped value)")
    Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension
    Thursday, May 14, 2009 9:59 AM
  • bigdaddyo,

    The shape you create with Shape.AddLine, Shapes.AddRectangle etc. is actually the smallest rectangle with (0,0) as one corner that just contains the figure. Most of this is of course invisible but is the reason GetLeft and GetTop report zeros. This is important to remember if you wish to rotate a Shape (using Shapes.Rotate). The centre of rotation is actually the centre of this imaginary rectangle.
    If you add a line from (0,0) to (100,100) and rotate it, it will rotate about its centre as the centre of the enclosing rectangle is (50,50).
    If you add a line from (100,100) to (200,200) and rotate it, it will rotate about its end. Try adding different shapes and rotating them to get a feel for it.
    Remember, the enclosing rectangle is formed with (0,0) as one corner and the extreme x and extreme y co-ordinates within the added shape defining the size.

    You said,
    "I also made the terrible realization that all variables are global.  I sure hope that's changed!"

    Oh no it hasn't! If you're interested, read some of the earlier posts and track down the video of the interview with Vijaye for some explanation. Basically it boils down to the fact that as a beginner's language it was decided not to have scope for the variables.

    I don't understand your concern over event processing, it doesn't seem to be a problem. Perhaps you should flag events and process them in the main loop if you're worried.

    Debugging help is one of those things that has been requested for v0.5 (See the thread "Features & Bug Fixes for v0.5"). Wether it will be added is up to Vijaye. You have to remember that Small Basic is intended to be a teaching language and for that reason it can be argued that its strength lies in its minimalism.


    Thursday, May 14, 2009 5:55 PM

All replies

  • This is a known problem.
    When you add lines, be sure adding them at the origin, and then moving them.

    GraphicsWindow.Show()
    L = Shapes.AddLine(5,5,55,55)
    Dialogs.ShowMessage("Left: " + Shapes.GetLeft(L) + " (5 is the hoped value)")

    ==>

    GraphicsWindow.Show()
    L = Shapes.AddLine(0,0,50,50)
    Shapes.Move(L, 5, 5)
    Dialogs.ShowMessage("Left: " + Shapes.GetLeft(L) + " (5 is the hoped value)")
    Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension
    Thursday, May 14, 2009 9:59 AM
  • bigdaddyo,

    The shape you create with Shape.AddLine, Shapes.AddRectangle etc. is actually the smallest rectangle with (0,0) as one corner that just contains the figure. Most of this is of course invisible but is the reason GetLeft and GetTop report zeros. This is important to remember if you wish to rotate a Shape (using Shapes.Rotate). The centre of rotation is actually the centre of this imaginary rectangle.
    If you add a line from (0,0) to (100,100) and rotate it, it will rotate about its centre as the centre of the enclosing rectangle is (50,50).
    If you add a line from (100,100) to (200,200) and rotate it, it will rotate about its end. Try adding different shapes and rotating them to get a feel for it.
    Remember, the enclosing rectangle is formed with (0,0) as one corner and the extreme x and extreme y co-ordinates within the added shape defining the size.

    You said,
    "I also made the terrible realization that all variables are global.  I sure hope that's changed!"

    Oh no it hasn't! If you're interested, read some of the earlier posts and track down the video of the interview with Vijaye for some explanation. Basically it boils down to the fact that as a beginner's language it was decided not to have scope for the variables.

    I don't understand your concern over event processing, it doesn't seem to be a problem. Perhaps you should flag events and process them in the main loop if you're worried.

    Debugging help is one of those things that has been requested for v0.5 (See the thread "Features & Bug Fixes for v0.5"). Wether it will be added is up to Vijaye. You have to remember that Small Basic is intended to be a teaching language and for that reason it can be argued that its strength lies in its minimalism.


    Thursday, May 14, 2009 5:55 PM
  • ...event processing, it doesn't seem to be a problem. Perhaps you should flag events and process them in the main loop if you're worried.

    Thanks for your replies.  The events preemption can make code really really difficult to debug, especially w/o a debugger.  Things can be so much easier by not allowing events to process until a delay() is called.  An example is someone getting the keypress to move left in which a velocity variable is set to some constant.  A chain of code is executing using this velocity to do numerous things such as moving perhaps many objects on the screen.  Each object may or may not move at that velocity that a begginer would assume to be constant in that chain of code.  It happened to me!  My objects on the screen were separating...

    To have what can be a very complicated capability such as preemptive execution and allow all variables to be global seems to have a conflict of goals for a beginner.  Programmers will be guaranteed to be stepping on variables, especially if they imported someone else's functions.  I never thought variable scope was a difficult concept, it's incredibly useful.  Global variables were a demon like line numbers in old compilers.  It also forces you to make nutty variables like velocity1, velocity2, etc. and keep track of all of them so that they don't step on each other.

    The reference coordinates for rotation and GetLeft/Top will guaranteed confuse a beginner, and an aged programmer (of 30 yr).

    I do seriously appreciate the minimalist nature of what you guys are creating, I think it's great!  But a stepping debugger is the beginners necessity.  I realize it could be a serious amount of work, but you'd be doing it for a much much better product.  If kids enjoy using Microsoft compilers then of course they'll wish to continue using them into their professional career, I figure one of your main goals.

    I hope you don't mind an old geezer spamming you a little more.  For "Beginners All-purpose Symbolic Instruction Code" I honestly don't think you need to preface things like Addline with Shape, Sin with Math, etc.  I understand it's use in larger languages, but it's just a waste of time & effort always preceding these things like that, and getting the spelling right or forgetting the term "GraphicsWindow".  If there's an add-on library that duplicates these methods, then the programmer will know it and then can precede the function with that library name e.g. "Comport.".  I've never understood why 'higher" languages can't follow that methodology, there should always be a base library by default.  Otherwise couldn't GraphicsWindow be simplified to Window?  These can be big words to a 9 year old.

    I'm working on a fun game to put up here.  Thanks for listening!
    • Edited by bigdaddyo Thursday, May 14, 2009 8:10 PM
    Thursday, May 14, 2009 8:04 PM