Wish XUI drawline/rectangle straight line as non antialised

emexes

Expert
Licensed User
B4A test, no antialiasing:
B4X:
rctTest.Initialize(10, 500, 60, 500)
cvsMain.DrawRect(rctTest, Colors.Red, False, 1)
rctTest.Initialize(10, 520, 60, 521)
cvsMain.DrawRect(rctTest, Colors.Red, False, 1)
rctTest.Initialize(10, 540, 60, 542)
cvsMain.DrawRect(rctTest, Colors.Red, False, 1)
rctTest.Initialize(10, 560, 60, 563)
cvsMain.DrawRect(rctTest, Colors.Red, False, 1)
And the result.
View attachment 82765
When I zoom in on your test image (same method as used above, ie no fancy smoothing) I see antialiasing of what are supposed to be 1-pixel lines:
upload_2019-8-2_23-45-9.png
 
Last edited:

emexes

Expert
Licensed User
What is confusing is that the lines are spread across 3 (even maybe 4) pixel-widths, not 2.

Is it possible that something in between your screen capture and the forum post has smudged the image?
 

emexes

Expert
Licensed User
Now, just my two cents.
Your two centimes is worth 100x to most us here :) especially since the Great Rotating Gauge discussion.
Should draw two lines 1 prixel wide,
but it did warm my heart to see that even you make the occasional mistake (unless a prixel is an actual thing, in which case: I give up)
 

klaus

Expert
Licensed User
Longtime User
When I zoom in on your test image (same method as used above, ie no fancing smoothing) I see antialiasing of what are supposed to be 1-pixel lines:
Is it possible that something in between your screen capture and the forum post has smudged the image?
Yes, it is added by my screen capture program Snagit.
When I zoom in the ScreenShot window of the IDE it's OK.

Should draw two lines 1 prixel wide,
Unfortunately there is no autocorrection.
 
Last edited:

emexes

Expert
Licensed User
Yes, it is added by my screen capture program Snagit.
Snagit smudges screen captures? I wouldn't have thought you'd put up with that for very long.

Then again, I use PicPick, which does captures perfectly, but I have to use a different program (ThumbsPlus) to do the scaling without any "enhancement".
Unfortunately there is no autocorrection.
I did actually check later, because Prixel sounded like a plausible word. Like, it could have been a term for pixels aligned to the coordinate system rather than to the display raster grid.
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Should draw two lines 1 prixel wide,
I corrected it now in post #19 to avoid any confusion.
I have the French orthography correction activated in Firefox and when I write any English texts there are lots of words underlined in red.
Now I found that other languages can be added, so I added English.
 

sorex

Expert
Licensed User
Longtime User
When I wrote my answer in post #2 I missed the fact that you were talking about Canvas. BitmapCreator.DrawRect should draw thin lines properly.

actually the code and weirdness in post #7 is pure bitmapcreator. (notice the bc. instead of c.)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
For now the best option is to use these simple methods for drawing thin lines:
B4X:
Sub DrawHorizontalLine(bc As BitmapCreator, x1 As Int, y As Int, x2 As Int, clr As Int)
   If y < 0 Or y >= bc.mHeight Then Return
   Dim argb As ARGBColor
   bc.ColorToARGB(clr, argb)
   For x = Max(0, Min(x1, x2)) To Min(Max(x1, x2), bc.mWidth - 1)
       bc.SetARGB(x, y, argb)
   Next
End Sub

Sub DrawVerticalLine(bc As BitmapCreator, x As Int, y1 As Int, y2 As Int, clr As Int)
   If x < 0 Or x >= bc.mWidth Then Return
   Dim argb As ARGBColor
   bc.ColorToARGB(clr, argb)
   For y = Max(0, Min(y1, y2)) To Min(Max(y1, y2), bc.mHeight - 1)
       bc.SetARGB(x, y, argb)
   Next
End Sub
 

emexes

Expert
Licensed User
For now the best option is to use these simple methods
Earlier in this thread I was going to make a wisearse remark about the old days of drawing lines pixel-by-pixel but now I'm glad I kept my mouth shut :)
 

emexes

Expert
Licensed User
As straight lines is a simple edge case I don't want (for now) to modify DrawRect or DrawLine and treat it specially.
I agree, except for the "(for now)" bit.

We have the options of both ways already, so personally I think the existing anti-aliased routines should continue working the way that they do, rather than veer off for special cases - that's just going to invite a new round of why doesn't this draw the way I think it should questions, but from the other direction.

For most drawing tasks, where the resolution nowadays means that we can think at a level of shapes rather than pixels, it is better that X and Y are continuous coordinates rather than discrete pixel column/row numbers.

The only part of that concept that I was iffy about is the way that Rect borders fall half-a-StrokeWidth outside of the Rect coordinates (and thus often half outside of the drawable/visible area). I rationalised that iffiness away by considering the extreme case case of what should happen if the Rect had Width and/or Height < StrokeWidth.
 
Last edited:

klaus

Expert
Licensed User
Longtime User
For now the best option is to use these simple methods for drawing thin lines:
For my need I went back to the roots and have already written my own routine to draw thin lines without antialiasing.

In the routines in post#29, what is the reason for using:
B4X:
Dim argb As ARGBColor
bc.ColorToARGB(clr, argb)
For x = Max(0, Min(x1, x2)) To Min(Max(x1, x2), bc.mWidth - 1)
    bc.SetARGB(x, y, argb)
Next
Instead of using directly the clr color Int:
B4X:
For x = Max(0, Min(x1, x2)) To Min(Max(x1, x2), bc.mWidth - 1)
    bc.SetColor(x, y, clr)
Next
 
Last edited:

sorex

Expert
Licensed User
Longtime User
the outer max & min has no use either when the 4 coordinates have been given, not?

Edit: ok, I get it... it's there to ignore out of boundary coordinates.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Instead of using directly the clr color Int:
Small optimization. The int color is converted to an ARGB color and then to a premultiplied color. Calling SetARGB saves one of the conversions.

Note that the built-in BitmapCreator methods are more sophisticated and are faster (horizontal lines are drawn as single array copy calls) however in the case of single pixel lines these methods should be fast enough.
 
Top