BoxXY()

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

BoxXY()

Post by infratec »

Hi together,

Marc, a new user, had a problem with the Box() procedure.
So at least I found a working solution for him.
Maybe it is also usefull for someone else:

Code: Select all

Procedure BoxXY(x1, y1, x2, y2, Colour = -1)
  
  If x2 < x1
    x = x1
    x1 = x2
    x2 = x + 1
  Else
    x2 + 1
  EndIf
  
  If y2 < y1
    y = y1
    y1 = y2
    y2 = y + 1
  Else
    y2 + 1
  EndIf
  
  If Colour = -1
    Box(x1, y1, x2 - x1, y2 - y1)
  Else
    Box(x1, y1, x2 - x1, y2 - y1, Colour)
  EndIf
  
EndProcedure
The original thread with an example is here:
http://www.purebasic.fr/english/viewtop ... 13&t=42599

Best regards,

Bernd
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: BoxXY()

Post by STARGÅTE »

it is a little faster and easier if it so you write: :wink:

Code: Select all

Procedure BoxXY(x1, y1, x2, y2, Colour = #PB_Default)
  
  If x2 < x1
    Swap x1, x2
  EndIf
  
  If y2 < y1
    Swap y1, y2
  EndIf
  
  If Colour = #PB_Default
    Box(x1, y1, x2 - x1 + 1, y2 - y1 + 1)
  Else
    Box(x1, y1, x2 - x1 + 1, y2 - y1 + 1, Colour)
  EndIf
  
EndProcedure
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: BoxXY()

Post by infratec »

Event more simpilfied:

Code: Select all

Procedure BoxXY(x1, y1, x2, y2, Colour = #PB_Default)
  If x2 < x1 : Swap x1, x2 : EndIf
  If y2 < y1 : Swap y1, y2 : EndIf
  Box(x1, y1, x2 - x1 + 1, y2 - y1 + 1, Colour)
EndProcedure
:mrgreen: :mrgreen: :mrgreen:

P.S.: Thanks
Last edited by infratec on Fri Jun 25, 2010 12:29 pm, edited 1 time in total.
User avatar
Michael Vogel
Addict
Addict
Posts: 2810
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: BoxXY()

Post by Michael Vogel »

I know, this was not the inital intention of this thread, but I can't resist... :wink:

Code: Select all

Procedure BoxXY(x1, y1, x2, y2, Colour = #PB_Default)
  Box(Min(x1,x2), Min(y1,y2), Abs(x1 - x2) + 1, Abs(y1 - y2) + 1, Colour)
EndProcedure
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: BoxXY()

Post by infratec »

Hi Michael,

which version of PureBASIC do you use? 4.51 :?: :?: :?:
Are you a beta tester? :mrgreen:
I have no Min() procedure in 4.50.

And if you use Abs(), I think it is much slower cause of the floating point stuff.
User avatar
Michael Vogel
Addict
Addict
Posts: 2810
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: BoxXY()

Post by Michael Vogel »

infratec wrote:Hi Michael,

which version of PureBASIC do you use? 4.51
Nope, 4.60 - seems, that you still don't have it :lol:
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: BoxXY()

Post by Demivec »

@infratec: you edited out some of the original functionality, here's the correction:

Code: Select all

Procedure BoxXY(x1, y1, x2, y2, Colour = #PB_Default)
  If x2 < x1 : Swap x1, x2 : EndIf
  If y2 < y1 : Swap y1, y2 : EndIf
  If Colour = #PB_Default
    Box(x1, y1, x2 - x1 + 1, y2 - y1 + 1)
  Else 
    Box(x1, y1, x2 - x1 + 1, y2 - y1 + 1, Colour)
  EndIf 
EndProcedure
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: BoxXY()

Post by infratec »

Demivec wrote:@infratec: you edited out some of the original functionality
Hi Demivec,

yes, I removed this if ... else ... stuff, because it is not neccessary.
If no Colour is given as parameter the default value is in Colour.
So I can also call the original Box() with this Colour. It makes no difference.

At least:
It's not a bug, it's a feature!
Bernd
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: BoxXY()

Post by Demivec »

infratec wrote:
Demivec wrote:
It's not a bug, it's a feature!
Bernd
I can understand your desire to remove unnecessary parts but I think you over did it.

Here's a demonstration:

Code: Select all

Procedure BoxXY(x1, y1, x2, y2, Colour = #PB_Default)
  If x2 < x1 : Swap x1, x2 : EndIf
  If y2 < y1 : Swap y1, y2 : EndIf
  Box(x1, y1, x2 - x1 + 1, y2 - y1 + 1, Colour)
EndProcedure
  
Procedure BoxXY2(x1, y1, x2, y2, Colour = #PB_Default)
  If x2 < x1 : Swap x1, x2 : EndIf
  If y2 < y1 : Swap y1, y2 : EndIf
  If Colour = #PB_Default
    Box(x1, y1, x2 - x1 + 1, y2 - y1 + 1)
  Else
    Box(x1, y1, x2 - x1 + 1, y2 - y1 + 1, Colour)
  EndIf
EndProcedure

#Size = 50

OpenWindow(0, 0, 0, 215, 180,"Example Colors",#PB_Window_SystemMenu)
CreateImage(0, #Size * 3, #Size * 3, 32)
StartDrawing(ImageOutput(0))
  ;these boxes should use the default FrontColor()
  BoxXY(0, 0, #Size - 2, #Size - 2)
  Box(#Size, 0, #Size - 1, #Size - 1)
  BoxXY2(#Size * 2, 0, #Size * 3 - 2, #Size - 2)
  
  ;these boxes should use the specified color
  BoxXY(0, #Size, #Size - 2, #Size * 2 - 2, RGB(0, 255, 0))
  Box(#Size, #Size, #Size - 1, #Size - 1, RGB(0, 255, 0))
  BoxXY2(#Size * 2, #Size, #Size * 3 - 2, #Size * 2 - 2, RGB(0, 255, 0))
  
  ;these boxes should use the specified FrontColor()
  FrontColor(RGB(255, 0, 0))
  BoxXY(0, #Size * 2, #Size - 2, #Size * 3 - 2) ; <=== this one doesn't
  Box(#Size, #Size * 2, #Size - 1, #Size - 1) 
  BoxXY2(#Size * 2, #Size * 2, #Size * 3 - 2, #Size * 3 - 2)
StopDrawing()

ImageGadget(0, 0, 20, 0, 0, ImageID(0))
TextGadget(1, 0             , 0             , #Size, 20, "BoxXY()" , #PB_Text_Center)
TextGadget(2, #Size         , 0             , #Size, 20, "Box()"   , #PB_Text_Center)
TextGadget(3, #Size * 2     , 0             , #Size, 20, "BoxXY2()", #PB_Text_Center)
TextGadget(4, 10 + #Size * 3, 30            , #Size, 40, "Default FrontColor")
TextGadget(5, 10 + #Size * 3, 30 + #Size    , #Size, 40, "Green")
TextGadget(6, 10 + #Size * 3, 30 + #Size * 2, #Size, 40, "Red FrontColor")

Repeat: Until WaitWindowEvent() = #PB_Event_CloseWindow
Only the BoxXY2() uses the FrontColor() when the color isn't specified. I would presume this would be the desired way. :)
User avatar
Michael Vogel
Addict
Addict
Posts: 2810
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: BoxXY()

Post by Michael Vogel »

#PB_Default is still not supported in PB 4.60 :wink: , but it is interesting, demivecs example brings up different results using different PB versions (4.50: white, 4.30: black)...

Would be interesting, if PB 4.60 has something like a GetFrontColor() function (beside Min and integer Abs) :|...

Code: Select all

Procedure BoxXY(x1, y1, x2, y2, Colour = #PB_Default)
	If Colour = #PB_Default
		Colour=GetFrontColor()
	EndIf
	Box(Min(x1,x2), Min(y1,y2), Abs(x1 - x2) + 1, Abs(y1 - y2) + 1, Colour)
EndProcedure
PS I wouldn't know hot to write a GetFrontColor() function for PB 4.50 compatibility, the Min() could be done in so many ways, even wicked things like Macro Min(a,b) : ((a>b)*b+(a<=b)*a) : EndMacro
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: BoxXY()

Post by infratec »

@ Demivec,

you are definately right.
But for me this is a kind of bug in PureBASIC.
Because when I set the fontcolor, this is the new default color.
This is also written in the 'german' help.

@ Michael,

Differrent colors in different versions of PureBASIC is not very good,
but it is not really a kind of bug, since nowhere is written what the
default color is.

But now I'm unsure if I should open a bug thread or not. :?

Bernd
Post Reply