Author Topic: Line + Draw  (Read 6671 times)

0 Members and 1 Guest are viewing this topic.

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Line + Draw
« on: September 26, 2020, 07:29:47 pm »
Good night, Bplus
Me again here learning from you the instructions of Qbasic 64
This time I won't take your time too much.
I'm not used to code with graphics, I'm not familiar with graphics
The most I did was the boxes that I use in codes and that I want to use in the sequence code that you created for me.
Reading http://www.qb64.org/wiki/LINE I saw that the line is an excellent instruction for creating boxes.
The boxes that I create are with very obsolete instruction.
I managed to create a box with the line, but I wanted to make the lines a little thicker with the Draw statement.
As I mentioned, I have no knowledge of this instruction
I saw just one example of a slightly thick line that Draw created
Just below examples:
My obsolete boxes:

COLOR 11
LOCATE 1, 1: PRINT STRING$(80, CHR$(196)) 'box or frame
LOCATE 3, 1: PRINT STRING$(80, CHR$(196))
LOCATE 23, 1: PRINT STRING$(80, CHR$(196))
FOR R = 1 TO 23
    LOCATE R, 1: PRINT CHR$(179)
    LOCATE R, 80: PRINT CHR$(179)
NEXT R
LOCATE 3, 1: PRINT CHR$(195)
LOCATE 3, 80: PRINT CHR$(180)
LOCATE 1, 80: PRINT CHR$(191)
LOCATE 23, 80: PRINT CHR$(217)
LOCATE 23, 1: PRINT CHR$(192)
LOCATE 1, 1: PRINT CHR$(218)

Line instruction boxes: ‘Magnificent instruction
SCREEN 12
LINE (6, 5)-(632, 462), 11, B ' box or frame
'LINE (7, 6)-(631, 461), 11, B  'Slightly thicker lines
LINE (6, 5)-(632, 50), 11, B

Example I searched for at http://www.qb64.org/wiki/DRAW
SCREEN 12
PSET (20, 20), 12
DRAW "E2R30F2G2L30H2BR5P12,12" 'A little thick stroke

To summarize, I just want to make the box a little thicker.
I would like to know from you if there is this possibility.

Carlos

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Line + Draw
« Reply #1 on: September 26, 2020, 08:03:02 pm »
When I was creating my current avatar I made a very nifty sub:

Code: QB64: [Select]
  1. SUB drawFrame (x, y, w, h, th, k AS _UNSIGNED LONG)
  2.     LINE (x, y)-STEP(w, th), k, BF 'two horizontal
  3.     LINE (x, y + h - th)-STEP(w, th), k, BF
  4.     LINE (x, y + th + 1)-STEP(th, h - 2 * th - 2), k, BF 'try not to overlap boxes in case k is transparent
  5.     LINE (x + w - th, y + th + 1)-STEP(th, h - 2 * th - 2), k, BF

This creates a box top left corner is x, y.
Width and height is w, h
Thickness of lines (really they are skinny filled boxes) = th
k is color and may work in screen 12

The modern way to setup your screen is with:
SCREEN _NEWIMAGE(MyScreenWidth, MyScreenHeight, 32)  ' < the 32 is for 32 bit colors = _RGB32(red, green, blue)

It (the frame SUB) is exactly what you could use.

Demo:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. drawFrame 0, 0, 800, 600, 10, _RGB32(0, 0, 128)
  3. drawFrame 10, 10, 780, 50, 10, _RGB(255, 0, 0)
  4. drawFrame 10, 60, 780, 530, 10, _RGB32(200)
  5. drawFrame 10, 60, 260, 530, 10, _RGB32(255, 255, 0)
  6. drawFrame 270, 60, 260, 530, 10, _RGB32(0, 128, 0)
  7. drawFrame 530, 60, 260, 530, 10, _RGB32(240, 120, 0)
  8.  
  9. SUB drawFrame (x, y, w, h, th, k AS _UNSIGNED LONG)
  10.     LINE (x, y)-STEP(w, th), k, BF 'two horizontal
  11.     LINE (x, y + h - th)-STEP(w, th), k, BF
  12.     LINE (x, y + th + 1)-STEP(th, h - 2 * th - 2), k, BF 'try not to overlap boxes in case k is transparent
  13.     LINE (x + w - th, y + th + 1)-STEP(th, h - 2 * th - 2), k, BF
  14.  
  15.  
« Last Edit: September 26, 2020, 08:29:32 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Line + Draw
« Reply #2 on: September 26, 2020, 08:42:45 pm »
Here's another way to draw thick lines
Code: QB64: [Select]
  1. 'update 2020-01-24 to include PD2 inside the sub
  2. SUB thic (x1, y1, x2, y2, thick, K AS _UNSIGNED LONG)
  3.     DIM PD2 AS DOUBLE, t2 AS SINGLE, a AS SINGLE, x3 AS SINGLE, y3 AS SINGLE, x4 AS SINGLE, y4 AS SINGLE
  4.     DIM x5 AS SINGLE, y5 AS SINGLE, x6 AS SINGLE, y6 AS SINGLE
  5.     PD2 = 1.570796326794897
  6.     t2 = thick / 2
  7.     IF t2 < 1 THEN t2 = 1
  8.     a = _ATAN2(y2 - y1, x2 - x1)
  9.     x3 = x1 + t2 * COS(a + PD2)
  10.     y3 = y1 + t2 * SIN(a + PD2)
  11.     x4 = x1 + t2 * COS(a - PD2)
  12.     y4 = y1 + t2 * SIN(a - PD2)
  13.     x5 = x2 + t2 * COS(a + PD2)
  14.     y5 = y2 + t2 * SIN(a + PD2)
  15.     x6 = x2 + t2 * COS(a - PD2)
  16.     y6 = y2 + t2 * SIN(a - PD2)
  17.     ftri x6, y6, x4, y4, x3, y3, K
  18.     ftri x3, y3, x5, y5, x6, y6, K
  19.  
  20. '2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
  21. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  22.     DIM D AS LONG
  23.     STATIC a&
  24.     D = _DEST
  25.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  26.     _DEST a&
  27.     _DONTBLEND a& '  '<<<< new 2019-12-16 fix
  28.     PSET (0, 0), K
  29.     _BLEND a& '<<<< new 2019-12-16 fix
  30.     _DEST D
  31.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  32.  
  33.  
  34.  

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: Line + Draw
« Reply #3 on: September 26, 2020, 08:49:21 pm »
Bplus, it's like I said in the other posts, you make advanced codes. I wanted a code so I could choose the line thickness of the box. A very simple code.
It would be possible?
Carlos

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: Line + Draw
« Reply #4 on: September 26, 2020, 08:55:43 pm »
The latter does not appear the box on the screen
Box.jpg
* Box.jpg (Filesize: 161.03 KB, Dimensions: 1007x879, Views: 184)

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Line + Draw
« Reply #5 on: September 26, 2020, 09:19:23 pm »
You could run your line commands in a loop and just change its size to suit. Dead simple, unsophisticated, but if it works in your ap, go for it.

Code: QB64: [Select]
  1. thick = 5
  2. FOR x = 0 TO thick
  3.     LINE (6 + x, 5 + x)-(632 - x, 462 - x), 11, B ' box or frame
  4.     'LINE (7, 6)-(631, 461), 11, B  'Slightly thicker lines
  5.     LINE (6 + x, 5 + x)-(632 - x, 50 - x), 11, B

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: Line + Draw
« Reply #6 on: September 26, 2020, 09:28:35 pm »
Ok, great reasoning :)
I will do that.
thank you again

