2DDrawing Line,Box or Circle w any Thickness and Color

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

2DDrawing Line,Box or Circle w any Thickness and Color

Post by RASHAD »

Hi
The title say it all

# 1: Line

Code: Select all

OpenWindow(0, 0, 0, 640,640, "Image DC", #PB_Window_SystemMenu |#PB_Window_ScreenCentered)
  LoadImage(1, #PB_Compiler_Home + "Examples\Sources\Data\Background.bmp")
  ResizeImage(1,640,640)
  color = $0000FF  
  stroke = 20  
  x1 = 10 : y1 = 30 : x2 = 500 : y2 = 500
  x3 = x1 + stroke : y3 = y1 - stroke :x4 = x2+ stroke : y4 = y2 - stroke
  StartDrawing(ImageOutput(1))
    LineXY(x1,y1,x2,y2,color)        
    LineXY(x3,y3,x4,y4,color)
    LineXY(x1,y1,x3,y3,color)
    LineXY(x2,y2,x4,y4,color)
    FillArea(x1+1,y1,color,color)
  StopDrawing()  
  
  ImageGadget(2, 0, 0,640,640, ImageID(1))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
         Quit = 1
  EndSelect
Until Quit = 1
# 2: Round Box

Code: Select all

OpenWindow(0, 0, 0, 640,640, "Image DC", #PB_Window_SystemMenu |#PB_Window_ScreenCentered)
  LoadImage(1, #PB_Compiler_Home + "Examples\Sources\Data\Background.bmp")
  ResizeImage(1,640,640)
  color = $0000FF  
  stroke = 20  
  x1 = 100 : y1 = 200 : w1 = 400 : h1 = 200
  x2 = x1 + stroke : y2 = y1 + stroke :w2 = w1 - 2*stroke : h2 = h1 - 2*stroke
  StartDrawing(ImageOutput(1))
    DrawingMode(#PB_2DDrawing_Outlined )
    RoundBox(x1,y1,w1,h1,20,20,color)
    RoundBox(x2,y2,w2,h2,20,20,color)
    FillArea(x2-1,y2+h2/2,color,color)
  StopDrawing()  
  
  ImageGadget(2, 0, 0,640,640, ImageID(1))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
         Quit = 1
  EndSelect
Until Quit = 1
# 3: Circle

Code: Select all

OpenWindow(0, 0, 0, 640,640, "Image DC", #PB_Window_SystemMenu |#PB_Window_ScreenCentered)
  LoadImage(1, #PB_Compiler_Home + "Examples\Sources\Data\Background.bmp")
  ResizeImage(1,640,640)
  color = $0000FF
  cx = 320 : cy = 320: r = 200
  stroke = 40  
  x = cx - r + stroke/2
  y = cy 
  StartDrawing(ImageOutput(1))
    DrawingMode(#PB_2DDrawing_Outlined )
    Circle(cx,cy,r,color)
    Circle(cx,cy,r-stroke,color)
    FillArea(x,y,color,color)
  StopDrawing()  
  
  ImageGadget(2, 0, 0,640,640, ImageID(1))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
         Quit = 1
  EndSelect
Until Quit = 1
Feel the pure power with PureBasic
Have fun
Egypt my love
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: 2DDrawing Line,Box or Circle w any Thickness and Color

Post by mk-soft »

Very nice :wink:

but I calculate inner round

Bugfix

Code: Select all

OpenWindow(0, 0, 0, 640,640, "Image DC", #PB_Window_SystemMenu |#PB_Window_ScreenCentered)
  LoadImage(1, #PB_Compiler_Home + "Examples\Sources\Data\Background.bmp")
  ResizeImage(1,640,640)
  color = $0000FF  
  stroke = 10  
  x1 = 100 : y1 = 200 : w1 = 400 : h1 = 200
  x2 = x1 + stroke : y2 = y1 + stroke :w2 = w1 - 2*stroke : h2 = h1 - 2*stroke
  
  r1 = 20
  r2 = r1 - stroke
  If r1 < 0 : r1 = 0 : EndIf
  
  StartDrawing(ImageOutput(1))
    DrawingMode(#PB_2DDrawing_Outlined )
    RoundBox(x1,y1,w1,h1,r1,r1,color)
    RoundBox(x2,y2,w2,h2,r2,r2,color)
    FillArea(x2-1,y2+h2/2,color,color)
  StopDrawing()  
  
  ImageGadget(2, 0, 0,640,640, ImageID(1))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
         Quit = 1
  EndSelect
Until Quit = 1
Last edited by mk-soft on Fri Nov 26, 2021 12:30 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: 2DDrawing Line,Box or Circle w any Thickness and Color

Post by RASHAD »

Thanks mk-soft
I was thinking about it but I decided to watch a western movie to relax first :D
Egypt my love
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: 2DDrawing Line,Box or Circle w any Thickness and Color

Post by mestnyi »

mk-soft wrote: Thu Nov 25, 2021 7:11 pm Very nice :wink:

but I calculate inner round

Code: Select all

OpenWindow(0, 0, 0, 640,640, "Image DC", #PB_Window_SystemMenu |#PB_Window_ScreenCentered)
  LoadImage(1, #PB_Compiler_Home + "Examples\Sources\Data\Background.bmp")
  ResizeImage(1,640,640)
  color = $0000FF  
  stroke = 10  
  x1 = 100 : y1 = 200 : w1 = 400 : h1 = 200
  x2 = x1 + stroke : y2 = y1 + stroke :w2 = w1 - 2*stroke : h2 = h1 - 2*stroke
  
  r1 = 20
  r2 = 1.0 *r1*h2/h1/2
  
  StartDrawing(ImageOutput(1))
    DrawingMode(#PB_2DDrawing_Outlined )
    RoundBox(x1,y1,w1,h1,r1,r1,color)
    RoundBox(x2,y2,w2,h2,r2,r2,color)
    FillArea(x2-1,y2+h2/2,color,color)
  StopDrawing()  
  
  ImageGadget(2, 0, 0,640,640, ImageID(1))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
         Quit = 1
  EndSelect
Until Quit = 1
behaves better this way
r2 = r1-stroke ;1.0 *r1*h2/h1
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: 2DDrawing Line,Box or Circle w any Thickness and Color

Post by mk-soft »

@mestnyl

You are right, my code have a bug. Fixed.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply