Skip to first unread message

Jörg Kowalski

unread,
Jan 9, 2017, 2:55:37 PM1/9/17
to MIT App Inventor Forum
Hello,
I think there must be a bug in the "canvas.drawline". See the appendix aia.file and the images below. In the horizontal direction there is a gap assuming a width/height of 20 for each blue square. Three squares should completely fill a horizontal canvas area as it does in canvas 3, but in it using a square length of 21 (x2 = x1 + width instead of x2 = x1 + width -1) to fill the gap. 
So the coordinates/range of a canvas should be 0..19, 20..39, 40..59 for each drawn square. The width of each canvas is 60/60. Canvas 3 uses horizontal 21 px and the gap is closed but this is not mathematically correct! 

The "drawline" function ignores the last pixel. 

You get also too a short line if you draw a line diagonally (not shown). The same happens vertically (see drawing of canvas 4 with slightly modified functions). What I don't understand as well is the result of canvas 2. Why do I get at all white lines either line 59 or line 0? A canvas of 60 px... Is it really to be addressed from 0 .. 59 as you comment in your guide?
The bug is harmful!
Sincerely and thanks for testing it.
Jörg Kowalski, Germany


MITAppInventor_CanvasBug.aia

Taifun

unread,
Jan 9, 2017, 4:13:02 PM1/9/17
to mitappinv...@googlegroups.com
. Is it really to be addressed from 0 .. 59 as you comment in your guide?
where did you find that comment? any link?
App Inventor is 1 indexed 
and as far as I know, everything starts at 1 (and not 0)


Canvas

A two-dimensional touch-sensitive rectangular panel on which drawing can be done and sprites can be moved.

The BackgroundColor, PaintColor, BackgroundImage, Width, and Height of the Canvas can be set in either the Designer or in the Blocks Editor. The Width and Height are measured in pixels and must be positive.

Any location on the Canvas can be specified as a pair of (X, Y) values, where

  • X is the number of pixels away from the left edge of the Canvas
  • Y is the number of pixels away from the top edge of the Canvas

Taifun

Trying to push the limits of App Inventor! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun. 

Jörg Kowalski

unread,
Jan 9, 2017, 4:58:29 PM1/9/17
to MIT App Inventor Forum

Jörg Kowalski

unread,
Jan 9, 2017, 5:56:53 PM1/9/17
to MIT App Inventor Forum

Taifun

unread,
Jan 9, 2017, 7:04:27 PM1/9/17
to MIT App Inventor Forum
yes, after thinking again about it of course it makes sense to have the Canvas start at 0,0...
probably someone can help to find out more about your issue?

Taifun

Ghica

unread,
Jan 10, 2017, 10:31:41 AM1/10/17
to MIT App Inventor Forum
It would really help if you would prepare a SMALL sample that shows your problem. I looked at your code, but it would take me hours to trace it, which is time I cannot spend.
My first thought would be to replace 59 everywhere with 60, because that is what it should be. Maybe a bug in the "course in a box" book.
Cheers, Ghica.

Jörg Kowalski

unread,
Jan 10, 2017, 6:22:11 PM1/10/17
to MIT App Inventor Forum
Thank you Ghica! 

Even now I am convinced that there is a bug in the predefined "canvas.drawline". After that discussion here I tested it last night under various conditions again. It is not relevant whether one reflects a 0-indexed or 1-indexed canvas. It is obviously 0-indexed as explained in the book. This you can proove: Draw a line with "x1" = 1 then you see a gap at the beginning which can on the other hand be closed in case of "x1" = 0.

But the problem is how to define the coordinates "x1" and "x2". In my opinion it should "x1" be the first pixel of a line with "x2" representing the last pixel. But if you draw a line from e. g. cells 10 to 15 (x1, x2) then you get a line of only 5 pixels where it actually should be 6! pixels (cells 10, 11, 12, 13, 14, 15), so lacking the last pixel. 

For the moment I've solved that problem by putting in as "x2": x1 + the wanted width, in the example 10 + 6 (16) and it works, but it is in fact beyond 15. It is strange because normally in any graphic function x1/x2 is expected to represent the first/last pixel. It cost me much time to explore the adequate use of the function which does not follow, to my opinion, the "normal" logic.

Another proof is: draw a line (x1=0; x2 = 15). Draw another line, this time using the canvas.drawPoint-function using a loop from 0 to 15. Then you see that the latter one creates a line of 16 pixels (see app) while the "drawLine"-function only creates a line of 15 pixels (see the gap of one pixel to the blue vertical line). 

If you want to draw a diagonal line from cell (0,0) to cell (10,10) you must plug in: drawLine ((0,0)(11,11)). That cannot be expected intuitively!

Was it really this way the "drawLine"-function should work while having implemented it? 

I've simplified the functions so that you can follow them more easily.

Again, thank you very much for having engaged in this problem.

Cheers, Jörg


MITAppInventor_CanvasBug (1).aia

Ghica

unread,
Jan 11, 2017, 5:21:08 AM1/11/17
to MIT App Inventor Forum


Hi Jorg,
Even now I am not convinced. From your blocks I do not understand what you are trying to do, so I made a really simple app:
It has one canvas of 60x60 pixels, then with the click of a button you can draw 2 lines, with linewidth of 10 to make this more clear.
One line from 0 to 60 and one line from 1 to 59. Yellow lines on a blue background.
See attached screenshot, blocks and .aia
Cheers, Ghica.

canvas60.aia

Jörg Kowalski

unread,
Jan 11, 2017, 4:17:34 PM1/11/17
to MIT App Inventor Forum
Hello,
very interesting. But if a canvas is 0-indexed (see the book), so in this case from 0..59, then it should normally not be allowed to draw a pixel at position 60. If it were 1-indexed it should not be allowed to set a pixel at position 0. Your second line is the proof: last pixel at position 59 followed by a gap to the margin. To get a full line you have to define a scope of 61 pixels: that is x1 = 0 and x2 = 60 makes a total of 61 pixels, so one more than the canvas has. It should not be possible, so the only explanation is that "drawLine" swallows up the last pixel and that is for me a bug - or one has to redefine the variable x2 which would not match the intuitive logic.
cheers, Jörg


Ghica

unread,
Jan 11, 2017, 5:37:37 PM1/11/17
to MIT App Inventor Forum
Jörn,
Maybe you are confusing pixels with coordinates. Pixels are like stretches of a road, the coordinates like trees marking the stretches. You need always one more tree than the number of stretches.
Cheers, Ghica

Jörg Kowalski

unread,
Jan 11, 2017, 6:19:34 PM1/11/17
to MIT App Inventor Forum
Anyway...
Thanks for your discussion!
Jörg

APPARTTECH Ltd.

unread,
Jan 13, 2017, 8:09:28 AM1/13/17
to MIT App Inventor Forum
We agree with Jörg.

Ghica

unread,
Jan 13, 2017, 8:53:06 AM1/13/17
to MIT App Inventor Forum
@apparttech: What do you agree with? That there is a bug? Or with my explanation?
I agree that the explanation in the Course In a Box is not clear. The coordinate of the last pixel is indeed 59,0 but if you want to draw a line through the pixel, you have to draw to the other end, which is at x+1.
You should know, because you made this beautiful app!

By the way appinvontor.org is not part of MIT, but I still will try to make this known to them.
Cheers, Ghica
Reply all
Reply to author
Forward
0 new messages