Page 1 of 3

LineXY

Posted: Sat Sep 12, 2020 8:52 pm
by Ivan
I am playing with 2D graphics and I'm used to when y = 0 then I will be at the bottom.

Is't easy for me to reverse but can PB be ajusted to do that by a setting or similar to change that behaviar?

Re: LineXY

Posted: Sun Sep 13, 2020 1:06 am
by BarryG
[Edit] I was mistaken - sorry. I was thinking of the 3D Engine library where X/Y/Z default to bottom-left of the screen.

Re: LineXY

Posted: Sun Sep 13, 2020 7:00 am
by Little John
BarryG wrote:
Ivan wrote:I'm used to when y = 0 then I will be at the bottom.
PureBasic does that,
No, it does not do so with the 2DDrawing functions such as LineXY().

When using PB's 2DDrawing functions, x = 0 is at the very left, and y = 0 is at the very top of the output area (this is the classic behaviour of the BASIC programming language).
The following example demonstrates this behaviour:

Code: Select all

If OpenWindow(0, #PB_Ignore, #PB_Ignore, 200, 200, "2DDrawing Example") = 0
   End   
EndIf

If CreateImage(0, DesktopScaledX(200), DesktopScaledY(200)) And StartDrawing(ImageOutput(0))
   LineXY(0, 0, DesktopScaledX(100), DesktopScaledY(150))
   StopDrawing()
   ImageGadget(0, 0, 0, 200, 200, ImageID(0))
EndIf

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
@Ivan: There is no built-in command to change this for the 2DDrawing functions.
However, you have much more possibilities when using PB's more modern vector drawing library.

Re: LineXY

Posted: Sun Sep 13, 2020 9:47 am
by #NULL
BarryG wrote:My apologies. I could've sworn I've previously seen a diagram in the PureBasic Help that showed the X/Y/Z dimensions as a 3D box (with Y at the bottom), but can't find it now to defend myself.
Maybe you saw https://www.purebasic.com/documentation/engine3d/index.html
That's 3d space though and completely separate from normal 2d pixel space.

Re: LineXY

Posted: Sun Sep 13, 2020 9:49 am
by BarryG
Yes #NULL, that's what I was thinking of. Thanks for finding it for me. Edited my other post.

Re: LineXY

Posted: Sun Sep 13, 2020 10:53 am
by Ivan
@Little John

"(this is the classic behaviour of the BASIC programming language)." - not the BBC Basic I was used to in the eighties. :)

That very little I know about a coordinate system and math: Incrementing y-values goes up - not down.

I'm in a learning process of trigonometry and was surprised.

Although I'm used to text location (x,y) goes from the top and down.

Re: LineXY

Posted: Sun Sep 13, 2020 11:16 am
by Little John
Ivan wrote:@Little John

"(this is the classic behaviour of the BASIC programming language)." - not the BBC Basic I was used to in the eighties. :)
OK. But it works that way in many classic BASIC flavours, IIRC e.g. in GW-BASIC, QBASIC, QuickBASIC, Turbo BASIC, PowerBASIC, ...
Ivan wrote:That very little I know about a coordinate system and math: Incrementing y-values goes up - not down.
This is true for the "normal" Cartesian coordinate system that most people learn at school. Hoewever, mathematicians also work with various other coordinate systems, and everyone is free to define her/his own coordinate system. The creators of many classic BASIC flavours took the freedom to define it the way that incrementing y-values go down from the top.

Re: LineXY

Posted: Sun Sep 13, 2020 12:58 pm
by Ivan
It's proberly becauce I not a mathematician, but what is the advantace in a 2D-system, to let y go down when increcing in value and not using the "normal" Cartesian coordinate system.

Is't possible to explain that in a pedagogical way to a matematical layman like me?

Re: LineXY

Posted: Sun Sep 13, 2020 1:10 pm
by Little John
There is no advantage. Whether 0 on the y-axis is at the bottom or at the top is just a convention.

Re: LineXY

Posted: Sun Sep 13, 2020 1:16 pm
by Saki
An image that is stored in memory starts at this point.
The coordinate you want is somewhere before the end of the image in memory.
It is simply illogical and unwise to complicate this simple thing.

Best Regards Saki

Re: LineXY

Posted: Sun Sep 13, 2020 1:25 pm
by spikey
You can't be that much of a mathematical layman if you've recognised that the Cartesian co-ordinate system is being ignored!
There really isn't any 'logic' to it - it goes back to when computer terminals were really just jumped up electric typewriters.

A positive column movement moved the teletypewriter head from left to right across the paper. A positive row movement fed paper through the head - effectively moving the head 'down' the page. This system doesn't really have an origin because it depends where the operator leaves the paper when they load it (I'm talking continuous tractor feed paper here) but its 'virtual' origin would be at the top left corner of the page, if the operator loaded it properly.

When terminals started moving to block video rather than paper, the tab, line feed, carriage return concepts came with and no-one ever corrected the inconsistency because there was a fuzzy changeover with both systems being used simultaneously - not a clearly defined one.

Nor did it get corrected when true graphic displays were introduced for the same reason. The tab column and line analogy was dropped in favour of X and Y based co-ordinates to differentiate 'high resolution' video from block video, but the Y direction was reversed with respect to normal mathematical practice.

When true page printers were introduced they followed the same odd convention...

Re: LineXY

Posted: Sun Sep 13, 2020 2:04 pm
by Ivan
@spikey - thanks.

It seems logical when cr/lf scrolls up seen in the historical perspective.

I find it interesting to know, if one of the PB-programmers would explain why, they made that decision...

@Saki - would you elaborate?

Re: LineXY

Posted: Sun Sep 13, 2020 2:20 pm
by Saki
Hi,
Images are always stored line by line,
the way you would read it.

Also with sprites or shapes, the color of the mask is read from coordinate 0, 0 by default.

Re: LineXY

Posted: Sun Sep 13, 2020 2:53 pm
by wilbert
Saki wrote:Images are always stored line by line,
the way you would read it.
That depends on the OS.
If you look at the documentation for DrawingBufferPixelFormat() you see there's a flag #PB_PixelFormat_ReversedY to determine how the image is stored in memory.
On Linux and macOS it's stored as you see it but on Windows a BMP is usually stored upside down.

Re: LineXY

Posted: Sun Sep 13, 2020 3:44 pm
by mk-soft
For this I like VectorDrawing ...

Example with some help macros

Code: Select all

;-TOP
; Flip Y-Coordinates by mk-soft, v0.6

EnableExplicit

Global _IsFlipped_, _OldY_.d

Macro _PB_(Function)
  Function
EndMacro

Macro BeginFlipCoordinates()
  FlipCoordinatesY(VectorOutputHeight() * 0.5) : _IsFlipped_ = #True
EndMacro

Macro EndFlipCoordinates()
  _PB_(ResetCoordinates)() : _IsFlipped_ = #False
EndMacro

Macro ResetCoordinates(_Value_=#PB_Coordinate_User)
  _PB_(ResetCoordinates)(_Value_) : If _IsFlipped_ : FlipCoordinatesY(VectorOutputHeight() * 0.5, _Value_) : EndIf
EndMacro

Macro DrawVectorText(_Text_)
  If _IsFlipped_
    _OldY_ = PathCursorY()
    FlipCoordinatesY(_OldY_) : _PB_(DrawVectorText)(_Text_) : FlipCoordinatesY(_OldY_)
  Else
    _PB_(DrawVectorText)(_Text_)
  EndIf
EndMacro

Define i

If OpenWindow(0, 0, 0, 450, 300, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 0, 0, 450, 300)
  LoadFont(0, "Impact", 11)
  
  If StartVectorDrawing(CanvasVectorOutput(0))
    VectorFont(FontID(0))
    
    MovePathCursor(10, 10)
    DrawVectorText("Flip Coordinates Example")
    
    BeginFlipCoordinates()
    
    ; Part 1
    ResetCoordinates()
    
    AddPathSegments("M 40 20 L 120 20 L 120 60 L 200 60 L 200 100 L 280 100 L 280 140 L 360 140 L 360 180")
    VectorSourceColor(RGBA(255, 0, 0, 255))
    StrokePath(5, #PB_Path_RoundCorner)
    
    MovePathCursor(10, 30)
    RotateCoordinates(10, 30, 45)
    DrawVectorText("Red Line")
    
    ;Part 2
    ResetCoordinates()
    
    TranslateCoordinates(50, 50)      
    
    AddPathSegments("M 40 20 L 120 20 L 120 60 L 200 60 L 200 100 L 280 100 L 280 140 L 360 140 L 360 180")
    VectorSourceColor(RGBA(0, 0, 255, 255))
    StrokePath(5, #PB_Path_RoundCorner)
    
    MovePathCursor(10, 30)
    RotateCoordinates(10, 30, 45)
    DrawVectorText("Blue Line")
    
    ; Part 3
    ResetCoordinates()
    
    AddPathCircle(80, 80, 50, 0, 45)
    VectorSourceColor(RGBA(255, 0, 0, 255))
    StrokePath(5, #PB_Path_RoundCorner)
    
    AddPathCircle(80, 80, 50, 45, 90)
    VectorSourceColor(RGBA(0, 0, 255, 255))
    StrokePath(5, #PB_Path_RoundCorner)
    
    ; Part 4
    ResetCoordinates()
    
    VectorSourceColor(RGBA(64, 64, 64, 255))
    For i = 0 To 260 Step 20
      MovePathCursor(420, i + VectorTextHeight("X"))
      DrawVectorText(Str(i))
    Next
    
    EndFlipCoordinates()
    
    StopVectorDrawing()
  EndIf
  
  Repeat
    Define Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf