09-15-2023, 03:00 PM
(This post was last modified: 09-15-2023, 03:00 PM by James D Jarvis.)
I'm getting an average 1.9766667 seconds on the 1st speed test in that program.
I realized I didn't have one just for lines so I did this:
I realized I didn't have one just for lines so I did this:
Code: (Select All)
'drawtriangle_lines_test_v0.3
' by James D. Jarvis , Sept 15,2023
'just a test of the line drawing speed
'
'HEADER
'$dynamic
Dim Shared xmax, ymax
Dim Shared line_endcap
Dim count As _Unsigned _Integer64
xmax = 800: ymax = 500
Screen _NewImage(xmax, ymax, 32)
Dim Shared pk& 'must be included in a program that uses polyFT
Dim Shared lk&
pk& = _NewImage(3, 3, 32) 'must be included in a program that uses polyFT
lk& = _NewImage(3, 3, 32)
'======================================
' demo
'======================================
' This demo draws 64000 random polygons, and then clears the screen and draws a handful of polygons rotating
Randomize Timer
_Title "Draw Lines Test v0.3 "
count = 0
Cls
Print "200 random lines with varied endcaps"
For n = 1 To 200
_Limit 30
line_endcap = Int(Rnd * 16)
lineFT Rnd * _Width, Rnd * _Height, Rnd * _Height, Rnd * _Width, Int(1 + Rnd * 8), _RGB32(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
Next n
_Delay 1
Print "press any key for the speed test"
Sleep
_KeyClear
_Title "Draw Lines Test v0.3 (Press ANY key to STOP)"
Cls
t1 = Timer
Do
line_endcap = Int(Rnd * 16)
lineFT Rnd * _Width, Rnd * _Height, Rnd * _Height, Rnd * _Width, Int(1 + Rnd * 8), _RGB32(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
count = count + 1
Loop Until InKey$ <> ""
t2 = Timer
Print count; " random lines in "; t2 - t1; " seconds."
_KeyClear
Print "press any key to see how fast it is when it isn't random."
Sleep
Cls
_KeyClear
use_endcap "arrow1"
rr = 100: gg = 100: bb = 100
count = 0
x1 = 50: x2 = 750
yy = 5
t3 = Timer
Do
lineFT x1, yy, x2, yy, 3, _RGB32(rr, gg, bb)
count = count + 1
yy = yy + 15
rr = rr + 1
If rr = 250 Then
rr = 0
gg = gg + 5
End If
If gg > 250 Then
bb = bb + 2
End If
If yy >= _Height Then yy = 5
Loop Until InKey$ <> ""
t4 = Timer
Print count; " calcualted lines in "; t4 - t3; " seconds."
'==========================================================================
'subroutines
'
' polyFT draw a filled polygon
' lineFT - draw a thick line constructed from 2 mapped triangles
' line_toangle - draw a line from x,y to a specific lenght along an anfle defiend in degrees
' aplotline - draw a line defined in a two dimensinal array of x and y coordinates
' dplotline - draw a line defined by a two diensional array of length and angle coordinates
' closeplot -draw an close a set of lines defined by a two dimensional array of lenght and ngles
'
' DegTo - return angle (in degrees) between two points , used as an internal function in lineFT
' setklr is an sub to build the color image used byt triangles in polyT
'====================================== ==================================
Sub setklr (klr As Long)
'internal routine to setup an image to copy a colored triangle from in the color klr
'called by polyT
od& = _Dest
_Dest pk&
Line (0, 0)-(2, 2), klr, BF
_Dest od&
End Sub
Sub lineFT (x1, y1, x2, y2, thk, klr As _Unsigned Long)
'draw a line of thickness thk on color klr from x1,y1 to x2,y2
'orientation of line is set in the middle of line thickness
od& = _Dest
_Dest lk&
Line (0, 0)-(2, 2), klr, BF 'set the color for the line
_Dest od&
cang = DegTo!(x1, y1, x2, y2) 'get the calcualted angle from x1,y1 to x2,y2
ta = cang + 90 'the anngle from center of line to botton edge
tb = ta + 180 'the angle from center of line to the top edge
tax1 = x1 + (thk \ 2) * Cos(0.01745329 * ta)
tay1 = y1 + (thk \ 2) * Sin(0.01745329 * ta)
tax4 = x1 + (thk \ 2) * Cos(0.01745329 * tb)
tay4 = y1 + (thk \ 2) * Sin(0.01745329 * tb)
tax2 = x2 + (thk \ 2) * Cos(0.01745329 * ta)
tay2 = y2 + (thk \ 2) * Sin(0.01745329 * ta)
tax3 = x2 + (thk \ 2) * Cos(0.01745329 * tb)
tay3 = y2 + (thk \ 2) * Sin(0.01745329 * tb)
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(tax1, tay1)-(tax2, tay2)-(tax4, tay4), od&
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(tax2, tay2)-(tax3, tay3)-(tax4, tay4), od&
Select EveryCase line_endcap
Case 1, 3 'round at start of segment
fcirc x1, y1, (thk \ 2) - .5, klr
Case 2, 3 'round at end of segment
fcirc x2, y2, (thk \ 2) - .5, klr
Case 4, 6 'bullet at start of segment
fcirc x1, y1, thk + 1, klr
Case 5, 6 'bullet at start of segment
fcirc x2, y2, thk + 1, klr
Case 7, 9 'square at start of segment
sx1 = x1 + (thk * 2) * Cos(0.01745329 * (cang + 45))
sx2 = x1 + (thk * 2) * Cos(0.01745329 * (cang + 135))
sx3 = x1 + (thk * 2) * Cos(0.01745329 * (cang + 225))
sx4 = x1 + (thk * 2) * Cos(0.01745329 * (cang + 315))
sy1 = y1 + (thk * 2) * Sin(0.01745329 * (cang + 45))
sy2 = y1 + (thk * 2) * Sin(0.01745329 * (cang + 135))
sy3 = y1 + (thk * 2) * Sin(0.01745329 * (cang + 225))
sy4 = y1 + (thk * 2) * Sin(0.01745329 * (cang + 315))
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(sx1, sy1)-(sx2, sy2)-(sx4, sy4), od&
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(sx2, sy2)-(sx3, sy3)-(sx4, sy4), od&
Case 8, 9 'square at end of segment
sx1 = x2 + (thk * 2) * Cos(0.01745329 * (cang + 45))
sx2 = x2 + (thk * 2) * Cos(0.01745329 * (cang + 135))
sx3 = x2 + (thk * 2) * Cos(0.01745329 * (cang + 225))
sx4 = x2 + (thk * 2) * Cos(0.01745329 * (cang + 315))
sy1 = y2 + (thk * 2) * Sin(0.01745329 * (cang + 45))
sy2 = y2 + (thk * 2) * Sin(0.01745329 * (cang + 135))
sy3 = y2 + (thk * 2) * Sin(0.01745329 * (cang + 225))
sy4 = y2 + (thk * 2) * Sin(0.01745329 * (cang + 315))
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(sx1, sy1)-(sx2, sy2)-(sx4, sy4), od&
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(sx2, sy2)-(sx3, sy3)-(sx4, sy4), od&
Case 10, 12 'draw arrow 1 at start of segment
ax1 = x1 + (thk * 3) * Cos(0.01745329 * (cang + 180))
ax2 = x1 + (thk * 2) * Cos(0.01745329 * (cang + 90))
ax3 = x1 + (thk * 2) * Cos(0.01745329 * (cang + 270))
ay1 = y1 + (thk * 3) * Sin(0.01745329 * (cang + 180))
ay2 = y1 + (thk * 2) * Sin(0.01745329 * (cang + 90))
ay3 = y1 + (thk * 2) * Sin(0.01745329 * (cang + 270))
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(ax1, ay1)-(ax2, ay2)-(ax3, ay3), od&
Case 11, 12 'draw arrow1 at end of segment
ax1 = x2 - (thk * 3) * Cos(0.01745329 * (cang + 180))
ax2 = x2 + (thk * 2) * Cos(0.01745329 * (cang + 90))
ax3 = x2 + (thk * 2) * Cos(0.01745329 * (cang + 270))
ay1 = y2 - (thk * 3) * Sin(0.01745329 * (cang + 180))
ay2 = y2 + (thk * 2) * Sin(0.01745329 * (cang + 90))
ay3 = y2 + (thk * 2) * Sin(0.01745329 * (cang + 270))
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(ax1, ay1)-(ax2, ay2)-(ax3, ay3), od&
Case 13, 15 'draw arrow2 at start of segment
ax1 = x1 + (thk * 3) * Cos(0.01745329 * (cang + 180))
ax2 = x1 + (thk * 3) * Cos(0.01745329 * (cang + 40))
ax3 = x1 + (thk * 3) * Cos(0.01745329 * (cang + 320))
ay1 = y1 + (thk * 3) * Sin(0.01745329 * (cang + 180))
ay2 = y1 + (thk * 3) * Sin(0.01745329 * (cang + 40))
ay3 = y1 + (thk * 3) * Sin(0.01745329 * (cang + 320))
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(ax1, ay1)-(ax2, ay2)-(x1, y1), od&
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(ax1, ay1)-(ax3, ay3)-(x1, y1), od&
Case 14, 15 'draw arrow2 at end of segment
ax1 = x2 - (thk * 3) * Cos(0.01745329 * (cang + 180))
ax2 = x2 - (thk * 3) * Cos(0.01745329 * (cang + 40))
ax3 = x2 - (thk * 3) * Cos(0.01745329 * (cang + 320))
ay1 = y2 - (thk * 3) * Sin(0.01745329 * (cang + 180))
ay2 = y2 - (thk * 3) * Sin(0.01745329 * (cang + 40))
ay3 = y2 - (thk * 3) * Sin(0.01745329 * (cang + 320))
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(ax1, ay1)-(ax2, ay2)-(x2, y2), od&
_MapTriangle (0, 0)-(0, 2)-(2, 0), lk& To(ax1, ay1)-(ax3, ay3)-(x2, y2), od&
End Select
End Sub
Sub line_toangle (x1, y1, lnth, thk, ang, klr As _Unsigned Long)
x2 = x1 + lnth * Cos(0.01745329 * ang)
y2 = y1 + lnth * Sin(0.01745329 * ang)
lineFT x1, y1, x2, y2, thk, klr
End Sub
Sub aplotline (aa(), scap$, ecap$, thk, klr As _Unsigned Long)
'plots an 2-d array of pre calculated points
lp = UBound(aa)
ocap = line_endcap 'get the current general line_endcap
For n = 0 To lp - 1
Select Case n
Case 0: use_endcap scap$
Case lp - 1: use_endcap ecap$
Case Else: use_endcap "round"
End Select
lineFT aa(n, 1), aa(n, 2), aa(n + 1, 1), aa(n + 1, 2), thk, klr
line_endcap = ocap 'reset the current general line_endcap
Next n
End Sub
Sub dplotline (aa(), thk, klr As _Unsigned Long)
'plots a line from starting point x1,yy1 built fro an array of lnths and angles
' ]the line starts at x1,y1 as deffined in aa(0,1) and aa(0,2)
lp = UBound(aa)
lastline = line_endcap
x1 = aa(0, 1)
y1 = aa(0, 2)
lasta = 0
For n = 1 To lp
x2 = x1 + aa(n, 1) * Cos(0.01745329 * (aa(n, 2) + lasta))
y2 = y1 + aa(n, 1) * Sin(0.01745329 * (aa(n, 2) + lasta))
lineFT x1, y1, x2, y2, thk, klr
x1 = x2
y1 = y2
lasta = lasta + aa(n, 2)
Next n
End Sub
Sub closeplot (aa(), thk, klr As _Unsigned Long)
'plots a line from starting point x1,yy1 built fro an array of lnths and angles
' ]the line starts at x1,y1 as deffined in aa(0,1) and aa(0,2)
lp = UBound(aa)
line_endcap = 3
x1 = aa(0, 1)
y1 = aa(0, 2)
lasta = 0
For n = 1 To lp
x2 = x1 + aa(n, 1) * Cos(0.01745329 * (aa(n, 2) + lasta))
y2 = y1 + aa(n, 1) * Sin(0.01745329 * (aa(n, 2) + lasta))
lineFT x1, y1, x2, y2, thk, klr
x1 = x2
y1 = y2
lasta = lasta + aa(n, 2)
Next n
lineFT aa(0, 1), aa(0, 2), x2, y2, thk, klr
End Sub
Sub use_endcap (ec$)
'tells the line routine which endcap type to use
Select Case LCase$(ec$)
Case "", "none": line_endcap = 0
Case "round", "rnd", "r": line_endcap = 3
Case "roundend", "rndend", "rnde", "re": line_endcap = 2
Case "roundstart", "rndstart", "rnds", "rs": line_endcap = 1
Case "bullet", "blt", "b": line_endcap = 6
Case "bulletend", "bltend", "blte", "be": line_endcap = 5
Case "bulletstart", "blystart", "blts", "bs": line_endcap = 4
Case "square", "sqr", "s": line_endcap = 9
Case "sqyareend", "squaree", "sqrend", "sqre", "se": line_endcap = 8
Case "squarestart", "squarestart", "squares", "sqrs", "ss": line_endcap = 7
Case "arrow1", "arw1", "a1": line_endcap = 12
Case "arrow1end", "arrow1end", "arw1e", "a1e": line_endcap = 11
Case "arrow1start", "arw1start", "arw1s", "a1s": line_endcap = 10
Case "arrow2", "arw2", "a2": line_endcap = 15
Case "arrow2end", "arrow2end", "arw2e", "a2e": line_endcap = 14
Case "arrow2start", "arw2start", "arw2s", "a2s": line_endcap = 13
Case Else
line_endcap = 0
End Select
End Sub
Sub fcirc (CX As Long, CY As Long, R, klr As _Unsigned Long)
'draw a filled circle with the quickest filled circle routine in qb64, not my development
Dim subRadius As Long, RadiusError As Long
Dim X As Long, Y As Long
subRadius = Abs(R)
RadiusError = -subRadius
X = subRadius
Y = 0
If subRadius = 0 Then PSet (CX, CY), klr: Exit Sub
Line (CX - X, CY)-(CX + X, CY), klr, BF
While X > Y
RadiusError = RadiusError + Y * 2 + 1
If RadiusError >= 0 Then
If X <> Y + 1 Then
Line (CX - Y, CY - X)-(CX + Y, CY - X), klr, BF
Line (CX - Y, CY + X)-(CX + Y, CY + X), klr, BF
End If
X = X - 1
RadiusError = RadiusError - X * 2
End If
Y = Y + 1
Line (CX - X, CY - Y)-(CX + X, CY - Y), klr, BF
Line (CX - X, CY + Y)-(CX + X, CY + Y), klr, BF
Wend
End Sub
Function DegTo! (x1, y1, x2, y2)
'========================
' returns an angle in degrees from point x1,y1 to point x2,y2
aa! = _Atan2((y2 - y1), (x2 - x1)) / 0.01745329
DegTo! = aa!
End Function