Carlos
« Last Edit: September 26, 2020, 09:34:02 pm by carloscordeiro »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Line + Draw
« Reply #7 on: September 26, 2020, 09:31:26 pm »
Here's a demo for thick

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. DEFINT A-Z
  3. 'try a box target in middle of screem
  4. FOR i = 1 TO 20
  5.     IF i MOD 3 = 1 THEN k = _RGB32(255)
  6.     IF i MOD 3 = 2 THEN k = _RGB32(200, 0, 0)
  7.     IF i MOD 3 = 0 THEN k = _RGB32(0, 0, 200)
  8.  
  9.     thic _WIDTH / 2 - i * 10 - 3, _HEIGHT / 2 - i * 10, _WIDTH / 2 + i * 10 + 3, _HEIGHT / 2 - i * 10, 6, k
  10.     thic _WIDTH / 2 - i * 10, _HEIGHT / 2 - i * 10, _WIDTH / 2 - i * 10, _HEIGHT / 2 + i * 10, 6, k
  11.     thic _WIDTH / 2 + i * 10, _HEIGHT / 2 - i * 10, _WIDTH / 2 + i * 10, _HEIGHT / 2 + i * 10, 6, k
  12.     thic _WIDTH / 2 - i * 10 - 3, _HEIGHT / 2 + i * 10, _WIDTH / 2 + i * 10 + 3, _HEIGHT / 2 + i * 10, 6, k
  13.  
  14.     _LIMIT 10
  15.  
  16.  
  17. 'update 2020-01-24 to include PD2 inside the sub
  18. SUB thic (x1, y1, x2, y2, thick, K AS _UNSIGNED LONG)
  19.     DIM PD2 AS DOUBLE, t2 AS SINGLE, a AS SINGLE, x3 AS SINGLE, y3 AS SINGLE, x4 AS SINGLE, y4 AS SINGLE
  20.     DIM x5 AS SINGLE, y5 AS SINGLE, x6 AS SINGLE, y6 AS SINGLE
  21.     PD2 = 1.570796326794897
  22.     t2 = thick / 2
  23.     IF t2 < 1 THEN t2 = 1
  24.     a = _ATAN2(y2 - y1, x2 - x1)
  25.     x3 = x1 + t2 * COS(a + PD2)
  26.     y3 = y1 + t2 * SIN(a + PD2)
  27.     x4 = x1 + t2 * COS(a - PD2)
  28.     y4 = y1 + t2 * SIN(a - PD2)
  29.     x5 = x2 + t2 * COS(a + PD2)
  30.     y5 = y2 + t2 * SIN(a + PD2)
  31.     x6 = x2 + t2 * COS(a - PD2)
  32.     y6 = y2 + t2 * SIN(a - PD2)
  33.     ftri x6, y6, x4, y4, x3, y3, K
  34.     ftri x3, y3, x5, y5, x6, y6, K
  35.  
  36. '2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
  37. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  38.     DIM D AS LONG
  39.     STATIC a&
  40.     D = _DEST
  41.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  42.     _DEST a&
  43.     _DONTBLEND a& '  '<<<< new 2019-12-16 fix
  44.     PSET (0, 0), K
  45.     _BLEND a& '<<<< new 2019-12-16 fix
  46.     _DEST D
  47.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  48.  
  49.  
  50.  

Here is how to draw a simple line:
Code: QB64: [Select]
  1. line (x1, y1)-(x2, y2), Kolor

Here is how to draw a simple thick line:
Code: QB64: [Select]
  1. thic x1, y1, x2, y2, thickness, Kolor

Can't get simpler, where ever you want a line just use thic

« Last Edit: September 26, 2020, 09:36:41 pm by bplus »

Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: Line + Draw
« Reply #8 on: September 26, 2020, 09:40:23 pm »
I wanted exactly that, but with few lines.
I will stay with your penultimate reasoning.

Code: QB64: [Select]
SCREEN 12
thick = 5
FOR x = 0 TO thick
    LINE (6 + x, 5 + x)-(632 - x, 462 - x), 11, B ' box or frame
    'LINE (7, 6)-(631, 461), 11, B  'Slightly thicker lines
    LINE (6 + x, 5 + x)-(632 - x, 50 - x), 11, B
