### Author Topic: SUB thickLine  (Read 3216 times)

0 Members and 1 Guest are viewing this topic.

#### FellippeHeitor

• Guest ##### SUB thickLine
« on: February 25, 2018, 05:56:05 pm »
This can be used to draw a line with a specified lineWidth%:

Code: QB64: [Select]
1. SUB thickLine (x1!, y1!, x2!, y2!, lineWidth%)
2.     DIM angle%, distance%, halfThickness%
3.     angle% = INT(_R2D(_ATAN2(y2! - y1!, x2! - x1!)))
4.     distance% = _HYPOT((x2! - x1!), (y2! - y1!))
5.     halfThickness% = lineWidth% / 2
6.     IF halfThickness% < 1 THEN halfThickness% = 1
7.     IF halfThickness% = 1 THEN
8.         DRAW "bm" + STR\$(x1!) + "," + STR\$(y1!) + " TA" + STR\$(-angle%) + " R" + STR\$(distance%)
9.         DRAW "bm" + STR\$(x1!) + "," + STR\$(y1!) + " TA" + STR\$(-angle%) + "U" + STR\$(halfThickness%) + " R" + STR\$(distance%) + "D" + STR\$(halfThickness% * 2) + " L" + STR\$(distance%) + "U" + STR\$(halfThickness%) + " B R" + STR\$(distance% / 2) + "P" + STR\$(_DEFAULTCOLOR) + "," + STR\$(_DEFAULTCOLOR)

Sample code using the method:
Code: QB64: [Select]
1. CONST false = 0, true = NOT false
2.
3. DIM x1, y1, x2, y2
4. DIM mouseDown AS _BYTE
5. DIM totalDots AS INTEGER
6. DIM thickness AS INTEGER
7.
8. thickness = 10
9.
10. SCREEN _NEWIMAGE(800, 600, 32)
11.
12.     WHILE _MOUSEINPUT: thickness = thickness + _MOUSEWHEEL: WEND
13.
14.     IF thickness < 1 THEN thickness = 1
15.
16.         IF NOT mouseDown THEN
17.             mouseDown = true
18.             totalDots = totalDots + 1
19.             IF totalDots > 2 THEN totalDots = 1
20.             SELECT CASE totalDots
21.                 CASE 1
22.                     x1 = _MOUSEX
23.                     y1 = _MOUSEY
24.                 CASE 2
25.                     x2 = _MOUSEX
26.                     y2 = _MOUSEY
27.         mouseDown = false
28.
29.     PRINT "Click to set the initial line coordinate,"
30.     PRINT "click again to set the final line coordinate."
31.     PRINT "Use the mousewheel to make the line thicker/thinner."
32.     PRINT "Current thickness:"; thickness
33.
34.     IF totalDots = 1 THEN
35.         PSET (x1, y1)
36.         thickLine x1, y1, x2, y2, thickness
37.     _LIMIT 30
38.
39. SUB thickLine (x1!, y1!, x2!, y2!, lineWidth%)
40.     DIM angle%, distance%, halfThickness%
41.     angle% = INT(_R2D(_ATAN2(y2! - y1!, x2! - x1!)))
42.     distance% = _HYPOT((x2! - x1!), (y2! - y1!))
43.     halfThickness% = lineWidth% / 2
44.     IF halfThickness% < 1 THEN halfThickness% = 1
45.     IF halfThickness% = 1 THEN
46.         DRAW "bm" + STR\$(x1!) + "," + STR\$(y1!) + " TA" + STR\$(-angle%) + " R" + STR\$(distance%)
47.         DRAW "bm" + STR\$(x1!) + "," + STR\$(y1!) + " TA" + STR\$(-angle%) + "U" + STR\$(halfThickness%) + " R" + STR\$(distance%) + "D" + STR\$(halfThickness% * 2) + " L" + STR\$(distance%) + "U" + STR\$(halfThickness%) + " B R" + STR\$(distance% / 2) + "P" + STR\$(_DEFAULTCOLOR) + "," + STR\$(_DEFAULTCOLOR)

#### FellippeHeitor

• Guest ##### Re: SUB thickLine
« Reply #1 on: February 25, 2018, 06:12:00 pm »
Same idea, different method. This version of the SUB uses _MAPTRIANGLE to achieve similar results. You can replace this in the sample above and it'll work as expected:

Code: QB64: [Select]
1. SUB thickLine (x1 AS SINGLE, y1 AS SINGLE, x2 AS SINGLE, y2 AS SINGLE, lineWeight%)
2.     DIM a AS SINGLE, x0 AS SINGLE, y0 AS SINGLE
3.     DIM prevDest AS LONG, prevColor AS _UNSIGNED LONG
4.     STATIC colorSample AS LONG
5.
6.     IF colorSample = 0 THEN
7.         colorSample = _NEWIMAGE(1, 1, 32)
8.
9.     prevDest = _DEST
10.     prevColor = _DEFAULTCOLOR
11.     _DEST colorSample
12.     PSET (0, 0), prevColor
13.     _DEST prevDest
14.
15.     a = _ATAN2(y2 - y1, x2 - x1)
16.     a = a + _PI / 2
17.     x0 = 0.5 * lineWeight% * COS(a)
18.     y0 = 0.5 * lineWeight% * SIN(a)
19.
20.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), colorSample TO(x1 - x0, y1 - y0)-(x1 + x0, y1 + y0)-(x2 + x0, y2 + y0), , _SMOOTH
21.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), colorSample TO(x1 - x0, y1 - y0)-(x2 + x0, y2 + y0)-(x2 - x0, y2 - y0), , _SMOOTH

This is the same method Ashish and I used for p5js.bas
« Last Edit: February 25, 2018, 06:21:16 pm by FellippeHeitor »

Marked as best answer by on Yesterday at 12:16:52 am

#### Pete ##### Re: SUB thickLine
« Reply #2 on: February 25, 2018, 06:50:40 pm »
How about underlined letters? SmallBASIC for Android has that, oooh and italics! I can't recall if this was ever added to QB64 or not. I  recall making something close to this for a graphics GUI that had http hypertext I wanted underlined. Hmmm, maybe I need to go dig that one up.

Atari BASIC actually had a very easy way to customize the 8x8 pixel grid of each ASCII character, so you could make your own underline characters very easily, although italics would be a lot more work involved.

Pete :)

PS The mouse wheel was a nice effect!
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

#### FellippeHeitor

• Guest ##### Re: SUB thickLine
« Reply #3 on: February 25, 2018, 07:05:19 pm »
PS The mouse wheel was a nice effect!

Thanks, Pete!