Page 1 of 1

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

Posted: Thu Nov 25, 2021 6:22 pm
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

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

Posted: Thu Nov 25, 2021 7:11 pm
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

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

Posted: Thu Nov 25, 2021 7:32 pm
by RASHAD
Thanks mk-soft
I was thinking about it but I decided to watch a western movie to relax first :D

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

Posted: Thu Nov 25, 2021 9:56 pm
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

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

Posted: Fri Nov 26, 2021 12:28 pm
by mk-soft
@mestnyl

You are right, my code have a bug. Fixed.