LineXY

Everything else that doesn't fall into one of the other PB categories.
Ivan
User
User
Posts: 25
Joined: Fri Sep 11, 2020 1:53 pm

LineXY

Post 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?
BarryG
Addict
Addict
Posts: 1506
Joined: Thu Apr 18, 2019 8:17 am

Re: LineXY

Post 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.
Last edited by BarryG on Sun Sep 13, 2020 9:48 am, edited 2 times in total.
Little John
Addict
Addict
Posts: 3981
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: LineXY

Post 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.
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups
#NULL
Addict
Addict
Posts: 1334
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: LineXY

Post 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.
BarryG
Addict
Addict
Posts: 1506
Joined: Thu Apr 18, 2019 8:17 am

Re: LineXY

Post by BarryG »

Yes #NULL, that's what I was thinking of. Thanks for finding it for me. Edited my other post.
Ivan
User
User
Posts: 25
Joined: Fri Sep 11, 2020 1:53 pm

Re: LineXY

Post 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.
Little John
Addict
Addict
Posts: 3981
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: LineXY

Post 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.
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups
Ivan
User
User
Posts: 25
Joined: Fri Sep 11, 2020 1:53 pm

Re: LineXY

Post 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?
Little John
Addict
Addict
Posts: 3981
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: LineXY

Post 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.
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups
User avatar
Saki
Enthusiast
Enthusiast
Posts: 779
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: LineXY

Post 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
地球上の平和
User avatar
spikey
Enthusiast
Enthusiast
Posts: 393
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: LineXY

Post 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...
Ivan
User
User
Posts: 25
Joined: Fri Sep 11, 2020 1:53 pm

Re: LineXY

Post 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?
User avatar
Saki
Enthusiast
Enthusiast
Posts: 779
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: LineXY

Post 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.
地球上の平和
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3734
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: LineXY

Post 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.
macOS 10.15 Catalina, Windows 10
User avatar
mk-soft
Addict
Addict
Posts: 3069
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: LineXY

Post 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
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
Post Reply