Attribute VB_Name = "ModAdders"
' ModAdders.bas

Option Explicit

Public AdderIndex As Integer

Public Enum EADDER
   [SStreaks] = 0
   [CCircles]
   [DDiagonal]
   [XEllipses]
   [BarsH]
   [BarsV]
   [BarsHV]
   [PPepper]
   [SShadeH]
   [SShadeV]
   [SShadeHV]
   [SSpokes]
   [TThicklinesH]
   [TThicklinesV]
   [TThicklinesHV]
   [NNet]
   [HHexagons] = 16
   [SSquares] = 17
   [WWavesH]
   [WWavesV]
   [WWavesHV]
   [TTriangles] = 21
   [JigSaw] = 22
   [ABlindsH]
   [ABlindsV]
   [ABorder] = 25
   [LinesH]
   [LinesV]
End Enum

' Public aUseRightColor As Boolean
' Public BresLine for drawing lines into an array in ModMaths

Private ix As Long, iy As Long
Private ixx As Long, iyy As Long   ' Mask coords for Public MaskArray()

Private r As Long, g As Long, B As Long

Private picData2() As Byte

' For JigSaw

Public XJ() As Single, YJ()  As Single   ' Offset lug pattern
Public XLugCen As Single, YLugCen As Single
Public LugRadius As Single
Public ncols As Long, nrows As Long
Public XLine() As Single, YLine() As Single
Public XLine2() As Single, YLine2() As Single

Private zAng As Single
Private dx As Single, DY As Single
Private Mul As Single
Private kx() As Long, ky() As Long
Private mx() As Single, cx() As Single
Private my() As Single, cy() As Single
' y = m.x + c
Private Tension As Single

'<<<< Progress bar >>>>>>>>>>
Private xd As Single, xp As Single


Public Sub BlindsHorz(Hit As Long)
' HIT = blind size
'Private R As Long, G As Long, b As Long
Dim Offset As Long
   picDATA() = picDATAORG()
   Offset = 0
   StartProgress (HIY)
   If aSelectAccepted Then
      For iy = LOY To HIY
      iyy = iy - LOY
      For ix = LOX To HIX
         ixx = ix - LOX
         If MaskArray(ixx, iyy) = 0 Then BlindsHorzCALC Hit, Offset
      Next ix
      Offset = Offset + 1
      If Offset > Hit Then Offset = 0
      DrawProgress
      Next iy
   Else
      For iy = LOY To HIY
      For ix = LOX To HIX
         BlindsHorzCALC Hit, Offset
      Next ix
      Offset = Offset + 1
      If Offset > Hit Then Offset = 0
      DrawProgress
      Next iy
   End If
   
   frmExtras.picPB.Cls
End Sub

Private Sub BlindsHorzCALC(Hit As Long, Offset As Long)
      B = picDATAORG(0, ix, iy)
      g = picDATAORG(1, ix, iy)
      r = picDATAORG(2, ix, iy)
      B = B - (Offset * B) / Hit
      g = g - (Offset * g) / Hit
      r = r - (Offset * r) / Hit
      If B < 0 Then B = 255 '0
      If g < 0 Then g = 255 '0
      If r < 0 Then r = 255 '0
      picDATA(0, ix, iy) = B
      picDATA(1, ix, iy) = g
      picDATA(2, ix, iy) = r
End Sub

Public Sub BlindsVert(Hit As Long)
' HIT = blind size
'Private R As Long, G As Long, b As Long
Dim Offset As Long
   picDATA() = picDATAORG()
   Offset = 0
   StartProgress (HIY)
   If aSelectAccepted Then
      For ix = LOX To HIX
      ixx = ix - LOX
      For iy = LOY To HIY
         iyy = iy - LOY
         If MaskArray(ixx, iyy) = 0 Then BlindsVertCALC Hit, Offset
      Next iy
      Offset = Offset + 1
      If Offset = Hit Then Offset = 0
      DrawProgress
      Next ix
   Else
      For ix = LOX To HIX
      For iy = LOY To HIY
         BlindsVertCALC Hit, Offset
      Next iy
      Offset = Offset + 1
      If Offset = Hit Then Offset = 0
      DrawProgress
      Next ix
   End If
   frmExtras.picPB.Cls
End Sub

Private Sub BlindsVertCALC(Hit As Long, Offset As Long)
      B = picDATAORG(0, ix, iy)
      g = picDATAORG(1, ix, iy)
      r = picDATAORG(2, ix, iy)
      B = B - (Offset * B) / Hit
      g = g - (Offset * g) / Hit
      r = r - (Offset * r) / Hit
      If B < 0 Then B = 0
      If g < 0 Then g = 0
      If r < 0 Then r = 0
      picDATA(0, ix, iy) = B
      picDATA(1, ix, iy) = g
      picDATA(2, ix, iy) = r
End Sub

Public Sub Border(zPTHICKLINE As Single)
' zPTHICKLINE  .0 -> 0.5
Dim k As Long
Dim CH As Long, CW As Long
Dim TY As Single, TX As Single
Dim LR As Byte, LG As Byte, LB As Byte
Dim RR As Byte, RG As Byte, RB As Byte
Dim zlo As Single, zhi As Single
Dim zYSteps As Single
Dim zXSteps As Single
Dim ziy As Single, zix As Single
   picDATA() = picDATAORG()
   If zPTHICKLINE = 0 Then Exit Sub
   
   LngToRGB LeftColor, LR, LG, LB
   LngToRGB RightColor, RR, RG, RB
   
   CH = HIY - LOY
   CW = HIX - LOX
   TY = zPTHICKLINE * CH
   If TY < 1 Then TY = 1
   TX = zPTHICKLINE * CW

   If TX < 1 Then TX = 1
   
   If FullW > FullH Then
      NSteps = FullW \ 2 ' Arbitrary steps
   Else
      NSteps = FullH \ 2
   End If
   
   ' Bottom
   zlo = LOY
   zhi = LOY - 1 + TY
   zYSteps = Abs(zhi - zlo) / NSteps
   If zYSteps = 0 Then zYSteps = 0.01
   GetColorSteps
   For ziy = zlo To zhi Step zYSteps
      If ziy > LOY - 1 Then
      If ziy <= HIY Then
         If aSelectAccepted Then
               iyy = ziy - LOY
               For ix = LOX To HIX
                  ixx = ix - LOX
                  If MaskArray(ixx, iyy) = 0 Then
                     picDATA(0, ix, ziy) = zBL
                     picDATA(1, ix, ziy) = zGL
                     picDATA(2, ix, ziy) = zRL
                  End If
               Next ix
         Else
               For ix = LOX To HIX
                  picDATA(0, ix, ziy) = zBL
                  picDATA(1, ix, ziy) = zGL
                  picDATA(2, ix, ziy) = zRL
               Next ix
         End If
         GetNextColors
      End If
      End If
   Next ziy
   
   ' Top
   zlo = HIY - TY + 1
   zhi = HIY
   GetColorStepsReversed
   For ziy = zlo To zhi Step zYSteps
      If ziy > LOY - 1 Then
      If ziy <= HIY Then
         If aSelectAccepted Then
            iyy = ziy - LOY
            For ix = LOX To HIX
               ixx = ix - LOX
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, ix, ziy) = zBL
                  picDATA(1, ix, ziy) = zGL
                  picDATA(2, ix, ziy) = zRL
               End If
            Next ix
         Else
            For ix = LOX To HIX
               picDATA(0, ix, ziy) = zBL
               picDATA(1, ix, ziy) = zGL
               picDATA(2, ix, ziy) = zRL
            Next ix
         End If
         GetNextColors
      End If
      End If
   Next ziy

   ' Left
   zlo = LOX
   zhi = LOX - 1 + TX
   zXSteps = Abs(zhi - zlo) / NSteps
   If zXSteps = 0 Then zXSteps = 0.01
   GetColorSteps
   k = 0
   For zix = zlo To zhi Step zXSteps
      If zix > LOX - 1 Then
      If zix <= HIX Then
         If aSelectAccepted Then
            ixx = zix - LOX
            For iy = LOY + k * zYSteps To LOY - 1 + CH - k * zYSteps
               iyy = iy - LOY
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, zix, iy) = zBL
                  picDATA(1, zix, iy) = zGL
                  picDATA(2, zix, iy) = zRL
               End If
            Next iy
         Else
            For iy = LOY + k * zYSteps To LOY - 1 + CH - k * zYSteps
               picDATA(0, zix, iy) = zBL
               picDATA(1, zix, iy) = zGL
               picDATA(2, zix, iy) = zRL
            Next iy
         End If
         k = k + 1
         GetNextColors
      End If
      End If
   Next zix

   ' Right
   zlo = HIX - TX + 1
   zhi = HIX
   GetColorSteps
   k = 0
   StartProgress (zhi)
   For zix = zhi To zlo Step -zXSteps
      If zix > LOX - 1 Then
      If zix <= HIX Then
         If aSelectAccepted Then
            ixx = zix - LOX
            For iy = LOY + k * zYSteps To HIY - k * zYSteps
               iyy = iy - LOY
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, zix, iy) = zBL
                  picDATA(1, zix, iy) = zGL
                  picDATA(2, zix, iy) = zRL
               End If
            Next iy
         Else
            For iy = LOY + k * zYSteps To HIY - k * zYSteps
               picDATA(0, zix, iy) = zBL
               picDATA(1, zix, iy) = zGL
               picDATA(2, zix, iy) = zRL
            Next iy
         End If
         k = k + 1
         GetNextColors
      End If
      End If
      DrawProgress
   Next zix
   frmExtras.picPB.Cls
End Sub


Public Sub AddCircles(zPLENS As Single)
' zPLENS 0.5 -> 10
Dim zA As Single
Dim zD As Single
Dim ixv As Long
Dim iyv As Long
Dim zRM As Single
Dim CH As Long, CW As Long
Dim zix As Single, ziy As Single

Dim RR As Byte, GG As Byte, BB As Byte
   
   picDATA() = picDATAORG()
   
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   
   zD = Sqr(CW * CW + CH * CH)
   zRM = zD / (16 * zPLENS)
   StartProgress ((HIY + zRM - LOY) / (2 * zRM)) ' ??
   For ziy = LOY To HIY + zRM Step 2 * zRM
   For zix = LOX To HIX + zRM Step 2 * zRM
      If aSelectAccepted Then
         For zA = 0 To 2 * pi# Step 0.01
            ixv = zix + zRM * Cos(zA)
            iyv = ziy + zRM * Sin(zA)
            If ixv > LOX - 1 Then
            If ixv <= HIX Then
            If iyv > LOY - 1 Then
            If iyv <= HIY Then
               ixx = ixv - LOX
               iyy = iyv - LOY
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, ixv, iyv) = BB
                  picDATA(1, ixv, iyv) = GG
                  picDATA(2, ixv, iyv) = RR
               End If
            End If
            End If
            End If
            End If
         Next zA
      Else
         For zA = 0 To 2 * pi# Step 0.01
            ixv = zix + zRM * Cos(zA)
            iyv = ziy + zRM * Sin(zA)
            If ixv > LOX - 1 Then
            If ixv <= HIX Then
            If iyv > LOY - 1 Then
            If iyv <= HIY Then
               picDATA(0, ixv, iyv) = BB
               picDATA(1, ixv, iyv) = GG
               picDATA(2, ixv, iyv) = RR
            End If
            End If
            End If
            End If
         Next zA
      End If
   Next zix
   DrawProgress
   Next ziy
   frmExtras.picPB.Cls
End Sub

Public Sub AddDiagNet(PDNET As Long)
' PDNET  1 -> 200
Dim n As Long
Dim zix As Single, ziy As Single
Dim sstep As Single

Dim RR As Byte, GG As Byte, BB As Byte
Dim CH As Long, CW As Long
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   n = 0
   ' / TL
   zix = HIX
   sstep = (CH / PDNET)
   If sstep = 0 Then sstep = 0.1
   For ziy = LOY To HIY Step sstep
      ' BresLine ix1,iy1,ix2,iy2,R,G,B,ND
      
      If aSelectAccepted Then
         BresLineCALC LOX, CLng(ziy), CLng(zix), HIY, RR, GG, BB
      Else
         BresLine LOX, CLng(ziy), CLng(zix), HIY, RR, GG, BB
      End If
      n = n + 1
      zix = HIX - n * (CW / PDNET)
   Next ziy
   ' / BR
   n = 0
   ziy = HIY
   For zix = LOX To HIX Step (CW / PDNET)
      If aSelectAccepted Then
         BresLineCALC CLng(zix), LOY, HIX, ziy, RR, GG, BB
      Else
         BresLine CLng(zix), LOY, HIX, ziy, RR, GG, BB
      End If
      n = n + 1
      ziy = HIY - n * (CH / PDNET)
   Next zix
   ' \ BL
   n = 0
   zix = LOX
   For ziy = LOY To HIY Step (CH / PDNET)
      If aSelectAccepted Then
         BresLineCALC LOX, CLng(ziy), CLng(zix), LOY, RR, GG, BB
      Else
         BresLine LOX, CLng(ziy), CLng(zix), LOY, RR, GG, BB
      End If
      n = n + 1
      zix = LOX + n * (CW / PDNET)
   Next ziy
   ' \ TR
   n = 0
   ziy = LOY
   StartProgress ((HIX - LOX) / (CW / PDNET)) ' ??
   For zix = LOX To HIX Step (CW / PDNET)
      If aSelectAccepted Then
         BresLineCALC CLng(zix), HIY, HIX, ziy, RR, GG, BB
      Else
         BresLine CLng(zix), HIY, HIX, ziy, RR, GG, BB
      End If
      n = n + 1
      ziy = LOY + n * (CH / PDNET)
   DrawProgress
   Next zix
   frmExtras.picPB.Cls
End Sub


Public Sub AddEllipses(zPLENS As Single)
' zPLENS  0.5 -> 4
Dim zA As Single
Dim zD As Single
Dim ixv As Long
Dim iyv As Long
Dim zRM As Single
Dim zix As Single, ziy As Single
Dim RR As Byte, GG As Byte, BB As Byte
Dim CH As Long, CW As Long
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   
   zD = Sqr(CW * CW + CH * CH)
   zRM = zD / (8 * zPLENS)
   StartProgress (HIY + zRM - LOY) / (zRM) ' ??
   For ziy = LOY To HIY + zRM Step zRM
   For zix = LOX To HIX + zRM Step 2 * zRM
      If aSelectAccepted Then
         For zA = 0 To 2 * pi# Step 0.01
            ixv = zix + zRM * Cos(zA)
            iyv = ziy + zRM * Sin(zA) / 2
            If ixv > LOX - 1 Then
            If ixv <= HIX Then
            If iyv > LOY - 1 Then
            If iyv <= HIY Then
               ixx = ixv - LOX
               iyy = iyv - LOY
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, ixv, iyv) = BB
                  picDATA(1, ixv, iyv) = GG
                  picDATA(2, ixv, iyv) = RR
               End If
            End If
            End If
            End If
            End If
         Next zA
      Else
         For zA = 0 To 2 * pi# Step 0.01
            ixv = zix + zRM * Cos(zA)
            iyv = ziy + zRM * Sin(zA) / 2
            If ixv > LOX - 1 Then
            If ixv <= HIX Then
            If iyv > LOY - 1 Then
            If iyv <= HIY Then
                picDATA(0, ixv, iyv) = BB
                picDATA(1, ixv, iyv) = GG
                picDATA(2, ixv, iyv) = RR
            End If
            End If
            End If
            End If
         Next zA
      End If
   Next zix
   DrawProgress
   Next ziy
   frmExtras.picPB.Cls
End Sub

Public Sub AddHBars(PLINES As Long)
' PLINES  1 -> 40
Dim iiy As Long
Dim CH As Long, CW As Long
Dim sstep As Single
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   sstep = (CH / PLINES)
   If sstep < 1 Then sstep = 1
   StartProgress (HIY - LOY) / sstep
   NSteps = 10
   GetColorSteps
   For iy = LOY To HIY Step sstep
      For ix = LOX To HIX
         ixx = ix - LOX
         GetColorSteps
         If aSelectAccepted Then
            For iiy = iy To iy + 9
               If iiy <= HIY Then
               iyy = iiy - LOY
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, ix, iiy) = zBL
                  picDATA(1, ix, iiy) = zGL
                  picDATA(2, ix, iiy) = zRL
                  GetNextColors
               End If
               End If
            Next iiy
         Else
            For iiy = iy To iy + 9
               If iiy <= HIY Then
                  picDATA(0, ix, iiy) = zBL
                  picDATA(1, ix, iiy) = zGL
                  picDATA(2, ix, iiy) = zRL
                  GetNextColors
               End If
            Next iiy
         End If
      Next ix
      DrawProgress
   Next iy
   
   frmExtras.picPB.Cls
End Sub

Public Sub AddVBars(PLINES As Long)
' PLINES 1 -> 40
Dim iix As Long
Dim CH As Long, CW As Long
Dim sstep As Single
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   NSteps = 10
   GetColorSteps
   sstep = (CW / PLINES)
   If sstep < 1 Then sstep = 1
   StartProgress (HIX - LOX) / sstep
   For ix = LOX To HIX Step sstep
      For iy = LOY To HIY
         iyy = iy - LOY
         GetColorSteps
         If aSelectAccepted Then
            For iix = ix To ix + 9
               If iix <= HIX Then
               ixx = iix - LOX
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, iix, iy) = zBL
                  picDATA(1, iix, iy) = zGL
                  picDATA(2, iix, iy) = zRL
                  GetNextColors
               End If
               End If
            Next iix
         Else
            For iix = ix To ix + 9
               If iix <= HIX Then
                  picDATA(0, iix, iy) = zBL
                  picDATA(1, iix, iy) = zGL
                  picDATA(2, iix, iy) = zRL
                  GetNextColors
               End If
            Next iix
         End If
      Next iy
      DrawProgress
   Next ix
   frmExtras.picPB.Cls
End Sub

Public Sub AddHVBars(PLINES As Long)
' PLINES 1 -> 40
   ReDim picData2(0 To 3, 0 To FullW - 1, 0 To FullH - 1)
   picData2() = picDATAORG()
   AddHBars PLINES
   picDATAORG() = picDATA()
   AddVBars PLINES
   picDATAORG() = picData2()
End Sub

Public Sub PepperEm(PAR As Long)
' Par 1 to 4
Dim RR As Byte, RG As Byte, RB As Byte
Dim n As Long
   If aUseRightColor Then
      LngToRGB RightColor, RR, RG, RB
   Else
      LngToRGB LeftColor, RR, RG, RB
   End If
   picDATA() = picDATAORG()
   ReDim picData2(0 To 3, 0 To FullW - 1, 0 To FullH - 1)
   picData2() = picDATAORG()
   
   StartProgress (PAR * HIY - 1)
   For n = 1 To PAR
      picData2() = picDATA()
      For iy = LOY To HIY
      For ix = LOX To HIX
         If aSelectAccepted Then
            ixx = ix - LOX
            iyy = iy - LOY
            If MaskArray(ixx, iyy) = 0 Then
               If (Rnd - 0.01) < 0 Then
                  picData2(0, ix, iy) = RB
                  picData2(1, ix, iy) = RG
                  picData2(2, ix, iy) = RR
               End If
            End If
         Else
            If (Rnd - 0.01) < 0 Then
               picData2(0, ix, iy) = RB
               picData2(1, ix, iy) = RG
               picData2(2, ix, iy) = RR
            End If
         End If
      Next ix
      DrawProgress
      Next iy
      picDATA() = picData2()
      frmExtras.Refresh
  Next n
  picDATA() = picData2()
  frmExtras.picPB.Cls
End Sub

Public Sub ShadeH(PSHADE As Long)
' PSHADE  -100 -> +100
Dim zA As Single ', zB As Single
   picDATA() = picDATAORG()
   zA = PSHADE
   StartProgress (HIX)
   For ix = LOX To HIX
   For iy = LOY To HIY
      If aSelectAccepted Then
         iyy = iy - LOY
         ixx = ix - LOX
         If MaskArray(ixx, iyy) = 0 Then ShadeHCALC zA
      Else
         ShadeHCALC zA
      End If
   Next iy
   DrawProgress
   Next ix
   frmExtras.picPB.Cls
End Sub

Private Sub ShadeHCALC(zA As Single)
Dim zB As Single
      zB = 256 * Abs(1 - 2 * (iy - LOY) / (HIY - LOY + 1))
      B = picDATAORG(0, ix, iy)
      g = picDATAORG(1, ix, iy)
      r = picDATAORG(2, ix, iy)
      B = zA * (zB - B) \ 256 + B
      g = zA * (zB - g) \ 256 + g
      r = zA * (zB - r) \ 256 + r
      If B < 0 Then B = 0
      If B > 255 Then B = 255
      If g < 0 Then g = 0
      If g > 255 Then g = 255
      If r < 0 Then r = 0
      If r > 255 Then r = 255
      picDATA(0, ix, iy) = B
      picDATA(1, ix, iy) = g
      picDATA(2, ix, iy) = r
End Sub

Public Sub ShadeV(PSHADE As Long)
' PSHADE -200 -> +200
Dim zA As Single ', zB As Single
   picDATA() = picDATAORG()
   zA = PSHADE
   StartProgress (HIY)
   For iy = LOY To HIY
   For ix = LOX To HIX
      If aSelectAccepted Then
         iyy = iy - LOY
         ixx = ix - LOX
         If MaskArray(ixx, iyy) = 0 Then ShadeVCALC zA
      Else
         ShadeVCALC zA
      End If
   Next ix
   DrawProgress
   Next iy
   frmExtras.picPB.Cls
End Sub

Public Sub ShadeVCALC(zA As Single)
Dim zB As Single
      zB = 256 * Abs(1 - 2 * (ix - LOX) / (HIX - LOX + 1))
      B = picDATAORG(0, ix, iy)
      g = picDATAORG(1, ix, iy)
      r = picDATAORG(2, ix, iy)
      B = zA * (zB - B) \ 256 + B
      g = zA * (zB - g) \ 256 + g
      r = zA * (zB - r) \ 256 + r
      If B < 0 Then B = 0
      If B > 255 Then B = 255
      If g < 0 Then g = 0
      If g > 255 Then g = 255
      If r < 0 Then r = 0
      If r > 255 Then r = 255
      picDATA(0, ix, iy) = B
      picDATA(1, ix, iy) = g
      picDATA(2, ix, iy) = r
End Sub

Public Sub ShadeHV(PSHADE As Long)
' PSHADE  -200 -> +200
Dim zA As Single ', zB As Single
   picDATA() = picDATAORG()
   zA = PSHADE
   ' Shade Horz
   StartProgress (HIX)
   For ix = LOX To HIX 'Step 2
   For iy = LOY To HIY 'Step 2
      If aSelectAccepted Then
         iyy = iy - LOY
         ixx = ix - LOX
         If MaskArray(ixx, iyy) = 0 Then ShadeHVCALC zA
      Else
         ShadeHVCALC zA
      End If
   Next iy
   DrawProgress
   Next ix
   frmExtras.picPB.Cls
End Sub

Private Sub ShadeHVCALC(zA As Single)
Dim zB As Single
      B = picDATAORG(0, ix, iy)
      g = picDATAORG(1, ix, iy)
      r = picDATAORG(2, ix, iy)

      zB = 256 * Abs(1 - 2 * (ix - LOX) / (HIX - LOX + 1))
      B = zA * (zB - B) \ 256 + B
      g = zA * (zB - g) \ 256 + g
      r = zA * (zB - r) \ 256 + r

      zB = 256 * Abs(1 - 2 * (iy - LOY) / (HIY - LOY + 1))
      B = zA * (zB - B) \ 256 + B
      g = zA * (zB - g) \ 256 + g
      r = zA * (zB - r) \ 256 + r

      If B < 0 Then B = 0
      If B > 255 Then B = 255
      If g < 0 Then g = 0
      If g > 255 Then g = 255
      If r < 0 Then r = 0
      If r > 255 Then r = 255
      picDATA(0, ix, iy) = B
      picDATA(1, ix, iy) = g
      picDATA(2, ix, iy) = r
End Sub

Public Sub AddSpokes(PSPOKES As Long)
' PSPOKES 1 -> 400
Dim zradi As Single
Dim zphi As Single
Dim xc As Single, yc As Single
Dim zRMax As Single
Dim RR As Byte, GG As Byte, BB As Byte
Dim CH As Long, CW As Long
Dim ASP As Single
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   xc = CW / 2: yc = CH / 2
   zRMax = Abs(CW - xc)
   If Abs(CH - yc) > Abs(CW - xc) Then
      zRMax = Abs(CH - yc)
   End If
   xc = LOX - 1 + CW / 2: yc = LOY - 1 + CH / 2
   ASP = CW / CH
   If CW < CH Then ASP = CH \ CW
   StartProgress (zRMax)
   For zradi = 1 To 2 * zRMax
   For zphi = 0 To 360 Step 360 / (ASP * PSPOKES)
      ix = xc + zradi * Sin(zphi * d2r#)
      iy = yc + zradi * Cos(zphi * d2r#)
      If ix > LOX - 1 Then
      If ix <= HIX Then
      If iy > LOY - 1 Then
      If iy <= HIY Then
         If aSelectAccepted Then
            ixx = ix - LOX
            iyy = iy - LOY
            If MaskArray(ixx, iyy) = 0 Then
               picDATA(0, ix, iy) = BB
               picDATA(1, ix, iy) = GG
               picDATA(2, ix, iy) = RR
            End If
         Else
            picDATA(0, ix, iy) = BB
            picDATA(1, ix, iy) = GG
            picDATA(2, ix, iy) = RR
         End If
      End If
      End If
      End If
      End If
   Next zphi
   DrawProgress
   Next zradi
   frmExtras.picPB.Cls
End Sub

Public Sub AddThickLineH(zPTHICKLINE As Single)
' zPTHICKLINE  .0 -> 1.0
Dim TY As Single
Dim RR As Byte, GG As Byte, BB As Byte
Dim CH As Long, CW As Long
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   
   TY = zPTHICKLINE * CH
   If TY < 1 Then TY = 1
   StartProgress (LOY - 1 + CH / 2 + TY / 2)
   For iy = LOY - 1 + CH / 2 - TY / 2 To LOY - 1 + CH / 2 + TY / 2
   If iy > LOY - 1 Then
   If iy <= HIY Then
      For ix = LOX To HIX
         If aSelectAccepted Then
            iyy = iy - LOY
            ixx = ix - LOX
            If MaskArray(ixx, iyy) = 0 Then
               picDATA(0, ix, iy) = BB
               picDATA(1, ix, iy) = GG
               picDATA(2, ix, iy) = RR
            End If
         Else
            picDATA(0, ix, iy) = BB
            picDATA(1, ix, iy) = GG
            picDATA(2, ix, iy) = RR
         End If
      Next ix
   End If
   End If
   DrawProgress
   Next iy
   frmExtras.picPB.Cls
End Sub

Public Sub AddThickLineV(zPTHICKLINE As Single)
' zPTHICKLINE  .0 -> 1.0
Dim TX As Single
Dim RR As Byte, GG As Byte, BB As Byte
Dim CH As Long, CW As Long
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   
   TX = zPTHICKLINE * CW
   If TX < 1 Then TX = 1
   StartProgress (LOX - 1 + CW / 2 + TX / 2)
   For ix = LOX - 1 + CW / 2 - TX / 2 To LOX - 1 + CW / 2 + TX / 2
   If ix > LOX - 1 Then
   If ix <= HIX Then
      For iy = LOY To HIY
         If aSelectAccepted Then
            iyy = iy - LOY
            ixx = ix - LOX
            If MaskArray(ixx, iyy) = 0 Then
               picDATA(0, ix, iy) = BB
               picDATA(1, ix, iy) = GG
               picDATA(2, ix, iy) = RR
            End If
         Else
            picDATA(0, ix, iy) = BB
            picDATA(1, ix, iy) = GG
            picDATA(2, ix, iy) = RR
         End If
      Next iy
   End If
   End If
   DrawProgress
   Next ix
   frmExtras.picPB.Cls
End Sub

Public Sub AddThickLineHV(zPTHICKLINE As Single)
' zPTHICKLINE  .0 -> 1.0
   ReDim picData2(0 To 3, 0 To FullW - 1, 0 To FullH - 1)
   picData2() = picDATAORG()
   AddThickLineH zPTHICKLINE
   picDATAORG() = picDATA()
   AddThickLineV zPTHICKLINE
   picDATAORG() = picData2()
End Sub

Public Sub AddLinesH(PLINES As Long)
' PLINES 1 - 400
Dim RR As Byte, GG As Byte, BB As Byte
Dim CH As Long, CW As Long
Dim sstep As Single
   
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   sstep = (CH / PLINES)
   If sstep < 1 Then sstep = 1
   For iy = LOY To HIY Step sstep
      For ix = LOX To HIX
         If aSelectAccepted Then
            iyy = iy - LOY
            ixx = ix - LOX
            If MaskArray(ixx, iyy) = 0 Then
               picDATA(0, ix, iy) = BB
               picDATA(1, ix, iy) = GG
               picDATA(2, ix, iy) = RR
            End If
         Else
            picDATA(0, ix, iy) = BB
            picDATA(1, ix, iy) = GG
            picDATA(2, ix, iy) = RR
         End If
      Next ix
      DrawProgress
   Next iy
   frmExtras.picPB.Cls
End Sub

Public Sub AddLinesV(PLINES As Long)
' PLINES 1 - 400
Dim RR As Byte, GG As Byte, BB As Byte
Dim CH As Long, CW As Long
Dim sstep As Single
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   sstep = (CH / PLINES)
   If sstep < 1 Then sstep = 1
   StartProgress (HIX / sstep)
   For ix = LOX To HIX Step sstep
   For iy = LOY To HIY
         If aSelectAccepted Then
            iyy = iy - LOY
            ixx = ix - LOX
            If MaskArray(ixx, iyy) = 0 Then
               picDATA(0, ix, iy) = BB
               picDATA(1, ix, iy) = GG
               picDATA(2, ix, iy) = RR
            End If
         Else
            picDATA(0, ix, iy) = BB
            picDATA(1, ix, iy) = GG
            picDATA(2, ix, iy) = RR
         End If
   Next iy
   DrawProgress
   Next ix
   frmExtras.picPB.Cls
End Sub

Public Sub AddNet(PLINES As Long)
' PLINES 1 - 400
Dim RR As Byte, GG As Byte, BB As Byte
Dim CH As Long, CW As Long
Dim sstep As Single
   
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   sstep = (CH / PLINES)
   If sstep < 1 Then sstep = 1
   For iy = LOY To HIY Step sstep
   For ix = LOX To HIX
      If aSelectAccepted Then
         iyy = iy - LOY
         ixx = ix - LOX
         If MaskArray(ixx, iyy) = 0 Then
            picDATA(0, ix, iy) = BB
            picDATA(1, ix, iy) = GG
            picDATA(2, ix, iy) = RR
         End If
      Else
         picDATA(0, ix, iy) = BB
         picDATA(1, ix, iy) = GG
         picDATA(2, ix, iy) = RR
      End If
   Next ix
   Next iy
   sstep = (CH / PLINES)
   If sstep < 1 Then sstep = 1
   StartProgress (HIX / sstep)
   For ix = LOX To HIX Step sstep
   For iy = LOY To HIY
      If aSelectAccepted Then
         iyy = iy - LOY
         ixx = ix - LOX
         If MaskArray(ixx, iyy) = 0 Then
            picDATA(0, ix, iy) = BB
            picDATA(1, ix, iy) = GG
            picDATA(2, ix, iy) = RR
         End If
      Else
         picDATA(0, ix, iy) = BB
         picDATA(1, ix, iy) = GG
         picDATA(2, ix, iy) = RR
      End If
   Next iy
   DrawProgress
   Next ix
   frmExtras.picPB.Cls
End Sub

Public Sub AddHexagons(zPRAD As Single)
' Zrad .5 to 4.0
Dim zA As Single
Dim zD As Single
Dim ixv As Long
Dim iyv As Long
Dim pixv As Long
Dim piyv As Long
Dim zRM As Single
Dim CH As Long, CW As Long
Dim zix As Single, ziy As Single
Dim RR As Byte, GG As Byte, BB As Byte
   
   picDATA() = picDATAORG()
   
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   
   zD = Sqr(CW * CW + CH * CH)
   zRM = zD / (16 * zPRAD)
   StartProgress ((HIY + zRM - LOY) / (2 * zRM))
   For ziy = LOY To HIY + zRM Step Sqr(3) * zRM
   For zix = LOX To HIX + zRM Step 2 * zRM
         pixv = zix + zRM
         piyv = ziy
         For zA = 0 To 2 * pi# Step pi# / 3
            ixv = zix + zRM * Cos(zA)
            iyv = ziy + zRM * Sin(zA)
            If aSelectAccepted Then
               BresLineCALC pixv, piyv, ixv, iyv, RR, GG, BB
            Else
               BresLine pixv, piyv, ixv, iyv, RR, GG, BB
            End If
            pixv = ixv
            piyv = iyv
         Next zA
   Next zix
   DrawProgress
   Next ziy
   frmExtras.picPB.Cls
End Sub

Public Sub AddSquares(zPRAD As Single)
' zPar 0.5 to 15
Dim zD As Single
Dim zRM As Single
Dim CH As Long, CW As Long

Dim RR As Byte, GG As Byte, BB As Byte
Dim n As Long
   picDATA() = picDATAORG()
   
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   
   zD = Sqr(CW * CW + CH * CH)
   zRM = zD / (16 * zPRAD)
   StartProgress ((HIY + zRM - LOY) / (2 * zRM))
   n = 0
   For iy = LOY To HIY + zRM Step zRM + 4
   For ix = LOX To HIX + zRM Step zRM + 4
      If aSelectAccepted Then
         BresLineCALC ix, iy, ix + zRM, iy, RR, GG, BB
         BresLineCALC ix, iy, ix, iy + zRM, RR, GG, BB
         BresLineCALC ix + zRM, iy + zRM, ix + zRM, iy, RR, GG, BB
         BresLineCALC ix + zRM, iy + zRM, ix, iy + zRM, RR, GG, BB
      Else
         BresLine ix, iy, ix + zRM, iy, RR, GG, BB
         BresLine ix, iy, ix, iy + zRM, RR, GG, BB
         BresLine ix + zRM, iy + zRM, ix + zRM, iy, RR, GG, BB
         BresLine ix + zRM, iy + zRM, ix, iy + zRM, RR, GG, BB
      End If
   Next ix
   DrawProgress
   Next iy
   frmExtras.picPB.Cls
End Sub

Public Sub AddTriangles(zPRAD As Single)
Dim zD As Single
Dim zRM As Single
Dim CH As Long, CW As Long

Dim RR As Byte, GG As Byte, BB As Byte
Dim n As Long
   picDATA() = picDATAORG()
   
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   
   zD = Sqr(CW * CW + CH * CH)
   zRM = zD / (16 * zPRAD)
   StartProgress ((HIY + zRM - LOY) / (2 * zRM))
   n = 0
   For iy = LOY To HIY + zRM Step zRM + 4
   For ix = LOX To HIX + zRM Step zRM + 4
      If aSelectAccepted Then
         BresLineCALC ix, iy, ix + zRM, iy, RR, GG, BB
         BresLineCALC ix + zRM, iy, ix + zRM \ 2, iy + zRM, RR, GG, BB
         BresLineCALC ix + zRM \ 2, iy + zRM, ix, iy, RR, GG, BB
      Else
         BresLine ix, iy, ix + zRM, iy, RR, GG, BB
         BresLine ix + zRM, iy, ix + zRM \ 2, iy + zRM, RR, GG, BB
         BresLine ix + zRM \ 2, iy + zRM, ix, iy, RR, GG, BB
      End If
   Next ix
   DrawProgress
   Next iy
   frmExtras.picPB.Cls
End Sub

Public Sub AddHWaves(PLINES As Long)
' PLINES  0 -> 30
Dim jj As Long, zix As Single
Dim zA As Single
Dim RR As Byte, GG As Byte, BB As Byte
Dim CH As Long, CW As Long
Dim zziy As Single
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   zA = (10 + PLINES) / 2
   StartProgress (HIY / (CH / (PLINES + 1)))
   For zziy = LOY To HIY Step CH / (PLINES + 1)
      For zix = LOX To HIX + zA Step 1 / zA
         If zix > LOX - 1 Then
         If zix <= HIX Then
            jj = zziy + zA * Sin(zix * 2 * zA / CW)
            If jj > LOY - 1 Then
            If jj <= HIY Then
               If aSelectAccepted Then
                  iyy = jj - LOY
                  ixx = zix - LOX
                  If MaskArray(ixx, iyy) = 0 Then
                     picDATA(0, zix, jj) = BB
                     picDATA(1, zix, jj) = GG
                     picDATA(2, zix, jj) = RR
                  End If
               Else
                  picDATA(0, zix, jj) = BB
                  picDATA(1, zix, jj) = GG
                  picDATA(2, zix, jj) = RR
               End If
            End If
            End If
         End If
         End If
      Next zix
      DrawProgress
   Next zziy
   frmExtras.picPB.Cls
End Sub

Public Sub AddVWaves(PLINES As Long)
' PLINES  0 -> 30
Dim ii As Long, ziy As Single
Dim zA As Single
Dim RR As Byte, GG As Byte, BB As Byte
Dim CH As Long, CW As Long
Dim zzix As Single
   picDATA() = picDATAORG()
   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If
   zA = (10 + PLINES) / 2
   StartProgress (HIX / (CW / (PLINES + 1)))
   For zzix = LOX To HIX Step CW / (PLINES + 1)
      For ziy = LOY To HIY + 2 * zA Step 1 / zA
         If ziy > LOY - 1 Then
         If ziy <= HIY Then
            ii = zzix + zA * Sin(ziy * 2 * zA / CH)
            If ii > LOX - 1 Then
            If ii <= HIX Then
               If aSelectAccepted Then
                  iyy = ziy - LOY
                  ixx = ii - LOX
                  If MaskArray(ixx, iyy) = 0 Then
                     picDATA(0, ii, ziy) = BB
                     picDATA(1, ii, ziy) = GG
                     picDATA(2, ii, ziy) = RR
                  End If
               Else
                  picDATA(0, ii, ziy) = BB
                  picDATA(1, ii, ziy) = GG
                  picDATA(2, ii, ziy) = RR
               End If
            End If
            End If
         End If
         End If
      Next ziy
   DrawProgress
   Next zzix
   frmExtras.picPB.Cls
End Sub

Public Sub AddHVWaves(PLINES As Long)
' PLINES  0 -> 30
   ReDim picData2(0 To 3, 0 To FullW - 1, 0 To FullH - 1)
   picData2() = picDATAORG()
   AddHWaves PLINES
   picDATAORG() = picDATA()
   AddVWaves PLINES
   picDATAORG() = picData2()
End Sub

Public Sub Streaks(PAR As Long)
' Streaks
' Par 1 - 15
Dim i As Long, j As Long
Dim BR As Byte, bG As Byte, BB As Byte
Dim CH As Long
   CH = HIY - LOY
   picDATA() = picDATAORG()
   StartProgress (HIY / (10 * (16 - PAR)))
   For iy = LOY To HIY Step (10 * (16 - PAR))
   For ix = LOX To HIX Step (10 * (16 - PAR))
      i = ix * Rnd
      j = iy * Abs(Rnd)
      BB = picDATAORG(0, i, j)
      bG = picDATAORG(1, i, j)
      BR = picDATAORG(2, i, j)
      If aSelectAccepted Then
         BresLineCALC ix, iy, i, HIY - j - 1, BR, bG, BR
      Else
         BresLine ix, iy, i, HIY - j - 1, BR, bG, BR
      End If
   Next ix
   DrawProgress
   Next iy
   frmExtras.picPB.Cls
End Sub

Public Sub AddJigSaw(ByVal PAR As Single)
' PAR = 2 to scrMax
' GRID
'                                 FullW
'                              Columns   -> X
'      kx(0,0),ky(0,0)  kx(2,0),ky(2,0)        kx(ncols,0),ky(ncols,0)
'             ---------------------------------------------
'             |       |        |        |        |        |
'             |       |        |        |        |        |
'             |       |        |        |        |        |
'           (0,1)---(1,1)----(2,1)----(3,1)----(4,1)(ncols,0)
'             |       |        |        |        |        |
'             |       |        |        |        |        |
'             |       |        |        |        |        |
'             ---------------(2,2)-------------------------
'   Rows      |       |        |        |        |        |
'    FullH        |       |        |        |        |        |
'             |       |        |        |        |        |
'    |        ------------------------(3,3)----------------
'    Y        |       |        |        |        |        |
'             |       |        |        |        |        |
'             |       |        |        |        |        |
'           (0,4)---(1,4)----------------------(4,4)(ncols,4)
'             |       |        |        |        |        |
'             |       |        |        |        |        |
'             |       |        |        |        |        |
'             ---------------------------------------------
'  kx(0,nrows),ky(0,nrows)                 kx(ncols,nrows),ky(ncols,nrows)

Dim k As Long
Dim j As Long
Dim ranmul As Long
Dim neww As Single, newh As Single
Dim xofft As Long, xoffb As Long
Dim yoffl As Long, yoffr As Long
Dim px As Long, py As Long
Dim swap As Single
Dim yoff As Single
Dim CH As Long, CW As Long
Dim RR As Byte, GG As Byte, BB As Byte
   
   Tension = 0.5
   
   picDATA() = picDATAORG()
   ' 9 pt side lines
   Randomize
   yoff = 0.4 + Rnd / 3
   XLine(0) = 0: YLine(0) = 0
   XLine(1) = 8: YLine(1) = 2 * yoff
   XLine(2) = 15: YLine(2) = 1 * yoff
   XLine(3) = 16: YLine(3) = 0 * yoff
   
   Randomize
   yoff = 0.4 + Rnd / 3
   XLine2(0) = 0: YLine2(0) = 0
   XLine2(1) = 1: YLine2(1) = 1 * yoff
   XLine2(2) = 6: YLine2(2) = 2 * yoff
   XLine2(3) = 13: YLine2(3) = 0 * yoff
   XLine2(4) = 16: YLine2(4) = 0 * yoff

   CH = HIY - LOY
   CW = HIX - LOX
   If aUseRightColor Then
      LngToRGB RightColor, RR, GG, BB
   Else
      LngToRGB LeftColor, RR, GG, BB
   End If

   If CH < CW Then
      nrows = PAR
      If PAR <= 4 Then  ' Make equal for small rows & columns
         ncols = nrows
      Else
         ncols = (HIX - LOX) \ 60
      End If
   Else
      ncols = PAR
      If PAR <= 4 Then
         nrows = ncols
      Else
         nrows = (HIY - LOY) \ 60
      End If
   End If
   
   If ncols < 2 Then ncols = 2
   If nrows < 2 Then nrows = 2
   neww = CW / ncols
   newh = CH / nrows
   If ncols < 2 Then
      ncols = 2
      neww = CW / ncols
   End If
   If nrows < 2 Then
      nrows = 2
      newh = CH / nrows
   End If
   
   
   ReDim kx(0 To ncols, 0 To nrows)
   ReDim ky(0 To ncols, 0 To nrows)
   
   Randomize
   ' Fill edge coords
   For k = 0 To nrows
      kx(0, k) = LOX
      kx(ncols, k) = HIX - 1
   Next k
   For k = 0 To ncols
      ky(k, 0) = LOY
      ky(k, nrows) = HIY - 1
   Next k
   ' Set vertical lines
   ranmul = 19 + 2 * (Rnd - 0.5)
   If ncols = 2 Then ranmul = 0
   For k = 1 To ncols - 1
      xofft = ranmul * Rnd
      xoffb = ranmul * Rnd
      kx(k, 0) = LOX + k * neww + xofft
      kx(k, nrows) = LOX + k * neww + xoffb
   Next k
   ' Set horizontal lines
   ranmul = 19 + 2 * (Rnd - 0.5)
   If nrows = 2 Then ranmul = 0
   For k = 1 To nrows - 1
      yoffl = ranmul * Rnd
      yoffr = ranmul * Rnd
      ky(0, k) = LOY + k * newh + yoffl
      ky(ncols, k) = LOY + k * newh + yoffr
   Next k
   ' Find intersections
   ReDim mx(1 To ncols - 1)
   ReDim cx(1 To ncols - 1)
   ReDim my(1 To nrows - 1)
   ReDim cy(1 To nrows - 1)
   
   ' x vert lines  slopes & cuts
   For k = 1 To ncols - 1
      mx(k) = kx(k, nrows) - kx(k, 0)
      If mx(k) <> 0 Then
         mx(k) = (ky(k, nrows) - ky(k, 0)) / mx(k)
      Else
         mx(k) = 1000
      End If
      cx(k) = ky(k, 0) - mx(k) * kx(k, 0)
   Next k
   ' y horz lines  slopes & cuts
   For k = 1 To nrows - 1
      my(k) = kx(ncols, k) - kx(0, k)
      If my(k) <> 0 Then
         my(k) = (ky(ncols, k) - ky(0, k)) / my(k)
      Else
         my(k) = 1000
      End If
      cy(k) = ky(0, k) - my(k) * kx(0, k)
   Next k
   ' Calc intersections
   For j = 1 To nrows - 1
   For k = 1 To ncols - 1
      kx(k, j) = (cy(j) - cx(k)) / (mx(k) - my(j))
      ky(k, j) = my(j) * kx(k, j) + cy(j)
   Next k
   Next j
   
   ' Draw all vertical sides
   StartProgress (nrows)
   For j = 1 To nrows '- 1
   For k = 1 To ncols - 1
      xs = kx(k, j - 1)
      ys = ky(k, j - 1)
      px = kx(k, j)
      py = ky(k, j)
      ' Lugs randomly to left/right
      swap = Rnd - 0.5
      If swap < 0 Then
         swap = xs
         xs = px
         px = swap
         swap = ys
         ys = py
         py = swap
      End If
      DrawSide xs, ys, px, py, RR, GG, BB
   Next k
   DrawProgress
   Next j
   ' Draw all horizontal sides
   StartProgress (nrows)
   For j = 1 To nrows - 1
   For k = 1 To ncols
      xs = kx(k - 1, j)
      ys = ky(k - 1, j)
      px = kx(k, j)
      py = ky(k, j)
      ' Lugs randomly to up/down
      swap = Rnd - 0.5
      If swap < 0 Then
      swap = xs
      xs = px
      px = swap
      swap = ys
      ys = py
      py = swap
      End If
      DrawSide xs, ys, px, py, RR, GG, BB
   Next k
   DrawProgress
   Next j
   
   ' Box inside
   If aSelectAccepted Then
      BresLineCALC LOX + 1, LOY, LOX + 1, HIY, RR, GG, BB      ' Left
      BresLineCALC HIX - 1, HIY, HIX - 1, LOY, RR, GG, BB      ' Right
      BresLineCALC LOX + 1, LOY + 1, HIX, LOY + 1, RR, GG, BB  ' Bottom
      BresLineCALC HIX, HIY - 1, LOX + 1, HIY - 1, RR, GG, BB  ' Top
   Else
      BresLine LOX + 1, LOY, LOX + 1, HIY, RR, GG, BB      ' Left
      BresLine HIX - 1, HIY, HIX - 1, LOY, RR, GG, BB      ' Right
      BresLine LOX + 1, LOY + 1, HIX, LOY + 1, RR, GG, BB  ' Bottom
      BresLine HIX, HIY - 1, LOX + 1, HIY - 1, RR, GG, BB  ' Top
   End If
   frmExtras.picPB.Cls
End Sub

Private Sub DrawSide(ByVal xs As Long, ByVal ys As Long, ByVal xp As Long, ByVal yp As Long, _
                     RR As Byte, GG As Byte, BB As Byte)
Dim LineLen As Single
Dim LLug As Single
Dim x1 As Single, y1 As Single
Dim x2 As Single, y2 As Single
Dim xc As Single, yc As Single
      dx = xp - xs
      DY = yp - ys
      zAng = zATan2(DY, dx)
      LineLen = Sqr(dx * dx + DY * DY)
      Mul = LineLen / 100 '80      ' arbitrary
      'If Mul > 1 Then Mul = 1
      If Mul > 0.75 Then Mul = 0.75
      LLug = 11 * Mul         ' half lug along line
      dx = LLug * Cos(zAng)   ' offsets to beginning & end of lug on line
      DY = LLug * Sin(zAng)
      xc = (xp + xs) / 2  ' Centre of line
      yc = (yp + ys) / 2
      x1 = xc - dx        ' pts where lug joins line
      y1 = yc - DY
      x2 = xc + dx
      y2 = yc + DY
      ' NB Draws lug to left of line direction
      DrawLug x1, y1, zAng, Mul, RR, GG, BB
      LineLen = Sqr((xs - x1) ^ 2 + (ys - y1) ^ 2)
      Mul = LineLen / 16 ' arbitrary
      DrawLine1 xs, ys, zAng, Mul, RR, GG, BB
      LineLen = Sqr((xp - x2) ^ 2 + (yp - y2) ^ 2)
      Mul = LineLen / 16 ' arbitrary
      DrawLine2 x2, y2, zAng, Mul, RR, GG, BB
End Sub

Private Sub DrawLug(ByVal x As Single, ByVal y As Single, zangle As Single, Mul As Single, _
          RR As Byte, GG As Byte, BB As Byte)
' Private XJ() As Single, YJ() As Single  ' Offset lug pattern
' zAngle in radians
Dim pts(0 To 9) As PointF
Dim zcos As Single, zsin As Single
Dim k As Long
   zcos = Cos(zangle)
   zsin = Sin(zangle)
   pts(0).x = x - 1
   pts(0).y = y - 1
   ' Rotate lug into line & Draw
   For k = 1 To 9
      pts(k).x = pts(k - 1).x + (Mul * XJ(k)) * zcos - (Mul * YJ(k)) * zsin
      pts(k).y = pts(k - 1).y + (Mul * YJ(k)) * zcos + (Mul * XJ(k)) * zsin
   Next k
   ModSpline.DrawCurve 0.01, Tension, pts, RR, GG, BB
   
   ' TEST
   ' Lug center
'   xlc = xr(0) + (Mul * XLugCen) * zcos - (Mul * YLugCen) * zsin
'   ylc = yr(0) + (Mul * YLugCen) * zcos + (Mul * XLugCen) * zsin
'   picDRAW.PSet (xlc, ylc), vbBlue
'
'   LRad = Mul * LugRadius
'   picDRAW.Line (xlc, ylc)-(xr(7), yr(7)), vbBlue  ' From centre to edge of lug
End Sub


Private Sub DrawLine1(ByVal x As Single, ByVal y As Single, zangle As Single, Mul As Single, _
          RR As Byte, GG As Byte, BB As Byte)
Dim pts(0 To 3) As PointF
Dim zcos As Single, zsin As Single
Dim k As Long
   zcos = Cos(zangle)
   zsin = Sin(zangle)
   pts(0).x = x - 1
   pts(0).y = y - 1
   ' Rotate Line Draw
   For k = 1 To 3
      pts(k).x = pts(0).x + (Mul * XLine(k)) * zcos - (Mul * YLine(k)) * zsin
      pts(k).y = pts(0).y + (Mul * YLine(k)) * zcos + (Mul * XLine(k)) * zsin
   Next k
   ModSpline.DrawCurve 0.01, Tension, pts, RR, GG, BB
End Sub

Private Sub DrawLine2(ByVal x As Single, ByVal y As Single, zangle As Single, Mul As Single, _
          RR As Byte, GG As Byte, BB As Byte)
Dim pts(0 To 4) As PointF
Dim zcos As Single, zsin As Single
Dim k As Long
   zcos = Cos(zangle)
   zsin = Sin(zangle)
   pts(0).x = x - 1
   pts(0).y = y - 1
   ' Rotate Line Draw
   For k = 1 To 4
      pts(k).x = pts(0).x + (Mul * XLine2(k)) * zcos - (Mul * YLine2(k)) * zsin
      pts(k).y = pts(0).y + (Mul * YLine2(k)) * zcos + (Mul * XLine2(k)) * zsin
   Next k
   ModSpline.DrawCurve 0.01, Tension, pts, RR, GG, BB
End Sub

Private Sub StartProgress(ByVal zPAR As Single)
' Private xd As Single, xp As Single
' EG: Par = PRELIEF * FullH
   frmExtras.picPB.DrawWidth = 3
   frmExtras.picPB.Cls
   xp = 0
   xd = frmExtras.picPB.Width / zPAR
   frmExtras.picPB.PSet (xp, 2)
End Sub
Private Sub DrawProgress()
' Private xd As Single, xp As Single
   frmExtras.picPB.Line -(xp, 2)
   frmExtras.picPB.Refresh
   xp = xp + xd
End Sub