NEXT x
Box.jpg
* Box.jpg (Filesize: 167.3 KB, Dimensions: 925x999, Views: 205)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Line + Draw
« Reply #9 on: September 26, 2020, 09:47:40 pm »
Here is simpler demo:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. DEFINT A-Z
  3.     FOR i = 1 TO 20
  4.         thic RND * _WIDTH, RND * _HEIGHT, RND * _WIDTH, RND * _HEIGHT, RND * 30, _RGB32(RND * 255)
  5.         _LIMIT 10
  6.     NEXT
  7.     _DELAY .5
  8.     CLS
  9.  
  10. 'update 2020-01-24 to include PD2 inside the sub
  11. SUB thic (x1, y1, x2, y2, thick, K AS _UNSIGNED LONG)
  12.     DIM PD2 AS DOUBLE, t2 AS SINGLE, a AS SINGLE, x3 AS SINGLE, y3 AS SINGLE, x4 AS SINGLE, y4 AS SINGLE
  13.     DIM x5 AS SINGLE, y5 AS SINGLE, x6 AS SINGLE, y6 AS SINGLE
  14.     PD2 = 1.570796326794897
  15.     t2 = thick / 2
  16.     IF t2 < 1 THEN t2 = 1
  17.     a = _ATAN2(y2 - y1, x2 - x1)
  18.     x3 = x1 + t2 * COS(a + PD2)
  19.     y3 = y1 + t2 * SIN(a + PD2)
  20.     x4 = x1 + t2 * COS(a - PD2)
  21.     y4 = y1 + t2 * SIN(a - PD2)
  22.     x5 = x2 + t2 * COS(a + PD2)
  23.     y5 = y2 + t2 * SIN(a + PD2)
  24.     x6 = x2 + t2 * COS(a - PD2)
  25.     y6 = y2 + t2 * SIN(a - PD2)
  26.     ftri x6, y6, x4, y4, x3, y3, K
  27.     ftri x3, y3, x5, y5, x6, y6, K
  28.  
  29. '2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
  30. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  31.     DIM D AS LONG
  32.     STATIC a&
  33.     D = _DEST
  34.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  35.     _DEST a&
  36.     _DONTBLEND a& '  '<<<< new 2019-12-16 fix
  37.     PSET (0, 0), K
  38.     _BLEND a& '<<<< new 2019-12-16 fix
  39.     _DEST D
  40.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  41.  

Just copy paste the 2 subs into your code and ignore them, all you need to know is

Code: QB64: [Select]
  1. thic x1, y1, x2, y2, thickness, Kolor

Draws a thick line with thickness of thickness and color of Kolor. It's one line of code to draw a thick line!




Offline carloscordeiro

  • Forum Regular
  • Posts: 102
    • View Profile
Re: Line + Draw
« Reply #10 on: September 26, 2020, 10:05:57 pm »
You are very funny today, this is what I like about human beings. Witty.

Thanks Bplus for your reasoning of the loop, I will keep it, as it is the simplest and with few lines.

Thanks
Carlos
Box.jpg
* Box.jpg (Filesize: 255.19 KB, Dimensions: 1161x997, Views: 241)

FellippeHeitor

  • Guest
Re: Line + Draw
« Reply #11 on: September 26, 2020, 11:03:13 pm »
Here's sub box. Pass it the x, y coordinates and the width, height of the box, then the thickness and color to draw.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. box 0, 0, 200, 130, 10, _RGB32(183, 133, 33)
  4.  
  5. SUB box (x, y, w, h, thickness, theColor AS _UNSIGNED LONG)
  6.     LINE (x, y)-STEP(w, h), theColor, BF
  7.     LINE (x + thickness, y + thickness)-STEP(w - thickness * 2, h - thickness * 2), _BACKGROUNDCOLOR, BF

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Line + Draw
« Reply #12 on: September 26, 2020, 11:04:18 pm »
You are very funny today, this is what I like about human beings. Witty.

Thanks Bplus for your reasoning of the loop, I will keep it, as it is the simplest and with few lines.

Thanks
Carlos

Well now hold on, Old Moses had a great idea too!

Here I added something to make it look like a frame to a picture!

Again just copy/paste the sub into the program, ignore how it works and enjoy the one line call

Code: QB64: [Select]
  1. framed2 x1, y1, x2, y2, thickness, kolor

x1, y1 has to top left corner of frame/box,  x2, y2 bottom right corner of frame or box, kolor best in mid range or more.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.     FOR i = 1 TO 10
  3.         x1 = RND * _WIDTH: y1 = RND * _HEIGHT: x2 = RND * _WIDTH: y2 = RND * _HEIGHT
  4.         IF x1 > x2 THEN SWAP x1, x2: SWAP y1, y2
  5.         IF y1 > y2 THEN SWAP y1, y2
  6.         k = _RGB32(RND * 200 + 55, RND * 255, RND * 255)
  7.         framed2 x1, y1, x2, y2, RND * 15 + 2, k
  8.         LOCATE 1, 1: PRINT SPACE$(50)
  9.         LOCATE 1, 1: PRINT x1, y1, x2, y2
  10.         SLEEP
  11.     NEXT
  12.     _DELAY .5
  13.     CLS
  14.  
  15. SUB framed2 (x1, y1, x2, y2, thick, kolor AS _UNSIGNED LONG)
  16.     red = _RED32(kolor)
  17.     green = _GREEN32(kolor)
  18.     blue = _BLUE32(kolor)
  19.     FOR i = 0 TO thick
  20.         LINE (x1 + i, y1 + i)-(x2 - i, y2 - i), _RGB32(red - 10 * i, green - 10 * i, blue - 10 * i), B
  21.     NEXT
  22.  

Framed2 demo.PNG
* Framed2 demo.PNG (Filesize: 10.57 KB, Dimensions: 803x607, Views: 202)
« Last Edit: September 26, 2020, 11:08:26 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Line + Draw
« Reply #13 on: September 26, 2020, 11:40:33 pm »
Yeah, I like Fellippe's width and height better as parameters x, y is top left corner and then width w to right and height h down:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.     COLOR , _RGB32(255 * RND, 255 * RND, 100 * RND + 155)
  3.     CLS
  4.     FOR i = 1 TO 10
  5.         x1 = RND * (_WIDTH - 200) + 100: y1 = RND * (_HEIGHT - 200) + 100: w = 200 * RND + 35: h = RND * 200 + 35
  6.         framed3 x1, y1, w, h, RND * 20 + 4, _RGB32(170 * RND + 80, 255 * RND, 255 * RND)
  7.         LOCATE 1, 1: PRINT SPACE$(50)
  8.         LOCATE 1, 1: PRINT x1, y1, w, h
  9.         SLEEP
  10.     NEXT
  11.     _DELAY .5
  12.     CLS
  13.  
  14. SUB framed3 (x, y, w, h, thick, kolor AS _UNSIGNED LONG)
  15.     red = _RED32(kolor)
  16.     green = _GREEN32(kolor)
  17.     blue = _BLUE32(kolor)
  18.     FOR i = 0 TO thick
  19.         LINE (x + i, y + i)-(x + w - i, y + h - i), _RGB32(red - 10 * i, green - 10 * i, blue - 10 * i), B
  20.     NEXT
  21.  

 
Framed3 demo.PNG


Again to use copy/paste sub into program and call with:
Code: QB64: [Select]
  1. framed3 x, y, w, h, thick, Kolor
« Last Edit: September 26, 2020, 11:42:48 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Line + Draw
« Reply #14 on: September 27, 2020, 02:36:14 pm »
Another 3D version of frame probably best yet:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.     COLOR , _RGB32(128 + RND * 128 - 64)
  3.     CLS
  4.     FOR i = 1 TO 10
  5.         x1 = RND * (_WIDTH - 200) + 100: y1 = RND * (_HEIGHT - 200) + 100: w = 200 * RND + 35: h = RND * 200 + 35
  6.         framed4 x1, y1, w, h, RND * 10 + 4, _RGB32(128 + RND * 128 - 64, 128 + RND * 128 - 64, 128 + RND * 128 - 64)
  7.         LOCATE 1, 1: PRINT SPACE$(50)
  8.         LOCATE 1, 1: PRINT x1, y1, w, h
  9.         SLEEP
  10.     NEXT
  11.     _DELAY .5
  12.     CLS
  13.  
  14. SUB framed4 (x, y, w, h, thick, kolor AS _UNSIGNED LONG)
  15.     red = _RED32(kolor)
  16.     green = _GREEN32(kolor)
  17.     blue = _BLUE32(kolor)
  18.     DIM saveC AS _UNSIGNED LONG
  19.     FOR i = 0 TO thick * .5
  20.         red = red + 15: green = green + 15: blue = blue + 15
  21.         LINE (x + i, y + i)-(x + w - i, y + h - i), _RGB32(red, green, blue), B
  22.     NEXT
  23.     FOR i = thick * .5 TO thick
  24.         red = red - 15: green = green - 15: blue = blue - 15
  25.         LINE (x + i, y + i)-(x + w - i, y + h - i), _RGB32(red, green, blue), B
  26.     NEXT
  27.  

 
frames4 demo.PNG