Attribute VB_Name = "ModDRAW"
' ModDRAW.bas
 
 Option Explicit


' Adding new Tools:

' 1. Set generic NAMETool eg Tool, DotToool etc
' 2. List Sub Tools in Enum eg ENAMETool [SubName1],[SubName2], etc
' 3. Add image boxes imNAME(), with icon, to frmToolStore with LabelNumTools()
'     containing the number of sub-tools & LabTools No. NAME for ref.
' 4. On Form1 in Sub optTools_Click(Index As Integer)
'     add Case NAME
' 5  Also on Form1 in Sub optSubTools_Click(Index As Integer)
'     add Case NAME:  NAMETool = Index
' 6. Develop 3 routines StartNAME    (in picDRAW_Mouse_Down)
'                       DrawMoveNAME (in picDRAW Mouse_Move)
'                       CompleteNAME (in picDRAW Mouse_Up)
'       these 3 routines to be placed in this module ModDRAW
' 7. For new main tool also include in Form1 Sub Setbackground
'      ie increase Number of main tools
' 8. For Extra sub Tools add to existing imNAME() (with extra icon),
'     LabelNumTools() (increment number of sub tools) and add to
'     appropriate Enum list, lastly add extra item to Start,DrawMove,
'     & Complete routines.


' TEST
' Private Declare Function timeGetTime& Lib "winmm.dll" ()
 
'Private Declare Function ExtractICON Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
'Private Declare Function DrawIcon Lib "user32.dll" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
'Private Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Long) As Long
'
'Private Declare Function DrawIconEx Lib "user32.dll" _
'                  (ByVal hDC As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, _
'                   ByVal cxWidth As Long, ByVal cyWidth As Long, _
'                   ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, _
'                   ByVal diFlags As Long) As Long

'Private hIcon As Long  ' handle to the function gotten from the executable file

Private Type COORD
    x As Long
    y As Long
End Type

Private Type RECT
    Left As Long
    TOp As Long
    Right As Long
    Bottom As Long
End Type

Private REC As RECT

'Public hRgnPic
'Private Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long

Private Declare Function PtInRect Lib "USER32" _
   (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long

Private Declare Function SetRect Lib "USER32" _
(lpRect As RECT, ByVal x1 As Long, ByVal y1 As Long, _
  ByVal x2 As Long, ByVal y2 As Long) As Long
  
'Private REC As RECT
'SetRect REC, 0, 0, FullW - 1, FullH - 1
'If PtInRect(REC, XX, YY) <> 0 Then ' Inside REC
'  If = 0 then outside REC

Const ALTERNATE = 1 ' ALTERNATE and WINDING are
Const WINDING = 2 ' constants for FillMode.
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal NCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long
Private Poly() As COORD
Private Poly2() As COORD

' Drawing
' Main Tool Groups
Public Tool As Long
Public Enum EToolType
   None = 0
   Singles = 1
   Freedraw = 2
   ALines = 3
   Boxes = 4
   Cirlipses = 5
   Arcs
   PolyLines
   CurvyLines
   Arrows
   Junctions
   Tubulars
   Shapes
   Radials
   Circuits
   Waves
   Fillers
   Fillers2
   Sweepers = 18
   Erasers = 19
End Enum

Public SingleTool As Long
Public Enum ESingleTool
   [DSingle] = 0
   [SprayDots]
   [Grass]
   [FBush1] = 3
   [FBush2]
   [FBush3]
   [FBush4]
   [FBush5]
   [FBush6]
   [FBush7] = 9
   [DStamp] = 10
End Enum

Public FreeDrawTool As Long
Public Enum EFreeDrawTool
   [FDraw] = 0
   [FStalks]
   [FLeaves]
   [FRibbon]
   [BRibbon]
   [FRibbonSolid]
   [BRibbonSolid]
   [RibbonHorz]
   [RibbonVert]
   [RibbonHorzSolid]
   [RibbonVertSolid]
   [FreeSpray]
   [FBricks]
   [FBeads]
   [FBlocks]
   [FBubbles]
   [FBroken]
   [FDotted]
   [FSpacedDots]
   [FCustom] = 19
End Enum
Public LineTool As Long
Public Enum ELineTool
   [LSolid] = 0
   [LDotted]
   [LDouble]
   [LDoubleEnded]
   [LGradient]
   [LBEZ]
   [LBEZFilled] = 6
   [LAGradient] = 7
End Enum
Public BoxTool As Long
Public Enum EBoxTool
   [BSolid] = 0
   [BDotted]
   [BDouble]
   [BGradient1]
   [BGradient2]
   [BGradient3]
   [BGradient33]
   [BGradient4]
   [BGradient5]
   [BGradient6]  ' Solid Fill
   [BAngBox]
   [BAngBoxFilled]
   [BAngBoxGradient]
   [BoxTop]
   [BoxRight]
   [BoxRounded] = 15
   [BDoubleShadowed] = 16
End Enum
Public CircTool As Long
Public Enum ECircTool
   [CSolid] = 0
   [CDotted]
   [CDouble]
   [CGradient1]
   [CGradient2]
   [CGradient3]
   [CGradient4]
   [CGradient5]
   [CFilled]  ' Solid Fill
   [COffsetCen]
   [CRotate]
   [CRotateFill] = 11
   [CConcentric]
End Enum
Public ArcTool As Long
Public Enum EArcTool
   [Arc1] = 0
   [Arc2]
   [Arc3]
   [Arc4]
   [Arc5]
   [Arc6]
   [Arc7]
   [Arc8]
   [Arc9]
   [Arc10]
   [Arc11]
   [Arc12]
   [Arc13]
   [Arc14]
   [Arc15]
   [Arc16] = 15
End Enum
Public PolyLineTool As Long
Public Enum EPolyTool
   [PLSolid] = 0
   [PLDouble]
   [PLDoubleEnded]
   [PLGradient1]
   [PLConnect]
   [PLParagram]
   [PLDoubleConnect]
   [PLDoubleParagram] = 7
   [PLFillTri] = 8
   [PLFillPoly] = 9
End Enum
Public CurvyLineTool As Long
Public Enum ECurvyTool
   [CLSolid] = 0
   [CLDouble]
   [CLDoubleEnded]
   [CLGradient1] = 3
End Enum
Public ArrowTool As Long
Public Enum EArrows  ' See DrawArrows for types
   [Arrow1] = 0
   [Arrow2]
   [Arrow3]
   [Arrow4]
   [Arrow5]
   [Arrow6]
   [Arrow7] = 6
End Enum
Public JunctionTool As Long
Public Enum EJunction
   [JTee1] = 0
   [JCross1]
   [JCorner1]
   [JLine] = 3
End Enum
Public TubularTool As Long
Public Enum ETubular
   [CONE1] = 0
   [CONE2]
   [CONE3]
   [CONE4]
   [CONE5]
   [CONE6]
   [TUBE1]
   [TUBE2]
   [TUBE3]
   [BULLET1]
   [BULLET2]
   [BULLET3] = 11
   [OvalTan] = 12
End Enum
Public ShapeTool As Long
Public Enum EShape
   [FWedge] = 0
   [BWedge]
   [Frustrum]
   [Triangle]
   [Square]
   [Hexagon] = 5
   [Paragram45] = 6
   [Hexagon45] = 7
End Enum
Public RadialTool
Public Enum ERadial
   [Spokes] = 0
   [Stars]
   [Circles]
   [Polygons]
   [Teeth]
   [CSpokes]
   [CStars]
   [CPolygons]
   [OddRoses]
   [EvenRoses]
   [Hypocycloid]
   [Spiral] = 11
End Enum
Public CircuitTool As Long
Public Enum ECircuitTool
   [Resistor] = 0
   [Capacitor]
   [Inductor]
   [Battery]
   [Switch]
   [Earth]
   [CBox]
   [CLine]
   [CDiode] = 8
End Enum
Public WaveTool As Long
Public Enum WWaveTool
   [SineWave] = 0
   [Rectified]
   [SquareWave]
   [Sawtooth1]
   [Sawtooth2]
   [Sawtooth3]
   [CircWaves] = 6
End Enum
Public SweepTool As Long
Public Enum ESweepTool
   [Smudger] = 0
   [Stirer]
   [Sharpener]
   [Smoother]
   [Warper]
   [Eraser]
   [Darker] = 6
   [brighter]
   [DiffDarker] = 8
   [DiffBrighter] = 9
   [DiffLessBlue] = 10
   [DiffMoreBlue] = 11
   [DiffLessGreen] = 12
   [DiffMoreGreen] = 13
   [DiffLessRed] = 14
   [DiffMoreRed] = 15
End Enum
Public RubberTool As Long
Public Enum ERubberTool
   [SmallRubber]
   [LargeRubber]
   [SmallCRubber]
   [LargeCRubber]
End Enum

Public Cul As Long
Public Cul2 As Long

Public NumPoints As Long         ' Top scrollbar, scrPoints
' For Bricks, Waves, Sweepers
Public NPt As Long, HAR As Long  ' NumPoints(Width,Cycles,Bright/Dark)
'               2nd scrollbar,scrBricksWavySweepers (HAR HeightAmpRad)

' For Fillers
Public FillTool As Long
Public FillName$()
Public bmp() As Byte

Public bMark() As Byte  ' Marked region
' Region extent
Public xmax As Long, xmin As Long
Public ymax As Long, ymin As Long
Private xpts() As Long, ypts() As Long
Private bPattern() As Byte

Public PixelSize As Long   ' 1,2,3,4,
Public zspace As Single    ' 4,8,16
Public XorCul As Long   ' For XOR Drawing, Cul = final color, passed from Form1
Public xs As Single, ys As Single
Public xp As Single, yp As Single

' For Color steps
Public NSteps As Long
Public zIStep As Single
Public zRL As Single, zGL As Single, zBL As Single
Public zRR As Single, zGR As Single, zBR As Single
Public zRIncr As Single, zGIncr As Single, zBIncr As Single

Public zRL2 As Single, zGL2 As Single, zBL2 As Single
Public zRR2 As Single, zGR2 As Single, zBR2 As Single
Public zRIncr2 As Single, zGIncr2 As Single, zBIncr2 As Single

' For Wavy line
Public NCycles As Single
Public Amp As Single
Public aJoinEndPoints As Boolean

' For Arcs
Public aAddRadii As Boolean
Public aArc As Boolean

' For Drop shadow box
Public aTransparent As Boolean

' For 2PtShapes
Public aProject As Boolean


'x,y store
Public PointCount As Long
Public xsto() As Long, ysto() As Long

' Count cursor clicks & for Multisegment polylines, curvylines,,
Public aEnd As Boolean
Public aComplete As Boolean
Public StartClick As Long   ' 0,1,2 None,Left,Right
Public NClicks As Long, NLeftClicks As Long, NRightClicks As Long

' Parallel points for shading
Public pxstoL() As Long, pystoL() As Long   ' Left
Public pxstoR() As Long, pystoR() As Long   ' Right

' Saved points for Curvylines - to allow repeat with Space bar
Public svxsto() As Long, svysto() As Long
Public svPointCount As Long
' Save points
Public svxsto2 As Single, svysto2 As Single
' Ellipse angle point
Public xang As Single, yang As Single

' PointInside for Cones
Public PointInside As Boolean
Public Alpha1 As Single
Public Alpha2 As Single
Public Alpha3 As Single
' Outline tubulars
Public aOutline As Boolean

' Radials' # sides
Public zR As Single

' Dotted lines,, etc
Private px As Long, py As Long
Private NDots As Long
Private LineLength As Long
Private zangle As Single
Private zangle2 As Single   ' For Radials
Private cosA As Single, sinA As Single
' Ribbons
Private Rib As Long
Private rx1 As Long, ry1 As Long, rx2 As Long, ry2 As Long
' Parallel lines
Public ixa As Single, iya As Single, ixb As Single, iyb As Single
Public ixc As Single, iyc As Single, ixd As Single, iyd As Single
Public ixe As Single, iye As Single, ixf As Single, iyf As Single
Public ixg As Single, iyg As Single, ixh As Single, iyh As Single
' Cirlipses
Private zRadDiff As Single            ' Double cirlipses
Private aCirc As Boolean               ' Detect move over
Private aClearOrigOval As Boolean

' Arrows' head
Private zarrlen As Single     ' Default side arrow length
Private zarrang As Single     ' Default side arrow angle
' Junctions
Private zJuncLen As Single    ' Default side length

' For Sweepers
Public SweepRad As Single

' For diagonal shading etc
Private zdx As Single, zdy As Single
Private zx1 As Single, zy1 As Single
Private zx2 As Single, zy2 As Single
Private zx As Single, zy As Single
Private zyscale As Single, zxscale As Single

Public svCul As Long       ' Saving color for key space
Public aTheKey As Boolean
Public svRound As Boolean   ' Saving aRound
Public svAA As Boolean
Public pxsv As Long      ' Saving PixelSize
Public pxsvORG As Long   ' Saving Global PixelSize

' scrPoints
Public NumPointsStore() As Byte  ' (Tool, SubTool) (0 to 18, 0 to 30)
Public Minscr() As Byte
Public Maxscr() As Byte

Public NumHAR() As Byte         ' 0 Freedraw Bricks height, 1 Waves Amp, 2 Sweepers Radius
Public MinHAR() As Byte
Public MaxHAR() As Byte

' For rounded [LAGRADIENT] line
' Circle centre and radius
Private xc As Single, yc As Single, Rad As Single
' Diameter end points
Private x1 As Single, y1 As Single, x2 As Single, y2 As Single
' Diameter angle
Private Alpha As Single
' Point to point offsets
Private dx As Single, DY As Single
' Tangent points
Private xt1 As Single, yt1 As Single, xt2 As Single, yt2 As Single
' For intersection points
Private ix1 As Single, iy1 As Single, ix2 As Single, iy2 As Single
Private NegTest As Single
Private ZDD As Single
Private zstep As Single



' scrBricksWavySweepers
' FreeDraw Bricks
' All Waves apart from circles
' Sweepers radii.
'Public

Public Sub InitScrollbars()
Dim k As Long

   'ReDim NumHAR(0 To 2, 0 To 8)
   'ReDim MinHAR(0 To 2, 0 To 8)
   'ReDim MaxHAR(0 To 2, 0 To 8)
   
   ReDim NumHAR(0 To 2, 0 To 15)
   ReDim MinHAR(0 To 2, 0 To 15)
   ReDim MaxHAR(0 To 2, 0 To 15)
   
   
   ' 0 Freedraw, 1 Waves, 2 Sweepers
   ' use scrBricksWavySweepers for:-
   ' FreeDraw Bricks
   ' All Waves apart from circles
   ' Sweepers bright/dark.
   
   ' Bricks height pixels
   NumHAR(0, 0) = 6
   MinHAR(0, 0) = 2
   MaxHAR(0, 0) = 24
   ' Waves Amp pixels
   For k = 0 To 5
      NumHAR(1, k) = 8
      MinHAR(1, k) = 1
      MaxHAR(1, k) = 50
   Next k
   ' Sweepers Bright/Dark & >< RGB power
   For k = 6 To 15
      NumHAR(2, k) = 1
      MinHAR(2, k) = 1
      MaxHAR(2, k) = 9
   Next k
   
   ReDim NumPointsStore(0 To 18, 0 To 30)
   ReDim Minscr(0 To 18, 0 To 30)
   ReDim Maxscr(0 To 18, 0 To 30)
   
   NumPointsStore(Singles, [SprayDots]) = 4
      Minscr(Singles, [SprayDots]) = 4
      Maxscr(Singles, [SprayDots]) = 36
   NumPointsStore(Singles, [Grass]) = 4
      Minscr(Singles, [Grass]) = 4
      Maxscr(Singles, [Grass]) = 36
   For k = [FBush1] To [FBush7]
      NumPointsStore(Singles, k) = 4
         Minscr(Singles, k) = 1
         Maxscr(Singles, k) = 8
   Next k
   
   NumPointsStore(Freedraw, [FreeSpray]) = 4
      Minscr(Freedraw, [FreeSpray]) = 1 '4
      Maxscr(Freedraw, [FreeSpray]) = 36
   NumPointsStore(Freedraw, [FBricks]) = 12
      Minscr(Freedraw, [FBricks]) = 2
      Maxscr(Freedraw, [FBricks]) = 24
   NumPointsStore(Freedraw, [FSpacedDots]) = 2
      Minscr(Freedraw, [FSpacedDots]) = 1
      Maxscr(Freedraw, [FSpacedDots]) = 36
   NumPointsStore(Freedraw, [FCustom]) = 1
      Minscr(Freedraw, [FCustom]) = 1
      Maxscr(Freedraw, [FCustom]) = 64

   NumPointsStore(ALines, [LDouble]) = 2
      Minscr(ALines, [LDouble]) = 1
      Maxscr(ALines, [LDouble]) = 36
   NumPointsStore(ALines, [LDoubleEnded]) = 2
      Minscr(ALines, [LDoubleEnded]) = 1
      Maxscr(ALines, [LDoubleEnded]) = 36

   NumPointsStore(Boxes, [BDouble]) = 2
      Minscr(Boxes, [BDouble]) = 1
      Maxscr(Boxes, [BDouble]) = 36
   NumPointsStore(Boxes, [BAngBox]) = 5
      Minscr(Boxes, [BAngBox]) = 1
      Maxscr(Boxes, [BAngBox]) = 10
   NumPointsStore(Boxes, [BAngBoxFilled]) = 5
      Minscr(Boxes, [BAngBoxFilled]) = 1
      Maxscr(Boxes, [BAngBoxFilled]) = 10
   NumPointsStore(Boxes, [BAngBoxGradient]) = 5
      Minscr(Boxes, [BAngBoxGradient]) = 1
      Maxscr(Boxes, [BAngBoxGradient]) = 10

   NumPointsStore(Cirlipses, [CDouble]) = 2
      Minscr(Cirlipses, [CDouble]) = 1
      Maxscr(Cirlipses, [CDouble]) = 36
   
'   [PLDouble = 1
'   [PLDoubleEnded] = 2
'   [PLDoubleConnect] = 6
'   [PLDoubleParagram] = 7

   NumPointsStore(PolyLines, [PLDouble]) = 2
      Minscr(PolyLines, [PLDouble]) = 1
      Maxscr(PolyLines, [PLDouble]) = 36
   NumPointsStore(PolyLines, [PLDoubleEnded]) = 2
      Minscr(PolyLines, [PLDoubleEnded]) = 1
      Maxscr(PolyLines, [PLDoubleEnded]) = 36
   NumPointsStore(PolyLines, [PLDoubleConnect]) = 2
      Minscr(PolyLines, [PLDoubleConnect]) = 1
      Maxscr(PolyLines, [PLDoubleConnect]) = 36
   NumPointsStore(PolyLines, [PLDoubleParagram]) = 2
      Minscr(PolyLines, [PLDoubleParagram]) = 1
      Maxscr(PolyLines, [PLDoubleParagram]) = 36
   
'   [CLDouble] = 1
'   [CLDoubleEnded] = 2

   NumPointsStore(CurvyLines, [CLDouble]) = 2
      Minscr(CurvyLines, [CLDouble]) = 1
      Maxscr(CurvyLines, [CLDouble]) = 36
   NumPointsStore(CurvyLines, [CLDoubleEnded]) = 2
      Minscr(CurvyLines, [CLDoubleEnded]) = 1
      Maxscr(CurvyLines, [CLDoubleEnded]) = 36

'   [JTee1] = 0
'   [JCross1]
'   [JCorner1]
'   [JLine] = 3
   For k = 0 To 3
      NumPointsStore(Junctions, k) = 2
         Minscr(Junctions, k) = 1
         Maxscr(Junctions, k) = 36
   Next k

   NumPointsStore(Radials, [Spokes]) = 3
      Minscr(Radials, [Spokes]) = 3
      Maxscr(Radials, [Spokes]) = 36
   NumPointsStore(Radials, [CSpokes]) = 3
      Minscr(Radials, [CSpokes]) = 3
      Maxscr(Radials, [CSpokes]) = 36
   NumPointsStore(Radials, [Stars]) = 2
      Minscr(Radials, [Stars]) = 2
      Maxscr(Radials, [Stars]) = 36
   NumPointsStore(Radials, [CStars]) = 2
      Minscr(Radials, [CStars]) = 2
      Maxscr(Radials, [CStars]) = 36
   
   NumPointsStore(Radials, [Circles]) = 2
      Minscr(Radials, [Circles]) = 2
      Maxscr(Radials, [Circles]) = 36
      
   NumPointsStore(Radials, [Polygons]) = 3
      Minscr(Radials, [Polygons]) = 3
      Maxscr(Radials, [Polygons]) = 36
   NumPointsStore(Radials, [CPolygons]) = 3
      Minscr(Radials, [CPolygons]) = 3
      Maxscr(Radials, [CPolygons]) = 36
   
   NumPointsStore(Radials, [Teeth]) = 3
      Minscr(Radials, [Teeth]) = 2
      Maxscr(Radials, [Teeth]) = 36
   
   NumPointsStore(Radials, [OddRoses]) = 3
      Minscr(Radials, [OddRoses]) = 3
      Maxscr(Radials, [OddRoses]) = 37
   
   NumPointsStore(Radials, [EvenRoses]) = 2
      Minscr(Radials, [EvenRoses]) = 2
      Maxscr(Radials, [EvenRoses]) = 18

   NumPointsStore(Radials, [Hypocycloid]) = 3
      Minscr(Radials, [Hypocycloid]) = 3
      Maxscr(Radials, [Hypocycloid]) = 9

   NumPointsStore(Radials, [Spiral]) = 4
      Minscr(Radials, [Spiral]) = 4
      Maxscr(Radials, [Spiral]) = 20

   ' Num cycles
   NumPointsStore(Circuits, [Resistor]) = 2
      Minscr(Circuits, [Resistor]) = 1
      Maxscr(Circuits, [Resistor]) = 12
   NumPointsStore(Circuits, [Inductor]) = 2
      Minscr(Circuits, [Inductor]) = 1
      Maxscr(Circuits, [Inductor]) = 12

   For k = 0 To 6 ' Cycles
      NumPointsStore(Waves, k) = 1
         Minscr(Waves, k) = 1
         Maxscr(Waves, k) = 12
   Next k

   For k = 0 To 15 ' Radius
      NumPointsStore(Sweepers, k) = 4
         Minscr(Sweepers, k) = 4
         Maxscr(Sweepers, k) = 128
   Next k

End Sub


'#### STARTS ####################################################################

Public Sub PreviewSprayDots(picP As PictureBox)
Dim ix As Long, iy As Long
Dim x As Single, y As Single
Dim k As Long
   picP.DrawWidth = PixelSize
   x = 4 * picP.Width / 5
   y = picP.Height - 8
   For k = 1 To NumPoints
      ix = x - NumPoints * Rnd
      iy = y - NumPoints * Rnd
      picP.PSet (ix, iy), LeftColor
   Next k
End Sub

Public Sub PreviewGrass(picP As PictureBox)
Dim ix As Long, iy As Long
Dim x As Single, y As Single
Dim k As Long
   picP.DrawWidth = PixelSize
   x = picP.Width / 2
   y = picP.Height - 2
   For k = 1 To 16
      ix = x + 3 * NumPoints * (Rnd - 0.5)
      'iy = y - (NumPoints + NumPoints * Rnd)
      iy = y - NumPoints * (1 + Rnd) * 3 / 4
      picP.Line (x, y)-(ix, iy), LeftColor
   Next k
End Sub

Public Sub StartSingles(picDRAW As PictureBox, x As Single, y As Single)
Dim k As Long
Dim ix As Long, iy As Long
Dim Cul As Long

   pxsv = PixelSize
   Select Case SingleTool
   Case [DSingle]
      xsto(PointCount) = x
      ysto(PointCount) = y
      Cul = LeftColor
      If StartClick = vbRightButton Then Cul = RightColor
      picDRAW.DrawMode = vbCopyPen
      picDRAW.DrawWidth = 1
      DrawDot xsto(1), ysto(1), Cul
      DISPLAY Form1, Form1.picDRAW
      
   Case [SprayDots]
      If NClicks = 1 Then
         For k = 1 To NumPoints
            ix = x - NumPoints * Rnd
            iy = y - NumPoints * Rnd
            xsto(PointCount) = ix
            ysto(PointCount) = iy
            PointCount = PointCount + 1
            If PointCount > UBound(xsto()) Then
               ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
            End If
            picDRAW.PSet (ix, iy), XorCul
         Next k
      End If
      
   Case [Grass]
      If NClicks = 1 Then
         xsto(PointCount) = x
         ysto(PointCount) = y
         For k = 2 To 16
            ix = x + 3 * NumPoints * (Rnd - 0.5)
            iy = y - NumPoints * (1 + Rnd) * 3 / 4
            PointCount = PointCount + 1
            If PointCount > UBound(xsto()) Then
               ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
            End If
            xsto(PointCount) = ix
            ysto(PointCount) = iy
            picDRAW.Line (xsto(1), ysto(1))-(ix, iy), XorCul
         Next k
      End If
      
   Case [FBush1] To [FBush7]
      ModExtraTools.StartEndFBush picDRAW, x, y
   
   Case [DStamp]
      If NClicks = 1 Then
         picDRAW.DrawWidth = 1
         xsto(1) = x
         ysto(1) = y
         picDRAW.Line (x, y)-(x + StampW - 1, y + StampH - 1), XorCul, B
      End If
   End Select
End Sub

Public Sub StartFreeDraw(picDRAW As PictureBox, x As Single, y As Single)
   Select Case FreeDrawTool
   Case [FDraw], [FBeads], [FBlocks], [FBubbles], [FBroken]
   Case [FStalks]: picDRAW.DrawWidth = 1
   Case [FLeaves]: picDRAW.DrawWidth = 1
   Case [FRibbon], [FRibbonSolid]   ' //
      '      2
      '     /
      '    x
      '   /
      '  1
      picDRAW.DrawWidth = 1
      Rib = PixelSize
      If Rib = 1 Then Rib = 2
      rx1 = -Rib
      ry1 = Rib
      rx2 = Rib
      ry2 = -Rib
      
   Case [RibbonHorz], [RibbonHorzSolid]   ' ==
      
      ' Thick Horz
      picDRAW.DrawWidth = 1
      Rib = PixelSize
      If Rib = 1 Then Rib = 2
      rx1 = 0
      ry1 = Rib
      rx2 = 0
      ry2 = -Rib
      
   Case [BRibbon], [BRibbonSolid]   ' \\
      '  1
      '   \
      '    x
      '     \
      '      2
      picDRAW.DrawWidth = 1
      Rib = PixelSize
      If Rib = 1 Then Rib = 2
      rx1 = -Rib
      ry1 = -Rib
      rx2 = Rib
      ry2 = Rib
      

   Case [RibbonVert], [RibbonVertSolid]   '||
      ' Thick Vert
      picDRAW.DrawWidth = 1
      Rib = PixelSize
      If Rib = 1 Then Rib = 2
      rx1 = -Rib
      ry1 = 0
      rx2 = Rib
      ry2 = 0
   
   End Select
   
   Select Case FreeDrawTool
   Case [FDraw] To [RibbonVertSolid], [FBeads], [FBlocks], [FBubbles], [FBroken]
      If NClicks = 1 Then
         xsto(1) = x
         ysto(1) = y
         picDRAW.PSet (xsto(1), ysto(1)), XorCul
         PointCount = PointCount + 1
         If PointCount > UBound(xsto()) Then
            ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
         End If
      End If
   End Select

   Select Case FreeDrawTool
   Case [FreeSpray]
      If NClicks = 1 Then
         PointCount = 1
         Randomize
      End If
   End Select

   Select Case FreeDrawTool
   Case [FBricks]
      If NClicks = 1 Then
         ReDim xsto(1 To 20), ysto(1 To 20)
         xsto(1) = x
         ysto(1) = y
      End If
      
   Case [FDotted]
      If NClicks = 1 Then
         xsto(PointCount) = x
         ysto(PointCount) = y
         picDRAW.PSet (xsto(PointCount), ysto(PointCount)), XorCul
         PointCount = PointCount + 1
         If PointCount > UBound(xsto()) Then
            ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
         End If
      End If
   
   Case [FSpacedDots]
         
      If NClicks = 1 Then
         xsto(PointCount) = x
         ysto(PointCount) = y
         picDRAW.PSet (xsto(PointCount), ysto(PointCount)), XorCul
         PointCount = PointCount + 1
         If PointCount > UBound(xsto()) Then
            ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
         End If
      End If
   
   Case [FCustom]

      If NClicks = 1 Then
         picDRAW.DrawWidth = 1
         xsto(PointCount) = x
         ysto(PointCount) = y
         picDRAW.Line (x, y)-(x + StampW - 1, y + StampH - 1), XorCul, B
         PointCount = PointCount + 1
         If PointCount > UBound(xsto()) Then
            ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
         End If
      End If

   End Select
End Sub

Public Sub StartALines(picDRAW As PictureBox, x As Single, y As Single)
   If NClicks <> 1 Then Exit Sub
   aBEZ = False
   Select Case LineTool
   Case [LSolid] To [LAGradient] '[LBEZFilled]
      StartTwoCoords
      Select Case LineTool
      Case [LGradient]
        picDRAW.DrawWidth = PixelSize
      End Select
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
   End Select
   Select Case LineTool
   Case [LDouble], [LDoubleEnded]
      zspace = NumPoints + PixelSize
      If aAA And NumPoints <= 2 Then zspace = zspace + 1
      '  GetParallelCoords (IN) zspace,X1,Y1,X2,Y2, (OUT)ixa,iya,ixb,iyb
      GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), _
            ixa, iya, ixb, iyb
      picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
   End Select
End Sub

Public Sub StartTwoCoords()
' Xs,Ys,xp,yp set @ picDRAW_MouseDown
  ReDim xsto(1 To 20), ysto(1 To 20)

   xsto(1) = xs   ' From picDRAW_MouseDown
   ysto(1) = ys
   xsto(2) = xsto(1) 'xp
   ysto(2) = ysto(1) 'yp
End Sub

Public Sub StartBoxes(picDRAW As PictureBox, x As Single, y As Single)
Dim k As Long
   If NClicks <> 1 Then Exit Sub
   Select Case BoxTool
   Case [BSolid] To [BGradient6]   ' All start with simple box apart from [BDoubleShadowed]
      StartTwoCoords
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B
   End Select
   
   Select Case BoxTool
   Case [BDouble]
      zspace = NumPoints + PixelSize ' 3 * PixelSize
      'If PixelSize > 1 Then zspace = zspace + PixelSize
      If aAA And NumPoints <= 2 Then zspace = zspace + 1 '0.5
      GetBoxMoveCoords
      picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul, B
   
   Case [BDoubleShadowed]
      pxsv = PixelSize
      svAA = aAA
      svRound = aRound
      PixelSize = 1
      aAA = False
      picDRAW.DrawWidth = 1
      StartTwoCoords
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B
      zspace = NumPoints + PixelSize ' 3 * PixelSize
      If aAA And NumPoints <= 2 Then zspace = zspace + 0.5
      GetBoxMoveCoords
      picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul, B
   
   
   Case [BAngBox], [BAngBoxFilled], [BAngBoxGradient]
      PointCount = 4
      For k = 1 To PointCount
         xsto(k) = x
         ysto(k) = y
      Next k
      DrawAngBox picDRAW, XorCul
   Case [BoxTop], [BoxRight], [BoxRounded]
      StartTwoCoords
      DrawPartBox picDRAW, XorCul
   End Select
End Sub

Public Sub StartCirlipses(picDRAW As PictureBox, x As Single, y As Single)
aClearOrigOval = False
   If NClicks = 1 Then
      StartTwoCoords       ' x/ysto(1)=x/ys  x/ysto(2)=x/yp
      
      Select Case CircTool
      Case [CSolid], [CDotted], [CGradient1] To [CConcentric] '[CRotateFill]
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio
      Case [CDouble]
         zspace = NumPoints + PixelSize / 2
         If aAA And NumPoints <= 2 Then zspace = zspace + 0.125
            
' TEST
'Form1.Label3 = Str$(zspace)
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
         EvalCirlipse xsto(1), ysto(1), xp, yp, zspace
         zRadDiff = zrad + zspace
         If zRadDiff < 0 Then zRadDiff = 0
         picDRAW.Circle (xsto(1), ysto(1)), zRadDiff, XorCul, , , zratio ' Clear
      End Select
   
   ElseIf NClicks = 2 Then
      Select Case CircTool
      Case [COffsetCen], [CRotate], [CRotateFill], [CConcentric] ' Start line
         xsto(2) = x
         ysto(2) = y
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
      End Select
   ElseIf NClicks = 3 Then  ' Move
   aCirc = False
   ElseIf NClicks = 4 Then  ' Complete
   End If
End Sub

Public Sub StartArcs(picDRAW As PictureBox, x As Single, y As Single)
   If NClicks <> 1 Then Exit Sub
   StartTwoCoords       ' x/ysto(1)=x/ys  x/ysto(2)=x/yp
   Select Case ArcTool
   Case [Arc1] To [Arc16]
      EvalCirlipse xsto(1), ysto(1), xp, yp
      picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, vbzAS, vbzAE, zratio
   End Select
   aArc = False
End Sub

Public Sub StartPolyLines(picDRAW As PictureBox, Button As Integer, x As Single, y As Single)
   If NClicks = 1 Then ' Start new polyline Draw
      aEnd = False
      aComplete = False
      StartTwoCoords
      PointCount = 1
      Select Case PolyLineTool
      Case [PLSolid], [PLConnect], [PLParagram], [PLFillTri], [PLFillPoly]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul
      
      Case [PLDouble], [PLDoubleEnded], [PLDoubleConnect], [PLDoubleParagram]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul
         zspace = NumPoints + PixelSize
         If aAA And NumPoints <= 2 Then zspace = zspace + 1
         '  GetParallelCoords (IN) zspace,X1,Y1,X2,Y2, (OUT)ixa,iya,ixb,iyb
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
                        
         If zspace <= 3 Then
         If PolyLineTool = [PLDoubleConnect] Or PolyLineTool = [PLDoubleParagram] Then
            zspace = zspace + 0.5
         End If
         End If
         
      Case [PLGradient1]
         picDRAW.DrawWidth = PixelSize
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul
      End Select
   
   ElseIf Button = StartClick And aEnd = False Then ' DRAW NEXT SEGMENT
      PointCount = PointCount + 1
      If PointCount > UBound(xsto()) Then
         ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
      End If
      xsto(PointCount) = xp
      ysto(PointCount) = yp
      
      Select Case PolyLineTool
      Case [PLSolid], [PLConnect], [PLParagram], [PLFillTri], [PLFillPoly] ', [PLDoubleParagram]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
         
         ' PLP  XDone
         If PolyLineTool = [PLParagram] And NClicks = 3 Then ' Finished [PLParagram] now move
            PointCount = PointCount + 2
            xsto(4) = xsto(1) - (xsto(2) - xsto(3))
            ysto(4) = ysto(1) + (ysto(3) - ysto(2))
            xsto(5) = xsto(1)
            ysto(5) = ysto(1)
            picDRAW.Line (xsto(3), ysto(3))-(xsto(3), ysto(3)), XorCul  ' Draw out Dot
            picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), XorCul  ' Draw
            picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul  ' Draw
            aEnd = True
         End If
         'PLDP  XDone
         
         
         If PolyLineTool = [PLFillTri] And NClicks = 3 Then ' Finished [PLFilled Tri] outline now move
            PointCount = PointCount + 1
            xsto(4) = xsto(1) '- (xsto(2) - xsto(3))
            ysto(4) = ysto(1) '+ (ysto(3) - ysto(2))
            picDRAW.Line (xsto(3), ysto(3))-(xsto(3), ysto(3)), XorCul  ' Draw out Dot
            picDRAW.Line (xsto(4), ysto(4))-(xsto(4), ysto(4)), XorCul  ' Draw out Dot
            picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), XorCul  ' Draw last line
            aEnd = True
         End If
         
      
      Case [PLDoubleParagram]
         If NClicks = 3 Then  ' Finished [PLDoubleParagram] now move
            PointCount = PointCount + 1
            xsto(4) = xsto(1) - (xsto(2) - xsto(3))
            ysto(4) = ysto(1) + (ysto(3) - ysto(2))
            
            picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), XorCul  ' Draw connecting lines
            GetParallelCoords zspace, xsto(3), ysto(3), xsto(4), ysto(4), _
                  ixa, iya, ixb, iyb
            picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
            
            picDRAW.Line (xsto(4), ysto(4))-(xsto(1), ysto(1)), XorCul  ' Draw connecting lines
            GetParallelCoords zspace, xsto(4), ysto(4), xsto(1), ysto(1), _
                  ixa, iya, ixb, iyb
            picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
            aEnd = True
         End If

      Case [PLDouble], [PLDoubleEnded], [PLDoubleConnect]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      
      Case [PLGradient1]
         'picDRAW.DrawWidth = 2 * PixelSize
         picDRAW.DrawWidth = PixelSize
         
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
      End Select
   
   ElseIf (Button <> StartClick And aEnd = False) Then
      aEnd = True
      PointCount = PointCount + 1  ' Last point
      If PointCount > UBound(xsto()) Then
         ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
      End If
      xsto(PointCount) = xp
      ysto(PointCount) = yp
      
      If PolyLineTool = [PLConnect] Or PolyLineTool = [PLFillPoly] Then
         If NClicks > 2 Then
         picDRAW.Line (xsto(1), ysto(1))-(xsto(PointCount), ysto(PointCount)), XorCul  ' Draw connect line
         End If
      End If
      
      ' PLP XDone
      If PolyLineTool = [PLParagram] And NClicks = 3 Then
         PointCount = PointCount + 2
         xsto(4) = xsto(1) - (xsto(2) - xsto(3))
         ysto(4) = ysto(1) + (ysto(3) - ysto(2))
         xsto(5) = xsto(1)
         ysto(5) = ysto(1)
         'picDRAW.Line (xsto(3), ysto(3))-(xsto(3), ysto(3)), XorCul  ' Draw out Dot
         picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), XorCul  ' Draw connecting lines
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul  '
         aEnd = True
      End If
      
         If PolyLineTool = [PLFillTri] And NClicks = 3 Then ' Finished [PLFilled Tri] outline now move
            PointCount = PointCount + 1
            xsto(4) = xsto(1) '- (xsto(2) - xsto(3))
            ysto(4) = ysto(1) '+ (ysto(3) - ysto(2))
            picDRAW.Line (xsto(4), ysto(4))-(xsto(4), ysto(4)), XorCul  ' Draw out Dot
            picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), XorCul  ' Draw
            aEnd = True
         End If
      
      
      ' PLDC XDone
      If PolyLineTool = [PLDoubleConnect] And NClicks > 2 Then
         picDRAW.Line (xsto(1), ysto(1))-(xsto(PointCount), ysto(PointCount)), XorCul  ' Draw connecting lines
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xsto(1), ysto(1), _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         aEnd = True
      End If
      
      ' PLDP XDone
      If PolyLineTool = [PLDoubleParagram] And NClicks > 2 Then
         PointCount = PointCount + 1
         xsto(4) = xsto(1) - (xsto(2) - xsto(3))
         ysto(4) = ysto(1) + (ysto(3) - ysto(2))
         
         picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), XorCul  ' Draw connecting lines
         GetParallelCoords zspace, xsto(3), ysto(3), xsto(4), ysto(4), _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         
         picDRAW.Line (xsto(4), ysto(4))-(xsto(1), ysto(1)), XorCul  ' Draw connecting lines
         GetParallelCoords zspace, xsto(4), ysto(4), xsto(1), ysto(1), _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         aEnd = True
      End If
   
   ElseIf aEnd = True Then ' ElseIf (Button <> StartClick And aEnd = False) Then
      aComplete = True
   
   End If
End Sub

Public Sub StartCurvyLines(picDRAW As PictureBox, Button As Integer, x As Single, y As Single)
   If NClicks = 1 Then ' Start new polyline Draw
      aEnd = False
      aComplete = False
      StartTwoCoords
      PointCount = 1
      Select Case CurvyLineTool
      Case [CLSolid]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul
      Case [CLDouble], [CLDoubleEnded]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul
         zspace = NumPoints + PixelSize
         If aAA And NumPoints <= 2 Then zspace = zspace + 1
         '  GetParallelCoords (IN) zspace,X1,Y1,X2,Y2, (OUT)ixa,iya,ixb,iyb
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      Case [CLGradient1]
         'picDRAW.DrawWidth = 2 * PixelSize
         picDRAW.DrawWidth = PixelSize
         
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul
      End Select
   ElseIf Button = StartClick And aEnd = False Then ' Draw Next segment
      PointCount = PointCount + 1
      If PointCount > UBound(xsto()) Then
         ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
      End If
      xsto(PointCount) = xp
      ysto(PointCount) = yp
      
      Select Case CurvyLineTool
      Case [CLSolid]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
      Case [CLDouble], [CLDoubleEnded]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         
      Case [CLGradient1]
         'picDRAW.DrawWidth = 2 * PixelSize
         picDRAW.DrawWidth = PixelSize
         
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
         
      End Select
   
   ElseIf Button <> StartClick And aEnd = False Then  ' End now Move
      aEnd = True
      PointCount = PointCount + 1  ' Last point
      If PointCount > UBound(xsto()) Then
         ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
      End If
      xsto(PointCount) = x
      ysto(PointCount) = y
      
   ElseIf aEnd = True Then ' Complete
      aComplete = True
   
   End If
End Sub

Public Sub StartArrows(picDRAW As PictureBox, x As Single, y As Single, Optional aRRow As Boolean = True)
'Private zarrlen As Single
'Private zarrang As Single
   If NClicks <> 1 Then Exit Sub
   zarrlen = 10 + 3 * PixelSize
   zarrang = 0.333
   StartTwoCoords
   DrawArrow picDRAW, CInt(x), CInt(y), xp, yp, XorCul, aRRow
End Sub

Public Sub StartJunctions(picDRAW As PictureBox, x As Single, y As Single)
   If NClicks <> 1 Then Exit Sub
   zJuncLen = 10 + PixelSize ' Default side length
   zspace = NumPoints + PixelSize
   If aAA And NumPoints <= 2 Then zspace = zspace + 1
   StartTwoCoords
   DrawJunction picDRAW, CInt(x), CInt(y), xp, yp, XorCul
End Sub

Public Sub StartTubulars(picDRAW As PictureBox, x As Single, y As Single)

If Tool = Tubulars Then
   Select Case TubularTool
   Case [CONE2] To [CONE5], [TUBE2], [TUBE3], [BULLET2], [BULLET3]
      PixelSize = 1
      Form1.cboPixelSize.ListIndex = 0
      pxsvORG = 1
   End Select
End If


   aClearOrigOval = False
   
   If NClicks = 0 Then Exit Sub
   
   If NClicks = 1 Then
      StartTwoCoords       ' x/ysto(1)=x/ys  x/ysto(2)=x/yp
      EvalCirlipse xsto(1), ysto(1), xp, yp
      xp = x
      yp = y
   ElseIf NClicks = 2 Then ' Move
      xsto(2) = x
      ysto(2) = y
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
   ElseIf NClicks = 3 Then  ' Move
      aCirc = False
   ElseIf NClicks = 4 Then  ' Complete
   End If
End Sub

Public Sub StartShape(picDRAW As PictureBox, x As Single, y As Single)
   If NClicks = 0 Then Exit Sub
   If aProject Then
      StartShape3D picDRAW, x, y
      Exit Sub
   End If
   If NClicks = 1 Then
      StartTwoCoords       ' x/ysto(1)=x/ys  x/ysto(2)=x/yp
      xsto(3) = xsto(1)
      ysto(3) = ysto(1)
      xsto(4) = xsto(1)
      ysto(4) = ysto(1)
      xsto(5) = xsto(1)
      ysto(5) = ysto(1)
      PointCount = 5
      If ShapeTool = [Hexagon] Or ShapeTool = [Hexagon45] Then
         PointCount = 7
         xsto(6) = xsto(1)
         ysto(6) = ysto(1)
         xsto(7) = xsto(1)
         ysto(7) = ysto(1)
         xsto(8) = xsto(1)
         ysto(8) = ysto(1)
      End If
   ElseIf NClicks = 2 Then ' Move shape
   ElseIf NClicks = 3 Then ' Complete shape
   End If
End Sub

Public Sub StartShape3D(picDRAW As PictureBox, x As Single, y As Single)
Dim k As Long
   If NClicks = 1 Then
      StartTwoCoords       ' x/ysto(1)=x/ys  x/ysto(2)=x/yp
      xsto(3) = xsto(1)
      ysto(3) = ysto(1)
      xsto(4) = xsto(1)
      ysto(4) = ysto(1)
      xsto(5) = xsto(1)
      ysto(5) = ysto(1)
      PointCount = 5
      If ShapeTool = [Hexagon] Or ShapeTool = [Hexagon45] Then
         PointCount = 7
         xsto(6) = xsto(1)
         ysto(6) = ysto(1)
         xsto(7) = xsto(1)
         ysto(7) = ysto(1)
         xsto(8) = xsto(1)
         ysto(8) = ysto(1)
      ElseIf ShapeTool = [Triangle] Then
         PointCount = 5
      End If
   ElseIf NClicks = 2 Then ' Start line
      
      For k = 15 To 20
         xsto(k) = xsto(k - 14)
         ysto(k) = ysto(k - 14)
      Next k
      
      Select Case ShapeTool
      Case [BWedge], [FWedge], [Frustrum], [Square], [Paragram45]
         For k = 15 To 18
            picDRAW.Line (xsto(k - 14), ysto(k - 14))-(xsto(k), ysto(k)), XorCul
         Next k
      Case [Triangle]
         For k = 15 To 17
            picDRAW.Line (xsto(k - 14), ysto(k - 14))-(xsto(k), ysto(k)), XorCul
         Next k
      Case [Hexagon], [Hexagon45]
         For k = 15 To 20
            picDRAW.Line (xsto(k - 13), ysto(k - 13))-(xsto(k), ysto(k)), XorCul
         Next k
      End Select
   ElseIf NClicks = 3 Then ' Move 3D shape
   ElseIf NClicks = 4 Then ' Complete 3D shape
   End If
End Sub

Public Sub StartRadials(picDRAW As PictureBox, x As Single, y As Single)
Dim k As Long
Dim dx As Single, DY As Single

   If NClicks <> 1 Then Exit Sub
   If NClicks = 1 Then
      StartTwoCoords       ' x/ysto(1)=x/ys  x/ysto(2)=x/yp
      Select Case RadialTool
      Case [Spokes], [CSpokes]
         PointCount = NumPoints + 1 ' NumPoints public set in Form1
      Case [Stars], [CStars]
         PointCount = 2 * NumPoints + 1
      Case [Circles]
         PointCount = NumPoints + 1
         If (NumPoints And 1) = 1 Then   ' ODD  Avoiding spurious dot
            picDRAW.Circle (xsto(1), ysto(1)), 0, XorCul
         End If
      Case [Polygons], [CPolygons]
         PointCount = NumPoints + 1
         If (NumPoints And 1) = 1 Then   ' ODD  Avoiding spurious dot
            picDRAW.Line (xsto(1), ysto(1))-(xsto(1), ysto(1)), XorCul
         End If
      Case [Teeth]
         PointCount = 2 * NumPoints + 1
      Case [OddRoses], [EvenRoses]
         PointCount = NumPoints + 1
         xsto(2) = x ' Draw
         ysto(2) = y
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         For k = 0 To 360 Step 1 ' Draw
            zangle = k * 180 / pi#
            zR = zrad * Cos(NumPoints * zangle)
            px = xsto(1) + zR * Cos(zangle)
            py = ysto(1) + zR * Sin(zangle)
            picDRAW.PSet (px, py), XorCul
         Next k
      Case [Hypocycloid]
         xsto(2) = x ' Draw
         ysto(2) = y
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         For k = 0 To 360 Step 1 ' Clear
            zangle = k * 180 / pi#
            px = xsto(1) + zrad * Cos(zangle) ^ NumPoints
            py = ysto(1) + zrad * Sin(zangle) ^ NumPoints
            picDRAW.PSet (px, py), XorCul
         Next k
         
      Case [Spiral]
         ' For Xor
         pxsv = PixelSize
         StartTwoCoords       ' x/ysto(1)=x/ys  x/ysto(2)=x/yp
         zrad = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
         DrawSpiral picDRAW
      
      End Select
      
      If PointCount > 1 Then
         zangle = 2 * pi# / (PointCount - 1)
         ReDim xsto(1 To PointCount), ysto(1 To PointCount)
         For k = 1 To PointCount
            xsto(k) = x: ysto(k) = y
         Next k
      End If
      zrad = 0
      zrad2 = 0
      
      Select Case RadialTool
      Case [CSpokes], [CPolygons], [CStars]
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul ''''''''
      End Select

   ElseIf NClicks = 2 Then ' Draw move
   ElseIf NClicks = 3 Then
      'CompleteRadials
   End If
End Sub

Public Sub StartCircuits(picDRAW As PictureBox, x As Single, y As Single)
   If NClicks <> 1 Then Exit Sub
   Select Case CircuitTool
   Case [Capacitor], [Battery], [CDiode]
      PointCount = 8    ' ---||---
      ReDim xsto(1 To PointCount), ysto(1 To PointCount)
   Case [Inductor], [Resistor], [CBox], [CLine]
      PointCount = 2
      ReDim xsto(1 To PointCount), ysto(1 To PointCount)
   Case [Switch]
      PointCount = 5    ' --   ---
   Case [Earth]
      PointCount = 11
      xsto(11) = x
      ysto(11) = y
   End Select

   xsto(1) = x
   ysto(1) = y
   xs = x
   ys = y
   xp = x
   yp = y
   xsto(2) = xp
   ysto(2) = yp
   DrawCircuitComp picDRAW, x, y, XorCul
End Sub

Public Sub StartWaves(picDRAW As PictureBox, x As Single, y As Single)
   If NClicks <> 1 Then Exit Sub
   Select Case WaveTool
   Case [SineWave], [Rectified]
      StartTwoCoords
      DrawSineWave picDRAW, XorCul
   Case [SquareWave]
      StartTwoCoords
      DrawSquareWave picDRAW, XorCul
   Case [Sawtooth1]
      StartTwoCoords
      DrawSawtooth1 picDRAW, XorCul
   Case [Sawtooth2]
      StartTwoCoords
      DrawSawtooth2 picDRAW, XorCul
   Case [Sawtooth3]
      StartTwoCoords
      DrawSawtooth3 picDRAW, XorCul
   Case [CircWaves]
      StartTwoCoords
      DrawCircWaves picDRAW, XorCul
   End Select
End Sub

'#### DRAW MOVE ###########################################################################

' NB Lines like
'   If TheKey = vbKeySpace Then
'      picDRAW.PSet (xsto(k), ysto(k)), XorCul  ' Clear
'   TheKey = 0
'   End If
' are to counteract an XorCul draw on the last completed shape
' when a copy is being done using the Space bar.

Public Sub DrawMoveSingles(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Long, DY As Long, k As Long
   Select Case SingleTool
   Case [DSingle] ' No moving for single dot !?
   
   Case [SprayDots]
      If NClicks = 1 Then
         For k = 1 To PointCount - 1
            picDRAW.PSet (xsto(k), ysto(k)), XorCul  ' Clear
         Next k
         
         If TheKey = vbKeySpace Then
            For k = 1 To PointCount - 1
               picDRAW.PSet (xsto(k), ysto(k)), XorCul  ' XDraw
            Next k
            TheKey = 0
         End If
         
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount - 1
            xsto(k) = xsto(k) + dx     ' Bump
            ysto(k) = ysto(k) + DY
         Next k
         
         
         For k = 1 To PointCount - 1   ' Draw @ new position
            picDRAW.PSet (xsto(k), ysto(k)), XorCul
         Next k
         xp = x
         yp = y
      End If
      
   Case [Grass]
      If NClicks = 1 Then
         picDRAW.Line (xsto(1), ysto(1))-(xsto(1), ysto(1)), XorCul ' Clear
         For k = 1 To PointCount
            picDRAW.Line (xsto(1), ysto(1))-(xsto(k), ysto(k)), XorCul ' Clear
         Next k
         
         If TheKey = vbKeySpace Then
            picDRAW.Line (xsto(1), ysto(1))-(xsto(1), ysto(1)), XorCul ' XDraw
            For k = 1 To PointCount
               picDRAW.Line (xsto(1), ysto(1))-(xsto(k), ysto(k)), XorCul ' XDraw
            Next k
            TheKey = 0
         End If
         ' Draw at new position
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx     ' Bump
            ysto(k) = ysto(k) + DY
         Next k
         picDRAW.Line (xsto(1), ysto(1))-(xsto(1), ysto(1)), XorCul ' Clear
         For k = 1 To PointCount
            picDRAW.Line (xsto(1), ysto(1))-(xsto(k), ysto(k)), XorCul ' Draw
         Next k
         xp = x
         yp = y
      End If
   Case [FBush1] To [FBush7]
      If NClicks = 1 Then
         ModExtraTools.MoveBush picDRAW, x, y
         xp = x
         yp = y
      End If
   
   Case [DStamp]
      'xsto(1) = x
      'ysto(1) = y
      If NClicks = 1 Then
         
         picDRAW.DrawWidth = 1

         picDRAW.Line (xsto(1), ysto(1))-(xsto(1) + StampW - 1, ysto(1) + StampH - 1), XorCul, B
         
         If TheKey = vbKeySpace Then
            picDRAW.DrawWidth = 1
            picDRAW.Line (xsto(1), ysto(1))-(xsto(1) + StampW - 1, ysto(1) + StampH - 1), XorCul, B
         TheKey = 0
         End If
         ' Draw at new position
         dx = x - xp
         DY = y - yp
         xsto(1) = xsto(1) + dx     ' Bump
         ysto(1) = ysto(1) + DY
         picDRAW.Line (xsto(1), ysto(1))-(xsto(1) + StampW - 1, ysto(1) + StampH - 1), XorCul, B
         xp = x
         yp = y
      End If
   
   End Select
End Sub

Public Sub DrawMoveFreeDraw(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Long, DY As Long, k As Long
Dim ix As Long, iy As Long
Dim DL As Single
   
   Select Case FreeDrawTool
   Case [FDraw], [FBeads], [FBlocks], [FBubbles], [FBroken]
      If NClicks = 1 Then  ' Draw
         xsto(PointCount) = x
         ysto(PointCount) = y
         picDRAW.Line -(xsto(PointCount), ysto(PointCount)), XorCul
         PointCount = PointCount + 1
         If PointCount > UBound(xsto()) Then
            ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
         End If
      ElseIf NClicks = 2 Then ' Move
         ' Clear old
         picDRAW.PSet (xsto(1), ysto(1)), XorCul
         For k = 2 To PointCount - 1
            picDRAW.Line -(xsto(k), ysto(k)), XorCul
         Next k
         If TheKey = vbKeySpace Then
            picDRAW.PSet (xsto(1), ysto(1)), XorCul
            For k = 2 To PointCount - 1
               picDRAW.Line -(xsto(k), ysto(k)), XorCul
            Next k
            TheKey = 0
         End If
         ' Draw new
         dx = x - xp
         DY = y - yp
         xsto(1) = xsto(1) + dx
         ysto(1) = ysto(1) + DY
         picDRAW.PSet (xsto(1), ysto(1)), XorCul
         For k = 2 To PointCount - 1
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
            picDRAW.Line -(xsto(k), ysto(k)), XorCul
         Next k
         xp = x
         yp = y
      End If
      
   Case [FStalks] ' Up stalks on branch
      picDRAW.DrawWidth = 1
      If NClicks = 1 Then  ' Draw
         xsto(PointCount) = x
         ysto(PointCount) = y
         picDRAW.Line -(xsto(PointCount), ysto(PointCount)), XorCul
         PointCount = PointCount + 1
         If PointCount > UBound(xsto()) Then
            ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
         End If
         zyscale = (Rnd(-2) - 0.5) * 16
         zxscale = Rnd * 6 - 3
         picDRAW.Line (x, y)-(x + 1 * zxscale, y - 1 * zyscale), XorCul
         picDRAW.Line -Step(1 * zxscale, -2 * zyscale), XorCul
         picDRAW.Line -Step(0 * zxscale, -1 * zyscale), XorCul
         picDRAW.PSet (x, y), XorCul
      ElseIf NClicks = 2 Then ' Move (clear old, bump points, draw new)
         zyscale = (Rnd(-2) - 0.5) * 16
         zxscale = Rnd * 6 - 3
         ' Clear old
         picDRAW.PSet (xsto(1), ysto(1)), XorCul
         For k = 2 To PointCount - 1
            picDRAW.Line -(xsto(k), ysto(k)), XorCul
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + 1 * zxscale, ysto(k) - 1 * zyscale), XorCul
            picDRAW.Line -Step(1 * zxscale, -2 * zyscale), XorCul
            picDRAW.Line -Step(0 * zxscale, -1 * zyscale), XorCul
            picDRAW.PSet (xsto(k), ysto(k)), XorCul
         Next k
         
         If TheKey = vbKeySpace Then
            picDRAW.PSet (xsto(1), ysto(1)), XorCul
            For k = 2 To PointCount - 1
               picDRAW.Line -(xsto(k), ysto(k)), XorCul
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + 1 * zxscale, ysto(k) - 1 * zyscale), XorCul
               picDRAW.Line -Step(1 * zxscale, -2 * zyscale), XorCul
               picDRAW.Line -Step(0 * zxscale, -1 * zyscale), XorCul
               picDRAW.PSet (xsto(k), ysto(k)), XorCul
            Next k
            TheKey = 0
         End If
         
         ' Draw new
         dx = x - xp
         DY = y - yp
         xsto(1) = xsto(1) + dx
         ysto(1) = ysto(1) + DY
         picDRAW.PSet (xsto(1), ysto(1)), XorCul
         zyscale = (Rnd(-2) - 0.5) * 16
         zxscale = Rnd * 6 - 3
         For k = 2 To PointCount - 1
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
            picDRAW.Line -(xsto(k), ysto(k)), XorCul
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + 1 * zxscale, ysto(k) - 1 * zyscale), XorCul
            picDRAW.Line -Step(1 * zxscale, -2 * zyscale), XorCul
            picDRAW.Line -Step(0 * zxscale, -1 * zyscale), XorCul
            picDRAW.PSet (xsto(k), ysto(k)), XorCul
         Next k
         xp = x
         yp = y
      End If
      
   Case [FLeaves]  ' Leaves on branch
      picDRAW.DrawWidth = 1
      If NClicks = 1 Then  ' Draw
         xsto(PointCount) = x
         ysto(PointCount) = y
         picDRAW.Line -(xsto(PointCount), ysto(PointCount)), XorCul
         PointCount = PointCount + 1
         If PointCount > UBound(xsto()) Then
            ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
         End If
         zxscale = Rnd(-2) * 4 - 2
         picDRAW.Line (x, y)-(x + 3 * zxscale, y - 1), XorCul
         picDRAW.Line -Step(1 * zxscale, 2 * zxscale), XorCul
         picDRAW.Line -Step(1 * zxscale, 2 * zxscale), XorCul
         picDRAW.Line -Step(-2 * zxscale, -1 * zxscale), XorCul
         picDRAW.Line -Step(-1 * zxscale, -1 * zxscale), XorCul
         picDRAW.Line -Step(-1 * zxscale, -1 * zxscale), XorCul
         picDRAW.Line -Step(-1 * zxscale, -2 * zxscale), XorCul
         picDRAW.PSet (x, y), XorCul
      ElseIf NClicks = 2 Then ' Move (clear old, bump points, draw new)
         zxscale = Rnd(-2) * 4 - 2
         ' Clear old
         picDRAW.PSet (xsto(1), ysto(1)), XorCul
         For k = 2 To PointCount - 1
            picDRAW.Line -(xsto(k), ysto(k)), XorCul
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + 3 * zxscale, ysto(k) - 1), XorCul
            picDRAW.Line -Step(1 * zxscale, 2 * zxscale), XorCul
            picDRAW.Line -Step(1 * zxscale, 2 * zxscale), XorCul
            picDRAW.Line -Step(-2 * zxscale, -1 * zxscale), XorCul
            picDRAW.Line -Step(-1 * zxscale, -1 * zxscale), XorCul
            picDRAW.Line -Step(-1 * zxscale, -1 * zxscale), XorCul
            picDRAW.Line -Step(-1 * zxscale, -2 * zxscale), XorCul
            picDRAW.PSet (xsto(k), ysto(k)), XorCul
         Next k
         If TheKey = vbKeySpace Then
            picDRAW.PSet (xsto(1), ysto(1)), XorCul
            For k = 2 To PointCount - 1
               picDRAW.Line -(xsto(k), ysto(k)), XorCul
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + 3 * zxscale, ysto(k) - 1), XorCul
               picDRAW.Line -Step(1 * zxscale, 2 * zxscale), XorCul
               picDRAW.Line -Step(1 * zxscale, 2 * zxscale), XorCul
               picDRAW.Line -Step(-2 * zxscale, -1 * zxscale), XorCul
               picDRAW.Line -Step(-1 * zxscale, -1 * zxscale), XorCul
               picDRAW.Line -Step(-1 * zxscale, -1 * zxscale), XorCul
               picDRAW.Line -Step(-1 * zxscale, -2 * zxscale), XorCul
               picDRAW.PSet (xsto(k), ysto(k)), XorCul
            Next k
            TheKey = 0
         End If
         ' Draw new
         dx = x - xp
         DY = y - yp
         xsto(1) = xsto(1) + dx
         ysto(1) = ysto(1) + DY
         picDRAW.PSet (xsto(1), ysto(1)), XorCul
         zxscale = Rnd(-2) * 4 - 2
         For k = 2 To PointCount - 1
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
            picDRAW.Line -(xsto(k), ysto(k)), XorCul
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + 3 * zxscale, ysto(k) - 1), XorCul
            picDRAW.Line -Step(1 * zxscale, 2 * zxscale), XorCul
            picDRAW.Line -Step(1 * zxscale, 2 * zxscale), XorCul
            picDRAW.Line -Step(-2 * zxscale, -1 * zxscale), XorCul
            picDRAW.Line -Step(-1 * zxscale, -1 * zxscale), XorCul
            picDRAW.Line -Step(-1 * zxscale, -1 * zxscale), XorCul
            picDRAW.Line -Step(-1 * zxscale, -2 * zxscale), XorCul
            picDRAW.PSet (xsto(k), ysto(k)), XorCul
         Next k
         xp = x
         yp = y
      End If
'   Case [RibbonHorz], [RibbonHorzSolid]   ' ==
'   Case [RibbonVert], [RibbonVertSolid]   '||

   Case [FRibbon], [BRibbon], [FRibbonSolid], [BRibbonSolid], _
        [RibbonHorz], [RibbonHorzSolid], [RibbonVert], [RibbonVertSolid]
         If NClicks = 1 Then  ' Draw
            xsto(PointCount) = x
            ysto(PointCount) = y
            picDRAW.Line (xsto(PointCount) + rx1, ysto(PointCount) + ry1)- _
                   (xsto(PointCount) + rx2, ysto(PointCount) + ry2), XorCul
            PointCount = PointCount + 1
            If PointCount > UBound(xsto()) Then
               ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
            End If
         ElseIf NClicks = 2 Then ' Move
            ' Clear old
            picDRAW.DrawWidth = 1
            picDRAW.PSet (xsto(1), ysto(1)), XorCul
            For k = 2 To PointCount - 1
               picDRAW.Line (xsto(k) + rx1, ysto(k) + ry1)- _
                   (xsto(k) + rx2, ysto(k) + ry2), XorCul
            Next k
            If TheKey = vbKeySpace Then
               picDRAW.PSet (xsto(1), ysto(1)), XorCul
               For k = 2 To PointCount - 1
                  picDRAW.Line (xsto(k) + rx1, ysto(k) + ry1)- _
                      (xsto(k) + rx2, ysto(k) + ry2), XorCul
               Next k
               TheKey = 0
            End If
            
            ' Draw new
            dx = x - xp
            DY = y - yp
            xsto(1) = xsto(1) + dx
            ysto(1) = ysto(1) + DY
            picDRAW.PSet (xsto(1), ysto(1)), XorCul
            k = picDRAW.DrawWidth
            For k = 2 To PointCount - 1
               xsto(k) = xsto(k) + dx
               ysto(k) = ysto(k) + DY
               picDRAW.Line (xsto(k) + rx1, ysto(k) + ry1)- _
                   (xsto(k) + rx2, ysto(k) + ry2), XorCul
            Next k
            xp = x
            yp = y
         End If
         
      Case [FreeSpray]
         If NClicks = 1 Then
            For k = 1 To NumPoints
               ix = x - NumPoints * Rnd
               iy = y - NumPoints * Rnd
               
               xsto(PointCount) = ix
               ysto(PointCount) = iy
               picDRAW.PSet (ix, iy), XorCul
               PointCount = PointCount + 1
               If PointCount > UBound(xsto()) Then
                  ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
               End If
            Next k
         ElseIf NClicks = 2 Then ' Move
            ' Clear old
            For k = 1 To PointCount - 1
               picDRAW.PSet (xsto(k), ysto(k)), XorCul ' Clear
            Next k
            If TheKey = vbKeySpace Then
               For k = 1 To PointCount - 1
                  picDRAW.PSet (xsto(k), ysto(k)), XorCul ' Clear
               Next k
               TheKey = 0
            End If
            ' Draw new
            dx = x - xp
            DY = y - yp
            For k = 1 To PointCount - 1
               xsto(k) = xsto(k) + dx
               ysto(k) = ysto(k) + DY
               picDRAW.PSet (xsto(k), ysto(k)), XorCul ' Draw
            Next k
            xp = x
            yp = y
        End If
   Case [FBricks]
      If NClicks = 1 Then  ' Draw boundary
         xsto(2) = x
         ysto(2) = y
         DrawSelectBoundary
      ElseIf NClicks = 2 Then  ' Move boundary
         If TheKey = vbKeySpace Then
            xsto(2) = x
            ysto(2) = y
            DrawSelectBoundary
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         xsto(1) = xsto(1) + dx
         ysto(1) = ysto(1) + DY
         xsto(2) = xsto(2) + dx
         ysto(2) = ysto(2) + DY
         DrawSelectBoundary
         xp = x
         yp = y
     End If
      
   Case [FDotted]
      If NClicks = 1 Then  ' Draw
         xsto(PointCount) = x
         ysto(PointCount) = y
         picDRAW.PSet (xsto(PointCount), ysto(PointCount)), XorCul
         PointCount = PointCount + 1
         If PointCount > UBound(xsto()) Then
            ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
         End If
      
      ElseIf NClicks = 2 Then ' Move (clear old, bump points, draw new)
         If TheKey = vbKeySpace Then
            For k = 1 To PointCount - 1
               picDRAW.PSet (xsto(k), ysto(k)), XorCul  ' Draw
            Next k
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount - 1
            picDRAW.PSet (xsto(k), ysto(k)), XorCul  ' Clear
            xsto(k) = xsto(k) + dx     ' Bump
            ysto(k) = ysto(k) + DY
         Next k
         
         
         For k = 1 To PointCount - 1   ' Draw @ new position
            picDRAW.PSet (xsto(k), ysto(k)), XorCul
         Next k
         xp = x
         yp = y
      End If
   
   Case [FSpacedDots]
      
      If NClicks = 1 Then  ' Draw
      
         If PointCount > 1 Then
            DL = Sqr((xsto(PointCount - 1) - x) ^ 2 _
                  + (ysto(PointCount - 1) - y) ^ 2)
            If DL >= NumPoints Then '2 * PixelSize Then
               xsto(PointCount) = x
               ysto(PointCount) = y
               picDRAW.PSet (xsto(PointCount), ysto(PointCount)), XorCul
               PointCount = PointCount + 1
               If PointCount > UBound(xsto()) Then
                  ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
               End If
            End If
         End If
      
      ElseIf NClicks = 2 Then ' Move (clear old, bump points, draw new)
         If TheKey = vbKeySpace Then
            For k = 1 To PointCount - 1
               picDRAW.PSet (xsto(k), ysto(k)), XorCul  ' Draw
            Next k
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount - 1
            picDRAW.PSet (xsto(k), ysto(k)), XorCul  ' Clear
            xsto(k) = xsto(k) + dx     ' Bump
            ysto(k) = ysto(k) + DY
         Next k
         For k = 1 To PointCount - 1   ' Draw @ new position
            picDRAW.PSet (xsto(k), ysto(k)), XorCul
         Next k
         xp = x
         yp = y
      End If
   
   
   Case [FCustom]
      
      If NClicks = 1 Then  ' Draw
      
         If PointCount > 1 Then
            picDRAW.DrawWidth = 1
            DL = Sqr((xsto(PointCount - 1) - x) ^ 2 _
                  + (ysto(PointCount - 1) - y) ^ 2)
            If DL >= NumPoints Then '3 * PixelSize Then
               xsto(PointCount) = x
               ysto(PointCount) = y
               picDRAW.Line (x, y)-(x + StampW - 1, y + StampH - 1), XorCul, B
               PointCount = PointCount + 1
               If PointCount > UBound(xsto()) Then
                  ReDim Preserve xsto(1 To PointCount + 10), ysto(1 To PointCount + 10)
               End If
            End If
         End If
      
      ElseIf NClicks = 2 Then ' Move (clear old, bump points, draw new)
         If TheKey = vbKeySpace Then  ' ? for IconBrush
            picDRAW.DrawWidth = 1
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + StampW - 1, ysto(k) + StampH - 1), XorCul, B
            Next k
            TheKey = 0
         End If
         
         dx = x - xp
         DY = y - yp
         picDRAW.DrawWidth = 1
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + StampW - 1, ysto(k) + StampH - 1), XorCul, B
            xsto(k) = xsto(k) + dx     ' Bump
            ysto(k) = ysto(k) + DY
         Next k
         
         picDRAW.DrawWidth = 1
         For k = 1 To PointCount - 1   ' Draw @ new position
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + StampW - 1, ysto(k) + StampH - 1), XorCul, B
         Next k
         xp = x
         yp = y
      End If
   
   End Select
End Sub

Public Sub DrawMoveALines(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Long, DY As Long, k As Long
   ShowAngleLen x, y
   
   Select Case LineTool
   Case [LGradient]
      picDRAW.DrawWidth = PixelSize
   End Select
   
   Select Case LineTool
   Case [LSolid], [LDotted], [LGradient], [LBEZ], [LBEZFilled], [LAGradient]
      If NClicks = 1 Then  ' Draw
         picDRAW.Line (xsto(1), ysto(1))-(xp, yp), XorCul  ' Clear
         xp = x
         yp = y
         xsto(2) = x
         ysto(2) = y
         picDRAW.Line (xsto(1), ysto(1))-(xp, yp), XorCul  ' Draw
      
      ElseIf NClicks = 2 Then ' Move
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
         
         If TheKey = vbKeySpace Then
            picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
            TheKey = 0
         End If
         
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
         xp = x
         yp = y
      End If
   
   Case [LDouble], [LDoubleEnded]
      If NClicks = 1 Then
         ALineMover picDRAW
         xp = x
         yp = y
         xsto(2) = x
         ysto(2) = y
         ALineMover picDRAW
      ElseIf NClicks = 2 Then ' Move
         ALineMover picDRAW
         If TheKey = vbKeySpace Then
            ALineMover picDRAW
            TheKey = 0
         End If
         
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         ALineMover picDRAW
         xp = x
         yp = y
      End If
   
   End Select
End Sub

Private Sub ALineMover(picDRAW As PictureBox)
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul ' Draw
      GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), _
         ixa, iya, ixb, iyb
      picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
End Sub

Public Sub DrawMoveBoxes(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Long, DY As Long, k As Long
   ShowAngleLen x, y

   Select Case BoxTool
   Case [BSolid], [BDotted], [BGradient1] To [BGradient6] ' Leave out [BDouble1-3]
      
      If NClicks = 1 Then  ' Draw
         picDRAW.Line (xsto(1), ysto(1))-(xp, yp), XorCul, B ' Clear
         xp = x
         yp = y
         xsto(2) = x
         ysto(2) = y
         picDRAW.Line (xsto(1), ysto(1))-(xp, yp), XorCul, B ' Draw
      ElseIf NClicks = 2 Then ' Move
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B 'Clear
         If TheKey = vbKeySpace Then
            picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B
         TheKey = 0
         End If
         
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B
         xp = x
         yp = y
      End If
   Case [BDouble], [BDoubleShadowed]
      If NClicks = 1 Then
         picDRAW.Line (xsto(1), ysto(1))-(xp, yp), XorCul, B ' Clear
         GetBoxMoveCoords   '  ixa = xsto(1) + zspace etc
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul, B
         
         xp = x
         yp = y
         xsto(2) = x
         ysto(2) = y
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B ' Draw
         GetBoxMoveCoords
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul, B
      ElseIf NClicks = 2 Then ' Move
         If TheKey = vbKeySpace Then
            zspace = NumPoints + PixelSize ' 3 * PixelSize
            If aAA And NumPoints <= 2 Then zspace = zspace + 0.5
         End If
         
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B ' Clear
         GetBoxMoveCoords
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul, B
         If TheKey = vbKeySpace Then
            picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B ' Clear
            GetBoxMoveCoords
            picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul, B
         TheKey = 0
         End If
         
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B ' Draw
         GetBoxMoveCoords
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul, B
         xp = x
         yp = y
      End If
      
   Case [BAngBox], [BAngBoxFilled], [BAngBoxGradient]
      If NClicks = 1 Then
         DrawAngBox picDRAW, XorCul ' Clear
         xp = x
         yp = y
         xsto(2) = xp
         ysto(2) = yp
         DrawAngBox picDRAW, XorCul ' Draw
      End If
      If NClicks = 2 Then
         DrawAngBox picDRAW, XorCul ' Clear
         If TheKey = vbKeySpace Then
            DrawAngBox picDRAW, XorCul ' Clear
         TheKey = 0
         End If
         dx = x - xsto(2)
         DY = y - ysto(2)
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         DrawAngBox picDRAW, XorCul  ' Draw
      End If
      
   Case [BoxTop], [BoxRight], [BoxRounded]
      If NClicks = 1 Then  ' Draw
         DrawPartBox picDRAW, XorCul  ' Clear
         xp = x
         yp = y
         xsto(2) = x
         ysto(2) = y
         DrawPartBox picDRAW, XorCul  ' Draw
      ElseIf NClicks = 2 Then ' Move
         DrawPartBox picDRAW, XorCul  ' Clear
         If TheKey = vbKeySpace Then
            DrawPartBox picDRAW, XorCul  ' Clear
         TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         DrawPartBox picDRAW, XorCul
         xp = x
         yp = y
      End If
      
   End Select
End Sub


Public Sub DrawMoveCirlipses(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Long, DY As Long, k As Long
   
   ShowAngleLen x, y
   
   Select Case CircTool
   Case [CSolid], [CDotted], [CGradient1] To [CConcentric] ' [CRotateFill]  ' NB Includes [COffsetCen]
      If NClicks = 1 Then  ' Draw
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
         xp = x
         yp = y
         xsto(2) = x
         ysto(2) = y
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio   ' Draw
      
      ElseIf NClicks = 2 Then ' Move
         Select Case CircTool
         Case [CSolid], [CDotted], [CGradient1] To [CFilled]  ' NB Includes [COffsetCen]
            EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2) ' Gets zradx,zrady from this
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
            If TheKey = vbKeySpace Then
               'EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
               picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
            TheKey = 0
            End If
            
            dx = x - xp
            DY = y - yp
            For k = 1 To 2
               xsto(k) = xsto(k) + dx
               ysto(k) = ysto(k) + DY
            Next k
            'EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Draw
            
         Case [COffsetCen]
   
            zangle = zATan2(y - CSng(ysto(1)), x - CSng(xsto(1)))
            Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
            
            picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul  ' Clear angline
            xp = x
            yp = y
            xsto(2) = x
            ysto(2) = y
            picDRAW.Line (xsto(1), ysto(1))-(x, y), XorCul  ' Draw angline
         
         Case [CRotate], [CRotateFill], [CConcentric]
            If Not aClearOrigOval Then
               picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear original oval
               RotEllipse picDRAW, XorCul, 0 ' Draw
               aClearOrigOval = True
            End If
            zangle = zATan2(y - CSng(ysto(1)), x - CSng(xsto(1)))
            'Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
            ShowAngleLen x, y
           
            RotEllipse picDRAW, XorCul, 0 ' Clear
            picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul  ' Clear angline
            
            xp = x
            yp = y
            xsto(2) = x
            ysto(2) = y
            picDRAW.Line (xsto(1), ysto(1))-(x, y), XorCul  ' Draw angline
            RotEllipse picDRAW, XorCul, 0 ' Draw
         
         End Select
         xp = x
         yp = y
      
      ElseIf NClicks = 3 Then ' Move zrad, zratio fixed
         Select Case CircTool
         Case [COffsetCen]
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
            picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
         If TheKey = vbKeySpace Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
            picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
         TheKey = 0
         End If
            dx = x - xp
            DY = y - yp
            For k = 1 To 2
               xsto(k) = xsto(k) + dx
               ysto(k) = ysto(k) + DY
            Next k
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Draw
            picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
         
         Case [CRotate], [CRotateFill], [CConcentric]
            If Not aCirc Then
               picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
               aCirc = True
            End If
            RotEllipse picDRAW, XorCul, 0 ' Clear
        
            If TheKey = vbKeySpace Then
               RotEllipse picDRAW, XorCul, 0
               TheKey = 0
            End If
            
         
            xp = xsto(2)  ' (1) Move ellipse to cursor
            yp = ysto(2)  ' (2) Leave ellipse at drawn place separated from mouse
            
            dx = x - xp
            DY = y - yp
            For k = 1 To 2
               xsto(k) = xsto(k) + dx
               ysto(k) = ysto(k) + DY
            Next k
            If Not aCirc Then
               picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Draw
               picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
            Else
               ' Rotate ellipse  & Draw
               RotEllipse picDRAW, XorCul, 0   ' Draw
            End If
         
         End Select
         
         xp = x
         yp = y
      
      End If
   
   Case [CDouble]
      If NClicks = 1 Then
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' XClear
         EvalCirlipse xsto(1), ysto(1), xp, yp, zspace
         zRadDiff = zrad + zspace
         If zRadDiff < 0 Then zRadDiff = 0
         picDRAW.Circle (xsto(1), ysto(1)), zRadDiff, XorCul, , , zratio ' XClear
         
         xp = x
         yp = y
         xsto(2) = x
         ysto(2) = y
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio   ' XDraw
         EvalCirlipse xsto(1), ysto(1), xp, yp, zspace
         zRadDiff = zrad + zspace
         If zRadDiff < 0 Then zRadDiff = 0
         picDRAW.Circle (xsto(1), ysto(1)), zRadDiff, XorCul, , , zratio ' XDraw
      ElseIf NClicks = 2 Then ' Move
         If TheKey = vbKeySpace Then
           zspace = NumPoints + PixelSize / 2
           If aAA And NumPoints <= 2 Then zspace = zspace + 0.125
         End If
         CirlipseMover picDRAW
         If TheKey = vbKeySpace Then
            CirlipseMover picDRAW
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         CirlipseMover picDRAW
         xp = x
         yp = y
      End If
   End Select
End Sub

Private Sub CirlipseMover(picDRAW As PictureBox)
   EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
   picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Draw
   EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2), zspace
   zRadDiff = zrad + zspace
   If zRadDiff < 0 Then zRadDiff = 0
   picDRAW.Circle (xsto(1), ysto(1)), zRadDiff, XorCul, , , zratio ' Draw
End Sub

Public Sub DrawMoveArcs(picDRAW As PictureBox, x As Single, y As Single)
' xp,yp set @ picDRAW_MouseDown
Dim dx As Long, DY As Long, k As Long
   
   ShowAngleLen x, y
 
   Select Case ArcTool
      Case [Arc1] To [Arc16]
      If NClicks = 1 Then  ' Draw
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, vbzAS, vbzAE, zratio
         xp = x
         yp = y
         xsto(2) = x
         ysto(2) = y
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, vbzAS, vbzAE, zratio  ' Draw
      
      ElseIf NClicks = 2 Then ' Move
         
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         ' Returns Public zrad,zratio,zradx,zrady
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, vbzAS, vbzAE, zratio   ' Clear
         If TheKey = vbKeySpace Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, vbzAS, vbzAE, zratio   ' Clear
         TheKey = 0
         End If
         
         If Not aArc Then
            xp = xsto(1)
            yp = ysto(1)
            aArc = True
         End If
         
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, vbzAS, vbzAE, zratio   ' Clear
         xp = x
         yp = y
      End If
    End Select
End Sub

Public Sub DrawMovePolyLines(picDRAW As PictureBox, Button As Integer, x As Single, y As Single)
Dim k As Long
Dim dx As Long, DY As Long
Dim sdx As Single, sdy As Single
Dim LLen As Long
   
   Select Case PolyLineTool
   Case [PLGradient1]
      picDRAW.DrawWidth = PixelSize
   End Select
   
   If NClicks = 1 Then  ' DRAW FIRST SEGMENT
      sdx = x - xsto(PointCount)
      sdy = y - ysto(PointCount)
      zangle = zATan2(sdy, sdx)
      LLen = Sqr(sdx * sdx + sdy * sdy)
      zangle = zATan2(sdy, sdx)
      Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
      Form1.LabShowLen = Str$(LLen) & " pels"
      
      Select Case PolyLineTool
      Case [PLSolid], [PLConnect], [PLParagram], [PLFillTri], [PLFillPoly]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         xp = x
         yp = y
         picDRAW.Line (xsto(1), ysto(1))-(xp, yp), XorCul  ' Draw
      
      Case [PLDouble], [PLDoubleEnded], [PLDoubleConnect], [PLDoubleParagram]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         xp = x
         yp = y
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      
      Case [PLGradient1]
         picDRAW.DrawWidth = PixelSize
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         xp = x
         yp = y
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
      End Select
   
   ElseIf aEnd = False Then ' DRAW NEXT SEGMENT
      sdx = xp - xsto(PointCount)
      sdy = yp - ysto(PointCount)
      zangle = zATan2(sdy, sdx)
      LLen = Sqr(sdx * sdx + sdy * sdy)
      zangle = zATan2(sdy, sdx)
      Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
      Form1.LabShowLen = Str$(LLen) & " pels"
      
      Select Case PolyLineTool
      Case [PLSolid], [PLConnect], [PLParagram], [PLFillTri], [PLFillPoly]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         xp = x
         yp = y
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
      
      Case [PLDouble], [PLDoubleEnded], [PLDoubleConnect], [PLDoubleParagram]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         xp = x
         yp = y
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      
      Case [PLGradient1]
         picDRAW.DrawWidth = PixelSize
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         xp = x
         yp = y
         picDRAW.DrawWidth = PixelSize
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
      End Select
   
   ElseIf aEnd = True And aComplete = False Then ' MOVE
      Select Case PolyLineTool
      Case [PLSolid], [PLParagram]
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
         Next k
         If TheKey = vbKeySpace Then
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
            Next k
         TheKey = 0
         End If
         
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Draw
         Next k
         xp = x
         yp = y
      
      Case [PLConnect], [PLFillTri], [PLFillPoly]
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
         Next k
         If NClicks > 2 Then
         picDRAW.Line (xsto(1), ysto(1))-(xsto(PointCount), ysto(PointCount)), XorCul ' Clear
         End If
         
         If TheKey = vbKeySpace Then
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
            Next k
            picDRAW.Line (xsto(1), ysto(1))-(xsto(PointCount), ysto(PointCount)), XorCul ' Clear
            TheKey = 0
         End If
         
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Draw
         Next k
         If NClicks > 2 Then
         picDRAW.Line (xsto(1), ysto(1))-(xsto(PointCount), ysto(PointCount)), XorCul ' Draw
         End If
         xp = x
         yp = y
         
      Case [PLDouble], [PLDoubleEnded], [PLDoubleConnect], [PLDoubleParagram]
         PolyMover picDRAW ' Clear
         ' PLDC
         If PolyLineTool = [PLDoubleConnect] And NClicks > 2 Then
            picDRAW.Line (xsto(1), ysto(1))-(xsto(PointCount), ysto(PointCount)), XorCul  ' XClear
            GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xsto(1), ysto(1), _
               ixa, iya, ixb, iyb
            picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         
         ElseIf PolyLineTool = [PLDoubleParagram] And NClicks > 2 Then
            picDRAW.Line (xsto(1), ysto(1))-(xsto(PointCount), ysto(PointCount)), XorCul  ' XClear
            GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xsto(1), ysto(1), _
               ixa, iya, ixb, iyb
            picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
            ' Elim odd point for PixelSize = 1
            If PixelSize = 1 Then
               picDRAW.PSet (xsto(1), ysto(1)), XorCul
               picDRAW.PSet (xsto(PointCount), ysto(PointCount)), XorCul
            End If
         End If
         
         If TheKey = vbKeySpace Then
            PolyMover picDRAW
            If PointCount > 2 Then
               NClicks = NClicks + 1   ' !!
            End If
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         PolyMover picDRAW
         ' PLDC
         If PolyLineTool = [PLDoubleConnect] And (NClicks > 2) Then
            picDRAW.Line (xsto(1), ysto(1))-(xsto(PointCount), ysto(PointCount)), XorCul  ' Draw
            GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xsto(1), ysto(1), _
               ixa, iya, ixb, iyb
            picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         ElseIf PolyLineTool = [PLDoubleParagram] And (NClicks > 2) Then
            picDRAW.Line (xsto(1), ysto(1))-(xsto(PointCount), ysto(PointCount)), XorCul  ' Draw
            GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xsto(1), ysto(1), _
               ixa, iya, ixb, iyb
            picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
            ' Elim odd point for PixelSize = 1
            If PixelSize = 1 Then
               picDRAW.PSet (xsto(1), ysto(1)), XorCul
               picDRAW.PSet (xsto(PointCount), ysto(PointCount)), XorCul
            End If
         End If
         
         xp = x
         yp = y
      
      Case [PLGradient1]
         picDRAW.DrawWidth = PixelSize
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
         Next k
         If TheKey = vbKeySpace Then
            picDRAW.DrawWidth = PixelSize
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
            Next k
         TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         'picDRAW.DrawWidth = 2 * PixelSize
         picDRAW.DrawWidth = PixelSize
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Draw
         Next k
         xp = x
         yp = y
      
      End Select
   End If
End Sub

Private Sub PolyMover(picDRAW As PictureBox)
Dim k As Long
   For k = 1 To PointCount - 1
      picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
      GetParallelCoords zspace, xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), _
            ixa, iya, ixb, iyb
      picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
   Next k
End Sub

Public Sub DrawMoveCurvyLines(picDRAW As PictureBox, Button As Integer, x As Single, y As Single)
Dim k As Long
Dim dx As Long, DY As Long
Dim sdx As Single, sdy As Single
Dim LLen As Long
   If NClicks = 1 Then  ' DRAW FIRST SEGMENT
      sdx = x - xsto(PointCount)
      sdy = y - ysto(PointCount)
      zangle = zATan2(sdy, sdx)
      LLen = Sqr(sdx * sdx + sdy * sdy)
      zangle = zATan2(sdy, sdx)
      Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
      Form1.LabShowLen = Str$(LLen) & " pels"
      
      Select Case CurvyLineTool
      Case [CLSolid]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         xp = x
         yp = y
         picDRAW.Line (xsto(1), ysto(1))-(xp, yp), XorCul  ' Draw
      
      Case [CLDouble], [CLDoubleEnded] ' To [CLGradient3]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         xp = x
         yp = y
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      
      Case [CLGradient1]
         picDRAW.DrawWidth = PixelSize
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         xp = x
         yp = y
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
      End Select
   
   ElseIf aEnd = False Then ' DRAW NEXT SEGMENT
      sdx = xp - xsto(PointCount)
      sdy = yp - ysto(PointCount)
      zangle = zATan2(sdy, sdx)
      LLen = Sqr(sdx * sdx + sdy * sdy)
      zangle = zATan2(sdy, sdx)
      Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
      Form1.LabShowLen = Str$(LLen) & " pels"
      
      Select Case CurvyLineTool
      Case [CLSolid]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         xp = x
         yp = y
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
      
      Case [CLDouble], [CLDoubleEnded]
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         xp = x
         yp = y
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xp, yp, _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         
      Case [CLGradient1]
         picDRAW.DrawWidth = PixelSize
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Clear
         xp = x
         yp = y
         picDRAW.DrawWidth = PixelSize
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xp, yp), XorCul  ' Draw
      End Select
         
   ElseIf aEnd = True And aComplete = False Then ' MOVE
      Select Case CurvyLineTool
      Case [CLSolid]
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
         Next k
         If TheKey = vbKeySpace Then
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
            Next k
         TheKey = 0
         End If
         
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Draw
         Next k
         xp = x
         yp = y
      Case [CLDouble], [CLDoubleEnded]
         PolyMover picDRAW
         If TheKey = vbKeySpace Then
            PolyMover picDRAW
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         PolyMover picDRAW
         xp = x
         yp = y
         
      Case [CLGradient1]
         'picDRAW.DrawWidth = 2 * PixelSize
         picDRAW.DrawWidth = PixelSize
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
         Next k
         If TheKey = vbKeySpace Then
            'picDRAW.DrawWidth = 2 * PixelSize
            picDRAW.DrawWidth = PixelSize
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Clear
            Next k
         TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         picDRAW.DrawWidth = PixelSize
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' Draw
         Next k
         xp = x
         yp = y
      
      End Select
   End If
End Sub

Public Sub DrawMoveArrows(picDRAW As PictureBox, x As Single, y As Single, Optional aRRow As Boolean = True)
Dim dx As Long, DY As Long, k As Long
   If NClicks = 1 Then  ' Draw
      DrawArrow picDRAW, xsto(1), ysto(1), xp, yp, XorCul, aRRow
      xp = x
      yp = y
      xsto(2) = x
      ysto(2) = y
      DrawArrow picDRAW, xsto(1), ysto(1), xp, yp, XorCul, aRRow
   ElseIf NClicks = 2 Then ' Move
      DrawArrow picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), XorCul, aRRow
      If TheKey = vbKeySpace Then
         DrawArrow picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), XorCul, aRRow
         TheKey = 0
      End If
      
      dx = x - xp
      DY = y - yp
      For k = 1 To 5
         xsto(k) = xsto(k) + dx
         ysto(k) = ysto(k) + DY
      Next k
      DrawArrow picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), XorCul, aRRow
      xp = x
      yp = y
   End If
End Sub

Public Sub DrawMoveJunctions(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Long, DY As Long, k As Long
   If NClicks = 1 Then  ' Draw
      DrawJunction picDRAW, xsto(1), ysto(1), xp, yp, XorCul
      xp = x
      yp = y
      xsto(2) = x
      ysto(2) = y
      DrawJunction picDRAW, xsto(1), ysto(1), xp, yp, XorCul
   ElseIf NClicks = 2 Then ' Move
      DrawJunction picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), XorCul
      If TheKey = vbKeySpace Then
         DrawJunction picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), XorCul
         TheKey = 0
      End If
      
      dx = x - xp
      DY = y - yp
      For k = 1 To 12
         xsto(k) = xsto(k) + dx
         ysto(k) = ysto(k) + DY
      Next k
      DrawJunction picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), XorCul
      xp = x
      yp = y
   End If
End Sub

Public Sub DrawMoveTubulars(picDRAW As PictureBox, x As Single, y As Single)

'Public Alpha1 As Single
'Public Alpha2 As Single
'Public Alpha3 As Single

Dim dx As Long, DY As Long, k As Long
Dim sdx As Single, sdy As Single
Dim LLen As Long
Dim svx As Long
Dim svy As Long

Select Case TubularTool
Case [CONE1] To [CONE6], [TUBE1] To [TUBE3]

      If NClicks = 1 Then  ' Make ellipse
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
         xp = x
         yp = y
         xsto(2) = x
         ysto(2) = y
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio   ' Draw
         
      ElseIf NClicks = 2 Then ' Draw line & Rotate ellipse
          
         If Not aClearOrigOval Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear original oval
            RotEllipse picDRAW, XorCul, 0 ' Draw
            aClearOrigOval = True
         End If
         zangle = zATan2(y - CSng(ysto(1)), x - CSng(xsto(1)))
         ShowAngleLen x, y
         
         RotEllipse picDRAW, XorCul, 0 ' Clear
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul  ' Clear angline
          
         xp = x
         yp = y
         xsto(2) = x
         ysto(2) = y
         picDRAW.Line (xsto(1), ysto(1))-(x, y), XorCul  ' Draw angline
         RotEllipse picDRAW, XorCul, 0 ' Draw
         xp = x
         yp = y
          
         xang = xsto(2)
         yang = ysto(2)
         
      ElseIf NClicks = 3 Then   ' Rotate external tangent point
         
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul 'Clear
         xsto(2) = x
         ysto(2) = y
         picDRAW.Line (xsto(1), ysto(1))-(x, y), XorCul  ' Draw new xor line
         
         zangle = zATan2(y - CSng(ysto(1)), x - CSng(xsto(1)))
         ShowAngleLen x, y
         
         xp = x
         yp = y
      
      ElseIf NClicks = 4 Then  ' Move zrad, zratio fixed
            
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
         svx = xsto(2)
         svy = ysto(2)
         xsto(2) = xang
         ysto(2) = yang
         Alpha1 = -1
         RotEllipse picDRAW, XorCul, 0, Alpha1 ' Clear
         xsto(2) = svx
         ysto(2) = svy
         
         dx = x - xp
         DY = y - yp
         xang = xang + dx
         yang = yang + DY
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         RotEllipse picDRAW, XorCul, 0, Alpha1 ' Draw
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
         
         ' Save for SpaceBar
         svxsto2 = xsto(2)
         svysto2 = ysto(2)
         
         xp = x
         yp = y
      
      ElseIf NClicks > 4 Then ' Complete
      
      End If

Case Else  ' BULLETS

   If NClicks = 1 Then ' Redraw circle
      zrad = Sqr((xsto(1) - xp) ^ 2 + (ysto(1) - yp) ^ 2)
      picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
      xp = x
      yp = y
      zrad = Sqr((xsto(1) - xp) ^ 2 + (ysto(1) - yp) ^ 2)
      picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
   ElseIf NClicks = 2 Then ' Redraw line
      sdx = x - CSng(xsto(1))
      sdy = y - CSng(ysto(1))
      zangle = zATan2(sdy, sdx)
      LLen = Sqr(sdx * sdx + sdy * sdy)
      Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
      Form1.LabShowLen = Str$(LLen) & " pels"
      
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
      xp = x
      yp = y
      xsto(2) = x
      ysto(2) = y
      picDRAW.Line (xsto(1), ysto(1))-(x, y), XorCul
   
   ElseIf NClicks = 3 Then ' Move
      picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
      
      If TheKey = vbKeySpace Then
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
         TheKey = 0
      End If
      
      dx = x - xp
      DY = y - yp
      For k = 1 To 2
         xsto(k) = xsto(k) + dx
         ysto(k) = ysto(k) + DY
      Next k
      picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
      xp = x
      yp = y
   ElseIf NClicks > 3 Then ' Complete
   End If

End Select

End Sub

Public Sub DrawMoveShape(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Single, DY As Single, k As Long
' For Paragram45
Dim zLen As Single
Dim LL As Single
Dim zalpha As Single
   
   ShowAngleLen x, y
   If aProject Then
      DrawMoveShape3D picDRAW, x, y
      Exit Sub
   End If
   If NClicks = 1 Then  ' XClear
      Select Case ShapeTool
      Case [Hexagon], [Hexagon45]
         For k = 3 To PointCount   ' Draw
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         
      Case [Paragram45]   ' 1-3-2-4-5
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
      Case Else
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
      End Select
      
      dx = x - xp ' Calc new coords
      DY = y - yp
      xsto(2) = xsto(2) + dx
      ysto(2) = ysto(2) + DY
      dx = xsto(2) - xsto(1)
      DY = ysto(2) - ysto(1)
      Select Case ShapeTool
      Case [FWedge]
         zspace = CSng(Sqr(dx * dx + DY * DY)) / 2
      Case [BWedge]
         zspace = -CSng(Sqr(dx * dx + DY * DY)) / 2
      Case [Frustrum]
         zspace = CSng(Sqr(dx * dx + DY * DY)) / 3
      Case [Triangle]
         zspace = CSng(Sqr(dx * dx + DY * DY))
      Case [Square]
         zspace = CSng(Sqr(dx * dx + DY * DY))
      Case [Hexagon]
         zspace = CSng(Sqr(dx * dx + DY * DY)) / 3
      End Select
      GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), ixa, iya, ixb, iyb
      GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), _
         ixa, iya, ixb, iyb, _
         ixc, iyc, ixd, iyd
      Select Case ShapeTool
      Case [FWedge], [BWedge]   ' 45 deg
         xsto(4) = ixa + dx / 2: ysto(4) = iya + DY / 2
         xsto(3) = ixb: ysto(3) = iyb
      Case [Frustrum]   ' 45 deg
         xsto(4) = ixa + dx / 3: ysto(4) = iya + DY / 3
         xsto(3) = ixa + 2 * dx / 3: ysto(3) = iya + 2 * DY / 3
      Case [Triangle]
         xsto(4) = ixa + dx / 2: ysto(4) = iya + dx / 2
         xsto(3) = xsto(4): ysto(3) = ysto(4)   ' 3 & 4 the same
      Case [Square]
         xsto(4) = ixa: ysto(4) = iya
         xsto(3) = ixb: ysto(3) = iyb
      Case [Hexagon], [Hexagon45]   ' 60 deg
         dx = x - xsto(2)
         DY = y - ysto(2)
         xsto(2) = xsto(2) + dx
         ysto(2) = ysto(2) + DY
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zLen = Sqr(dx ^ 2 + DY ^ 2)
         zalpha = zATan2(DY, dx)
         zangle = 60 * d2r#
         If ShapeTool = [Hexagon45] Then zangle = 45 * d2r#
         xsto(2) = xsto(1) + zLen * Cos(zalpha)
         ysto(2) = ysto(1) + zLen * Sin(zalpha)
         For k = 3 To PointCount   ' Draw
            zalpha = zalpha + zangle
            xsto(k) = xsto(1) + zLen * Cos(zalpha)
            ysto(k) = ysto(1) + zLen * Sin(zalpha)
         Next k
      Case [Paragram45]   ' 45 deg
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zLen = Sqr(dx ^ 2 + DY ^ 2)
         zalpha = zATan2(DY, dx)
         LL = (zLen / 2) / Cos(22.5 * d2r#)
         dx = LL * Cos(zalpha - 22.5 * d2r#)
         DY = LL * Sin(zalpha - 22.5 * d2r#)
         xsto(3) = xsto(1) + dx
         ysto(3) = ysto(1) + DY
         xsto(4) = xsto(2) - dx
         ysto(4) = ysto(2) - DY
         PointCount = 5
      End Select
      
      Select Case ShapeTool   ' XDraw
      Case [Hexagon], [Hexagon45]
         For k = 3 To PointCount   ' Draw
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         
      Case [Paragram45]   ' 1-3-2-4-5
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
      Case Else
         xsto(5) = xsto(1): ysto(5) = ysto(1)   ' 5 & 1 the same
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
      End Select
      xp = x
      yp = y
   
   ElseIf NClicks = 2 Then ' Move
      
      Select Case ShapeTool
      Case [Hexagon], [Hexagon45] ' XClear
         For k = 3 To PointCount   ' Draw
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         If TheKey = vbKeySpace Then
            For k = 3 To PointCount   ' Draw
               picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
            Next k
            picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
            TheKey = 0
         End If
      Case [Paragram45]   ' 1-3-2-4-5
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
         If TheKey = vbKeySpace Then
            xsto(5) = xsto(1): ysto(5) = ysto(1)
            picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
            picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
            picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
            picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
            TheKey = 0
         End If
      
      Case Else
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
         If TheKey = vbKeySpace Then
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
            Next k
            TheKey = 0
         End If
      End Select
      
      ' Move
      dx = x - xp
      DY = y - yp
      For k = 1 To PointCount
         xsto(k) = xsto(k) + dx
         ysto(k) = ysto(k) + DY
      Next k
      
      Select Case ShapeTool   ' XDraw
      Case [Hexagon], [Hexagon45]
         For k = 3 To PointCount   ' Draw
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
      Case [Paragram45]   ' 1-3-2-4-5
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
      Case Else
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
      End Select
      xp = x
      yp = y
   End If
End Sub

Public Sub DrawMoveShape3D(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Single, DY As Single, k As Long
Dim DDX As Single, ddy As Single
' For Paragram45
Dim zLen As Single
Dim LL As Single
Dim zalpha As Single
Dim sdx As Single, sdy As Single
Dim LLen As Long

   ShowAngleLen x, y
   If NClicks = 1 Then  ' XClear
      Select Case ShapeTool
      Case [Hexagon], [Hexagon45]
         For k = 3 To PointCount
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
      Case [Paragram45]   ' 1-3-2-4-5   5=1
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
      Case [Triangle]
         For k = 1 To PointCount
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
      Case Else
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
      End Select
      
      dx = x - xp ' Calc new coords
      DY = y - yp
      xsto(2) = xsto(2) + dx
      ysto(2) = ysto(2) + DY
         DDX = xsto(2) - xsto(1)
         ddy = ysto(2) - ysto(1)
      dx = xsto(2) - xsto(1)
      DY = ysto(2) - ysto(1)
      Select Case ShapeTool
      Case [FWedge]
         zspace = CSng(Sqr(dx * dx + DY * DY)) / 2
      Case [BWedge]
         zspace = -CSng(Sqr(dx * dx + DY * DY)) / 2
      Case [Frustrum]
         zspace = CSng(Sqr(dx * dx + DY * DY)) / 3
      Case [Triangle]
         zspace = CSng(Sqr(dx * dx + DY * DY))
      Case [Square]
         zspace = CSng(Sqr(dx * dx + DY * DY))
      Case [Hexagon]
         zspace = CSng(Sqr(dx * dx + DY * DY)) / 3
      End Select
      GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), ixa, iya, ixb, iyb
      GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), _
         ixa, iya, ixb, iyb, _
         ixc, iyc, ixd, iyd
      Select Case ShapeTool
      Case [FWedge], [BWedge]   ' 45 deg
         xsto(3) = ixb: ysto(3) = iyb
         xsto(4) = ixa + dx / 2: ysto(4) = iya + DY / 2
      Case [Frustrum]   ' 45 deg
         xsto(3) = ixa + 2 * dx / 3: ysto(3) = iya + 2 * DY / 3
         xsto(4) = ixa + dx / 3: ysto(4) = iya + DY / 3
      Case [Triangle]
         xsto(3) = ixa + dx / 2: ysto(3) = iya + dx / 2
         xsto(4) = xsto(3): ysto(4) = ysto(3)
      Case [Square]
         xsto(3) = ixb: ysto(3) = iyb
         xsto(4) = ixa: ysto(4) = iya
      Case [Hexagon], [Hexagon45]  ' 60, 45 deg
         dx = x - xsto(2)
         DY = y - ysto(2)
         xsto(2) = xsto(2) + dx
         ysto(2) = ysto(2) + DY
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zLen = Sqr(dx ^ 2 + DY ^ 2)
         zalpha = zATan2(DY, dx)
         zangle = 60 * d2r#
         If ShapeTool = [Hexagon45] Then zangle = 45 * d2r#
         xsto(2) = xsto(1) + zLen * Cos(zalpha)
         ysto(2) = ysto(1) + zLen * Sin(zalpha)
         For k = 3 To PointCount
            zalpha = zalpha + zangle
            xsto(k) = xsto(1) + zLen * Cos(zalpha)
            ysto(k) = ysto(1) + zLen * Sin(zalpha)
         Next k
      Case [Paragram45]   ' 45 deg
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zLen = Sqr(dx ^ 2 + DY ^ 2)
         zalpha = zATan2(DY, dx)
         LL = (zLen / 2) / Cos(22.5 * d2r#)
         dx = LL * Cos(zalpha - 22.5 * d2r#)
         DY = LL * Sin(zalpha - 22.5 * d2r#)
         xsto(3) = xsto(1) + dx
         ysto(3) = ysto(1) + DY
         xsto(4) = xsto(2) - dx
         ysto(4) = ysto(2) - DY
         PointCount = 5
      End Select
      
      Select Case ShapeTool   ' XDraw
      Case [Hexagon], [Hexagon45]
         For k = 3 To PointCount
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
      Case [Triangle]
         For k = 1 To PointCount
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
      Case [Paragram45]   ' 1-3-2-4-5  5=1
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
      Case Else
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
      End Select
      xp = x
      yp = y
   
   ElseIf NClicks = 2 Then ' Redraw 3D line
      sdx = x - CSng(xsto(2))
      sdy = y - CSng(ysto(2))
      zangle = zATan2(sdy, sdx)
      LLen = Sqr(sdx * sdx + sdy * sdy)
      Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
      Form1.LabShowLen = Str$(LLen) & " pels"
      Select Case ShapeTool
      Case [Hexagon], [Hexagon45]  ' 2-3-4-5-6-7   15-16- - 20
         For k = 2 To 7
            picDRAW.Line (xsto(k), ysto(k))-(xsto(13 + k), ysto(13 + k)), XorCul
         Next k
      Case [FWedge], [BWedge], [Frustrum], [Square]
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
         Next k
      Case [Triangle]
         For k = 1 To 3
            picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
         Next k
      Case [Paragram45] ' 1-3-2-4-5
            picDRAW.Line (xsto(1), ysto(1))-(xsto(15), ysto(15)), XorCul
            picDRAW.Line (xsto(3), ysto(3))-(xsto(17), ysto(17)), XorCul
            picDRAW.Line (xsto(2), ysto(2))-(xsto(16), ysto(16)), XorCul
            picDRAW.Line (xsto(4), ysto(4))-(xsto(18), ysto(18)), XorCul
      End Select
      
      xp = x
      yp = y
      sdx = x - CSng(xsto(2))
      sdy = y - CSng(ysto(2))
      Select Case ShapeTool
      Case [Hexagon], [Hexagon45]
         xsto(15) = x
         ysto(15) = y
         For k = 16 To 20   ' 3-7
            xsto(k) = xsto(k - 13) + sdx
            ysto(k) = ysto(k - 13) + sdy
         Next k
      Case Else
         xsto(16) = x
         ysto(16) = y
         xsto(15) = xsto(1) + sdx
         ysto(15) = ysto(1) + sdy
         For k = 17 To 20   ' 17-20 = 3-6
            xsto(k) = xsto(k - 14) + sdx
            ysto(k) = ysto(k - 14) + sdy
         Next k
      End Select
      
      Select Case ShapeTool
      Case [FWedge], [BWedge], [Frustrum], [Square]
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
         Next k
      Case [Triangle]
         For k = 1 To 3
            picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
         Next k
      Case [Hexagon], [Hexagon45]
         For k = 2 To 7 'PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(13 + k), ysto(13 + k)), XorCul
         Next k
      Case [Paragram45] ' 1-3-2-4-5
            picDRAW.Line (xsto(1), ysto(1))-(xsto(15), ysto(15)), XorCul
            picDRAW.Line (xsto(3), ysto(3))-(xsto(17), ysto(17)), XorCul
            picDRAW.Line (xsto(2), ysto(2))-(xsto(16), ysto(16)), XorCul
            picDRAW.Line (xsto(4), ysto(4))-(xsto(18), ysto(18)), XorCul
      End Select
      
   ElseIf NClicks = 3 Then ' Move shape
      
      Select Case ShapeTool   ' XClear
      Case [Hexagon], [Hexagon45]
         For k = 3 To PointCount   ' Draw
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         If TheKey = vbKeySpace Then
            For k = 3 To PointCount   ' Draw
               picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
            Next k
            picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
            For k = 2 To 7
               picDRAW.Line (xsto(k), ysto(k))-(xsto(13 + k), ysto(13 + k)), XorCul
            Next k
            TheKey = 0
         End If
      Case [Paragram45]   ' 1-3-2-4-5
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
         If TheKey = vbKeySpace Then
            xsto(5) = xsto(1): ysto(5) = ysto(1)
            picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
            picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
            picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
            picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
            picDRAW.Line (xsto(1), ysto(1))-(xsto(15), ysto(15)), XorCul
            picDRAW.Line (xsto(3), ysto(3))-(xsto(17), ysto(17)), XorCul
            picDRAW.Line (xsto(2), ysto(2))-(xsto(16), ysto(16)), XorCul
            picDRAW.Line (xsto(4), ysto(4))-(xsto(18), ysto(18)), XorCul
            TheKey = 0
         End If
      Case [Triangle]   ' 1-2-3-4-5
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
         If TheKey = vbKeySpace Then
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
            Next k
            For k = 1 To 3
               picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
            Next k
            TheKey = 0
         End If
      Case Else
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
         If TheKey = vbKeySpace Then
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
            Next k
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
            Next k
            TheKey = 0
         End If
      End Select
      
      ' 3D line
      Select Case ShapeTool
      Case [FWedge], [BWedge], [Frustrum], [Square]
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
         Next k
      Case [Triangle]
         For k = 1 To 3
            picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
         Next k
      Case [Hexagon], [Hexagon45]
         For k = 2 To 7
            picDRAW.Line (xsto(k), ysto(k))-(xsto(13 + k), ysto(13 + k)), XorCul
         Next k
      Case [Paragram45] ' 1-3-2-4-5
            picDRAW.Line (xsto(1), ysto(1))-(xsto(15), ysto(15)), XorCul
            picDRAW.Line (xsto(3), ysto(3))-(xsto(17), ysto(17)), XorCul
            picDRAW.Line (xsto(2), ysto(2))-(xsto(16), ysto(16)), XorCul
            picDRAW.Line (xsto(4), ysto(4))-(xsto(18), ysto(18)), XorCul
       End Select
      
      ' Move
      dx = x - xp
      DY = y - yp
      For k = 1 To PointCount
         xsto(k) = xsto(k) + dx
         ysto(k) = ysto(k) + DY
      Next k
      ' ALL
      For k = 15 To 20
         xsto(k) = xsto(k) + dx
         ysto(k) = ysto(k) + DY
      Next k
      
      Select Case ShapeTool   ' XDraw
      Case [Hexagon], [Hexagon45]
         For k = 3 To PointCount
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
      
      Case [Paragram45]   ' 1-3-2-4-5
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
      Case Else
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
      End Select
      ' 3D line
      Select Case ShapeTool
      Case [FWedge], [BWedge], [Frustrum], [Square]
         For k = 1 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
         Next k
      Case [Triangle]
         For k = 1 To 3
            picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
         Next k
      Case [Hexagon], [Hexagon45]
         For k = 2 To 7
            picDRAW.Line (xsto(k), ysto(k))-(xsto(13 + k), ysto(13 + k)), XorCul
         Next k
      Case [Paragram45]
            picDRAW.Line (xsto(1), ysto(1))-(xsto(15), ysto(15)), XorCul
            picDRAW.Line (xsto(3), ysto(3))-(xsto(17), ysto(17)), XorCul
            picDRAW.Line (xsto(2), ysto(2))-(xsto(16), ysto(16)), XorCul
            picDRAW.Line (xsto(4), ysto(4))-(xsto(18), ysto(18)), XorCul
            
      End Select
      
      xp = x
      yp = y
   End If
End Sub

Public Sub DrawMoveRadials(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Long, DY As Long, k As Long

   If NClicks = 1 Then  ' Draw
      Select Case RadialTool
      Case [Spokes], [CSpokes]
         For k = 2 To PointCount   ' Clear
            picDRAW.Line (xsto(1), ysto(1))-(xsto(k), ysto(k)), XorCul
         Next k
         If RadialTool = [CSpokes] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         End If
         
         xsto(2) = x
         ysto(2) = y
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         zangle2 = zATan2(DY, dx)
         For k = 2 To PointCount   ' Draw
            xsto(k) = xsto(1) + zrad * Cos(zangle2)
            ysto(k) = ysto(1) + zrad * Sin(zangle2)
            picDRAW.Line (xsto(1), ysto(1))-(xsto(k), ysto(k)), XorCul
            zangle2 = zangle2 + zangle
         Next k
         If RadialTool = [CSpokes] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         End If
         
      Case [Stars], [CStars]
         For k = 2 To PointCount - 1 ' Clear
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         If RadialTool = [CStars] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         End If
         
         xsto(2) = x
         ysto(2) = y
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         zrad2 = zrad / 5
         zangle2 = zATan2(DY, dx)
         xsto(2) = xsto(1) + zrad * Cos(zangle2)
         ysto(2) = ysto(1) + zrad * Sin(zangle2)
         For k = 3 To PointCount ' Draw
            zangle2 = zangle2 + zangle
            If (k Mod 2) = 1 Then
               xsto(k) = xsto(1) + zrad2 * Cos(zangle2)
               ysto(k) = ysto(1) + zrad2 * Sin(zangle2)
            Else
               xsto(k) = xsto(1) + zrad * Cos(zangle2)
               ysto(k) = ysto(1) + zrad * Sin(zangle2)
            End If
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         If RadialTool = [CStars] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         End If
      
      Case [Circles]
         zrad2 = zrad / 5
         For k = 2 To PointCount ' Clear
            picDRAW.Circle (xsto(k), ysto(k)), zrad2, XorCul
         Next k
         dx = x - xsto(2)
         DY = y - ysto(2)
         xsto(2) = xsto(2) + dx
         ysto(2) = ysto(2) + DY
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         zrad2 = zrad / 5
         zangle2 = zATan2(DY, dx)
         For k = 2 To PointCount   ' Draw
            xsto(k) = xsto(1) + zrad * Cos(zangle2)
            ysto(k) = ysto(1) + zrad * Sin(zangle2)
            picDRAW.Circle (xsto(k), ysto(k)), zrad2, XorCul
            zangle2 = zangle2 + zangle
         Next k
      
      Case [Polygons], [CPolygons]
         For k = 2 To PointCount - 1 ' Clear
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         If RadialTool = [CPolygons] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         End If
         
         dx = x - xsto(2)
         DY = y - ysto(2)
         xsto(2) = xsto(2) + dx
         ysto(2) = ysto(2) + DY
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         zangle2 = zATan2(DY, dx)
         xsto(2) = xsto(1) + zrad * Cos(zangle2)
         ysto(2) = ysto(1) + zrad * Sin(zangle2)
         For k = 3 To PointCount   ' Draw
            zangle2 = zangle2 + zangle
            xsto(k) = xsto(1) + zrad * Cos(zangle2)
            ysto(k) = ysto(1) + zrad * Sin(zangle2)
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         If RadialTool = [CPolygons] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         End If
      
      Case [Teeth]   ' NClicks=1
         ' Clear
         DrawTeeth picDRAW, XorCul
         LastTooth picDRAW, k, XorCul
         ' Draw
         dx = x - xsto(2)
         DY = y - ysto(2)
         xsto(2) = xsto(2) + dx
         ysto(2) = ysto(2) + DY
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         zangle2 = zATan2(DY, dx)
         xsto(2) = xsto(1) + zrad * Cos(zangle2)
         ysto(2) = ysto(1) + zrad * Sin(zangle2)
         For k = 3 To PointCount
            zangle2 = zangle2 + zangle
            xsto(k) = xsto(1) + zrad * Cos(zangle2)
            ysto(k) = ysto(1) + zrad * Sin(zangle2)
            If (k And 1) = 0 Then
               GetParallelCoords zrad / 4, xsto(k - 1), ysto(k - 1), xsto(k), ysto(k), _
                     ixa, iya, ixb, iyb
            
               picDRAW.Line (xsto(k - 1), ysto(k - 1))-(ixa, iya), XorCul
               picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
               picDRAW.Line (ixb, iyb)-(xsto(k), ysto(k)), XorCul
            Else
               picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
            End If
         Next k
         LastTooth picDRAW, k, XorCul
      
      Case [OddRoses], [EvenRoses]
         For k = 0 To 360 Step 1 ' Clear
            zangle = k * 180 / pi#
            zR = zrad * Cos(NumPoints * zangle)
            px = xsto(1) + zR * Cos(zangle)
            py = ysto(1) + zR * Sin(zangle)
            picDRAW.PSet (px, py), XorCul
         Next k
         xsto(2) = x ' Draw
         ysto(2) = y
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         For k = 0 To 360 Step 1 ' Draw
            zangle = k * 180 / pi#
            zR = zrad * Cos(NumPoints * zangle)
            px = xsto(1) + zR * Cos(zangle)
            py = ysto(1) + zR * Sin(zangle)
            picDRAW.PSet (px, py), XorCul
         Next k
      
      Case [Hypocycloid]
         For k = 0 To 360 Step 1 ' Clear
            zangle = k * 180 / pi#
            px = xsto(1) + zrad * Cos(zangle) ^ NumPoints
            py = ysto(1) + zrad * Sin(zangle) ^ NumPoints
            picDRAW.PSet (px, py), XorCul
         Next k
         xsto(2) = x ' Draw
         ysto(2) = y
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         For k = 0 To 360 Step 1 ' Clear
            zangle = k * 180 / pi#
            px = xsto(1) + zrad * Cos(zangle) ^ NumPoints
            py = ysto(1) + zrad * Sin(zangle) ^ NumPoints
            picDRAW.PSet (px, py), XorCul
         Next k
         
      Case [Spiral]
         zrad = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
         DrawSpiral picDRAW
         
         xsto(2) = x
         ysto(2) = y
         zrad = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
         DrawSpiral picDRAW
      
      End Select
   
   ElseIf NClicks = 2 Then ' Move
      Select Case RadialTool
      Case [Spokes], [CSpokes]
         
         If (NumPoints And 1) = 1 Then ' ODD  ' Avoiding spurious dots
            picDRAW.Line (xsto(1), ysto(1))-(xsto(1), ysto(1)), XorCul
         End If
         For k = 2 To PointCount   ' Clear
            picDRAW.Line (xsto(1), ysto(1))-(xsto(k), ysto(k)), XorCul
         Next k
         If RadialTool = [CSpokes] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         End If
         
         If TheKey = vbKeySpace Then
               If (NumPoints And 1) = 1 Then ' ODD  ' Avoiding spurious dots
                  picDRAW.Line (xsto(1), ysto(1))-(xsto(1), ysto(1)), XorCul
               End If
               For k = 2 To PointCount   ' Clear
                  picDRAW.Line (xsto(1), ysto(1))-(xsto(k), ysto(k)), XorCul
               Next k
               If RadialTool = [CSpokes] Then
                  picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
               End If
            TheKey = 0
         End If
         
         dx = x - xsto(2)
         DY = y - ysto(2)
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         
         If (NumPoints And 1) = 0 Then  'EVEN  ' Avoiding spurious dots
            picDRAW.Line (xsto(1), ysto(1))-(xsto(1), ysto(1)), XorCul
         End If
         
         For k = 1 To PointCount ' Draw
            picDRAW.Line (xsto(1), ysto(1))-(xsto(k), ysto(k)), XorCul
         Next k
         If RadialTool = [CSpokes] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         End If
      
      
      Case [Stars], [CStars]
         MoveStars picDRAW
         If TheKey = vbKeySpace Then
            MoveStars picDRAW
            TheKey = 0
         End If
         
         dx = x - xsto(2)
         DY = y - ysto(2)
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         MoveStars picDRAW
      
      Case [Circles]
         For k = 2 To PointCount
            picDRAW.Circle (xsto(k), ysto(k)), zrad2, XorCul
         Next k
         ' Correction 7/3/09
         If TheKey = vbKeySpace Then
            For k = 2 To PointCount
               picDRAW.Circle (xsto(k), ysto(k)), zrad2, XorCul
            Next k
            TheKey = 0
         End If
         
         dx = x - xsto(2)
         DY = y - ysto(2)
         For k = 2 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         For k = 2 To PointCount
            picDRAW.Circle (xsto(k), ysto(k)), zrad2, XorCul
         Next k
      
      Case [Polygons], [CPolygons]
         For k = 2 To PointCount - 1 ' Clear
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         If RadialTool = [CPolygons] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         End If
         
         If TheKey = vbKeySpace Then
               For k = 2 To PointCount - 1 ' Clear
                  picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
               Next k
               picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
               If RadialTool = [CPolygons] Then
                  picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
               End If
            TheKey = 0
         End If
         
         dx = x - xsto(2)
         DY = y - ysto(2)
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         For k = 2 To PointCount - 1 ' Draw
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         If RadialTool = [CPolygons] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
         End If
      
      Case [Teeth]   ' NClicks = 2
         ' Clear
         DrawTeeth picDRAW, XorCul
         LastTooth picDRAW, k, XorCul
         
         ' Correction 7/3/09
         If TheKey = vbKeySpace Then
            DrawTeeth picDRAW, XorCul
            LastTooth picDRAW, k, XorCul
            TheKey = 0
         End If
         
         ' Draw
         dx = x - xsto(2)
         DY = y - ysto(2)
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         DrawTeeth picDRAW, XorCul
         LastTooth picDRAW, k, XorCul
      
      Case [OddRoses], [EvenRoses]
         MoveRoses picDRAW
         If TheKey = vbKeySpace Then
            MoveRoses picDRAW
            TheKey = 0
         End If
         
         dx = x - xsto(2)  ' Draw
         DY = y - ysto(2)
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         xsto(2) = x
         ysto(2) = y
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         MoveRoses picDRAW
      
      Case [Hypocycloid]
         MoveHypocycloid picDRAW
         If TheKey = vbKeySpace Then
            MoveHypocycloid picDRAW
            TheKey = 0
         End If
         dx = x - xsto(2)  ' Draw
         DY = y - ysto(2)
         For k = 1 To PointCount
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         xsto(2) = x
         ysto(2) = y
         dx = xsto(2) - xsto(1)
         DY = ysto(2) - ysto(1)
         zrad = Sqr(dx * dx + DY * DY)
         MoveHypocycloid picDRAW
      
      Case [Spiral]
         zrad = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
      
         If TheKey = vbKeySpace Then
               zrad = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
               DrawSpiral picDRAW
            TheKey = 0
         End If
         
         DrawSpiral picDRAW
         dx = x - xsto(2)  ' Draw
         DY = y - ysto(2)
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
         xsto(2) = x
         ysto(2) = y
         zrad = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
            
         DrawSpiral picDRAW
      End Select
   End If
End Sub

Private Sub MoveStars(picDRAW As PictureBox)
Dim k As Long
   For k = 2 To PointCount - 1
      picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
   Next k
   picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
   If RadialTool = [CStars] Then
      picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul
   End If
End Sub

Private Sub MoveRoses(picDRAW As PictureBox)
Dim px As Long, py As Long
Dim zR As Single
Dim k As Long
   For k = 0 To 360 Step 1
      zangle = k * 180 / pi#
      zR = zrad * Cos(NumPoints * zangle)
      px = xsto(1) + zR * Cos(zangle)
      py = ysto(1) + zR * Sin(zangle)
      picDRAW.PSet (px, py), XorCul
   Next k
End Sub

Private Sub MoveHypocycloid(picDRAW As PictureBox)
Dim px As Long, py As Long
Dim k As Long
   For k = 0 To 360 Step 1 ' Clear
      zangle = k * 180 / pi#
      px = xsto(1) + zrad * Cos(zangle) ^ NumPoints
      py = ysto(1) + zrad * Sin(zangle) ^ NumPoints
      picDRAW.PSet (px, py), XorCul
   Next k
End Sub

Public Sub DrawMoveCircuits(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Long, DY As Long, k As Long

   ShowAngleLen x, y
   
   If NClicks = 1 Then
      DrawCircuitComp picDRAW, xp, yp, XorCul  ' Clear
      xp = x
      yp = y
      Select Case CircuitTool
      Case [Inductor], [Resistor], [CBox], [CLine]
         xsto(2) = xp
         ysto(2) = yp
      End Select
      DrawCircuitComp picDRAW, xp, yp, XorCul  ' Draw
   End If
   If NClicks = 2 Then  ' Move
      DrawCircuitComp picDRAW, xsto(PointCount), ysto(PointCount), XorCul  ' Clear
      
      If TheKey = vbKeySpace Then
         DrawCircuitComp picDRAW, xsto(PointCount), ysto(PointCount), XorCul  ' Clear
         TheKey = 0
      End If
      
      dx = x - xsto(PointCount)
      DY = y - ysto(PointCount)
      For k = 1 To PointCount
         xsto(k) = xsto(k) + dx
         ysto(k) = ysto(k) + DY
      Next k
      DrawCircuitComp picDRAW, xsto(PointCount), ysto(PointCount), XorCul  ' Draw
   End If
End Sub

Public Sub DrawMoveWaves(picDRAW As PictureBox, x As Single, y As Single)
Dim dx As Long, DY As Long, k As Long
   
   ShowAngleLen x, y
   
   Select Case WaveTool
   Case [SineWave], [Rectified]
      If NClicks = 1 Then
         DrawSineWave picDRAW, XorCul   ' Clear
            xp = x
            yp = y
            xsto(2) = x
            ysto(2) = y
         DrawSineWave picDRAW, XorCul   ' Draw
      ElseIf NClicks = 2 Then
         DrawSineWave picDRAW, XorCul   ' Clear
      
         If TheKey = vbKeySpace Then
            DrawSineWave picDRAW, XorCul
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
            DrawSineWave picDRAW, XorCul   ' Draw
         xp = x
         yp = y
      End If
   Case [SquareWave]
      If NClicks = 1 Then
         DrawSquareWave picDRAW, XorCul   ' Clear
            xp = x
            yp = y
            xsto(2) = x
            ysto(2) = y
         DrawSquareWave picDRAW, XorCul   ' Draw
      ElseIf NClicks = 2 Then
         DrawSquareWave picDRAW, XorCul   ' Clear
      
         If TheKey = vbKeySpace Then
            DrawSquareWave picDRAW, XorCul
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
            DrawSquareWave picDRAW, XorCul   ' Draw
         xp = x
         yp = y
      End If
   Case [Sawtooth1]
      If NClicks = 1 Then
         DrawSawtooth1 picDRAW, XorCul   ' Clear
            xp = x
            yp = y
            xsto(2) = x
            ysto(2) = y
         DrawSawtooth1 picDRAW, XorCul   ' Draw
      ElseIf NClicks = 2 Then
         DrawSawtooth1 picDRAW, XorCul   ' Clear
      
         If TheKey = vbKeySpace Then
            DrawSawtooth1 picDRAW, XorCul
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
            DrawSawtooth1 picDRAW, XorCul   ' Draw
         xp = x
         yp = y
      End If
   Case [Sawtooth2]
      If NClicks = 1 Then
         DrawSawtooth2 picDRAW, XorCul   ' Clear
            xp = x
            yp = y
            xsto(2) = x
            ysto(2) = y
         DrawSawtooth2 picDRAW, XorCul   ' Draw
      ElseIf NClicks = 2 Then
         DrawSawtooth2 picDRAW, XorCul   ' Clear
      
         If TheKey = vbKeySpace Then
            DrawSawtooth2 picDRAW, XorCul
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
            DrawSawtooth2 picDRAW, XorCul   ' Draw
         xp = x
         yp = y
      End If
   Case [Sawtooth3]
      If NClicks = 1 Then
         DrawSawtooth3 picDRAW, XorCul   ' Clear
            xp = x
            yp = y
            xsto(2) = x
            ysto(2) = y
         DrawSawtooth3 picDRAW, XorCul   ' Draw
      ElseIf NClicks = 2 Then
         DrawSawtooth3 picDRAW, XorCul   ' Clear
      
         If TheKey = vbKeySpace Then
            DrawSawtooth3 picDRAW, XorCul
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
            DrawSawtooth3 picDRAW, XorCul   ' Draw
         xp = x
         yp = y
      End If

   Case [CircWaves]
      If NClicks = 1 Then
         DrawCircWaves picDRAW, XorCul   ' Clear
            xp = x
            yp = y
            xsto(2) = x
            ysto(2) = y
         DrawCircWaves picDRAW, XorCul   ' Draw
      ElseIf NClicks = 2 Then
         DrawCircWaves picDRAW, XorCul   ' Clear
      
         If TheKey = vbKeySpace Then
            DrawCircWaves picDRAW, XorCul
            TheKey = 0
         End If
         dx = x - xp
         DY = y - yp
         For k = 1 To 2
            xsto(k) = xsto(k) + dx
            ysto(k) = ysto(k) + DY
         Next k
            DrawCircWaves picDRAW, XorCul   ' Draw
         xp = x
         yp = y
      End If
   End Select
End Sub


'#### COMPLETE ####################################################################

Public Sub CompleteSingles(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim k As Long
'Dim iw As Long, ih As Long
' For Stamp
Dim ixs As Long, iys As Long
Dim ixd As Long, iyd As Long
Dim SrcCul As Long ', DesCul As Long

   Select Case SingleTool
   Case [DSingle]
      aMouseDown = False  ' Completed at StartSingles
   Case [SprayDots]
      If NClicks > 1 Then
         aMouseDown = False
         ' XClear
         For k = 1 To PointCount - 1
             picDRAW.PSet (xsto(k), ysto(k)), XorCul
         Next k
         picDRAW.DrawMode = vbCopyPen  ' <<<< Overwrite
         picDRAW.DrawWidth = 1
         
         For k = 1 To PointCount - 1
             DrawDot xsto(k), ysto(k), Cul
         Next k
         DISPLAY Form1, Form1.picDRAW
      End If
   
   Case [Grass]
      If NClicks > 1 Then
         aMouseDown = False
         For k = 2 To PointCount '- 1
             picDRAW.Line (xsto(1), ysto(1))-(xsto(k), ysto(k)), XorCul  ' Clear
         Next k
         picDRAW.DrawMode = vbCopyPen
         
         For k = 2 To PointCount
            If Not aAA Then
               If PixelSize = 1 Then
                  ZDrawAALine xsto(1), ysto(1), xsto(k), ysto(k), Cul, 0.5
               ElseIf aRound Then
                  ZDrawAALine xsto(1), ysto(1), xsto(k), ysto(k), Cul, 0.5
               Else  ' Not AA but Square ends
                  ZDrawAALine xsto(1), ysto(1), xsto(k), ysto(k), Cul, 0.5
               End If
            Else  ' AA
               ZDrawAALine xsto(1), ysto(1), xsto(k), ysto(k), Cul, 0.5
            End If
         Next k
         DISPLAY Form1, Form1.picDRAW
      End If
   
   Case [FBush1] To [FBush7]
      If NClicks > 1 Then
         aMouseDown = False
         Screen.MousePointer = vbHourglass
         ModExtraTools.CompleteBush picDRAW, x, y, SingleTool
      End If
   
   Case [DStamp]
      If NClicks > 1 Then
         aMouseDown = False
         picDRAW.DrawWidth = 1
         ' XClear
         picDRAW.Line (xsto(1), ysto(1))-(xsto(1) + StampW - 1, ysto(1) + StampH - 1), XorCul, B
         ' Draw
         picDRAW.DrawMode = vbCopyPen
         ' BitBlt Doesn't do transparency
         ''''''''''''''''''''''''''''''''''''''''''''''''''''''
         PixelSize = 1
         ' Alternative Scan PicSelect & picDraw - Sprite?
         iyd = ysto(1)
         ixd = xsto(1)
         For iys = 0 To StampH - 1
         For ixs = 0 To StampW - 1
            SrcCul = GetPixel(Form1.picSelect.hDC, ixs, iys)
            If SrcCul >= 0 Then
               If SrcCul <> RGB(191, 197, 201) Then
                  'SetPixelV picDRAW.hDC, ixd, iyd, SrcCul
                  DrawDot ixd, iyd, SrcCul
               End If
            End If
            ixd = ixd + 1
            If ixd > xsto(1) + StampW - 1 Then
               iyd = iyd + 1
               ixd = xsto(1)
            End If
         Next ixs
         Next iys
         DISPLAY Form1, picDRAW
         ''''''''''''''''''''''''''''''''''''''''''''''''''''''
         picDRAW.Picture = picDRAW.Image
      End If
   
   End Select
   Screen.MousePointer = vbDefault
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub CompleteFreeDraw(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
' NPt,HAR from FreeDraw scrollbars for Bricks, Beads, Blocks & Bubbles  19/4/07
Dim k As Long, kk As Long, skk As Single
Dim px As Long, py As Long
' For FreeDraw
Dim sstep As Long
Dim xpv As Long, ypv As Long
Dim DL As Single
Dim Extn As Single

' For Stalks & Leaves
Dim pvx As Single, pvy As Single
Dim spx As Single, spy As Single
' For Custom brush
Dim ixs As Long, iys As Long
Dim ixd As Long, iyd As Long
Dim SrcCul As Long ', DesCul As Long
Dim fr As Byte, fg As Byte, fb As Byte
' For Blocks
Dim zRnd As Single

   If NClicks <> 3 Then Exit Sub
   aMouseDown = False
   Screen.MousePointer = vbHourglass
   Select Case FreeDrawTool
   Case [FDraw], [FBroken]
      
      aMouseDown = False
      picDRAW.PSet (xsto(1), ysto(1)), XorCul
      For k = 2 To PointCount - 1
         picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul  ' XClear
      Next k
   
      picDRAW.DrawMode = vbCopyPen
   
      If PointCount <= 3 Then
         DrawDot xsto(1), ysto(1), Cul
         SCOPY = True
         PixelSize = pxsvORG
         picDRAW.DrawWidth = PixelSize
         Screen.MousePointer = vbDefault
         Exit Sub
      End If

      LngToRGB Cul, fr, fg, fb

      If FreeDrawTool = [FDraw] Then
         xpv = xsto(1)
         ypv = ysto(1)
         If Not aAA And aRound Then ' VB
            For k = 2 To PointCount - 1
               ZDrawAALine xsto(k - 1), ysto(k - 1), xsto(k), ysto(k), Cul, 0, 1
            Next k
         Else   ' AA square or round OR non-AA square
            If Not aRound Then   ' Square
               svRound = aRound
               Extn = 0
               sstep = 1
               Select Case PixelSize
               Case 4 To 6: Extn = 0.1
                 sstep = 2
               Case 7 To 8: Extn = 0.1
                 sstep = 2
               Case 9 To 12: Extn = 0.1
                 sstep = 2
               End Select
               DL = 0
               For k = 2 To PointCount - 1 Step sstep
                  If k < 2 + PixelSize Or k >= PointCount - 1 - PixelSize Then
                     aRound = False  ' For square ends
                  Else   ' Forced round
                     aRound = True
                  End If
               
                  DL = DL + (xpv - xsto(k)) * (xpv - xsto(k)) _
                       + (ypv - ysto(k)) * (ypv - ysto(k))
                  If DL >= 4 Then
                     ZDrawAALine xpv, ypv, xsto(k), ysto(k), Cul, Extn, 0
                     xpv = xsto(k)
                     ypv = ysto(k)
                     DL = 0
                  End If
               Next k
               aRound = svRound
            
            Else  ' Round
               sstep = 1
               Select Case PixelSize
               Case 3 To 6
                 sstep = 2
               Case 7 To 9
                 sstep = 2
               Case 10 To 12
                 sstep = 2
               End Select
               
               For k = 2 To PointCount - 1 Step sstep
                     ZDrawAALine xpv, ypv, xsto(k), ysto(k), Cul, 0, 0
                     xpv = xsto(k)
                     ypv = ysto(k)
               Next k
            End If
         End If
         DISPLAY Form1, picDRAW
         
      Else  ' [FBroken]
            
         Select Case PixelSize
         Case Is < 6: sstep = 2
         Case Is < 9: sstep = 3
         Case Else: sstep = 4
         End Select
         
         If Not aAA And aRound Then ' VB
            For k = 2 To PointCount - 2 Step sstep
               ZDrawAALine xsto(k - 1), ysto(k - 1), xsto(k), ysto(k), Cul, 0, 1
               'picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), Cul
            Next k
         Else   ' AA square or round OR non-AA square
            If Not aRound Then   ' Square
               For k = 2 To PointCount - 2 Step sstep
                  ZDrawAALine xsto(k - 1), ysto(k - 1), xsto(k), ysto(k), Cul, 0.5
               Next k
            Else
               For k = 2 To PointCount - 2 Step sstep
                  ZDrawAALine xsto(k - 1), ysto(k - 1), xsto(k), ysto(k), Cul, 0, 0
               Next k
            End If
         End If
         DISPLAY Form1, picDRAW
         
      End If
   
   Case [FStalks]
      aMouseDown = False
      ' Clear
      zyscale = (Rnd(-2) - 0.5) * 16
      zxscale = Rnd * 6 - 3
      picDRAW.PSet (xsto(1), ysto(1)), XorCul
      For k = 2 To PointCount - 1
         picDRAW.Line -(xsto(k), ysto(k)), XorCul
         picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + 1 * zxscale, ysto(k) - 1 * zyscale), XorCul
         picDRAW.Line -Step(1 * zxscale, -2 * zyscale), XorCul
         picDRAW.Line -Step(0 * zxscale, -1 * zyscale), XorCul
         picDRAW.PSet (xsto(k), ysto(k)), XorCul
      Next k
      
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      pxsv = PixelSize
      PixelSize = 1
      picDRAW.DrawWidth = 1
      NSteps = PointCount
      If NSteps < 1 Then NSteps = 1
      GetColorSteps
      pvx = xsto(1)
      pvy = ysto(1)
      For k = 2 To PointCount - 1
         zyscale = (Rnd - 0.5) * pxsv + 2    'Rnd * 5    ' + Variations
         zxscale = (Rnd - 0.5) * pxsv * 2    'Rnd * 5 - 3
         spx = xsto(k)
         spy = ysto(k)
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         pvx = spx: pvy = spy
         spx = pvx + zxscale: spy = pvy - zyscale
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         pvx = spx: pvy = spy
         spx = pvx + 2 * zxscale: spy = pvy - 3 * zyscale
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         pvx = spx: pvy = spy
         spx = pvx + 2 * zxscale: spy = pvy - 4 * zyscale
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         GetNextColors
      Next k
      DISPLAY Form1, picDRAW
   
   Case [FLeaves]
      aMouseDown = False
      zxscale = Rnd(-2) * 4 - 2
      ' Clear
      picDRAW.PSet (xsto(1), ysto(1)), XorCul
      For k = 2 To PointCount - 1
         picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + 3 * zxscale, ysto(k) - 1), XorCul
         picDRAW.Line -Step(1 * zxscale, 2 * zxscale), XorCul
         picDRAW.Line -Step(1 * zxscale, 2 * zxscale), XorCul
         picDRAW.Line -Step(-2 * zxscale, -1 * zxscale), XorCul
         picDRAW.Line -Step(-1 * zxscale, -1 * zxscale), XorCul
         picDRAW.Line -Step(-1 * zxscale, -1 * zxscale), XorCul
         picDRAW.Line -Step(-1 * zxscale, -2 * zxscale), XorCul
         picDRAW.PSet (xsto(k), ysto(k)), XorCul
      Next k
      
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      pxsv = PixelSize
      PixelSize = 1
      picDRAW.DrawWidth = 1
      NSteps = PointCount
      If NSteps < 1 Then NSteps = 1
      GetColorSteps
      pvx = xsto(1)
      pvy = ysto(1)
      For k = 2 To PointCount - 1
         zyscale = (Rnd - 0.5) * pxsv + 2 - 2 'Rnd * 5    ' + Variations
         zxscale = (Rnd - 0.5) * pxsv * 2   'Rnd * 5 - 3
         spx = pvx + 3 * zxscale
         spy = pvy - 1
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         pvx = spx: pvy = spy
         spx = pvx + zxscale: spy = pvy + 2 * zyscale
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         pvx = spx: pvy = spy
         spx = pvx + zxscale: spy = pvy + 2 * zyscale
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         pvx = spx: pvy = spy
         spx = pvx - 2 * zxscale: spy = pvy - zyscale
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         pvx = spx: pvy = spy
         spx = pvx - zxscale: spy = pvy - zyscale
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         pvx = spx: pvy = spy
         spx = pvx - zxscale: spy = pvy - zyscale
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         pvx = spx: pvy = spy
         spx = pvx - zxscale: spy = pvy - 2 * zyscale
         ZDrawAALine pvx, pvy, spx, spy, RGB(zRL, zGL, zBL), 0.5
         pvx = xsto(k): pvy = ysto(k) + 2 * zyscale
         GetNextColors
      Next k
      DISPLAY Form1, picDRAW
   
   Case [FRibbon], [BRibbon], [FRibbonSolid], [BRibbonSolid], _
        [RibbonHorz], [RibbonHorzSolid], [RibbonVert], [RibbonVertSolid]
   
      aMouseDown = False
      ' Clear
      picDRAW.DrawMode = vbXorPen
      picDRAW.PSet (xsto(1), ysto(1)), XorCul
      For k = 2 To PointCount - 1
         picDRAW.Line (xsto(k) + rx1, ysto(k) + ry1)- _
             (xsto(k) + rx2, ysto(k) + ry2), XorCul
      Next k
      
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      picDRAW.PSet (xsto(1), ysto(1)), Cul   ' ??
      pxsv = PixelSize
      LngToRGB Cul, fr, fg, fb
      
      For k = 2 To PointCount - 1
         xsto(k) = xsto(k)
         ysto(k) = ysto(k)
         
         Select Case FreeDrawTool
         Case [FRibbonSolid], [BRibbonSolid], [RibbonHorzSolid], [RibbonVertSolid]
            
            ' Solid ribbons
            'picDRAW.DrawWidth = 1 ' 2
            PixelSize = 1
            If aAA Then
               PixelSize = 2
            End If
            
            LineLength = Sqr((xsto(k) - xsto(k - 1)) ^ 2 + (ysto(k) - ysto(k - 1)) ^ 2)
            zangle = zATan2(ysto(k) - ysto(k - 1), xsto(k) - xsto(k - 1))
            cosA = Cos(zangle)
            sinA = Sin(zangle)
            NDots = LineLength   ' / Dis Dis = 1
            
            For kk = 0 To NDots - 1
               px = xsto(k - 1) + kk * cosA
               py = ysto(k - 1) + kk * sinA
               
               If Not aAA Then
                  BresLine px + rx1, py + ry1, px + rx2, py + ry2, fr, fg, fb, 1, 1
               Else  ' AA
                  ZDrawAALine px + rx1, py + ry1, px + rx2, py + ry2, Cul  ', 0.5
                  AntiAlias picDATA(), px + rx1, py + ry1, Cul, 5, 0, 1
                  AntiAlias picDATA(), px + rx2, py + ry2, Cul, 5, 0, 1
               End If
            Next kk
            
         Case Else  'Sparse ribbons
            
            PixelSize = 1
            
            If Not aAA Then
               BresLine xsto(k) + rx1, ysto(k) + ry1, xsto(k) + rx2, ysto(k) + ry2, fr, fg, fb, 0, 1
            Else
               ZDrawAALine xsto(k) + rx1, ysto(k) + ry1, xsto(k) + rx2, ysto(k) + ry2, Cul, 0.5
            End If
            
         End Select
      Next k
      DISPLAY Form1, picDRAW
      
   Case [FreeSpray]
      For k = 1 To PointCount - 1
         picDRAW.PSet (xsto(k), ysto(k)), XorCul ' Clear
      Next k
      picDRAW.DrawMode = vbCopyPen
      picDRAW.DrawWidth = 1

      For k = 1 To PointCount - 1
         DrawDot xsto(k), ysto(k), Cul  ' Draw
      Next k
      DISPLAY Form1, picDRAW
   
   Case [FBricks]
      aMouseDown = False
      picDRAW.DrawMode = vbCopyPen  ' Draw
      FillInBricks picDRAW, Cul
      Form1.SHP(0).Visible = False
      Form1.SHP(1).Visible = False
   
   Case [FBeads]     ' BEADS
      aMouseDown = False
      ' Clear
      picDRAW.PSet (xsto(1), ysto(1)), XorCul
      For k = 2 To PointCount - 1
         picDRAW.Line -(xsto(k), ysto(k)), XorCul
      Next k
      
      LngToRGB Cul, fr, fg, fb
      
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      
      pxsv = PixelSize
      zradx = pxsv '+ 1
      zrady = zradx
      
      Cul2 = RightColor
      If StartClick = vbRightButton Then Cul2 = LeftColor
      
      If Not aAA Then
         For k = 1 To PointCount - 2
            BresLine xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), fr, fg, fb, 0, 1
            PixelSize = 1
            ZAACIRLIPSE xsto(k), ysto(k), zradx, zrady, Cul, 0, 2 * pi#, 1
            For skk = zradx - 1 To 0 Step -0.5
               ZAACIRLIPSE xsto(k), ysto(k), skk, skk, Cul2, 0, 2 * pi#, 1
            Next skk
         Next k
      Else   ' AA
         For k = 1 To PointCount - 2
            PixelSize = 1
            ZDrawAALine xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), Cul
            ZAACIRLIPSE xsto(k), ysto(k), zradx, zrady, Cul, 0, 2 * pi#
            For skk = zradx - 1 To 0 Step -0.5
               ZAACIRLIPSE xsto(k), ysto(k), skk, skk, Cul2, 0, 2 * pi#, 1
            Next skk
         Next k
      End If
      DISPLAY Form1, picDRAW
      PixelSize = pxsv
      
   Case [FBlocks] '
      aMouseDown = False
      ' Clear
      picDRAW.PSet (xsto(1), ysto(1)), XorCul
      For k = 2 To PointCount - 1
         picDRAW.Line -(xsto(k), ysto(k)), XorCul
      Next k
      
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      
      Cul2 = RightColor
      If StartClick = vbRightButton Then Cul2 = LeftColor
      
      pxsv = PixelSize
      PixelSize = 1
      For k = 2 To PointCount - 1 Step 2
         zRnd = Rnd
         BresBoxFill xsto(k - 1), ysto(k - 1), xsto(k) + 8 + 4 * zRnd, ysto(k) - 8 * pxsv * zRnd, Cul2, 0, 1
         BresBox xsto(k - 1), ysto(k - 1), xsto(k) + 8 + 4 * zRnd, ysto(k) - 8 * pxsv * zRnd, Cul
      Next k
      DISPLAY Form1, picDRAW
      
'      ' Random line
'      Dim px1 As Long, py1 As Long
'      Dim px2 As Long, py2 As Long
'      pxsv = PixelSize
'      px1 = xsto(1)
'      py1 = ysto(1)
'      For k = 2 To PointCount - 1
'         px2 = xsto(k) + 8 + 4 * Rnd
'         py2 = ysto(k) - 8 * pxsv * Rnd
'         picDRAW.Line (px1, py1)-(px2, py2), Cul
'         px1 = px2
'         py1 = py2
'      Next k
      
   Case [FBubbles]      ' BUBBLES
      aMouseDown = False
      ' Clear
      picDRAW.PSet (xsto(1), ysto(1)), XorCul
      For k = 2 To PointCount - 1
         picDRAW.Line -(xsto(k), ysto(k)), XorCul
      Next k
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      
      Cul2 = RightColor
      If StartClick = vbRightButton Then Cul2 = LeftColor
      
      pxsv = PixelSize
      If Not aAA Then
         For k = 2 To PointCount - 1 Step 2
            zradx = 2 * pxsv * Rnd + 1
            PixelSize = 1
            zrady = zradx

            ZAACIRLIPSE xsto(k - 1), ysto(k - 1), zradx, zradx, Cul, 0, 2 * pi#, 1
            For skk = zradx - 1 To 0 Step -0.5
               ZAACIRLIPSE xsto(k - 1), ysto(k - 1), skk, skk, Cul2, 0, 2 * pi#, 1
            Next skk
         Next k
      Else  ' AA
         For k = 2 To PointCount - 1 Step 2
            zradx = 2 * pxsv * Rnd + 1
            PixelSize = 1
            zrady = zradx
            ZAACIRLIPSE xsto(k - 1), ysto(k - 1), zradx, zrady, Cul, 0, 2 * pi#
            For skk = zradx - 1 To 0 Step -0.5
               ZAACIRLIPSE xsto(k - 1), ysto(k - 1), skk, skk, Cul2, 0, 2 * pi#, 1
            Next skk
         Next k
      End If
      DISPLAY Form1, picDRAW
      PixelSize = pxsv
   
   Case [FDotted]
      If NClicks = 3 Then ' Complete
         aMouseDown = False
         For k = 1 To PointCount - 1   ' <<<< Clear
            picDRAW.PSet (xsto(k), ysto(k)), XorCul
         Next k
         
         picDRAW.DrawMode = vbCopyPen  ' <<<< Draw
         For k = 1 To PointCount - 1
             DrawDot xsto(k), ysto(k), Cul
         Next k
         DISPLAY Form1, picDRAW
      End If
      
   Case [FSpacedDots]
      If NClicks = 3 Then ' Complete
         aMouseDown = False
         For k = 1 To PointCount - 1   ' <<<< Clear
            picDRAW.PSet (xsto(k), ysto(k)), XorCul
         Next k
         
         picDRAW.DrawMode = vbCopyPen  ' <<<< Draw
         For k = 1 To PointCount - 1
             DrawDot xsto(k), ysto(k), Cul
         Next k
         DISPLAY Form1, picDRAW
      End If
      
   Case [FCustom]
      If NClicks = 3 Then ' Complete
         aMouseDown = False
         picDRAW.DrawWidth = 1
         For k = 1 To PointCount - 1   ' <<<< Clear
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k) + StampW - 1, ysto(k) + StampH - 1), XorCul, B
         Next k
         
         picDRAW.DrawMode = vbCopyPen  ' <<<< Draw
         '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
         ' Alternative Scan PicSelect & picDraw - Sprite?
         For k = 1 To PointCount - 1
            iyd = ysto(k)
            ixd = xsto(k)
            For iys = 0 To StampH - 1
            For ixs = 0 To StampW - 1
               SrcCul = GetPixel(Form1.picSelect.hDC, ixs, iys)
               If SrcCul >= 0 Then
                  If SrcCul <> RGB(191, 197, 201) Then
                     DrawDot ixd, iyd, SrcCul
                     'SetPixelV picDRAW.hDC, ixd, iyd, SrcCul
                  End If
               End If
               ixd = ixd + 1
               If ixd > xsto(k) + StampW - 1 Then
                  iyd = iyd + 1
                  ixd = xsto(k)
               End If
            Next ixs
            Next iys
         Next k
         DISPLAY Form1, picDRAW

         '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
         picDRAW.Picture = picDRAW.Image
      
      End If
      
   End Select
   SCOPY = True
   Screen.MousePointer = vbDefault
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub CompleteALines(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim k As Long
Dim sik As Single
Dim zD As Single ', zLen As Single
Dim zdx As Single, zdy As Single
Dim CA As Single, SA As Single
' To centre final line
Dim kx1 As Single, ky1 As Single
Dim kx2 As Single, ky2 As Single
Dim rmd As Single  ' End semi rad-dis
Dim Exten As Single
Dim Anti As Long
   
   If NClicks <> 3 Then Exit Sub
   
   Select Case LineTool
   Case [LSolid], [LBEZ], [LBEZFilled]
      aMouseDown = False
      If LineTool = [LSolid] Then
         Anti = 0
         If Not aAA Then Anti = 1

         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul ' Clear
         picDRAW.DrawMode = vbCopyPen
         
         ZDrawAALine xsto(1), ysto(1) + 1, xsto(2), ysto(2) + 1, Cul, 0, Anti
         If aAA And PixelSize = 1 Then   ' Draw nonAA line as well
            ZDrawAALine xsto(1), ysto(1) + 1, xsto(2), ysto(2) + 1, Cul, 0, 1
         End If
         DISPLAY Form1, picDRAW
      
      ElseIf LineTool = [LBEZ] Or LineTool = [LBEZFilled] Then
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul ' <<<<<<<< Clear
         If PixelSize = 1 Then picDRAW.PSet (xsto(2), ysto(2)), XorCul
         aBEZ = True
      End If
   
   Case [LDotted]
      aMouseDown = False
      picDRAW.DrawMode = vbXorPen  ' <<<<<<<< Clear old
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
      picDRAW.DrawMode = vbCopyPen  ' <<<<<<<< Draw with Cul
      
      'picDRAW.DrawWidth = 1
      DrawDottedLine xsto(1), ysto(1), xsto(2), ysto(2), Cul
      DISPLAY Form1, picDRAW
      
   Case [LDouble], [LDoubleEnded]
      aMouseDown = False
      
      picDRAW.DrawMode = vbXorPen  ' <<<<<<<< Clear old
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
      If PixelSize = 1 Then picDRAW.PSet (xsto(2), ysto(2)), XorCul
      GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), _
         ixa, iya, ixb, iyb ', cosA, sinA
      picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      If PixelSize = 1 Then picDRAW.PSet (ixb, iyb), XorCul
      
      picDRAW.DrawMode = vbCopyPen
      
      Anti = 0
      If Not aAA Then Anti = 1
      ' Draw
      ZDrawAALine xsto(1), ysto(1) + 1, xsto(2), ysto(2) + 1, Cul, 0, Anti
      GetParallelCoords zspace, xsto(1), ysto(1) + 1, xsto(2), ysto(2) + 1, _
         ixa, iya, ixb, iyb ', cosA, sinA
      ZDrawAALine ixa, iya, ixb, iyb, Cul, 0, Anti
      If aAA And PixelSize = 1 Then
         ZDrawAALine xsto(1), ysto(1) + 1, xsto(2), ysto(2) + 1, Cul, 0, 1
         ZDrawAALine ixa, iya, ixb, iyb, Cul, 0, 1
      End If
      DISPLAY Form1, picDRAW
      
      Select Case LineTool ' DO ENDS
      Case [LDoubleEnded]
         Exten = 0
         zdx = 0
         zdy = 0
         If Not aRound Then
            Select Case PixelSize
            Case 4: Exten = 0.4
            Case 5: Exten = 0.45
            Case 6: Exten = 0.45
            Case 7: Exten = 0.45
            Case 8: Exten = 0.45
            Case 9: Exten = 0.46
            Case 10: Exten = 0.455
            Case 11: Exten = 0.46
            Case 12: Exten = 0.46
            End Select
         End If
      
         ZDrawAALine xsto(1), ysto(1), ixa, iya, Cul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), ixb, iyb, Cul, Exten, Anti
         If PixelSize = 1 Then
            ZDrawAALine xsto(1), ysto(1), ixa, iya, Cul, 0, 1
            ZDrawAALine xsto(2), ysto(2), ixb, iyb, Cul, 0, 1
         End If
         DISPLAY Form1, picDRAW
      
      End Select
      
   Case [LGradient] ' Add shading
      ' Color linear steps (LeftColor to RightColor) or (LeftColor)
      aMouseDown = False
      Screen.MousePointer = vbHourglass
      picDRAW.DrawWidth = PixelSize
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul ' Clear
      
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      picDRAW.DrawWidth = 1
      NSteps = PixelSize ' Arbitrary steps
      
      zIStep = (PixelSize) / NSteps
      
      zD = Sqr((xsto(1) - xsto(2)) ^ 2 + (ysto(1) - ysto(2)) ^ 2)
      If zD = 0 Then GoTo LGradientEXIT
      
      zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
      
      zdx = PixelSize * Sin(zangle)
      zdy = PixelSize * Cos(zangle)
      
      GetColorSteps
      pxsv = PixelSize
      PixelSize = 2
      
      ' Also adjust for position wrt mouse
      kx1 = xsto(1) + 0.5 * zdx
      ky1 = ysto(1) - 0.5 * zdy
      kx2 = xsto(2) + 0.5 * zdx
      ky2 = ysto(2) - 0.5 * zdy

'      ' Adjust location of final line to match Xor line
      GetParallelCoords -CSng(pxsv), kx1, ky1, kx2, ky2, _
            ixa, iya, ixb, iyb ', cosA, sinA
            
      kx1 = ixa
      ky1 = iya
      kx2 = ixb
      ky2 = iyb
      If pxsv >= 3 And aRound Then  ' To vary length at ends
         zangle = zATan2(ky2 - ky1, kx2 - kx1)
         CA = Cos(zangle)
         SA = Sin(zangle)
         sik = 2 * pxsv / (NSteps)
      End If
      
      For k = 1 To NSteps
      
         If Not aAA Then
            ZDrawAALine ixa, iya, ixb, iyb, RGB(zRL, zGL, zBL), Exten, 1
         Else  ' AA
            If k = 1 Or k = NSteps Then
               Exten = 0
               If aRound Then
                  Exten = 4
                  If k = NSteps Then Exten = 0.5
               End If
               ZDrawAALine ixa, iya, ixb, iyb, RGB(zRL, zGL, zBL), Exten
            Else
               ZDrawAALine ixa, iya, ixb, iyb, RGB(zRL, zGL, zBL), Exten
            End If
         End If
         
         GetParallelCoords zIStep * k, kx1, ky1, kx2, ky2, _
            ixa, iya, ixb, iyb ', cosA, sinA
         
         If pxsv >= 3 And aRound Then   ' Vary length at ends for round end
            'zD = Sqr((pxsv) ^ 2 - (pxsv - k * sik) ^ 2)
            ' radius of semi-circle = pxsv &
            ' distance from edge = k*sik
            rmd = (2 * pxsv - (k * sik))
            If rmd < 0 Then rmd = 0
            zD = Sqr((k * sik) * rmd)
            'zD = Sqr((k * sik) * (2 * pxsv - (k * sik)))
            zdx = zD * CA
            zdy = zD * SA
            ixa = ixa - zdx
            iya = iya - zdy
            ixb = ixb + zdx
            iyb = iyb + zdy
         End If
         GetNextColors   ' Shade colors Else fixed at LeftColor
      Next k
      DISPLAY Form1, picDRAW

LGradientEXIT:
      PixelSize = pxsv
      Screen.MousePointer = vbDefault

   Case [LAGradient]

      ' Color linear steps (LeftColor to RightColor) or (LeftColor)
      aMouseDown = False
      Screen.MousePointer = vbHourglass
      picDRAW.DrawWidth = PixelSize
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul ' Clear
      picDRAW.DrawMode = vbCopyPen
      
      ' Draw
      zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
      zD = Sqr((xsto(1) - xsto(2)) ^ 2 + (ysto(1) - ysto(2)) ^ 2)
      NSteps = zD
     
      If NSteps = 0 Then GoTo LAGradientEXIT
      zIStep = 1
      pxsv = PixelSize
      If (PixelSize And 1) = 1 Then ' ODD PixelSize
         PixelSize = PixelSize + 1 ' Make even
      End If
      
      If aRound Then
         Rad = (PixelSize) / 2
         ZDD = zD + 2 * Rad
         NSteps = ZDD
         xc = xsto(1)
         yc = ysto(1)
         Alpha = -zangle
         dx = Rad * Sin(Alpha)
         DY = Rad * Cos(Alpha)
         x1 = xc - dx: y1 = yc - DY
         x2 = xc + dx: y2 = yc + DY
         
         dx = Rad * Cos(Alpha)
         DY = Rad * Sin(Alpha)
         xt1 = x1 - dx: yt1 = y1 + DY  ' From Tangent line
         xt2 = x2 - dx: yt2 = y2 + DY
         
         GetColorSteps
   
         ' First semi-circ
         For zstep = 0 To ZDD '- 1
             If zstep <= Rad + 1 Then
               zspace = zstep
               ' Parallel and to right of left tangent
               GetParallelCoords zspace, xt1, yt1, xt2, yt2, ixa, iya, ixb, iyb
               'ModMaths.
               GetLineCircPts ixa, iya, ixb, iyb, xc, yc, Rad, ix1, iy1, ix2, iy2, NegTest
               If NegTest > 0 Then
                  If pxsv = 1 Then
                     picDRAW.DrawWidth = 1
                  Else
                     picDRAW.DrawWidth = 2
                  End If
                  PixelSize = 2 ' Faster ??
                  ZDrawAALine ix1, iy1, ix2, iy2, RGB(zRL, zGL, zBL), 0, 1
                  If aAA Then    ' !!! Cross lines are going beyond semi ???
                     PixelSize = 2 ' Solved
                     AntiAlias picDATA(), ix1, iy1, RGB(zRL, zGL, zBL), 5, 0, 1
                     AntiAlias picDATA(), ix2, iy2, RGB(zRL, zGL, zBL), 5, 0, 1
                  End If
               End If
            End If
            
            ' Straight section for aRound
            If zstep >= Rad + 1 And zstep <= ((ZDD - Rad)) Then
               zspace = zstep - (Rad + 1) + 1
               GetParallelCoords zspace, x1, y1, x2, y2, ixa, iya, ixb, iyb
               PixelSize = 2 ' Faster ??
               ZDrawAALine ixa, iya, ixb, iyb, RGB(zRL, zGL, zBL), 0, 0  '1
               If aAA Then
   ' avh  = 0 Area, 1 VertLeft, 2 VertRight, 3 HorzTop, 4 HorzBottom, 5 Change area weighting
   ' Anti = 0 (default) do AA, = 1 No AA just set pixels to DCul
                  PixelSize = 2 ' Faster ??
                  AntiAlias picDATA(), ixa, iya, RGB(zRL, zGL, zBL), 5, 0, 1
                  AntiAlias picDATA(), ixb, iyb, RGB(zRL, zGL, zBL), 5, 0, 1
               End If
            End If
             
             ' Second semi-circ
             If zstep > (ZDD - Rad) Then
               xc = xsto(2)
               yc = ysto(2)
               dx = (Rad) * Sin(Alpha)
               DY = (Rad) * Cos(Alpha)
               x1 = xc + dx: y1 = yc + DY ' From last square end
               x2 = xc - dx: y2 = yc - DY

               zspace = zstep - (ZDD - Rad)
               ' Parallel and to right of diameter
               GetParallelCoords -zspace, x1, y1, x2, y2, ixa, iya, ixb, iyb
               'ModMaths.
               GetLineCircPts ixa, iya, ixb, iyb, xc, yc, Rad, ix1, iy1, ix2, iy2, NegTest
               If NegTest > 0 Then
                  If pxsv = 1 Then
                     picDRAW.DrawWidth = 1
                  Else
                     picDRAW.DrawWidth = 2
                  End If
                  PixelSize = 2 ' Faster ??
                  ZDrawAALine ix1, iy1, ix2, iy2, RGB(zRL, zGL, zBL), 0, 0  '1
                  'picDRAW.Line (ix1, iy1)-(ix2, iy2), RGB(zRL, zGL, zBL)
                  If aAA Then
                     PixelSize = 2
                     AntiAlias picDATA(), ix1, iy1, RGB(zRL, zGL, zBL), 5, 0, 1
                     AntiAlias picDATA(), ix2, iy2, RGB(zRL, zGL, zBL), 5, 0, 1
                  End If
               End If
            End If
            GetNextColors   ' Shade colors Else fixed at LeftColor
         
         Next zstep
   
      Else  ' Square ends  as before
         
         zdx = PixelSize * Sin(zangle)
         zdy = PixelSize * Cos(zangle)
         ' End points of cross-lines & shifted center to mouse
         kx1 = xsto(1) + 0.5 * zdx
         ky1 = ysto(1) - 0.5 * zdy
         kx2 = xsto(1) - 0.5 * zdx
         ky2 = ysto(1) + 0.5 * zdy
         
         ixa = kx1
         iya = ky1
         ixb = kx2
         iyb = ky2
         
         GetColorSteps
         
         For k = 1 To NSteps + 1
            PixelSize = 2 ' Faster ??
            ZDrawAALine ixa, iya, ixb, iyb, RGB(zRL, zGL, zBL), 0, 0  '1
            If aAA Then
               PixelSize = 2 ' Faster ??
               AntiAlias picDATA(), ixa, iya, RGB(zRL, zGL, zBL), 5, 0, 1
               AntiAlias picDATA(), ixb, iyb, RGB(zRL, zGL, zBL), 5, 0, 1
            End If
            GetParallelCoords zIStep * k, kx1, ky1, kx2, ky2, ixa, iya, ixb, iyb ', cosA, sinA
            GetNextColors   ' Shade colors Else fixed at LeftColor
         Next k
         
      End If
   End Select
   DISPLAY Form1, picDRAW
   
LAGradientEXIT:
   PixelSize = pxsv
   Screen.MousePointer = vbDefault
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub CompleteBoxes(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim k As Long
'Dim Exten As Single
'Dim DZ As Single

Dim x1 As Long, y1 As Long
Dim x2 As Long, y2 As Long
' For [BAngBoxFilled] ?
'Dim Small As Single
'Dim zLen As Single, zD As Single

'Public svAA As Boolean
'Public pxsv As Long      ' Saving PixelSize
'Public svRound As Boolean   ' Saving aRound

' For centered gradient boxes
Dim kk As Single
Dim stepx As Single, stepy As Single
Dim xe As Single, ye As Single     ' points on ellipse
Dim TAlpha As Single 'Double
   
   If NClicks <> 3 Then Exit Sub
   
   Screen.MousePointer = vbHourglass
   
   Select Case BoxTool
   Case [BSolid]   ' Outline box
      aMouseDown = False
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B   ' XClear
      picDRAW.DrawMode = vbCopyPen  ' Draw
      
      AdjustBoxPoints xsto(1), ysto(1), xsto(2), ysto(2)
      
      If aRound Then
         BresBoxRounded xsto(1), ysto(1) + 0, xsto(2), ysto(2) + 0, Cul, pxsvORG
         DISPLAY Form1, picDRAW
      Else  ' SQ
         BresBox xsto(1), ysto(1) + 1, xsto(2), ysto(2) + 1, Cul, pxsvORG
         DISPLAY Form1, picDRAW
      End If
   
   Case [BDotted]
      aMouseDown = False
      picDRAW.DrawMode = vbXorPen  ' <<<<<<<< Clear old
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B
      
      picDRAW.DrawMode = vbCopyPen  ' <<<<<<<< Draw with Cul
      picDRAW.DrawWidth = 1
      
      If (xsto(1) = xsto(2)) And (ysto(1) = ysto(2)) Then
         DrawDot xsto(1), ysto(1), Cul
      Else
         'AdjustBoxPoints xsto(1), ysto(1), xsto(2), ysto(2)
         DrawDottedBox picDRAW, Cul
      End If
      
      DISPLAY Form1, Form1.picDRAW
   
   Case [BDouble], [BDoubleShadowed]
      aMouseDown = False
      
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B   ' XClear
      GetBoxMoveCoords
      picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul, B   ' Clear
      
      picDRAW.DrawMode = vbCopyPen  ' Draw
      
      Select Case BoxTool
      Case [BDouble]
         
         AdjustBoxPoints xsto(1), ysto(1), xsto(2), ysto(2)
svAA = aAA
         If aAA Then
            If aRound Then
               BresBoxRounded xsto(1), ysto(1) + 1, xsto(2), ysto(2) + 1, Cul, pxsvORG
            Else  ' AA & SQ
               BresBox xsto(1), ysto(1) + 1, xsto(2), ysto(2) + 1, Cul, pxsvORG
            End If
            zspace = zspace + 1
            GetBoxMoveCoords
            If aRound Then
               BresBoxRounded ixa, iya + 1, ixb, iyb + 1, Cul, pxsvORG
            Else  ' AA & SQ
               BresBox ixa, iya + 1, ixb, iyb + 1, Cul, pxsvORG
            End If
            zspace = zspace - 1
         
         Else  ' Not aAA
            If aRound Then
               BresBoxRounded xsto(1), ysto(1), xsto(2), ysto(2), Cul, pxsvORG
               zspace = zspace + 1
               GetBoxMoveCoords
               BresBoxRounded ixa, iya, ixb, iyb, Cul, pxsvORG
               zspace = zspace - 1
            Else  'Not AA & SQUARE
               BresBox xsto(1), ysto(1) + 1, xsto(2), ysto(2) + 1, Cul, pxsvORG
               GetBoxMoveCoords
               BresBox ixa, iya + 1, ixb, iyb + 1, Cul, pxsvORG
            End If
         End If
         
         DISPLAY Form1, picDRAW
         
         Case [BDoubleShadowed]
            
            'PixelSize = 1 : aAa = False Set at StartBox
            AdjustBoxPoints xsto(1), ysto(1), xsto(2), ysto(2)
            ' Double Boxes draw in ShadowBox
            ShadowBox picDRAW ' with Public xysto(1 & 2)
            
            aRound = svRound
            PixelSize = pxsv
            aAA = svAA
         End Select
      
   Case [BGradient6]   ' Filled box - not angled
      aMouseDown = False
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B   ' XClear
      picDRAW.DrawMode = vbCopyPen  ' Draw
      
      x1 = xsto(1): y1 = ysto(1)
      x2 = xsto(2): y2 = ysto(2)
      AdjustShadeBoxPoints x1, y1, x2, y2
      
      If aAA Then
         ' Box outline first
         BresBoxRounded x1, y1 + 1, x2, y2 + 1, Cul, pxsvORG
         ' Fill in
         BresBoxFill x1, y1, x2, y2, Cul, 0, 1
         DISPLAY Form1, picDRAW
      Else  ' Not AA  NB BF will not do Round
         If aRound Then
            BresBoxRounded x1, y1 + 1, x2, y2 + 1, Cul, pxsvORG
            BresBoxFill x1, y1 + 1, x2, y2 + 1, Cul, 0, 1
            DISPLAY Form1, picDRAW
         Else   ' Not AA & Square here
            BresBox x1, y1 + 1, x2, y2 + 1, Cul, pxsvORG
            BresBoxFill x1, y1, x2, y2, Cul, 0, 1
            DISPLAY Form1, picDRAW
         End If
      End If
   
   Case [BGradient1]   ' Horz Gradient    ||  No AA or Rounded
      aMouseDown = False
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B    ' XClear
      picDRAW.DrawMode = vbCopyPen  ' Draw
      
      AdjustBoxPoints xsto(1), ysto(1), xsto(2), ysto(2)
      
      NSteps = 1000 ' Arbitrary steps
      zIStep = Abs(xsto(2) - xsto(1)) / NSteps
      GetColorSteps
      If Not aRound Then picDRAW.DrawWidth = 1
      For k = 0 To NSteps - 1
         GetParallelCoords zIStep * k, _
            xsto(1), ysto(1), xsto(1), ysto(2), _
            ixa, iya, ixb, iyb ', cosA, sinA
         BresLine ixa, iya, ixb, iyb, CByte(zRL), CByte(zGL), CByte(zBL), 0, 1
         GetNextColors
      Next k
      DISPLAY Form1, picDRAW
   
   Case [BGradient2]   ' Vert Gradient  = No AA or Rounded
      aMouseDown = False
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B    ' XClear
      picDRAW.DrawMode = vbCopyPen  ' draw
      AdjustBoxPoints xsto(1), ysto(1), xsto(2), ysto(2)
      
      NSteps = 1000 ' Arbitrary steps
      zIStep = Abs(ysto(2) - ysto(1)) / NSteps
      GetColorSteps
      If Not aRound Then picDRAW.DrawWidth = 1
      For k = 0 To NSteps - 1
         GetParallelCoords zIStep * k, _
            xsto(2), ysto(1), xsto(1), ysto(1), _
            ixa, iya, ixb, iyb ', cosA, sinA
         BresLine ixa, iya, ixb, iyb, CByte(zRL), CByte(zGL), CByte(zBL), 0, 1
         GetNextColors
      Next k
      DISPLAY Form1, picDRAW
   
   Case [BGradient3]   ' Concentric gradient      O   No AA or Rounded
      aMouseDown = False
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B    ' XClear
      
      picDRAW.DrawMode = vbCopyPen  ' Draw
      
      svAA = aAA
      aAA = False
      
      AdjustBoxPoints xsto(1), ysto(1), xsto(2), ysto(2)
      
      NSteps = 1000
      zIStep = Abs(xsto(2) - xsto(1)) / (2 * NSteps)
      If zIStep > Abs(ysto(2) - ysto(1)) / (2 * NSteps) Then
         zIStep = Abs(ysto(2) - ysto(1)) / (2 * NSteps)
      End If
      GetColorSteps
      If Not aRound Then picDRAW.DrawWidth = 1
      For k = 0 To NSteps - 1
         ixa = xsto(1) + k * zIStep
         iya = ysto(1) + k * zIStep
         ixb = xsto(2) - k * zIStep
         iyb = ysto(2) - k * zIStep
         BresBox ixa, iya, ixb, iyb, RGB(zRL, zGL, zBL)
         GetNextColors
      Next k
      aAA = svAA
      DISPLAY Form1, picDRAW
   
   Case [BGradient33]   ' Center gradient
      aMouseDown = False
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B    ' XClear
      
      picDRAW.DrawMode = vbCopyPen  ' Draw
      AdjustBoxPoints xsto(1), ysto(1), xsto(2), ysto(2)
      EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
      zradx = (zradx + Abs(xsto(2) - xsto(1)) / 2) / 2
      zrady = (zrady + Abs(ysto(2) - ysto(1)) / 2) / 2
      If zradx = 0 Then zradx = 1
      If zrady = 0 Then zrady = 1
      If zradx > zrady Then
         NSteps = zradx * 4
      Else
         NSteps = zrady * 4
      End If
      If zrad = 0 Then zrad = 1
      ixc = (xsto(1) + xsto(2)) / 2
      iyc = (ysto(1) + ysto(2)) / 2
      stepx = zradx / NSteps
      stepy = zrady / NSteps
   
      pxsv = PixelSize
      ArcStep = 2 / zradx
      If zrady > zradx Then ArcStep = 2 / zrady
      GetColorSteps
      For kk = 0 To NSteps - 1
         ' Run around ellipse
         For TAlpha = 0 To 2 * pi# + ArcStep / 2 Step ArcStep / 2  ' Necessary to avoid holes
            xe = ixc + zradx * Cos(TAlpha)
            ye = iyc + zrady * Sin(TAlpha)
            If xe > xsto(1) And xe < xsto(2) Then
            If ye > ysto(1) And ye < ysto(2) Then
               xe = xe + xlo
               ye = picDRAW.Height - ye + ylo
               If xe > 0 And xe < FullW - 1 Then
               If ye > 0 And ye < FullH - 1 Then
                  'SetPixelV picDRAW.hDC, xe, ye, RGB(zRL, zGL, zBL)   ' speed ~ 10 sec
                  ' Speed ~5 sec
                  picDATA(0, xe, ye) = zBL
                  picDATA(1, xe, ye) = zGL
                  picDATA(2, xe, ye) = zRL
               End If
               End If
            End If
            End If
         Next TAlpha
         zradx = zradx - stepx
         zrady = zrady - stepy
         GetNextColors
      Next kk
      BresBox xsto(1), ysto(1), xsto(2), ysto(2), LeftColor, 1 ', pxsvORG ' Draw outer box
      DISPLAY Form1, picDRAW
   
   Case [BGradient4], [BGradient5]  ' Diagonal shading    \\, //
      aMouseDown = False
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul, B    ' XClear
      
      picDRAW.DrawMode = vbCopyPen  ' Draw
      
      svAA = aAA
      aAA = False
      
      AdjustBoxPoints xsto(1), ysto(1), xsto(2), ysto(2) ' So that shading always \\
      
      NSteps = 1000
      zx1 = xsto(1): zy1 = ysto(1)
      zx2 = xsto(2): zy2 = ysto(2)
      GetColorSteps
      zdx = (zx2 - zx1) / NSteps
      zdy = (zy2 - zy1) / NSteps
      If Not aRound Then picDRAW.DrawWidth = 1
      If BoxTool = [BGradient4] Then  ' \\
         'For k = 0 To NSteps - 1
         For k = NSteps - 1 To 0 Step -1
            zy = zdy * k
            zx = zdx * k
            BresLine zx1 + zx, zy1, zx2, zy2 - zy, CByte(zRL), CByte(zGL), CByte(zBL), 0, 1
            BresLine zx2 - zx, zy2, zx1, zy1 + zy, CByte(zRL), CByte(zGL), CByte(zBL), 0, 1
            GetNextColors
         Next k
      Else  ' [BGradient5]  //
         'For k = 0 To NSteps - 1
         For k = NSteps - 1 To 0 Step -1
            zy = zdy * k
            zx = zdx * k
            BresLine zx2 - zx, zy1, zx1, zy2 - zy, CByte(zRL), CByte(zGL), CByte(zBL), 0, 1
            BresLine zx2, zy1 + zy, zx1 + zx, zy2, CByte(zRL), CByte(zGL), CByte(zBL), 0, 1
            GetNextColors
         Next k
      End If
      aAA = svAA
      DISPLAY Form1, picDRAW
   
   Case [BAngBox]
      aMouseDown = False
      DrawAngBox picDRAW, XorCul, 0 ' XClear
      
      picDRAW.DrawMode = vbCopyPen
      
      DrawAngBox picDRAW, Cul, 1 ' Draw
      DISPLAY Form1, picDRAW
   
   Case [BAngBoxFilled], [BAngBoxGradient]
      aMouseDown = False
      DrawAngBox picDRAW, XorCul ' XClear
      
      picDRAW.DrawMode = vbCopyPen
      
      DrawFilledAngBox picDRAW, Cul ' Draw
      DISPLAY Form1, picDRAW
      
   Case [BoxTop], [BoxRight], [BoxRounded]
      aMouseDown = False
      DrawPartBox picDRAW, XorCul, 0  ' XClear
      
      picDRAW.DrawMode = vbCopyPen
      DrawPartBox picDRAW, Cul, 1     ' Draw
      DISPLAY Form1, picDRAW
   End Select
   
   Screen.MousePointer = vbDefault
   
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub CompleteCirlipses(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
' ref
'Public zrad As Single, zratio As Single
'Public zradx As Single, zrady As Single
'Private NDots As Long
'Private LineLength As Long
'Private zangle As Single
'Private cosA As Single, sinA As Single
'Private zR As Single
Dim Dzangle As Single   ' For Dotted

Dim dx As Single, DY As Single
Dim xk As Single, yk As Single   ' For Gradient
Dim k As Long
' For offset gradient
Dim zxy As Single, zyx As Single
Dim zz1 As Single, zz2 As Single
Dim zalpha1 As Single
Dim zLen2 As Single, zstep As Single
Dim zxc As Single, zyc As Single
Dim Anti As Long
Dim svAA As Boolean

   Form1.chkAddRadii.Value = Unchecked
   aAddRadii = False
      
   Anti = 0
   If Not aAA Then Anti = 1

   If NClicks = 3 Then
      
      Screen.MousePointer = vbHourglass
   
      Select Case CircTool
      Case [CSolid]     ' Outline
         aMouseDown = False
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' XClear
         picDRAW.DrawMode = vbCopyPen
         
         ' DRAW FINAL ELLIPSE  Needs dot when xysto(1)=xysto(2)
         zAS = 0: zAE = 2 * pi#
         ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, 1
         End If
         DISPLAY Form1, picDRAW
         
      Case [CDotted]
         aMouseDown = False
         picDRAW.DrawMode = vbXorPen  ' <<<<<<<< Clear old
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
         
         picDRAW.DrawMode = vbCopyPen
         picDRAW.DrawWidth = 1
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         If zrad = 0 Then
            DrawDot xsto(1), ysto(1), Cul
         Else
            LineLength = 2 * pi# * zrad
            NDots = LineLength / (2 * PixelSize)
            If zrad = 0 Then zrad = 1
            Dzangle = (2 * PixelSize) / zrad
            For k = 0 To NDots - 1
               dx = zradx * Sin(k * Dzangle)    ' Looks OKish, 3Dish for ellipses
               DY = zrady * Cos(k * Dzangle)
               px = xsto(1) + dx '+ 0.5
               py = ysto(1) + DY '+ 0.5
               DrawDot px, py, Cul
            Next k
         End If
         DISPLAY Form1, picDRAW
         
      Case [CDouble]
         aMouseDown = False
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' XClear
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2), zspace
         zRadDiff = zrad + zspace
         If zRadDiff < 0 Then zRadDiff = 0
         picDRAW.Circle (xsto(1), ysto(1)), zRadDiff, XorCul, , , zratio ' Clear
         picDRAW.DrawMode = vbCopyPen
         
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         ' Inner ring
         ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, Anti
         If PixelSize = 1 And Anti = 0 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, 1
         End If
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2), zspace
         
         If zradx >= zrady Then
            zradx = zradx + zspace
            If zradx < 0 Then zradx = 0
            zrady = zratio * zradx
         Else
            zrady = zrady + zspace
            If zrady < 0 Then zrady = 0
            zradx = zrady / zratio
         End If
         ' outer ring
         ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, Anti
         If PixelSize = 1 And Anti = 0 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, 1
         End If
         DISPLAY Form1, picDRAW
     
      Case [CGradient1]     '||'  2D vert
         aMouseDown = False
         
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
         picDRAW.DrawMode = vbCopyPen
         
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         If zradx = 0 Then zradx = 1
         If zrady = 0 Then zrady = 1
         NSteps = 1 * (2 * zradx)
         GetColorSteps
         PixelSize = 1
         For xk = xsto(1) - zradx To xsto(1) + zradx Step 1 '0.5 '0.1
            DY = zrady * Sqr(1 - (xsto(1) - xk) ^ 2 / zradx ^ 2)
            ZDrawAALine xk, (ysto(1) - DY), xk, (ysto(1) + DY), RGB(zRL, zGL, zBL), 0, 1
            GetNextColors
         Next xk
         ' + Left color Border
         PixelSize = pxsvORG
         dx = 1: DY = 1
         If PixelSize = 1 Then
            dx = 0.5: DY = 0.5
         End If
         ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, LeftColor, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, LeftColor, 0, 2 * pi#, 1
         End If
         DISPLAY Form1, picDRAW
      
      Case [CGradient2]     '=='  2D horz
         aMouseDown = False
         
         EvalCirlipse xsto(1), ysto(1), xp, yp
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
         picDRAW.DrawMode = vbCopyPen
         
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         If zradx = 0 Then zradx = 1
         If zrady = 0 Then zrady = 1
         NSteps = 1 * (2 * zrady)
         GetColorSteps
         PixelSize = 1
         For yk = ysto(1) - zrady To ysto(1) + zrady Step 1 '0.1
            dx = zradx * Sqr(1 - (ysto(1) - yk) ^ 2 / zrady ^ 2)
            ZDrawAALine xsto(1) - dx, yk, xsto(1) + dx, yk, RGB(zRL, zGL, zBL), 0, 1
            GetNextColors
         Next yk
         ' + Left color Border
         PixelSize = pxsvORG
         picDRAW.DrawWidth = 1
         dx = 1.5: DY = 1.5
         If PixelSize = 1 Then
            dx = 0.75: DY = 0.75
         End If
         ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, LeftColor, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, LeftColor, 0, 2 * pi#, 1
         End If
         DISPLAY Form1, picDRAW
      
      Case [CGradient3]       ' (()) 3D onion shading
         aMouseDown = False
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' XClear
         picDRAW.DrawMode = vbCopyPen
         
         svAA = aAA
         aAA = False
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         If zradx = 0 Then zradx = 1
         If zrady = 0 Then zrady = 1
         NSteps = 1 * (2 * zradx)
         GetColorSteps
         PixelSize = 2
         For yk = ysto(1) - zrady To ysto(1) + zrady Step 1
            dx = zradx * Sqr(1 - (ysto(1) - yk) ^ 2 / zrady ^ 2)
            GetColorSteps
            For xk = xsto(1) - dx To xsto(1) + dx Step 1
               DrawDot xk, yk, RGB(zRL, zGL, zBL)
               GetNextColors
            Next xk
         Next yk
         aAA = svAA
         PixelSize = pxsvORG
         dx = 1.5: DY = 1.5
         If PixelSize = 1 Then
            dx = 1: DY = 1
         End If
         ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, LeftColor, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, LeftColor, 0, 2 * pi#, 1
         End If
         DISPLAY Form1, picDRAW
         
      Case [CGradient4]       ' (()) turned 90 degress
         aMouseDown = False
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' XClear
         
         picDRAW.DrawMode = vbCopyPen
         
         svAA = aAA
         aAA = False
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         If zradx = 0 Then zradx = 1
         If zrady = 0 Then zrady = 1
         NSteps = 1 * (2 * zrady)
         GetColorSteps
         PixelSize = 2
         For xk = xsto(1) - zradx To xsto(1) + zradx Step 1
            DY = zrady * Sqr(1 - (xsto(1) - xk) ^ 2 / zradx ^ 2)
            GetColorSteps
            For yk = ysto(1) - DY To ysto(1) + DY Step 1
               DrawDot xk, yk, RGB(zRL, zGL, zBL)
               GetNextColors
            Next yk
         Next xk
         aAA = svAA
         PixelSize = pxsvORG
         dx = 1.5: DY = 1.5
         If PixelSize = 1 Then
            dx = 1: DY = 1
         End If
         ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, LeftColor, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, LeftColor, 0, 2 * pi#, 1
         End If
         DISPLAY Form1, picDRAW
      
      Case [CGradient5]   ' O concentric shading
         
         aMouseDown = False
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' XClear
         picDRAW.DrawMode = vbCopyPen
         
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         If zradx = 0 Then zradx = 1
         If zrady = 0 Then zrady = 1
         If zradx > zrady Then
            NSteps = zradx
         Else
            NSteps = zrady
         End If
         GetColorSteps
         PixelSize = 2
         If zrad = 0 Then zrad = 1
         zxy = zradx / zrady
         zyx = zrady / zradx
         If zradx >= zrady Then
            For xk = 0 To zradx Step 1  '-0.5
               BresEllipse xsto(1), ysto(1), zradx - xk, zrady - xk * zyx, RGB(zRL, zGL, zBL), 0, 2, 1
               GetNextColors
            Next xk
         Else
            For xk = 0 To zrady Step 1  '-0.1
               BresEllipse xsto(1), ysto(1), zradx - xk * zxy, zrady - xk, RGB(zRL, zGL, zBL), 0, 2, 1
               GetNextColors
            Next xk
         End If
         PixelSize = pxsvORG
         dx = 1.5: DY = 1.5
         If PixelSize <= 2 Then
            dx = 0.75: DY = 0.75
         End If
         ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, LeftColor, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, LeftColor, 0, 2 * pi#, 1
         End If
         DISPLAY Form1, picDRAW
      
      Case [CFilled]
         
         aMouseDown = False
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' XClear
         picDRAW.DrawMode = vbCopyPen
         
         EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
         If zradx = 0 Then zradx = 1
         If zrady = 0 Then zrady = 1
         If zradx > zrady Then
            NSteps = zradx
         Else
            NSteps = zrady
         End If
         PixelSize = 2
         If zrad = 0 Then zrad = 1
         
          BresEllipse xsto(1), ysto(1), zradx, zrady, Cul, 1, 1, 1
         
         If Anti = 0 Then
            PixelSize = 1
            dx = 0.75 '1.25
            DY = 0.75 '1.25
            'If PixelSize <= 2 Then
            '   dx = 0.75: DY = 0.75
            'End If
            ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, Cul, 0, 2 * pi#, Anti
            If PixelSize = 1 Then
               ZAACIRLIPSE xsto(1), ysto(1), zradx + dx, zrady + DY, Cul, 0, 2 * pi#, 1
            End If
         End If
         PixelSize = pxsvORG
         DISPLAY Form1, picDRAW
      End Select
         

   ElseIf NClicks > 3 Then ' For [COffsetCen], [CRotate] & [CRotateFill]
      
      Screen.MousePointer = vbHourglass
      
      
      aMouseDown = False
      Select Case CircTool
      Case [COffsetCen]
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, , , zratio  ' Clear
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
         picDRAW.DrawMode = vbCopyPen

         'EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)   ' Done earlier & xysto(2) is a line coord
         pxsv = PixelSize
         PixelSize = 1
         picDRAW.DrawWidth = 1
         ' Offcentre shade if x/ysto(2) inside ellipse  xc,yc zradx,zrady
         ' Approx  or use API PtRect. Allows point slightly outside ellipse
         If (xsto(2) > xsto(1) - zradx) And (xsto(2) < xsto(1) + zradx) And _
            (ysto(2) > ysto(1) - zrady) And (ysto(2) < ysto(1) + zrady) Then
            
            zLen2 = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
            zalpha1 = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
            NSteps = 2 * zradx   ' 2
            If zrady > zradx Then
               NSteps = 2 * zrady
            End If
            GetColorSteps
            zstep = 2 * zLen2 / NSteps
            If zstep = 0 Then zstep = 0.01
            PixelSize = 3  ' To avoid gaps
            zyx = zrady / zradx
            zxy = zradx / zrady
            zz1 = zstep * Cos(zalpha1)
            zz2 = zstep * Sin(zalpha1)
            
            If zradx >= zrady Then
               For zR = zrad To 1 Step -0.5
                  zxc = xsto(1) + (zrad - zR) * zz1
                  zyc = ysto(1) + (zrad - zR) * zz2
                  BresEllipse zxc, zyc, zR, zR * zyx, RGB(zRL, zGL, zBL), 0, 3, 1
                  GetNextColors
               Next zR
            Else
               For zR = zrad To 1 Step -0.5
                  zxc = xsto(1) + (zrad - zR) * zz1
                  zyc = ysto(1) + (zrad - zR) * zz2
                  BresEllipse zxc, zyc, zR * zxy, zR, RGB(zRL, zGL, zBL), 0, 3, 1
                  GetNextColors
               Next zR
            End If
            If Anti = 0 Then
               PixelSize = 2
               ZAACIRLIPSE xsto(1), ysto(1), zradx + 1.5, zrady + 1.5, LeftColor, 0, 2 * pi#, Anti
            End If
         Else
            PixelSize = pxsvORG
            ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, LeftColor, 0, 2 * pi#, Anti
            If PixelSize = 1 Then
               ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, LeftColor, 0, 2 * pi#, 1
            End If
         End If
         PixelSize = pxsvORG
         DISPLAY Form1, picDRAW
      
      Case [CRotate], [CRotateFill], [CConcentric]
         ' Rotate ellipse  & Draw
         RotEllipse picDRAW, XorCul, 0     ' Clear
         picDRAW.DrawMode = vbCopyPen
      
         RotEllipse picDRAW, Cul, 1        ' Draw
         DISPLAY Form1, picDRAW
       Screen.MousePointer = vbDefault
      
      End Select

   End If   ' NClicks
   Screen.MousePointer = vbDefault
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Private Sub EllTangents(DCul As Long, _
            Optional EllRot As Single = -1, Optional EllRot2 As Single = -1)

' QUADRANTS - all need own treatment !!?
    
'   Q3     Q33 ---|--- Q44    Q4
'          /            \
'         | radx  C radx |   ' Special also when yT=yc
'          \            /
'   Q2     Q22 ---|--- Q11    Q1

Dim xc As Single, yc As Single
Dim xt As Single, yt As Single
Dim xxt As Single, yyt As Single
Dim rx As Single, ry As Single
Dim RA As Single
Dim Quad As Long
Dim Alpha1 As Single, Alpha2 As Single
Dim Ang1 As Single, Ang2 As Single

Dim x1 As Single, y1 As Single
Dim x2 As Single, y2 As Single
Dim A As Single, B As Single, c As Single
Dim BB As Single, BBB As Single
Dim zA As Single, zB As Single
'Dim EllRot As Single
'Dim EllRot2 As Single
Dim svxT As Single, svyT As Single

   xc = xsto(1): yc = ysto(1)
   xt = xsto(2): yt = ysto(2)

   ' Ellipse angle
   If EllRot = -1 Then
      EllRot = zATan2(yang - ysto(1), xang - xsto(1))
   End If
   RA = -EllRot
   
   If zradx >= zrady Then
      zA = zradx
      zB = zrady
   Else
      zA = zrady
      zB = zradx
   End If
   
   ' Shift (xt,yt) to ellipse at (0,0)
   rx = (xt - xc) * Cos(RA) - (yt - yc) * Sin(RA)
   ry = (xt - xc) * Sin(RA) + (yt - yc) * Cos(RA)
   
   ' To test condition that point is inside ellipse
   PointInside = False
   BB = ((rx) / zA) ^ 2 + ((ry) / zB) ^ 2
   If BB < 1 Then
      PointInside = True
      Select Case TubularTool
      Case [CONE1], [CONE6]
         DrawDot xc, yc, DCul
      Case [CONE2] To [CONE5]
         EllRot = zATan2(ysto(1) - yang, xang - xsto(1))
         InsideGRAD xc, yc, xsto(2), ysto(2), zA, zB, EllRot, 0
      End Select
      xsto(2) = xang
      ysto(2) = yang
      Exit Sub
   End If
   
   svxT = xt
   svyT = yt
   ' External point about ellipse at (0,0)
   xt = rx
   yt = ry
   ' SET QUAD
   ' (xt,yt) shifted to 0 so (xc,yc) = (0,0)
   If yt <= 0 Then ' Q 3 or 4 (or 33 or 44 when inside xc +/- radx)
      If xt <= 0 Then Quad = 3 Else Quad = 4
      If xt <= 0 And xt > 0 - zA Then Quad = 33
      If xt >= 0 And xt < 0 + zA Then Quad = 44
   Else  ' yT > yc,  Q 1 or 2  (or 55 or 66  when inside xc +/- radx)
      If xt <= 0 Then Quad = 2 Else Quad = 1
      If xt <= 0 And xt > 0 - zA Then Quad = 22
      If xt >= 0 And xt < 0 + zA Then Quad = 11
   End If
   
   ' TEST
   'Form1.Label3 = Str$(Quad)
   
   xxt = rx 'svxT - xc
   yyt = ry 'svyT - yc
   
   A = (zB ^ 2) * (xxt ^ 2) + (zA ^ 2) * (yyt ^ 2)
   B = -2 * (zA ^ 2) * (zB ^ 2) * xxt
   c = zA ^ 4 * (zB ^ 2 - yyt ^ 2)
   BBB = (B ^ 2 - (4 * A * c))
   If BBB < 0 Then BBB = -BBB    ' NB when yT=yc
   BB = Sqr(BBB)
   
   ' FIRST TANGENT
   x1 = -B + BB
   x1 = x1 / (2 * A)
   
   BBB = (zA ^ 2 - x1 ^ 2)
   If BBB <= 0 Then ' To correct any rounding error
      y1 = 0
   Else
      y1 = Sqr(BBB) * zB / zA
   End If
   
   Select Case Quad  ' Decide + or -
   Case 3, 33, 44, 4  'y1 < yc make y1 > yc
      y1 = -y1
   End Select
   
   ' SECOND TANGENT
   x2 = -B - BB
   x2 = x2 / (2 * A)
   BBB = (zA ^ 2 - x2 ^ 2)
   If BBB <= 0 Then ' To correct any rounding error
      y2 = 0
   Else
      y2 = Sqr(BBB) * zB / zA
   End If
   
   Select Case Quad  ' Decide + or -
   Case 3, 33, 44, 4
      y2 = -y2
   End Select

   Alpha1 = ArcCos(x1 / zA)
   Alpha2 = ArcCos(x2 / zA)
   ' Necessary angle adjustment ??
   Select Case Quad
   Case 4
      Ang1 = Alpha1: Ang2 = 2 * pi - Alpha2
   Case 2
      Ang1 = 2 * pi - Alpha2: Ang2 = Alpha1
   Case 22, 11
      Ang1 = Alpha2: Ang2 = Alpha1
   
   Case 1
      Ang1 = Alpha2: Ang2 = -Alpha1
   Case 3
      Ang1 = 2 * pi# - Alpha1: Ang2 = Alpha2
   Case 33, 44
      Ang1 = 2 * pi# - Alpha1: Ang2 = 2 * pi# - Alpha2
   End Select
   
   'Ang1 = Alpha1: Ang2 = Alpha2

'Form1.Label3 = "Q =" & Str$(Int(Quad))
   ' Rotate tangents' end line points
   
   ' Redefine ellipse angle !!
   If EllRot2 = -1 Then
      EllRot2 = zATan2(ysto(1) - yang, xang - xsto(1))
   End If
   RA = -EllRot2
   ' Points on ellipse at (0,0)
   x1 = zA * Cos(Ang1)
   y1 = zB * Sin(Ang1)
   x2 = zA * Cos(Ang2)
   y2 = zB * Sin(Ang2)
   ' Rotate & Translate
   rx = x1 * Cos(RA) - y1 * Sin(RA)
   ry = x1 * Sin(RA) + y1 * Cos(RA)
   x1 = rx + xc
   y1 = ry + yc
   rx = x2 * Cos(RA) - y2 * Sin(RA)
   ry = x2 * Sin(RA) + y2 * Cos(RA)
   x2 = rx + xc
   y2 = ry + yc
   
   xt = svxT
   yt = svyT

   ' Anti = 0(default)do AA, Anti >1 no AA (just SetPixel)
   
   ' FOR COLORED GRADIENT CONES
   Select Case TubularTool
   Case [CONE2] To [CONE5]
      CONEGRAD xc, yc, zA, zB, Ang1, Ang2, EllRot2, xt, yt, 0
   End Select
   
   
   If (TubularTool = [CONE1] Or TubularTool = [CONE6]) Or aOutline = True Then
   ' DRAW TANGENTS
      If Not aAA Then
         ZDrawAALine xt, yt, x1, y1, DCul, 0, 1
         ZDrawAALine xt, yt, x2, y2, DCul, 0, 1
      Else
         ZDrawAALine xt, yt, x1, y1, DCul, 0, 0
         ZDrawAALine xt, yt, x2, y2, DCul, 0, 0
         If PixelSize = 1 Then
            ZDrawAALine xt, yt, x1, y1, DCul, 0, 1
            ZDrawAALine xt, yt, x2, y2, DCul, 0, 1
         End If
      End If
   End If
   
   ' Tangents done but to maintain ellipse angle need:-
   xsto(2) = xang 'svxsto2
   ysto(2) = yang 'svysto2

End Sub

Private Sub RotEllipse(PIC As PictureBox, DCul As Long, Action As Long, Optional zalpha1 As Single = -1)
' Action = 0 Action, = 1 detailed   Action = 0  enables a faster locate
' Center @ xsto(1),ysto(1)
Dim eccentricity As Single
Dim theta As Single
'Dim zAlpha1 As Single, zR As Single
Dim zA As Single, zB As Single
Dim sstep As Single
Dim dzB As Single

Dim xp As Single, yp As Single     ' points on ellipse
Dim pix As Long ', zpix As Single
' For AA rotated ellipse
Dim xpr As Single, ypr As Single   ' prev points on ellipse
Dim Anti As Long
Dim svzradx As Single
Dim svzrady As Single
Dim svZA As Single
Dim svZB As Single
Dim svDCul As Long

'Dim maxx As Single, maxy As Single
'Dim minx As Single, miny As Single

   Anti = 0
   If Not aAA Then Anti = 1

   If zradx = 0 Then zradx = 0.001
   If zrady = 0 Then zrady = 0.001
   If zradx >= zrady Then
      zA = zradx
      zB = zrady
      svZA = zA
      svZB = zB
   Else
      zA = zrady
      zB = zradx
      svZB = zB
      svZA = zA
   End If
   
   
   If Action = 0 Then
      sstep = 0.01
   Else  ' Smaller steps for Final
      sstep = 0.01  '0.01
      If zA > 400 Or zB > 400 Then sstep = 0.001
   End If
   
'  EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
'  ets zradx,zrady from this before rotating in DrawMoveCirlipses

   eccentricity = Sqr(1 - (zB / zA) ^ 2)
   
   If zalpha1 = -1 Then
      zalpha1 = zAng(ysto(2) - ysto(1), xsto(2) - xsto(1))
   End If
   
   ' theta = 0
   
   dzB = eccentricity ^ 2
   If dzB = 1 Then dzB = 0.99
   zR = zB / Sqr(1 - dzB)
   xp = xsto(1) + zR * Cos(zalpha1)
   yp = ysto(1) - zR * Sin(zalpha1)
      
   ' Center @ xsto(1),ysto(1)
   ' Peripheral points calculated @ xp,yp
If Action = 0 Then   ' CLEAR
      ' Lines joining peripheral points
      PIC.PSet (xp, yp), DCul ' Clear
      For theta = sstep To 2 * pi# + sstep Step sstep
         dzB = (eccentricity * Cos(theta)) ^ 2
         If dzB = 1 Then dzB = 0.99
         zR = zB / Sqr(1 - dzB)
         xp = xsto(1) + zR * Cos(theta - zalpha1)
         yp = ysto(1) + zR * Sin(theta - zalpha1)
         PIC.Line -(xp, yp), DCul ' Clear
      Next theta
   
Else   ' Action = 1   'DRAW  Smaller steps for Final
      
   If (Tool = Cirlipses And (CircTool = [CRotate])) _
      Or (Tool = Tubulars) Then
      
' maxx = -10000
' maxy = -10000
' minx = 10000
' miny = 10000
 
      'Select Case CircTool
      'Case [CRotate], [CEllTan]

         ' Lines joining peripheral points
         If Not aAA Then
            svRound = aRound
            'aRound = False  ' Temp Square gives less bumps
            xpr = xp
            ypr = yp
            'PIC.PSet (xp, yp), DCul ' Clear or draw
            'PixelSize = 3
            
            For theta = sstep To 2 * pi# + sstep Step sstep / 4
               dzB = (eccentricity * Cos(theta)) ^ 2
               If dzB = 1 Then dzB = 0.99
               zR = zB / Sqr(1 - dzB)
               xp = xsto(1) + zR * Cos(theta - zalpha1)
               yp = ysto(1) + zR * Sin(theta - zalpha1)
'If yp > maxy Then
'   maxy = yp
'   maxx = xp
'End If
'If yp < miny Then
'   miny = yp
'   minx = xp
'End If
               
               ZDrawAALine xpr, ypr, xp, yp, DCul, 0, 1  'Anti
               xpr = xp: ypr = yp
            Next theta
            
            aRound = svRound
         Else   ' AA
            svRound = aRound
            'aRound = False  ' Temp Square gives less bumps
            xpr = xp
            ypr = yp
            
            For pix = 1 To PixelSize
               
               For theta = sstep To 2 * pi# + sstep Step sstep
                  dzB = (eccentricity * Cos(theta)) ^ 2
                  If dzB = 1 Then dzB = 0.99
                  zR = zB / Sqr(1 - dzB)
                  xp = xsto(1) + zR * Cos(theta - zalpha1)
                  yp = ysto(1) + zR * Sin(theta - zalpha1)
                  If xp = Int(xp) Then xp = xp + 0.001
                  If yp = Int(yp) Then yp = yp + 0.001
                  If xpr = xp Or ypr = yp Then
                     ZDrawAALine xpr, ypr, xp, yp, DCul, 0, 0
                     xpr = xp 'r2
                     ypr = yp 'r2
                  Else
                     ZAADRAWER xpr, ypr, xp, yp, DCul, 0, 0
                     ZAADRAWER xp, yp, xpr, ypr, DCul, 0, 0
                     If PixelSize = 1 Then
                        ZAADRAWER xpr, ypr, xp, yp, DCul, 0, 1
                     End If
                  End If
                  xpr = xp 'r2
                  ypr = yp 'r2
               Next theta
            
               If PixelSize = 1 Then Exit For
               
               zradx = zradx + 1
               zrady = zrady + 1
               If zradx >= zrady Then
                  zA = zradx
                  zB = zrady
                  eccentricity = Sqr(1 - (zrady / zradx) ^ 2)
               Else
                  zA = zrady
                  zB = zradx
                  eccentricity = Sqr(1 - (zradx / zrady) ^ 2)
               End If
               dzB = (eccentricity * Cos(theta)) ^ 2
               If dzB = 1 Then dzB = 0.99
               zR = zB / Sqr(1 - dzB)
               xp = xsto(1) + zR * Cos(theta - zalpha1)
               yp = ysto(1) + zR * Sin(theta - zalpha1)
            Next pix
            
            ' To allow border fill for AA
            If PixelSize <> 1 Then
               zA = svZA
               zB = svZB
               eccentricity = Sqr(1 - (zB / zA) ^ 2)
               dzB = eccentricity ^ 2
               zR = zB / Sqr(1 - dzB)
               xp = xsto(1) + zR * Cos(zalpha1)
               yp = ysto(1) - zR * Sin(zalpha1)
               xpr = xp 'r2
               ypr = yp 'r2
               PixelSize = 1
               For theta = sstep To 2 * pi# + sstep Step sstep
                  dzB = (eccentricity * Cos(theta)) ^ 2
                  If dzB = 1 Then dzB = 0.99
                  zR = zB / Sqr(1 - dzB)

                  xp = xsto(1) + zR * Cos(theta - zalpha1)
                  yp = ysto(1) + zR * Sin(theta - zalpha1)
                  If xp = Int(xp) Then xp = xp + 0.001
                  If yp = Int(yp) Then yp = yp + 0.001
                  If xpr = xp Or ypr = yp Then
                     ZDrawAALine xpr, ypr, xp, yp, DCul, 0, 1
                     xpr = xp 'r2
                     ypr = yp 'r2
                  Else
                     ZAADRAWER xpr, ypr, xp, yp, DCul, 0, 1
                  End If
                  xpr = xp 'r2
                  ypr = yp 'r2
               Next theta
               PixelSize = pxsvORG
            End If
            
            aRound = svRound

         End If   ' If Not aAA Then
   
   ElseIf (Tool = Cirlipses And CircTool = [CRotateFill]) _
       Or (Tool = Cirlipses And CircTool = [CConcentric]) Then

      'Case [CRotateFill]
         pxsv = PixelSize
            svzradx = zradx
            svzrady = zrady
            svRound = aRound
            'aRound = False  ' Temp Square gives less bumps
            xpr = xp
            ypr = yp
            PixelSize = 2

      svDCul = DCul
      If CircTool = [CConcentric] Then
         NSteps = zrady * 4
         If zrady > zradx Then NSteps = zradx * 4
         GetColorSteps
      End If
            Do
            
      If CircTool = [CConcentric] Then
         DCul = RGB(CByte(zRL), CByte(zGL), CByte(zBL))
      End If
               For theta = sstep To 2 * pi# + sstep Step sstep
                  dzB = (eccentricity * Cos(theta)) ^ 2
                  If dzB = 1 Then dzB = 0.99
                  zR = zB / Sqr(1 - dzB)
                  xp = xsto(1) + zR * Cos(theta - zalpha1)
                  yp = ysto(1) + zR * Sin(theta - zalpha1)
                  If xp = Int(xp) Then xp = xp + 0.001
                  If yp = Int(yp) Then yp = yp + 0.001
                  If xpr = xp Or ypr = yp Then
                     ZDrawAALine xpr, ypr, xp, yp, DCul, 0, 0
                     xpr = xp
                     ypr = yp
                  Else
                     ZAADRAWER xpr, ypr, xp, yp, DCul, 0, Anti '1
                     If PixelSize = 1 Then
                        ZAADRAWER xpr, ypr, xp, yp, DCul, 0, Anti '1
                     End If
                  End If
                  xpr = xp
                  ypr = yp
               Next theta

               zradx = zradx - 0.25
               zrady = zrady - 0.25
               If zradx <= 0 Or zrady <= 0 Then Exit Do  '>>>>>>>
               
               If zradx >= zrady Then
                  zA = zradx
                  zB = zrady
                  eccentricity = Sqr(1 - (zrady / zradx) ^ 2)
               Else
                  zA = zrady
                  zB = zradx
                  eccentricity = Sqr(1 - (zradx / zrady) ^ 2)
               End If
               dzB = (eccentricity * Cos(theta)) ^ 2
               If dzB = 1 Then dzB = 0.99
               zR = zB / Sqr(1 - dzB)
               xp = xsto(1) + zR * Cos(theta - zalpha1)
               yp = ysto(1) + zR * Sin(theta - zalpha1)

         If CircTool = [CConcentric] Then
            GetNextColors
         End If
            
            Loop
            
            zradx = svzradx
            zrady = svzrady
            DCul = svDCul
            aRound = svRound
   End If
End If   ' If Action = 0 Then
PixelSize = pxsvORG
End Sub

Public Sub CompleteArcs(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim ixc As Single, iyc As Single
Dim Anti As Long
   If NClicks <> 3 Then Exit Sub
   aMouseDown = False
      
   picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul, vbzAS, vbzAE, zratio   ' XClear
   picDRAW.DrawMode = vbCopyPen
   
   Anti = 0
   If Not aAA Then Anti = 1
   
   EvalCirlipse xsto(1), ysto(1), xsto(2), ysto(2)
   
   If (xsto(1) = xsto(2)) And (ysto(1) = ysto(2)) Then   ' SINGLE DOT
      picDRAW.DrawWidth = 1
      DrawDot xsto(1), ysto(1), Cul
      PixelSize = pxsvORG
      picDRAW.DrawWidth = PixelSize
      DISPLAY Form1, picDRAW
      Exit Sub
   End If
   
   pxsv = PixelSize
   PixelSize = PixelSize + 1
   svRound = aRound
   If aAddRadii Then
      aRound = False
   End If
   ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, zAS, zAE, Anti
   aRound = svRound
   ixc = xsto(1)
   iyc = ysto(1)
   If aAddRadii Then
      PixelSize = pxsv
      AddRadiiToArcs picDRAW, ixc, iyc, Cul, Anti
   End If
   DISPLAY Form1, picDRAW
   PixelSize = pxsv
      
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub AddRadiiToArcs(picDRAW As PictureBox, ixc As Single, iyc As Single, Cul As Long, Optional Anti As Long = 0)
Dim ixs As Single, iys As Single
Dim zrx As Single, zry As Single
Dim Exten As Single

' ZDrawAALine  x1, y1, x2, y2, DCul, Extend, Anti
'   GetExtension Exten
   
   Select Case PixelSize
   Case 1: Exten = 0.4
   Case 2: Exten = 0.2
   Case 3: Exten = 0.27
   Case 4: Exten = 0.3
   Case 5: Exten = 0.4
   Case 6: Exten = 0.3
   Case 7: Exten = 0.4
   Case 8: Exten = 0.4
   Case 9: Exten = 0.4
   Case 10: Exten = 0.4
   Case 11: Exten = 0.4
   Case 12: Exten = 0.4
   End Select

   If aRound Or (ArcTool >= [Arc13] And ArcTool <= [Arc16]) Then
      Select Case PixelSize
      Case Is < 8
         Exten = -(Exten - 0.1)
      Case Else
         Exten = -(Exten - 0.3)
      End Select
   End If
   
   If Not aAA And Not aRound Then
      Select Case PixelSize + 1
      Case 6 To 9
         Exten = (Exten - 0.1)
      Case Else
         Exten = Exten
      End Select
   End If
   
   If aAA Then
      Anti = 0
   Else
      Anti = 1
   End If
   
   If ArcTool >= [Arc1] And ArcTool <= [Arc4] Or _
      ArcTool >= [Arc9] And ArcTool <= [Arc12] Then    ' ie Quarter & 3/4 Arcs
      
      ixs = ixc + zradx * Cos(zAS)
      iys = iyc + zrady * Sin(zAS)
      If Not aRound Then
         'AdjustQuarterArcRadii ixc, iyc, ixs, iys
         'ZDrawAALine  ixc, iyc, ixs, iys, Cul, 0, Anti
         ZDrawAALine ixc, iyc, ixs, iys, Cul, Exten, Anti
         If PixelSize = 1 Then
            ZDrawAALine ixc, iyc, ixs, iys, Cul, Exten, 1
         End If
      Else   ' Round
         'ZDrawAALine  ixc, iyc, ixs, iys, Cul, -0.25, Anti
         ZDrawAALine ixc, iyc, ixs, iys, Cul, Exten, Anti
         If PixelSize = 1 Then
            ZDrawAALine ixc, iyc, ixs, iys, Cul, Exten, 1
         End If
      End If
      ixc = xsto(1)
      iyc = ysto(1)
      zrx = (ixc + zradx * Cos(zAE))
      zry = (iyc + zrady * Sin(zAE))
      If Not aRound Then
         'AdjustQuarterArcRadii ixc, iyc, zrx, zry
         'ZDrawAALine  ixc, iyc, zrx, zry, Cul, 0, Anti
         ZDrawAALine ixc, iyc, zrx, zry, Cul, Exten, Anti
         If PixelSize = 1 Then
            ZDrawAALine ixc, iyc, zrx, zry, Cul, Exten, 1
         End If
      Else   ' Round
         'ZDrawAALine  ixc, iyc, zrx, zry, Cul, -0.25, Anti
         ZDrawAALine ixc, iyc, zrx, zry, Cul, Exten, Anti
         If PixelSize = 1 Then
            ZDrawAALine ixc, iyc, zrx, zry, Cul, Exten, 1
         End If
      End If
      ' SQ Dot center
      If Not aRound Then
         If ArcTool >= [Arc1] Then    ' ie Quarter & 3/4 Arcs
            'ArcCenterDot picDRAW, ixc, iyc, Cul
         End If
         If ArcTool >= [Arc9] And ArcTool <= [Arc12] Then    ' ie Quarter & 3/4 Arcs
            'ArcCenterDot picDRAW, ixc, iyc, Cul
         End If
      End If
      ' Fill gap
      If ArcTool = [Arc1] And pxsv = 1 Then DrawDot ixs, iyc - 1, Cul
      
   ElseIf ArcTool >= [Arc5] And ArcTool <= [Arc8] Then   ' ie Semi-arcs
      ixs = ixc + zradx * Cos(zAS)
      iys = iyc + zrady * Sin(zAS)
      zrx = ixc + zradx * Cos(zAE)
      zry = iyc + zrady * Sin(zAE)
      AdjustSemiRadii ixs, iys, zrx, zry
      ZDrawAALine ixs, iys, zrx, zry, Cul, 0, Anti
      If PixelSize = 1 Then
         ZDrawAALine ixs, iys, zrx, zry, Cul, Exten, 1
      End If
   Else     ' [Arc13] To [Arc16]  3/4 segment Arcs
      ' Resort to Round !!
      svRound = aRound
      aRound = True
      If (PixelSize And 1) = 1 Then   ' Odd
         Exten = 0
      End If
      ixs = ixc + zradx * Cos(zAS)
      iys = iyc + zrady * Sin(zAS)
      'Adjust3QuarterArcRadii ixc, iyc, ixs, iys
      ZDrawAALine ixc, iyc, ixs, iys, Cul, Exten, 0    ' 0
      If PixelSize = 1 Then
         ZDrawAALine ixc, iyc, ixs, iys, Cul, Exten, 1
      End If
      ixc = xsto(1)
      iyc = ysto(1)
      zrx = ixc + zradx * Cos(zAE)
      zry = iyc + zrady * Sin(zAE)
      'Adjust3QuarterArcRadii ixc, iyc, zrx, zry
      ZDrawAALine ixc, iyc, zrx, zry, Cul, Exten, 0    '0
      If PixelSize = 1 Then
         ZDrawAALine ixc, iyc, zrx, zry, Cul, Exten, 1
      End If
      aRound = svRound
   End If
End Sub

'Public Sub AdjustQuarterArcRadii(ixc As Single, iyc As Single, x2 As Single, y2 As Single)
'' [Arc1] To [Arc4], [Arc9] To [Arc12]
'' 1st rad ixc , iyc, ixs, iys
'' 2nd rad ixc, iyc, zrx, zry
'' H&V radii always non-AA & Square ends
'Dim off1 As Single, off2 As Single
'   off1 = pxsv / 3
'   off2 = pxsv / 2
'
''   If pxsv < 3 Then Exit Sub
'
'   If Abs(x2 - ixc) > 2 Then   ' HORZ SQ END
'      Select Case ArcTool
'      Case [Arc1]
'         Select Case pxsv
'         Case 2: iyc = iyc + 1
'         Case 3, 6: iyc = iyc - 1
'         Case 4:    iyc = iyc + 1
'         Case 5:    iyc = iyc
'         Case 11:   iyc = iyc - off1 - 1.5
'         Case 12:   iyc = iyc - off1 - 0.5
'         Case Else: iyc = iyc - off1
'         End Select
'      Case [Arc2]
'         Select Case pxsv
'         Case 1, 2: iyc = iyc - 1
'         Case 3, 5:   iyc = iyc + off2 - 1
'         Case 7: iyc = iyc
'         Case Else: iyc = iyc + off2
'         End Select
'      Case [Arc3]
'         Select Case pxsv
'         Case 4:    iyc = iyc - off2
'         Case 7: iyc = iyc
'         Case 9: iyc = iyc - off2 + 0.5
'         Case Else: iyc = iyc - off2
'         End Select
'      Case [Arc4]
'         Select Case pxsv
'         Case 1:      iyc = iyc - 2
'         Case 2, 3:     iyc = iyc - 1
'         Case 4:      iyc = iyc + off1 - 1
'         Case 9:      iyc = iyc + off1 + 1
'         Case 11, 12: iyc = iyc + off2 - 1
'         Case Else:   iyc = iyc + off1
'         End Select
'
'      Case [Arc9]
'         Select Case pxsv
'         Case 11: iyc = iyc - off2 + 1
'         Case Else: iyc = iyc - off2
'         End Select
'      Case [Arc10]
'         Select Case pxsv
'         Case 1: iyc = iyc + off1
'         Case 2, 3: iyc = iyc + off1 - 2
'         Case 4: iyc = iyc + off1 - 0.5
'         Case 6: iyc = iyc + off1
'         Case 11: iyc = iyc + off1 + 1
'         Case Else: iyc = iyc + off1 + 0.5
'         End Select
'
'      Case [Arc11]
'         Select Case pxsv
'         Case 2: iyc = iyc - off1 + 1
'         Case 3: iyc = iyc - off1
'         Case 4: iyc = iyc - off1 + 1.25
'         Case 5: iyc = iyc - off1
'         Case 6: iyc = iyc - off1 + 0.75
'         Case 7: iyc = iyc - off1
'         Case 8: iyc = iyc - off1 + 0.25
'         Case 9: iyc = iyc - off1 - 0.5
'         Case 10: iyc = iyc - off1 - 0.5
'         Case Else: iyc = iyc - off1 - 1
'         End Select
'      Case [Arc12]
'         Select Case pxsv
'         Case 1, 2, 5, 9, 11: iyc = iyc + off2 - 1
'         Case 3: iyc = iyc + off2 - 2
'         Case Else: iyc = iyc + off2
'         End Select
'
'      End Select
'      y2 = iyc
'
'   Else   ' VERT SQ END
'
'      Select Case ArcTool
'      Case [Arc1]
'         Select Case pxsv
'         Case 3, 5:  ixc = ixc + off2
'         Case 7:   ixc = ixc
'         Case Else: ixc = ixc - off2
'         End Select
'      Case [Arc2]
'         Select Case pxsv
'         Case 1:   ixc = ixc - 1
'         Case 2:   ixc = ixc
'         Case 4:    ixc = ixc - 1
'         Case Else: ixc = ixc - off2 + 1
'         End Select
'      Case [Arc3]
'         Select Case pxsv
'         Case 2, 3:        ixc = ixc - off2
'         Case 4:         ixc = ixc + off1 - 1
'         Case 7: ixc = ixc + 3
'         Case 9, 11, 12: ixc = ixc + off1 + 1
'         Case Else:      ixc = ixc + off1
'         End Select
'      Case [Arc4]
'         Select Case pxsv
'         Case 1:   ixc = ixc - 2
'         Case 2:    ixc = ixc - 1
'         Case 3:    ixc = ixc - 1
'         Case Else: ixc = ixc + off2
'         End Select
'
'      Case [Arc9]
'         Select Case pxsv
'         Case 2, 4:    ixc = ixc - off1 + 1
'         Case 6:    ixc = ixc - off1 + 1
'         Case Else: ixc = ixc - off1
'         End Select
'      Case [Arc10]
'         Select Case pxsv
'         Case Else: ixc = ixc - off2
'         End Select
'      Case [Arc11]
'         Select Case pxsv
'         Case 1: ixc = ixc + off2 - 1.5
'         Case 2, 5: ixc = ixc + off2 - 1.25
'         Case 3: ixc = ixc + off2 - 2
'         Case 9, 11: ixc = ixc + off2 - 1
'         Case Else: ixc = ixc + off2
'         End Select
'      Case [Arc12]
'         Select Case pxsv
'         Case 2: ixc = ixc + off2 - 1.75
'         Case 3: ixc = ixc + off2 - 2.5
'         Case 7: ixc = ixc + off2 - 0.5
'         Case 9: ixc = ixc + off2 - 0.25
'         Case 11: ixc = ixc + off2 - 1
'         Case Else: ixc = ixc + off2 - 1.5
'         End Select
'
'      End Select
'      x2 = ixc
'   End If
'End Sub

Public Sub AdjustSemiRadii(x1 As Single, y1 As Single, x2 As Single, y2 As Single)
'ixs, iys, zrx, zry  [Arc5] To [Arc8]
Dim SW As Single
Dim off As Long
   
   If pxsv < 3 Then Exit Sub
   
   off = PixelSize \ 3
   If PixelSize > 9 Then off = off + 1
   Select Case ArcTool
   Case [Arc5], [Arc6] ' Left & Right  V
      If ArcTool = [Arc6] Then
         Select Case pxsv
         Case 3
            SW = y1
            y1 = y2
            y2 = SW
            x1 = x1 - 1
            x2 = x1
         Case Else
            SW = y1
            y1 = y2
            y2 = SW
         End Select
      End If
      y1 = y1 + off: y2 = y2 - off
      If aRound Then   ' To stop radii overwriting arc's round end
         y1 = y1 - PixelSize / 2
         y2 = y2 + PixelSize / 2
      End If
   Case [Arc7], [Arc8] ' Top & Bottom
      If ArcTool = [Arc7] Then
         SW = x1
         x1 = x2
         x2 = SW
      End If
      If ArcTool = [Arc8] Then
         Select Case PixelSize
         Case 3
            SW = x1
            x1 = x2
            x2 = SW
            y2 = y1
         End Select
      End If
      x1 = x1 + off: x2 = x2 - off
      If aRound Then   ' To stop radii overwriting arc's round end
         x1 = x1 - PixelSize / 2
         x2 = x2 + PixelSize / 2
      End If
   End Select
End Sub

'Public Sub Adjust3QuarterArcRadii(ixc As Single, iyc As Single, x2 As Single, y2 As Single)
'' Not used
'   If pxsv <= 3 Then Exit Sub
' ' ??
'End Sub

'Private Sub ArcCenterDot(picDRAW As PictureBox, ixc As Single, iyc As Single, ByVal Cul As Long)
'' Public pxsv
'' For [Arc9] to [Arc12]
'' Dot at radii ends always non-AA
'Dim px As Long
'   If pxsv < 3 Then Exit Sub
'
'   px = pxsv
'
'   ' TEST
'   'Cul = vbRed
'
'   svAA = aAA
'   Select Case ArcTool
'   Case [Arc1]
'      Select Case pxsv
'      Case 5: DrawDot  ixc, iyc, Cul
'      End Select
'   Case [Arc9]
'      Select Case pxsv
'      Case 3: DrawDot  ixc - pxsv / 3, iyc, Cul
'      Case 4: DrawDot  ixc - (pxsv / 3), iyc + 1, Cul
'      Case 5: DrawDot  ixc - pxsv / 3, iyc, Cul
'      Case 6: DrawDot  ixc - pxsv / 3 + 1, iyc + 1, Cul
'      Case 8: DrawDot  ixc - pxsv / 3, iyc + 1, Cul
'      Case 7: DrawDot  ixc - pxsv / 2 + 1, iyc + 0.5, Cul
'      Case 9: DrawDot  ixc - pxsv / 3, iyc, Cul
'      Case 10: DrawDot  ixc - pxsv / 3, iyc + 1, Cul
'      Case 11: DrawDot  ixc - pxsv / 3, iyc, Cul
'      Case 12: DrawDot  ixc - pxsv / 3, iyc + 1, Cul
'      End Select
'   Case [Arc10]
'      Select Case pxsv
'      Case 3: DrawDot  ixc, iyc + pxsv / 3 - 2, Cul
'      Case 4: DrawDot  ixc + 1, iyc + pxsv / 3 + 1, Cul
'      Case 5: DrawDot  ixc + 0.5, iyc + pxsv / 3, Cul
'      Case 6: DrawDot  ixc + 1, iyc + pxsv / 3 + 1, Cul
'      Case 7: DrawDot  ixc, iyc + pxsv / 3 + 1, Cul
'      Case 8: DrawDot  ixc + 1, iyc + pxsv / 3 + 1.5, Cul
'      Case 9: DrawDot  ixc, iyc + pxsv / 3 + 0.5, Cul
'      Case 10: DrawDot  ixc + 1, iyc + pxsv / 3 + 1.5, Cul
'      Case 11: DrawDot  ixc + 0.5, iyc + pxsv / 3 + 1.5, Cul
'      Case 12: DrawDot  ixc + 1, iyc + pxsv / 3 + 2, Cul
'      End Select
'   Case [Arc11]
'      Select Case pxsv
'      Case 3: DrawDot  ixc - 0.5, iyc - pxsv / 3, Cul
'      Case 4: DrawDot  ixc + 0.25, iyc - pxsv / 3 + 1, Cul
'      Case 5: DrawDot  ixc - 0.25, iyc - pxsv / 3 - 0.75, Cul
'      Case 6: DrawDot  ixc + 0.25, iyc - pxsv / 3 + 1, Cul
'      Case 7: DrawDot  ixc, iyc - pxsv / 3, Cul
'      Case 8: DrawDot  ixc + 0.25, iyc - pxsv / 3 + 1, Cul
'      Case 9: DrawDot  ixc, iyc - pxsv / 3 - 0.5, Cul
'      Case 10: DrawDot  ixc + 0.25, iyc - pxsv / 3 - 0.5, Cul
'      Case 11: DrawDot  ixc, iyc - pxsv / 3 - 1, Cul
'      Case 12: DrawDot  ixc + 0.25, iyc - pxsv / 3 - 0.75, Cul
'      End Select
'   Case [Arc12]
'      Select Case pxsv
'      Case 3: DrawDot  ixc + pxsv / 2 - 2.5, iyc, Cul
'      Case 4: DrawDot  ixc + pxsv / 2, iyc, Cul
'      Case 5: DrawDot  ixc + pxsv / 3 - 1, iyc - 0.5, Cul
'      Case 6: DrawDot  ixc + pxsv / 2 - 0.75, iyc, Cul
'      Case 7: DrawDot  ixc + pxsv / 2 - 0.75, iyc - 0.75, Cul
'      Case 8: DrawDot  ixc + pxsv / 2 - 0.25, iyc, Cul
'      Case 9: DrawDot  ixc + pxsv / 2 - 0.25, iyc, Cul
'      Case 10: DrawDot  ixc + pxsv / 2 - 0.75, iyc, Cul
'      Case 11: DrawDot  ixc + pxsv / 2 - 1, iyc, Cul
'      Case 12: DrawDot  ixc + pxsv / 2 - 1, iyc, Cul
'      End Select
'   End Select
'   aAA = svAA
'   pxsv = px
'End Sub

Private Sub BevelJoint(picDRAW As PictureBox, _
                          ByVal x1 As Long, ByVal y1 As Long, _
                          ByVal x2 As Long, ByVal y2 As Long, _
                          ByVal x3 As Long, ByVal y3 As Long, _
                          ByVal Cul As Long, Optional Exten As Single = 0.5, Optional Anti As Long = 0)
Dim za1 As Single, zA2 As Single
Dim zm1 As Single, zm2 As Single
Dim xs3 As Single, ys3 As Single
Dim zdx1 As Single, zdy1 As Single
Dim LR As Long
Dim pvx As Single
Dim px As Single


pvx = PixelSize
px = PixelSize / 2
If px < 1 Then px = 1
PixelSize = px

' EG
'      x1 = xsto(k - 1): y1 = ysto(k - 1)
'      x2 = xsto(k): y2 = ysto(k)
'      x3 = xsto(k + 1): y3 = ysto(k + 1)
      
      If (x2 - x1) <> 0 Then
         zm1 = (y2 - y1) / (x2 - x1)
      Else
         zm1 = 1000 * Sgn(y2 - y1) ' Vert
      End If
      If Abs(zm1) < 0.0001 Then zm1 = 0.0001
      If (x3 - x2) <> 0 Then
         zm2 = (y3 - y2) / (x3 - x2)
      Else
         zm2 = 1000 * Sgn(y3 - y2) ' Vert
      End If
      If Abs(zm2) < 0.0001 Then zm2 = 0.0001
      
      If Abs(zm1) <> Abs(zm2) Then
         ' Rotate these 3 points about x2,y2
         ' to get line 1-2 along 180 deg axis
         za1 = zATan2(y2 - y1, x2 - x1)
         xs3 = x2 + (x3 - x2) * Cos(za1) + (y3 - y2) * Sin(za1)
         ys3 = y2 + (y3 - y2) * Cos(za1) - (x3 - x2) * Sin(za1)
         za1 = zATan2(ys3 - y2, xs3 - x2)
   
         LR = -1   ' Left
         If za1 <= 0 Then LR = 1   ' Right
     Else
         LR = 0  ' In line
     End If
     
      If LR = -1 Then    ' To Left
         za1 = zATan2(y1 - y2, x1 - x2)
         zdx1 = px * Sin(za1)
         zdy1 = px * Cos(za1)
         x1 = x2 - zdx1: y1 = y2 + zdy1
         
         zA2 = zATan2(y2 - y3, x2 - x3)
         zdx1 = px * Sin(zA2)
         zdy1 = px * Cos(zA2)
         x2 = x2 - zdx1: y2 = y2 + zdy1
   
         ZDrawAALine x1, y1, x2, y2, Cul, Exten, Anti   'vbRed
      
      ElseIf LR = 1 Then ' To Right
         za1 = zATan2(y1 - y2, x1 - x2)
         zdx1 = px * Sin(za1)
         zdy1 = px * Cos(za1)
         x1 = x2 + zdx1: y1 = y2 - zdy1
   
         zA2 = zATan2(y2 - y3, x2 - x3)
         zdx1 = px * Sin(zA2)
         zdy1 = px * Cos(zA2)
         x2 = x2 + zdx1: y2 = y2 - zdy1
   
         ZDrawAALine x1, y1, x2, y2, Cul, Exten, Anti  'vbRed
      End If
'If PixelSize = 0 Then Stop
PixelSize = pvx
     
End Sub

Public Sub CompletePolyLines(picDRAW As PictureBox, Button As Integer, x As Single, y As Single, Cul As Long)
' Private zdx As Single, zdy As Single

Dim k As Long
Dim za1 As Single ', za2 As Single
   
' For 2 line intersection output
Dim xout As Single, yout As Single

Dim Exten As Single
Dim svPointCount As Long
Dim Anti As Long

Dim ix1 As Long, iy1 As Long
Dim ix2 As Long, iy2 As Long
Dim ix3 As Long, iy3 As Long

Dim BLR As Byte, BLG As Byte, BLB As Byte


   If Not aComplete Then Exit Sub
   
   aMouseDown = False
   
   svPointCount = PointCount
   
   Select Case PolyLineTool
   Case [PLSolid], [PLConnect], [PLParagram], [PLFillTri], [PLFillPoly]
      
      For k = 1 To PointCount - 1
         picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul ' XClear
      Next k

      If PolyLineTool = [PLConnect] Or PolyLineTool = [PLFillPoly] Or PolyLineTool = [PLFillTri] Then
         picDRAW.Line (xsto(k), ysto(k))-(xsto(1), ysto(1)), XorCul ' XClear
         PointCount = PointCount + 1
         xsto(PointCount) = xsto(1)
         ysto(PointCount) = ysto(1)
      End If
      
      If PointCount = 2 Then
        If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
           DrawDot xsto(1), ysto(1), Cul
           SCOPY = True
           PixelSize = pxsvORG
           picDRAW.DrawWidth = PixelSize
           PointCount = svPointCount
           DISPLAY Form1, picDRAW
           Exit Sub
        End If
      End If
      
      If PolyLineTool = [PLParagram] And PointCount = 3 Then  ' Right-Clicked on 3rd point
            xsto(4) = xsto(1) - (xsto(2) - xsto(3))
            ysto(4) = ysto(1) + (ysto(3) - ysto(2))
            xsto(5) = xsto(1)
            ysto(5) = ysto(1)
            PointCount = 5
      End If
      
      
      If PolyLineTool = [PLFillTri] And PointCount = 3 Then  ' Right-Clicked on 3rd point
            xsto(4) = xsto(1)
            ysto(4) = ysto(1)
            PointCount = 4
      End If
      
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      
   Anti = 0
   If Not aAA Then Anti = 1
      
      ' Redraws into picDATA() PointCount is one more than number of point put in
      
      If PolyLineTool = [PLFillPoly] Or PolyLineTool = [PLFillTri] Then
         If PixelSize > 1 Then PixelSize = 1
      End If
      
      For k = 1 To PointCount - 1
         ZDrawAALine xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), Cul, 0, Anti
      Next k
      
      If PolyLineTool = [PLFillTri] Then
         ix1 = xsto(1) + xlo
         iy1 = Form1.picDRAW.Height - ysto(1) + ylo

         ix2 = xsto(2) + xlo
         iy2 = Form1.picDRAW.Height - ysto(2) + ylo

         ix3 = xsto(3) + xlo
         iy3 = Form1.picDRAW.Height - ysto(3) + ylo
         LngToRGB LeftColor, BLR, BLG, BLB
         FillTriangle ix1, iy1, _
                      ix2, iy2, _
                      ix3, iy3, BLR, BLG, BLB
         If PixelSize > 1 Then PixelSize = 1
         For k = 1 To PointCount - 1
            ZDrawAALine xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), Cul, 0, Anti
         Next k
      
      
      End If
      
      
      If PolyLineTool = [PLFillPoly] Then
         ReDim pxstoL(PointCount) As Long, pystoL(PointCount) As Long
         For k = 1 To PointCount - 1
            pxstoL(k) = xsto(k)
            pystoL(k) = ysto(k)
         Next k
         
         ' Shift coords to picDATA() base
         For k = 1 To PointCount - 1
            xsto(k) = xsto(k) + xlo
            ysto(k) = Form1.picDRAW.Height - ysto(k) + ylo
         Next k
         '' svPIxelsize=Pixelsize
         PixelSize = 1
         LngToRGB LeftColor, BLR, BLG, BLB
         
         ' Find xmin/max & ymin/max
         xmin = 10000
         xmax = -10000
         ymin = 10000
         ymax = -10000
         For k = 1 To PointCount - 1
            If xsto(k) < xmin Then xmin = xsto(k)
            If ysto(k) < ymin Then ymin = ysto(k)
            If xsto(k) > xmax Then xmax = xsto(k)
            If ysto(k) > ymax Then ymax = ysto(k)
         Next k
         
         SetRect REC, 0, 0, FullW - 1, FullH - 1
         
         For iy1 = ymin To ymax
         For ix1 = xmin To xmax
            If PtInRect(REC, ix1, iy1) <> 0 Then ' Inside REC
               If PointInsidePOLY(ix1, iy1) Then
                  picDATA(0, ix1, iy1) = BLB
                  picDATA(1, ix1, iy1) = BLG
                  picDATA(2, ix1, iy1) = BLR
               End If
            End If
         Next ix1
         Next iy1
      
         For k = 1 To PointCount - 1
            xsto(k) = pxstoL(k)
            ysto(k) = pystoL(k)
         Next k
         Erase pxstoL(), pystoL()
      
      End If
      
      If PolyLineTool = [PLSolid] Then
         If PixelSize >= 3 And PointCount >= 3 Then   ' Add Bevel joints
            BevelSingle picDRAW, Cul, 0, Anti
         End If
      Else
         If PixelSize >= 3 And PointCount >= 3 Then   ' Add Bevel joints
            BevelSingle picDRAW, Cul, 1, Anti
         End If
      End If
      PointCount = svPointCount
      DISPLAY Form1, picDRAW
      
   Case [PLDouble], [PLDoubleEnded]
      For k = 1 To PointCount - 1
         picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul '<<<< Clear
         GetParallelCoords zspace, xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      Next k
      picDRAW.DrawMode = vbCopyPen
      
      ' DRAW
      If PointCount = 2 Then
         If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
            DrawDot xsto(1), ysto(1), Cul
            SCOPY = True
            PixelSize = pxsvORG
            picDRAW.DrawWidth = PixelSize
            PointCount = svPointCount
            DISPLAY Form1, picDRAW
            Exit Sub
         End If
      End If

      ' Parallel points
      ReDim pxstoL(PointCount) As Long, pystoL(PointCount) As Long
      ReDim pxstoR(PointCount) As Long, pystoR(PointCount) As Long
      picDRAW.DrawMode = vbCopyPen

      GetIntersections   ' Generates points either side of x/ysto()

      ' [PLDouble], [PLDoubleEnded] cont
      ' Draw whole poly
      For k = 1 To PointCount - 1
         ZDrawAALine pxstoL(k), pystoL(k), pxstoL(k + 1), pystoL(k + 1), Cul, 0, Anti
         ZDrawAALine pxstoR(k), pystoR(k), pxstoR(k + 1), pystoR(k + 1), Cul, 0, Anti
      Next k

      If PixelSize >= 3 And PointCount >= 3 Then   ' Add Bevel joints
         BevelDouble picDRAW, Cul, 0, Anti
      End If
      DISPLAY Form1, picDRAW

      Select Case PolyLineTool
      Case [PLDoubleEnded]
         Exten = 0
         If Not aRound Then Exten = 0.3
         zdx = 0
         zdy = 0
         If Not aRound Then
            za1 = zATan2(pystoL(2) - pystoL(1), pxstoL(2) - pxstoL(1))
            zdx = PixelSize / 2 * Cos(za1)
            zdy = PixelSize / 2 * Sin(za1)
         End If
         ZDrawAALine pxstoL(1) + zdx, pystoL(1) + zdy, _
              pxstoR(1) + zdx, pystoR(1) + zdy, Cul, Exten

         If Not aRound Then
            za1 = zATan2(pystoL(PointCount) - pystoL(PointCount - 1), pxstoL(PointCount) - pxstoL(PointCount - 1))
            zdx = (PixelSize - 1) / 2 * Cos(za1)
            zdy = (PixelSize - 1) / 2 * Sin(za1)
         End If
         ZDrawAALine pxstoL(PointCount) - zdx, pystoL(PointCount) - zdy, _
              pxstoR(PointCount) - zdx, pystoR(PointCount) - zdy, Cul, Exten, Anti
         DISPLAY Form1, picDRAW
       End Select
        
   Case [PLDoubleConnect]    ' Double Connect  PointCount>=3
      ' Join pxystoL(1) to pxystoL(PointCount) & pxystoR(1) to pxystoR(PointCount)
      
      ' PLDC
      If PointCount = 2 Then
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul  ' XClear
         GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), _
                  ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      Else
         If NClicks > 2 Then
            For k = 1 To PointCount - 1
               picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul '<<<< Clear
               GetParallelCoords zspace, xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), _
                     ixa, iya, ixb, iyb
               picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
            Next k
            ' Clear connecting lines
            picDRAW.Line (xsto(1), ysto(1))-(xsto(PointCount), ysto(PointCount)), XorCul
            GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xsto(1), ysto(1), _
                     ixa, iya, ixb, iyb
            picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
         End If
      End If
      
      picDRAW.DrawMode = vbCopyPen
      
      ' DRAW
      If PointCount = 2 Then
        If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
           DrawDot xsto(1), ysto(1), Cul
           SCOPY = True
           PixelSize = pxsvORG
           picDRAW.DrawWidth = PixelSize
           PointCount = svPointCount
           DISPLAY Form1, picDRAW
           Exit Sub
        End If
      End If
     
      ' [PLDoubleConnect] cont
      If PointCount = 2 Then
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), Cul, 0
         GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), _
            ixa, iya, ixb, iyb ', cosA, sinA
         ZDrawAALine ixa, iya, ixb, iyb, Cul, 0, Anti
         DISPLAY Form1, picDRAW
      
      Else  ' PointCount > 2
         ' [PLDoubleConnect] cont
         ' Parallel points
         PointCount = PointCount + 1
         ReDim pxstoL(PointCount) As Long, pystoL(PointCount) As Long
         ReDim pxstoR(PointCount) As Long, pystoR(PointCount) As Long
   
         xsto(PointCount) = xsto(1)
         ysto(PointCount) = ysto(1)
         
         GetIntersections   ' Generates points either side of x/ysto()  ' Public zspace
   
         Get2LineIntersect pxstoL(PointCount - 1), pystoL(PointCount - 1), pxstoL(PointCount), pystoL(PointCount), _
                           pxstoL(1), pystoL(1), pxstoL(2), pystoL(2), _
                           xout, yout
         
         
         pxstoL(PointCount) = xout
         pystoL(PointCount) = yout
         pxstoL(1) = xout
         pystoL(1) = yout
            
         For k = 1 To PointCount - 1
            ZDrawAALine pxstoL(k), pystoL(k), pxstoL(k + 1), pystoL(k + 1), Cul, 0, Anti
            ZDrawAALine pxstoR(k), pystoR(k), pxstoR(k + 1), pystoR(k + 1), Cul, 0, Anti
         Next k
         
      End If
      ' [PLDoubleConnect] cont
      If PixelSize >= 3 And PointCount >= 3 Then    ' Add Bevel joints
         BevelDouble picDRAW, Cul, 1, Anti ' 1 - bevel connect point
      End If
      DISPLAY Form1, picDRAW
      
   Case [PLDoubleParagram]
      For k = 1 To PointCount - 1
         picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul '<<<< Clear
         GetParallelCoords zspace, xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      Next k
      
      If PointCount > 2 Then
         GetParallelCoords zspace, xsto(PointCount), ysto(PointCount), xsto(1), ysto(1), _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      End If
      
      picDRAW.DrawMode = vbCopyPen
      
      ' DRAW
      If PointCount = 2 Then
        If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
           DrawDot xsto(1), ysto(1), Cul
           SCOPY = True
           PixelSize = pxsvORG
           picDRAW.DrawWidth = PixelSize
           PointCount = svPointCount
           DISPLAY Form1, picDRAW
           Exit Sub
        End If
      End If
      
      If PointCount = 2 Then
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), Cul, 0, Anti
         GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), _
             ixa, iya, ixb, iyb ', cosA, sinA
         ZDrawAALine ixa, iya, ixb, iyb, Cul, 0, Anti
         DISPLAY Form1, picDRAW

      Else  ' PointCount > 2
         ' [PLDoubleParagram] cont
         ' Parallel points
         ReDim pxstoL(PointCount + 2) As Long, pystoL(PointCount + 2) As Long
         ReDim pxstoR(PointCount + 2) As Long, pystoR(PointCount + 2) As Long
         
         picDRAW.DrawMode = vbCopyPen
         xsto(5) = xsto(1)
         ysto(5) = ysto(1)
         PointCount = 5
         
         GetIntersections  ' Generates points either side of px/ystoLR()
   
         Get2LineIntersect pxstoL(4), pystoL(4), pxstoL(5), pystoL(5), _
                           pxstoL(1), pystoL(1), pxstoL(2), pystoL(2), _
                           xout, yout
         pxstoL(5) = xout
         pystoL(5) = yout
         pxstoL(1) = xout
         pystoL(1) = yout
         Exten = 0
         'If Not aRound Then Exten = 0.25
         
         For k = 1 To 4
            ZDrawAALine pxstoL(k), pystoL(k), pxstoL(k + 1), pystoL(k + 1), Cul, Exten, Anti
            ZDrawAALine pxstoR(k), pystoR(k), pxstoR(k + 1), pystoR(k + 1), Cul, Exten, Anti
         Next k
      
         ' [PLDoubleParagram] cont
         If PixelSize >= 3 And PointCount >= 3 Then    ' Add Bevel joints
            pxsv = PixelSize
            'BevelAdjust
            BevelDouble picDRAW, Cul, 1, Anti '- bevel connect point
            PixelSize = pxsv
         End If
      End If
      DISPLAY Form1, picDRAW
         
         ' Shading
   Case [PLGradient1]
      'picDRAW.DrawWidth = 2 * PixelSize
      picDRAW.DrawWidth = PixelSize
      For k = 1 To PointCount - 1
          picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul '<<<< Clear
      Next k
      picDRAW.DrawMode = vbCopyPen
       
      ' DRAW
      If PointCount = 2 Then
        If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
           DrawDot xsto(1), ysto(1), Cul
           SCOPY = True
           PixelSize = pxsvORG
           picDRAW.DrawWidth = PixelSize
           PointCount = svPointCount
           DISPLAY Form1, picDRAW
           Exit Sub
        End If
      End If
      
      DoPolyShading picDRAW, x, y, Anti
      DISPLAY Form1, picDRAW
   
   End Select
   
   SCOPY = True
Erase pxstoL(), pystoL()
Erase pxstoR(), pystoR()
PointCount = svPointCount
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Private Function PointInsidePOLY(xp As Long, yp As Long) As Boolean
' Tests if point inside or outside polygon
' Adapted from  PSC CodeId=64595
' by Constantin Nterekas, Foundation Software
Dim k As Long
Dim aNode As Boolean
Dim xs1 As Long, ys1 As Long
Dim xs2 As Long, ys2 As Long

   aNode = False
   For k = 1 To PointCount - 1
      xs1 = xsto(k)
      ys1 = ysto(k)
      If k + 1 = PointCount - 1 + 1 Then
         xs2 = xsto(1)
         ys2 = ysto(1)
      Else
         xs2 = xsto(k + 1)
         ys2 = ysto(k + 1)
      End If
      
      If ((ys1 < yp) And (ys2 >= yp)) Or _
         ((ys2 < yp) And (ys1 >= yp)) Then
            
         If xs1 + ((yp - ys1) / (ys2 - ys1)) * (xs2 - xs1) < xp Then
            aNode = Not aNode
         End If
         
      End If
   Next k
   
   PointInsidePOLY = aNode
End Function


Private Sub BevelAdjust()
' Public pxsv
' Called when Not aRound,  NB Pixelsize may need reseting PixelSize = pxsv
      pxsv = PixelSize
      Select Case PixelSize
      Case 3, 4
         PixelSize = 0.5 * pxsv
      Case 5
         PixelSize = 0.6 * pxsv
      Case 6
         PixelSize = 0.6 * pxsv
      Case 7
         PixelSize = 0.75 * pxsv
      Case 8
         PixelSize = 0.7 * pxsv
      Case 9
         PixelSize = 0.7 * pxsv
      Case 10
         PixelSize = 0.7 * pxsv
      Case 11
         PixelSize = 0.7 * pxsv
      Case 12
         PixelSize = 0.7 * pxsv
      Case Else
         PixelSize = 0.5 * pxsv
      End Select
      If PixelSize < 1 Then PixelSize = 1
End Sub

Private Sub BevelSingle(picDRAW As PictureBox, Cul As Long, Optional AddOne As Long = 0, Optional Anti As Long = 0)
Dim k As Long
   If Not aRound Then
      BevelAdjust
      For k = 2 To PointCount - 1
         BevelJoint picDRAW, _
                       xsto(k - 1), ysto(k - 1), _
                       xsto(k), ysto(k), _
                       xsto(k + 1), ysto(k + 1), Cul, 0.3
      Next k
   
      'If PolyLineTool = [PLConnect] Or PolyLineTool = [PLParagram] Then
      If AddOne Then
         BevelJoint picDRAW, _
                       xsto(PointCount - 1), ysto(PointCount - 1), _
                       xsto(PointCount), ysto(PointCount), _
                       xsto(2), ysto(2), Cul, 0.5, Anti
      End If
   
      PixelSize = pxsv
   End If

   If aRound And PointCount >= 3 Then   ' AA & Round
      If Not aRound Then
         svRound = aRound
         aRound = True
         DrawDot xsto(1), ysto(1), Cul
         aRound = svRound
      End If
   End If

End Sub

Private Sub BevelDouble(picDRAW As PictureBox, Cul As Long, Optional AddOne As Long = 0, Optional Anti As Long = 0)
Dim k As Long
   
   If Not aRound Then
      BevelAdjust
      For k = 2 To PointCount - 1
         BevelJoint picDRAW, _
                    pxstoL(k - 1), pystoL(k - 1), _
                    pxstoL(k), pystoL(k), _
                    pxstoL(k + 1), pystoL(k + 1), Cul, 0.5, Anti
         BevelJoint picDRAW, _
                    pxstoR(k - 1), pystoR(k - 1), _
                    pxstoR(k), pystoR(k), _
                    pxstoR(k + 1), pystoR(k + 1), Cul, 0.5, Anti

      Next k


      If AddOne > 0 Then    ' For Connected polygons
         BevelJoint picDRAW, _
                       pxstoL(PointCount - 1), pystoL(PointCount - 1), _
                       pxstoL(PointCount), pystoL(PointCount), _
                       pxstoL(2), pystoL(2), Cul, 0.5, Anti
   
         BevelJoint picDRAW, _
                       pxstoR(PointCount - 1), pystoR(PointCount - 1), _
                       pxstoR(PointCount), pystoR(PointCount), _
                       pxstoR(2), pystoR(2), Cul, 0.5, Anti
      End If

      PixelSize = pxsv
   End If
   
   If aRound Then   ' AA & Round
      If PixelSize > 3 And PointCount >= 3 Then   ' Fill joints
         For k = 2 To PointCount - 1
            DrawDot pxstoL(k), pystoL(k), Cul
            DrawDot pxstoR(k), pystoR(k), Cul
         Next k
      End If
   End If
End Sub

Public Sub CompleteCurvyLines(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim k As Long
Dim zalpha As Single
Dim zdx As Single, zdy As Single
Dim DL As Single
Dim Exten As Single
Dim Anti As Long
   If Not aComplete Then Exit Sub
   ' Save for Space bar copying as GenerateCurvyPoints creates
   ' extra x/ysto() & the originals are needed to move the shape.
   ReDim svxsto(PointCount) As Long, svysto(PointCount) As Long
   svxsto() = xsto()
   svysto() = ysto()
   svPointCount = PointCount
   
   aMouseDown = False
   Select Case CurvyLineTool
   Case [CLSolid]
      For k = 1 To PointCount - 1
         picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul '<<<< Clear
      Next k
      picDRAW.DrawMode = vbCopyPen
   
   Anti = 0
   If Not aAA Then Anti = 1
   
      ' DRAW
      If PointCount = 2 Then
        If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
           DrawDot xsto(1), ysto(1), Cul
           SCOPY = True
           PixelSize = pxsvORG
           picDRAW.DrawWidth = PixelSize
           DISPLAY Form1, picDRAW
           Exit Sub
        End If
      End If
      
      GenerateCurvyPoints xsto(), ysto(), PointCount    ' New set of x/ysto()s
      
      For k = 1 To PointCount - 1
         ZDrawAALine xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), Cul, 0, Anti
      Next k
   
   
      If PixelSize > 3 And PointCount >= 3 Then
         If Not aRound Then
            svRound = aRound
            aRound = True
            pxsv = PixelSize
            PixelSize = PixelSize - 1
            For k = 2 To PointCount - 1
               DrawDot xsto(k), ysto(k), Cul
            Next k
            PixelSize = pxsv
            aRound = svRound
         End If
      End If
      DISPLAY Form1, picDRAW
   
   Case [CLDouble], [CLDoubleEnded]
      For k = 1 To PointCount - 1
         picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul '<<<< Clear
         GetParallelCoords zspace, xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), _
               ixa, iya, ixb, iyb
         picDRAW.Line (ixa, iya)-(ixb, iyb), XorCul
      Next k
      picDRAW.DrawMode = vbCopyPen
      
      ' DRAW
      If PointCount = 2 Then
        If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
           DrawDot xsto(1), ysto(1), Cul
           SCOPY = True
           PixelSize = pxsvORG
           picDRAW.DrawWidth = PixelSize
            DISPLAY Form1, picDRAW
           Exit Sub
        End If
      End If
      
      
      If PointCount > 2 Then   ' Generate a new set of points x/ysto()s
         GenerateCurvyPoints xsto(), ysto(), PointCount
      End If
      
      ' Parallel points
      ReDim pxstoL(PointCount) As Long, pystoL(PointCount) As Long
      ReDim pxstoR(PointCount) As Long, pystoR(PointCount) As Long
      
      GetIntersections  ' Returns px/ystoL/R()s from x/ysto()s
      ' Draw whole poly
      DL = 0
      For k = 1 To PointCount - 1
      
         DL = DL + (xsto(k) - xsto(k + 1)) * (xsto(k) - xsto(k + 1)) _
            + (ysto(k) - ysto(k + 1)) * (ysto(k) - ysto(k + 1))
            If DL >= 1 Then
      
               ZDrawAALine pxstoL(k), pystoL(k), pxstoL(k + 1), pystoL(k + 1), Cul, 0, Anti
               ZDrawAALine pxstoR(k), pystoR(k), pxstoR(k + 1), pystoR(k + 1), Cul, 0, Anti
               DL = 0
            End If
      
      Next k
      ' [CLDouble], [CLDoubleEnded] cont
      If PixelSize > 3 And PointCount >= 3 Then
         If Not aRound Then
            svRound = aRound
            aRound = True
            pxsv = PixelSize
            PixelSize = PixelSize - 1
            For k = 2 To PointCount - 1
               DrawDot pxstoL(k), pystoL(k), Cul
               DrawDot pxstoR(k), pystoR(k), Cul
            Next k
            aRound = svRound
            PixelSize = pxsv
         End If
      End If
      DISPLAY Form1, picDRAW
      
      Select Case CurvyLineTool
      Case [CLDoubleEnded]   ' DO ENDS
         Exten = 0
         If Not aRound Then Exten = 0.5
         If Not aRound Then
            zalpha = zATan2(pystoL(2) - pystoL(1), pxstoL(2) - pxstoL(1))
            zdx = PixelSize / 2 * Cos(zalpha)
            zdy = PixelSize / 2 * Sin(zalpha)
         End If
         ZDrawAALine pxstoL(1) + zdx, pystoL(1) + zdy, pxstoR(1) + zdx, pystoR(1) + zdy, Cul, Exten, Anti
         
         If Not aRound Then
            zalpha = zATan2(pystoL(PointCount) - pystoL(PointCount - 1), pxstoL(PointCount) - pxstoL(PointCount - 1))
            zdx = PixelSize / 2 * Cos(zalpha)
            zdy = PixelSize / 2 * Sin(zalpha)
         End If
         ZDrawAALine pxstoL(PointCount) - zdx, pystoL(PointCount) - zdy, pxstoR(PointCount) - zdx, pystoR(PointCount) - zdy, Cul, Exten, Anti
      End Select
      DISPLAY Form1, picDRAW
      
   Case [CLGradient1]
       'picDRAW.DrawWidth = 2 * PixelSize
       picDRAW.DrawWidth = PixelSize
       For k = 1 To PointCount - 1
         picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul '<<<< Clear
      Next k
      picDRAW.DrawMode = vbCopyPen
      
      ' DRAW
      If PointCount = 2 Then
        If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
           DrawDot xsto(1), ysto(1), Cul
           SCOPY = True
           PixelSize = pxsvORG
           picDRAW.DrawWidth = PixelSize
           DISPLAY Form1, picDRAW
           Exit Sub
        End If
      End If
      
      If PointCount > 2 Then   ' Generate a new set of points x/ysto()s
         GenerateCurvyPoints xsto(), ysto(), PointCount
      End If
      picDRAW.DrawMode = vbCopyPen
      DoPolyShading picDRAW, x, y, Anti
      DISPLAY Form1, picDRAW
   End Select
   
   SCOPY = True
Erase pxstoL(), pystoL()
Erase pxstoR(), pystoR()
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub CompleteArrows(picDRAW As PictureBox, x As Single, y As Single, Cul As Long, Optional aRRow As Boolean = True)
Dim Anti As Long
   If NClicks <> 3 Then Exit Sub
   
   aMouseDown = False
   DrawArrow picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), XorCul, aRRow, 0 ' XClear
   picDRAW.DrawMode = vbCopyPen
   
   Anti = 0
   If Not aAA Then Anti = 1
   
   ' DRAW
   DrawArrow picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), Cul, aRRow, 1, Anti
   DISPLAY Form1, picDRAW
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub CompleteJunctions(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim Anti As Long
    If NClicks <> 3 Then Exit Sub
   aMouseDown = False
   DrawJunction picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), XorCul  ' XClear
   picDRAW.DrawMode = vbCopyPen
   
   Anti = 0
   If Not aAA Then Anti = 1
   
   ' DRAW
   DrawJunction picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), Cul, 1
   DISPLAY Form1, picDRAW
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub CompleteTubulars(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
'Public Alpha1 As Single
'Public Alpha2 As Single
'Public Alpha3 As Single

Dim Anti As Long
Dim sx1 As Single, sy1 As Single
Dim sx2 As Single, sy2 As Single

   svRound = aRound
   aRound = True
   'TEST
   sx1 = svxsto2
   sy1 = svysto2
      Screen.MousePointer = vbHourglass
      aMouseDown = False
      ' DRAW
      Anti = 0
      If Not aAA Then Anti = 1
      
      Screen.MousePointer = vbHourglass
      'DoEvents
      
      Select Case TubularTool
      Case [BULLET1]
         NonGradientBullet picDRAW, x, y, Cul, Anti
      Case [BULLET2], [BULLET3]   ' Gradient
         SideCentreGradientBullets picDRAW, x, y, Cul, Anti
         If aOutline = True Then
            NonGradientBullet picDRAW, x, y, Cul, Anti
         End If
      
      Case [TUBE1], [TUBE2], [TUBE3] ' Tubes
         If NClicks >= 3 Then
            If TheKey <> vbKeySpace Then
               picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
            Else
               TheKey = 0
            End If
            
            picDRAW.DrawMode = vbCopyPen

            TubeDRAW Cul
            If TubularTool = [TUBE2] Or TubularTool = [TUBE3] Then
               If aOutline = True Then
                  RotEllipse picDRAW, Cul, 1
               End If
            End If
            If PointInside Then 'Or TubularTool = [TUBE1] Then '[Cone1] Then
               RotEllipse picDRAW, Cul, 1
            End If
         
         End If
      
      Case [CONE1] To [CONE5]
         If NClicks >= 3 Then
            If TheKey <> vbKeySpace Then
               picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), XorCul
            Else
               TheKey = 0
            End If
            
            picDRAW.DrawMode = vbCopyPen
            EllTangents Cul
            'CONE2 to CONE5 no ellipse ?
            If TubularTool = [CONE3] Or TubularTool = [CONE5] Then
               If aOutline = True Then
                  RotEllipse picDRAW, LeftColor, 1
               End If
            End If
            If PointInside Or TubularTool = [CONE1] Then
               RotEllipse picDRAW, Cul, 1
            End If
            'DISPLAY Form1, picDRAW
         End If
      
      Case [CONE6]   ' OXO for circles else ellipse angles different
      
         sx1 = xsto(1): sy1 = ysto(1)
         sx2 = xsto(2): sy2 = ysto(2)
         xsto(2) = (sx1 + sx2) / 2
         ysto(2) = (sy1 + sy2) / 2
         Alpha1 = -1
         Alpha2 = -1
         EllTangents Cul, Alpha1, Alpha2
         Alpha3 = -1
         RotEllipse picDRAW, Cul, 1, Alpha3
         
         xsto(1) = sx2
         ysto(1) = sy2
         xsto(2) = (sx1 + sx2) / 2
         ysto(2) = (sy1 + sy2) / 2
         
         EllTangents Cul, Alpha1, Alpha2
         RotEllipse picDRAW, Cul, 1, Alpha3

         If TheKey = vbKeySpace Then TheKey = 0
   
         xsto(1) = sx1: ysto(1) = sy1
         xsto(2) = svxsto2
         ysto(2) = svysto2
   
      End Select
      
      DISPLAY Form1, picDRAW
      Screen.MousePointer = vbDefault
 
   SCOPY = True
   aRound = svRound
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub


Private Sub TubeDRAW(Cul As Long)

'We have:-
' 1 (xang,yang)             End of ellipse rotation line
' 2 (xsto(2),ysto(2))       External tangent point (xt,yt)
' 3 (xsto(1),ysto(1))       center of ellipse (xc,yc)

' Saved points
'Public xang As Single, yang As Single
'Public PointInside As Boolean

Dim xc As Single, yc As Single
Dim xt As Single, yt As Single
Dim EllR As Single
Dim RA As Single
Dim rx As Single, ry As Single
Dim zA As Single
Dim zB As Single

Dim m As Single  ' Slope (xt,yt)-(xc,yc) = yt/xt for ellipse at (0,0)
Dim c As Single  ' Cut =0 for ellipse at (0,0)

Dim k1 As Single, k2 As Single
' Quadratic terms
Dim aa As Single, BB As Single, cc As Single

Dim zA2 As Single
Dim zB2 As Single
Dim BBSQ As Single
Dim yeSQ As Single
Dim xe1 As Single, ye1 As Single
Dim xe2 As Single, ye2 As Single
   
Dim xi As Single, yi As Single
Dim XJ As Single, YJ As Single
Dim Ci As Single, cj As Single
Dim mt As Single, ct As Single
Dim xri As Single, yri As Single
Dim xrj As Single, yrj As Single

Dim Quadi As Long, Quadj
Dim ALi As Single, ALj As Single
Dim Angi As Single
Dim Angj As Single
Dim TAlpha As Single
Dim z1 As Single ', z2 As Single
Dim zstep As Single
Dim xp As Single, yp As Single
Dim xpr As Single, ypr As Single
      
' For drawing Tubes
Dim rotx As Single, roty As Single
Dim aRev As Boolean
Dim theta As Single
Dim L As Single
Dim SX As Single, SY As Single
Dim Lx As Single, LY As Single
Dim xpo As Single, ypo As Single
Dim xxpr As Single, yypr As Single
Dim CCUL As Long
Dim AngH As Single, LAB As Single
Dim LABX As Single, LABY As Single

   ' Rotation of ellipse
   
   EllR = zAng(ysto(1) - yang, xang - xsto(1))
   RA = -EllR
   
   If zradx >= zrady Then
      zA = zradx
      zB = zrady
   Else
      zA = zrady
      zB = zradx
   End If
   
   xc = xsto(1): yc = ysto(1)
   xt = xsto(2): yt = ysto(2)
   
   If xt = xang And yt = yang Then
      xt = xt - 1
      If xt < 0 Then xt = 0
   End If
   
   ' Shift (xt,yt) to ellipse at (0,0)
   rx = (xt - xc) * Cos(RA) - (yt - yc) * Sin(RA)
   ry = (xt - xc) * Sin(RA) + (yt - yc) * Cos(RA)
   ' To test condition that point is inside ellipse
   PointInside = False
   BB = ((rx) / zA) ^ 2 + ((ry) / zB) ^ 2
   If BB < 1 Then
      PointInside = True
      Select Case TubularTool
      Case [TUBE1]
         DrawDot xc, yc, Cul
      Case [TUBE2] To [TUBE3]
         EllR = zATan2(ysto(1) - yang, xang - xsto(1))
         InsideGRAD xc, yc, xsto(2), ysto(2), zA, zB, EllR, 0
      End Select
      xsto(2) = xang 'svxsto2
      ysto(2) = yang 'svysto2
      Exit Sub
   End If
   
   zA2 = zA ^ 2
   zB2 = zB ^ 2
   
   'If rx = 0 Then
   If Abs(rx) < 0.00001 Then
      m = Sgn(ry) * 10000
   Else
      m = ry / rx
   End If
   
   c = 0
   ' Condition that chords are a tangent
   k1 = Sqr((zA * m) ^ 2 + zB2)
   k2 = -k1 ' makes no diff
   
   aa = zB2 + zA2 * m ^ 2
   BB = 2 * zA2 * m * k1
   cc = zA2 * (k1 ^ 2 - zB2)

   BBSQ = (BB ^ 2 - 4 * aa * cc)
   If BBSQ < 0 Then
      BBSQ = 0
   '   MsgBox "-ve"
   '   Exit Sub
   End If

   ' 2 points (xi,yi) & (xj,yj)
   ' on regular ellipse at (0,0)
   xi = (-BB - Sqr(BBSQ)) / (2 * aa)
   
   yeSQ = Sqr(zA2 * m ^ 2 + zB2)
   yi = m * xi + yeSQ
   
   ' Opposite side (-180 deg) of ellipse
   XJ = -xi
   YJ = -yi
   
   Ci = yi - m * xi
   cj = YJ - m * XJ
   'Now have 2 lines from points on ellipse @ (0,0)
   ' y = m*x + ci
   ' y = m*x + cj
   If m = 0 Then
      mt = -1000
   Else
      mt = -1 / m ' Slope @ rightangles to xt,yt line
   End If
   
'If xi = 0 Then
'   mt = Sgn(yi) * 10000
'Else
'   mt = yi / xi
'End If

   ' y=mt*x+ct
   ' ct=yt-mt*xt  ' ie
   ct = ry - mt * rx
   xri = (Ci - ct) / (mt - m)
   yri = xri * mt + ct
   
   xrj = (cj - ct) / (mt - m)
   yrj = xrj * mt + ct

   RA = -RA
   xe1 = xi * Cos(RA) - yi * Sin(RA)
   ye1 = xi * Sin(RA) + yi * Cos(RA)
   xe2 = XJ * Cos(RA) - YJ * Sin(RA)
   ye2 = XJ * Sin(RA) + YJ * Cos(RA)

   ' SET Quadi
   If yri <= 0 Then ' Q 3 or 4 (or 33 or 44)
      If xri <= 0 Then Quadi = 3 Else Quadi = 4
      If xri <= 0 And xri > 0 - zA Then Quadi = 33
      If xri >= 0 And xri < 0 + zA Then Quadi = 44
   Else  ' Q 1 or 2  (or 11 or 22)
      If xri <= 0 Then Quadi = 2 Else Quadi = 1
      If xri <= 0 And xri > 0 - zA Then Quadi = 22
      If xri >= 0 And xri < 0 + zA Then Quadi = 11
   End If
   ' SET Quadj
   If yrj <= 0 Then ' Q 3 or 4 (or 33 or 44)
      If xrj <= 0 Then Quadj = 3 Else Quadj = 4
      If xrj <= 0 And xrj > 0 - zA Then Quadj = 33
      If xrj >= 0 And xrj < 0 + zA Then Quadj = 44
   Else  ' Q 1 or 2  (or 11 or 22)
      If xrj <= 0 Then Quadj = 2 Else Quadj = 1
      If xrj <= 0 And xrj > 0 - zA Then Quadj = 22
      If xrj >= 0 And xrj < 0 + zA Then Quadj = 11
   End If
   ' 2 points (xi,yi) & (xj,yj)
   ' on regular ellipse at (0,0)
   ' auxilliary angles.  0 to pi#
   ALi = ArcCos(xi / zA)
   ALj = -ArcCos(XJ / zA)
   

   If Quadi = 1 Or Quadi = 4 Or Quadj = 1 Or Quadj = 4 Then  ' (-j,S)
      Angi = ALj
      Angj = ALi
   ElseIf (Quadi = 11 And Quadj = 44) Then   '( -j,i)
      z1 = ALj
      Angj = ALi
      Angi = z1
   ElseIf (Quadi = 22 And Quadj = 44) Then   ' (-j,i)
      z1 = ALi
      Angi = ALj
      Angj = z1
   Else  '(i,-j)
      Angi = ALi
      Angj = ALj
   End If

   ' ()===)
   
   ' Rotation of ellipse
   EllR = zAng(ysto(1) - yang, xang - xsto(1))
   RA = EllR
   aRev = False

   SX = (xt - xc) '* 0.75
   SY = (yt - yc) '* 0.75
   L = Sqr(SX ^ 2 + SY ^ 2)
   'theta = zAng(SY, SX)
   theta = pi / 2 - zATan2(SY, SX)  ' pi/2 -pi to pi/2+pi = -pi/2 to 3*pi/2
   Lx = L * Sin(theta)
   LY = L * Cos(theta)
   If Angj < Angi Then
      Angj = 2 * pi + Angj
   End If
   'Shorten L
   AngH = (Angi + Angj) / 2
   TAlpha = AngH
   xp = zA * Cos(TAlpha)
   yp = zB * Sin(TAlpha)
   xpo = xp
   ypo = yp
   ' Rotate
   rotx = xp * Cos(RA) - yp * Sin(RA)
   roty = xp * Sin(RA) + yp * Cos(RA)
   ' Translate
   ' At closest part of ellipse
   xp = rotx + xc
   yp = roty + yc
   xxpr = zA * Cos(Angi + Angj - TAlpha - pi)
   yypr = zB * Sin(Angi + Angj - TAlpha - pi)
   ' Rotate
   rotx = xxpr * Cos(RA) - yypr * Sin(RA)
   roty = xxpr * Sin(RA) + yypr * Cos(RA)
   ' Translate
   ' At Arc
   xxpr = rotx + xc
   yypr = roty + yc
   LAB = Sqr((xxpr - xp) ^ 2 + (yypr - yp) ^ 2)
   LAB = LAB / 2
   Lx = (L - LAB) * Sin(theta)
   LY = (L - LAB) * Cos(theta)
   LABX = LAB * Sin(theta)
   LABY = LAB * Cos(theta)

If TubularTool = [TUBE2] Or TubularTool = [TUBE3] Then
     ' Draw lines
     ' Points from ARC  Angi to Angj about (xt,yt)
     ' to to new Arc Angj to Angi about (xc,yc)
      
      zstep = 0.01
      If TubularTool = [TUBE2] Then
         NSteps = (Angj - Angi) / (zstep / 4)
      Else
         NSteps = (Angj - Angi) / (zstep / 2)
      End If
      GetColorSteps
      GetColorStepsReversed2
   
      For TAlpha = Angi To Angj Step zstep / 4
         ' Ellipse point about (0,0)
         xp = zA * Cos(TAlpha)
         yp = zB * Sin(TAlpha)
         xpo = xp
         ypo = yp
         ' Rotate
         rotx = xp * Cos(RA) - yp * Sin(RA)
         roty = xp * Sin(RA) + yp * Cos(RA)
         ' Translate
         ' At closest part of ellipse
         xp = rotx + xc
         yp = roty + yc
         ' At the Arc
         xpr = xp + Lx
         ypr = yp + LY
         If aAA Then
            If TAlpha <= (Angi + zstep / 4) Then 'Or TAlpha >= (Angj - zstep / 4) Then
               CCUL = RGB(CByte(zRL), CByte(zGL), CByte(zBL))
               'PixelSize = 2
               ZDrawAALine xpr, ypr, xp, yp, CCUL, 0, 0
               PixelSize = 1
'            Else
'               BresLine xpr, ypr, xp, yp, CByte(zRL), CByte(zGL), CByte(zBL), 1, 1
'            End If
         
            ElseIf TAlpha >= (Angj - zstep / 4) Then
               CCUL = RGB(CByte(zRL), CByte(zGL), CByte(zBL))
               'PixelSize = 2
               ZDrawAALine xp, yp, xpr, ypr, CCUL, 0, 0
               PixelSize = 1
            Else
               BresLine xpr, ypr, xp, yp, CByte(zRL), CByte(zGL), CByte(zBL), 1, 1
            End If
         
         Else
               BresLine xpr, ypr, xp, yp, CByte(zRL), CByte(zGL), CByte(zBL), 1, 1
         End If
         ' Above does not depend on rotation of the ellipse
         xxpr = zA * Cos(Angi + Angj - TAlpha - pi)
         yypr = zB * Sin(Angi + Angj - TAlpha - pi)
         ' Rotate
         rotx = xxpr * Cos(RA) - yypr * Sin(RA)
         roty = xxpr * Sin(RA) + yypr * Cos(RA)
         ' Translate
         ' At closest part of ellipse
         xxpr = rotx + xc
         yypr = roty + yc
         
         BresLine xp, yp, xxpr, yypr, CByte(zRL2), CByte(zGL2), CByte(zBL2), 1, 1
         
            If TubularTool = [TUBE3] Then
            If aRev = False Then
               If TAlpha >= (Angi + Angj) / 2 Then
                  aRev = True
                  zRIncr = -zRIncr
                  zGIncr = -zGIncr
                  zBIncr = -zBIncr
                  zRIncr2 = -zRIncr2
                  zGIncr2 = -zGIncr2
                  zBIncr2 = -zBIncr2
               End If
            End If
            End If
         
         GetNextColors
         GetNextColors2
         
      Next TAlpha
End If


If TubularTool = [TUBE1] Or aOutline = True Then
      zstep = 0.01
   
      If Angj < Angi Then
         Angj = 2 * pi + Angj
      End If
      
      xpr = zA * Cos(Angi - zstep / 4)
      ypr = zB * Sin(Angi - zstep / 4)
      ' Rotate
      rotx = xpr * Cos(RA) - ypr * Sin(RA)
      roty = xpr * Sin(RA) + ypr * Cos(RA)
      ' Translate
      xpr = rotx + xt
      ypr = roty + yt
   
      For TAlpha = Angi To Angj Step zstep / 4
         ' Ellipse point about (0,0)
         xp = zA * Cos(TAlpha)
         yp = zB * Sin(TAlpha)
         ' Rotate
         rotx = xp * Cos(RA) - yp * Sin(RA)
         roty = xp * Sin(RA) + yp * Cos(RA)
         ' Translate
         xp = rotx + xt
         yp = roty + yt
         'ZDrawAALine xpr + Lx, ypr + LY, xp + Lx, yp + LY, Cul, 0
         ' DOT?
         ZDrawAALine xp - LABX, yp - LABY, xp - LABX, yp - LABY, Cul, 0
         xpr = xp
         ypr = yp
      Next TAlpha
      
      'ELLIPSE
      
      xpr = zA '* Cos(z1 - zstep / 4)
      ypr = 0 'zB * Sin(z1 - zstep / 4)
      ' Rotate
      rotx = xpr * Cos(RA) - ypr * Sin(RA)
      roty = xpr * Sin(RA) + ypr * Cos(RA)
      ' Translate
      xpr = rotx + xc
      ypr = roty + yc
      
      For TAlpha = 0 To 2 * pi Step zstep / 4
         ' Ellipse point about (0,0)
         xp = zA * Cos(TAlpha)
         yp = zB * Sin(TAlpha)
         ' Rotate
         rotx = xp * Cos(RA) - yp * Sin(RA)
         roty = xp * Sin(RA) + yp * Cos(RA)
         ' Translate
         xp = rotx + xc
         yp = roty + yc
         ZDrawAALine xpr, ypr, xp, yp, Cul, 0
         xpr = xp
         ypr = yp
      Next TAlpha
      
      'SIDE LINES
      
      ZDrawAALine xe1 + xc, ye1 + yc, xe1 + xt - LABX, ye1 + yt - LABY, LeftColor 'Cul
      ZDrawAALine xe2 + xc, ye2 + yc, xe2 + xt - LABX, ye2 + yt - LABY, LeftColor ' Cul
End If

   ' Tangents done but to maintain ellipse angle need:-
   xsto(2) = xang 'svxsto2
   ysto(2) = yang 'svysto2
   
End Sub

Private Sub NonGradientBullet(picDRAW As PictureBox, x As Single, y As Single, _
            Cul As Long, Optional Anti As Long = 0)
' [Bullet1] ' Non-Gradient Bullet
' IN: Circle centre = x,y  radius = zrad
' OUT: Tangents kxp,kyp to kx1,ky1 to kx4,ky4
Dim kx1 As Single, ky1 As Single
Dim kx2 As Single, ky2 As Single
Dim kx3 As Single, ky3 As Single
Dim kx4 As Single, ky4 As Single
Dim svrad As Single
Dim Exten As Single

   Exten = 0
   EvalDiameters xsto(1), ysto(1), zrad, xsto(2), ysto(2), _
      kx1, ky1, kx2, ky2, kx3, ky3, kx4, ky4
   zAS = zATan2(ysto(1) - ky1, xsto(1) - kx1)
   zAE = zATan2(ysto(1) - ky2, xsto(1) - kx2)
   svrad = zrad
   If PixelSize > 1 Then zrad = zrad + 1
   EvalCirlipse xsto(1), ysto(1), xsto(1) + zrad, ysto(1) + zrad
   ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, zAS, zAE, Anti
   If Anti = 0 And PixelSize = 1 Then
      ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, zAS, zAE, 1
   End If
   zrad = svrad
   If PixelSize >= 6 Then
      PixelSize = PixelSize - 2
   ElseIf PixelSize > 1 Then
      PixelSize = PixelSize - 1
   End If
   ZDrawAALine kx1, ky1, kx3, ky3, Cul, Exten, Anti
   ZDrawAALine kx2, ky2, kx4, ky4, Cul, Exten, Anti
   ZDrawAALine kx3, ky3, kx4, ky4, Cul, Exten, Anti
   If Anti = 0 And PixelSize = 1 Then
      ZDrawAALine kx1, ky1, kx3, ky3, Cul, Exten, 1
      ZDrawAALine kx2, ky2, kx4, ky4, Cul, Exten, 1
      ZDrawAALine kx3, ky3, kx4, ky4, Cul, Exten, 1
   End If
   PixelSize = pxsv
End Sub


'Public Sub SideCentreGradientTubes(picDRAW As PictureBox, x As Single, y As Single, Cul As Long, Optional Anti As Long = 0)
'' Case [Tube2], [Tube3]
'' Tangent/Diameter coords
'Dim kx1 As Single, ky1 As Single
'Dim kx2 As Single, ky2 As Single
'Dim kx3 As Single, ky3 As Single
'Dim kx4 As Single, ky4 As Single
'' Extras for shading
'Dim zalpha As Single, zalpha1 As Single, zalpha2 As Single
'Dim ix As Long, iy As Long
'Dim ix2 As Long, iy2 As Long
'' Extras for centre shading
'Dim aRev As Boolean
'Dim zstep As Single
'Dim NAlt As Long
'Dim zdx As Single, zdy As Single
'Dim zx2 As Single, zy2 As Single
'
'Dim ixa As Single, iya As Single, ixb As Single, iyb As Single
'Dim kk As Long
'   'IN: Circ1: kxs,kys,zradius  Circ2: kxp,kyp,zradius both same radius
'   'OUT: Circ1 diam cords: kx1,ky1 -> kx3,ky3  Circ2 diam coords kx2,ky2 -> kx4,ky4
'   ' Draw semis
'   EvalDiameters xsto(2), ysto(2), zrad, xsto(1), ysto(1), _
'      kx1, ky1, kx2, ky2, kx3, ky3, kx4, ky4
'
''   If ky1 <= ky2 Then
''      zangle = zATan2(ky2 - ky1, kx1 - kx2)
''      ZAACIRLIPSE xsto(2), ysto(2), zrad, zrad, Cul, zangle, zangle + pi#, Anti
''   Else
''      zangle = pi# + zATan2(ky1 - ky2, kx2 - kx1)
''      ZAACIRLIPSE xsto(2), ysto(2), zrad, zrad, Cul, zangle, zangle - pi#, Anti
''   End If
'
'   zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
'   zx2 = xsto(2) - zrad * Cos(zangle)
'   zy2 = ysto(2) - zrad * Sin(zangle)
'
'
'   Select Case TubularTool
'   Case [TUBE2]  ' (SideShade), show inside base circle
'      ' Lines to  (-)---
'      ' Whole tube
'      zalpha1 = zATan2(ky3 - ysto(1), kx3 - xsto(1))
'      zalpha2 = zATan2(ky4 - ysto(1), kx4 - xsto(1))
'      If zalpha1 > zalpha2 Then zalpha2 = zalpha2 + 2 * pi#
'      NAlt = 0
'
'      NSteps = 8 * r2d# * (zalpha2 - zalpha1)
'      GetColorSteps
'      LngToRGB RightColor, CByte(zRL), CByte(zGL), CByte(zBL)
'      'PixelSize = 2
'      For zalpha = r2d# * zalpha1 To r2d# * zalpha2 Step 0.125
'         ix = xsto(1) + zrad * Cos(d2r# * zalpha)
'         iy = ysto(1) + zrad * Sin(d2r# * zalpha)
'
'         ix2 = zx2 + zrad * Cos(d2r# * (r2d# * (2 * zalpha2) - zalpha))
'         iy2 = zy2 + zrad * Sin(d2r# * (r2d# * (2 * zalpha2) - zalpha))
'
'         BresLine ix, iy, ix2, iy2, CByte(zRL), CByte(zGL), CByte(zBL), 1, 1
'
'         If Anti = 0 Then ' AA end points
'            If NAlt = 0 Then
'            PixelSize = 2
'            ix2 = xsto(1) + (zrad + 0.5) * Cos(d2r# * zalpha)
'            iy2 = ysto(1) + (zrad + 0.5) * Sin(d2r# * zalpha)
'            DrawDot ix2, iy2, RGB(zRL, zGL, zBL)
'            NAlt = NAlt + 1
'            Else
'               NAlt = NAlt + 1
'               If NAlt = 2 Then
'                  NAlt = 0
'               End If
'            End If
'         End If
'
'         GetNextColors
'      Next zalpha
''Exit Sub
'      ' Full bullet to here
'      ' overwrite for
'      ' Non-circlular part of tube
'      zalpha1 = zATan2(ky1 - ysto(2), kx1 - xsto(2))
'      zalpha2 = zATan2(ky2 - ysto(2), kx2 - xsto(2))
'      If zalpha2 > zalpha1 Then zalpha1 = zalpha1 + 2 * pi#
'
'      NSteps = 8 * r2d# * (zalpha1 - zalpha2)
'      GetColorSteps
'      'PixelSize = 2
'
'      For zalpha = r2d# * zalpha2 To r2d# * zalpha1 Step 0.125
'         ix = xsto(1) + zrad * Cos(d2r# * zalpha)
'         iy = ysto(1) + zrad * Sin(d2r# * zalpha)
'
'         ix2 = zx2 + zrad * Cos(d2r# * zalpha)
'         iy2 = zy2 + zrad * Sin(d2r# * zalpha)
'
'         BresLine ix, iy, ix2, iy2, CByte(zRL), CByte(zGL), CByte(zBL), 1, 1
'
'         If Anti = 0 Then ' AA end points
'            If NAlt = 0 Then
'            PixelSize = 2
'            DrawDot ix, iy, RGB(zRL, zGL, zBL)
'            DrawDot ix2, iy2, RGB(zRL, zGL, zBL)
'            NAlt = NAlt + 1
'            Else
'               NAlt = NAlt + 1
'               If NAlt = 2 Then
'                  NAlt = 0
'               End If
'            End If
'         End If
'
'         GetNextColors
'      Next zalpha
'
'      If Anti = 0 Then
'         PixelSize = 1
'         ix = xsto(1) + (zrad + 1) * Cos(zalpha2)
'         iy = ysto(1) + (zrad + 1) * Sin(zalpha2)
'         ix2 = zx2 + (zrad + 1) * Cos(zalpha2)
'         iy2 = zy2 + (zrad + 1) * Sin(zalpha2)
'         ZDrawAALine ix, iy, ix2, iy2, LeftColor, 0.5, Anti
'
'         ix = xsto(1) + (zrad + 1) * Cos(zalpha1)
'         iy = ysto(1) + (zrad + 1) * Sin(zalpha1)
'         ix2 = zx2 + (zrad + 1) * Cos(zalpha1)
'         iy2 = zy2 + (zrad + 1) * Sin(zalpha1)
'         ZDrawAALine ix, iy, ix2, iy2, RightColor, 0.5, Anti
'      End If
'
'   Case [TUBE3]  ' (CentreShade) show inside base circle
'      aRev = False
'      ' Lines to  (-)---
'      ' Whole tube  (-----)
'      zalpha1 = zATan2(ky3 - ysto(1), kx3 - xsto(1))
'      zalpha2 = zATan2(ky4 - ysto(1), kx4 - xsto(1))
'      If zalpha1 > zalpha2 Then zalpha2 = zalpha2 + 2 * pi#
'      NAlt = 0
'      NSteps = 4 * r2d# * (zalpha2 - zalpha1)
'      GetColorStepsReversed
'      'PixelSize = 2
'      For zalpha = r2d# * zalpha1 To r2d# * zalpha2 Step 0.125 'zstep
'         ix = xsto(1) + zrad * Cos(d2r# * zalpha)
'         iy = ysto(1) + zrad * Sin(d2r# * zalpha)
'         ix2 = zx2 + zrad * Cos(d2r# * (r2d# * (2 * zalpha2) - zalpha))
'         iy2 = zy2 + zrad * Sin(d2r# * (r2d# * (2 * zalpha2) - zalpha))
'
'         BresLine ix, iy, ix2, iy2, CByte(zRL), CByte(zGL), CByte(zBL), 1, 1
'
'         If Anti = 0 Then ' AA end points
'            If NAlt = 0 Then
'            PixelSize = 2
'            DrawDot ix, iy, RGB(zRL, zGL, zBL)
'            DrawDot ix2, iy2, RGB(zRL, zGL, zBL)
'            NAlt = NAlt + 1
'            Else
'               NAlt = NAlt + 1
'               If NAlt = 2 Then
'                  NAlt = 0
'               End If
'            End If
'         End If
'
'         If aRev = False Then
'         If zalpha >= r2d# * (zalpha2 + zalpha1) / 2 Then
'            aRev = True
'            zRIncr = -zRIncr
'            zGIncr = -zGIncr
'            zBIncr = -zBIncr
'            'GetColorSteps
'         End If
'         End If
'         GetNextColors
'      Next zalpha
''Exit Sub
'      ' (-)---
'      'Non-circlular part of tube
'      aRev = False
'      NAlt = 0
'      zalpha1 = zATan2(ky1 - ysto(2), kx1 - xsto(2))
'      zalpha2 = zATan2(ky2 - ysto(2), kx2 - xsto(2))
'      If zalpha2 > zalpha1 Then zalpha1 = zalpha1 + 2 * pi#
'
'      NSteps = 4 * r2d# * (zalpha1 - zalpha2)
'      GetColorSteps
'      LngToRGB RightColor, CByte(zRL), CByte(zGL), CByte(zBL)
'      'PixelSize = 2
'      For zalpha = r2d# * zalpha2 To r2d# * zalpha1 Step 0.125
'         ix = xsto(1) + zrad * Cos(d2r# * zalpha)
'         iy = ysto(1) + zrad * Sin(d2r# * zalpha)
'
'         ix2 = zx2 + zrad * Cos(d2r# * zalpha)
'         iy2 = zy2 + zrad * Sin(d2r# * zalpha)
'
'         BresLine ix, iy, ix2, iy2, CByte(zRL), CByte(zGL), CByte(zBL), 1, 1
'
'         If Anti = 0 Then ' AA end points
'            If NAlt = 0 Then
'            PixelSize = 2
'            DrawDot ix, iy, RGB(zRL, zGL, zBL)
'            DrawDot ix2, iy2, RGB(zRL, zGL, zBL)
'            NAlt = NAlt + 1
'            Else
'               NAlt = NAlt + 1
'               If NAlt = 2 Then
'                  NAlt = 0
'               End If
'            End If
'         End If
'
'         If aRev = False Then
'         If zalpha >= r2d# * (zalpha2 + zalpha1) / 2 Then
'            aRev = True
'            zRIncr = -zRIncr
'            zGIncr = -zGIncr
'            zBIncr = -zBIncr
'            'GetColorStepsReversed
'         End If
'         End If
'         GetNextColors
'      Next zalpha
'
'      If Anti = 0 Then
'         PixelSize = 1
'         ix = xsto(1) + (zrad + 1) * Cos(zalpha2)
'         iy = ysto(1) + (zrad + 1) * Sin(zalpha2)
'         ix2 = zx2 + (zrad + 1) * Cos(zalpha2)
'         iy2 = zy2 + (zrad + 1) * Sin(zalpha2)
'         ZDrawAALine ix, iy, ix2, iy2, LeftColor, 0.5, Anti
'
'         ix = xsto(1) + (zrad + 1) * Cos(zalpha1)
'         iy = ysto(1) + (zrad + 1) * Sin(zalpha1)
'         ix2 = zx2 + (zrad + 1) * Cos(zalpha1)
'         iy2 = zy2 + (zrad + 1) * Sin(zalpha1)
'         ZDrawAALine ix, iy, ix2, iy2, LeftColor, 0.5, Anti
'      End If
'
'   End Select
'End Sub

Public Sub SideCentreGradientBullets(picDRAW As PictureBox, x As Single, y As Single, Cul As Long, Optional Anti As Long = 0)
' Tangent/Diameter coords
Dim kx1 As Single, ky1 As Single
Dim kx2 As Single, ky2 As Single
Dim kx3 As Single, ky3 As Single
Dim kx4 As Single, ky4 As Single
' Extras for shading
'Dim zalpha As Single, zalpha1 As Single, zalpha2 As Single

Dim zalpha As Double, zalpha1 As Double, zalpha2 As Double
Dim zLen As Single
Dim ix As Long, iy As Long
Dim ix2 As Long, iy2 As Long
' Extras for centre shading
Dim aRev As Boolean
Dim zstep As Single
' For Gradient bullet
Dim zdx As Single, zdy As Single
Dim zx2 As Single, zy2 As Single
Dim NAlt As Long
   
'Dim ixa As Single, iya As Single, ixb As Single, iyb As Single
'Dim kk As Long
   ' Draw semi
   EvalDiameters xsto(1), ysto(1), zrad, xsto(2), ysto(2), _
      kx1, ky1, kx2, ky2, kx3, ky3, kx4, ky4
   If ky1 <= ky2 Then
      zangle = zATan2(ky2 - ky1, kx1 - kx2)
      'ZAACIRLIPSE xsto(1), ysto(1), zrad, zrad, Cul, zangle, zangle + pi#, Anti
   Else
      zangle = pi# + zATan2(ky1 - ky2, kx2 - kx1)
      'ZAACIRLIPSE xsto(1), ysto(1), zrad, zrad, Cul, zangle, zangle - pi#, Anti
   End If
   
   zLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
   If zLen < zrad Then
      zalpha1 = 2 * pi#
      zalpha2 = 0
   End If
   
   ' Shading
   Select Case TubularTool
   Case [BULLET2]  ' (SideShade)
      ' Lines to  (-)---
      zalpha1 = zATan2(ky1 - ysto(1), kx1 - xsto(1))
      zalpha2 = zATan2(ky2 - ysto(1), kx2 - xsto(1))
      If zalpha2 > zalpha1 Then zalpha1 = zalpha1 + 2 * pi#
      zdx = 2 * zrad * Cos(zalpha2)
      zdy = 2 * zrad * Sin(zalpha2)
      zx2 = xsto(2) + zrad * Cos(zalpha2)
      zy2 = ysto(2) + zrad * Sin(zalpha2)
      NSteps = 1000
      NAlt = 0
      GetColorSteps
      zstep = (zalpha1 - zalpha2) / NSteps
      If zstep = 0 Then zstep = 0.01
      zdx = zdx / NSteps
      zdy = zdy / NSteps
      PixelSize = 2
      For zalpha = zalpha2 To zalpha1 Step zstep
         ix = xsto(1) + zrad * Cos(zalpha)
         iy = ysto(1) + zrad * Sin(zalpha)
         zx2 = zx2 - zdx
         zy2 = zy2 - zdy
         BresLine ix, iy, zx2, zy2, CByte(zRL), CByte(zGL), CByte(zBL), 1, 1
         
         If Anti = 0 Then ' AA end points
            If NAlt = 0 Then
            PixelSize = 2
            ix2 = xsto(1) + (zrad + 0.5) * Cos(zalpha)
            iy2 = ysto(1) + (zrad + 0.5) * Sin(zalpha)
            DrawDot ix2, iy2, RGB(zRL, zGL, zBL)    ' Round
            DrawDot zx2, zy2, RGB(zRL, zGL, zBL)    ' Flat end of bullet
            NAlt = NAlt + 1
            Else
               NAlt = NAlt + 1
               If NAlt = 2 Then
                  NAlt = 0
               End If
            End If
         End If
         GetNextColors
      Next zalpha

      If Anti = 0 Then
         PixelSize = 1
         ix = xsto(1) + (zrad + 1) * Cos(zalpha2)
         iy = ysto(1) + (zrad + 1) * Sin(zalpha2)
         zx2 = xsto(2) + (zrad + 1) * Cos(zalpha2)
         zy2 = ysto(2) + (zrad + 1) * Sin(zalpha2)
         
         ZDrawAALine ix, iy, zx2, zy2, LeftColor, 0.5
         ix = xsto(1) + (zrad + 1) * Cos(zalpha1)
         iy = ysto(1) + (zrad + 1) * Sin(zalpha1)
         zx2 = xsto(2) + (zrad + 1) * Cos(zalpha1)
         zy2 = ysto(2) + (zrad + 1) * Sin(zalpha1)
         ZDrawAALine ix, iy, zx2, zy2, RightColor, 0.5
      End If
   
   Case [BULLET3]  ' (CentreShade)
      aRev = False
      zalpha1 = zATan2(ky1 - ysto(1), kx1 - xsto(1))
      zalpha2 = zATan2(ky2 - ysto(1), kx2 - xsto(1))
      If zalpha2 > zalpha1 Then zalpha1 = zalpha1 + 2 * pi#
      zdx = 2 * zrad * Cos(zalpha2)
      zdy = 2 * zrad * Sin(zalpha2)
      zx2 = xsto(2) + zrad * Cos(zalpha2)
      zy2 = ysto(2) + zrad * Sin(zalpha2)
      NAlt = 0
      NSteps = 1000
      GetColorSteps
      zstep = (zalpha1 - zalpha2) / (2 * NSteps)
      If zstep = 0 Then zstep = 0.01
      zdx = zdx / (2 * NSteps)
      zdy = zdy / (2 * NSteps)
      PixelSize = 2
      For zalpha = zalpha2 To zalpha1 Step zstep
         ix = xsto(1) + zrad * Cos(zalpha)
         iy = ysto(1) + zrad * Sin(zalpha)
         zx2 = zx2 - zdx
         zy2 = zy2 - zdy
         
         BresLine ix, iy, zx2, zy2, CByte(zRL), CByte(zGL), CByte(zBL), 1, 1
         
         If Anti = 0 Then ' AA end points
            If NAlt = 0 Then
            pxsv = PixelSize
            PixelSize = 2
            ix2 = xsto(1) + (zrad + 0.5) * Cos(zalpha)
            iy2 = ysto(1) + (zrad + 0.5) * Sin(zalpha)
            DrawDot ix2, iy2, RGB(zRL, zGL, zBL)    ' Round
            DrawDot zx2, zy2, RGB(zRL, zGL, zBL)    ' Flat end of bullet
            PixelSize = pxsv
            NAlt = NAlt + 1
            Else
               NAlt = NAlt + 1
               If NAlt = 2 Then
                  NAlt = 0
               End If
            End If
         End If
         
         If aRev = False Then
         If zalpha >= (zalpha2 + zalpha1) / 2 Then
            aRev = True
            GetColorStepsReversed
         End If
         End If
         GetNextColors
      Next zalpha
   
      If Anti = 0 Then
         PixelSize = 1
         ix = xsto(1) + (zrad + 1) * Cos(zalpha2)
         iy = ysto(1) + (zrad + 1) * Sin(zalpha2)
         zx2 = xsto(2) + (zrad + 1) * Cos(zalpha2)
         zy2 = ysto(2) + (zrad + 1) * Sin(zalpha2)
         ZDrawAALine ix, iy, zx2, zy2, LeftColor, 0.5
         
         ix = xsto(1) + (zrad + 1) * Cos(zalpha1)
         iy = ysto(1) + (zrad + 1) * Sin(zalpha1)
         zx2 = xsto(2) + (zrad + 1) * Cos(zalpha1)
         zy2 = ysto(2) + (zrad + 1) * Sin(zalpha1)
         ZDrawAALine ix, iy, zx2, zy2, LeftColor, 0.5
      End If
   
   End Select
End Sub

Public Sub CompleteShape(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim k As Long
Dim Exten As Single
Dim Anti As Long

' NB Only Wedges Bevelled ?

'[FWedge] = 0
'[BWedge]
'[Frustrum]
'[Triangle]
'[Square]
'[Hexagon]
   
   If aProject Then
      CompleteShape3D picDRAW, x, y, Cul
      Exit Sub
   End If
   
   If NClicks <> 3 Then Exit Sub
   aMouseDown = False
   
   If PointCount = 7 Then ' [Hexagon], [Hexagon45]
      For k = 3 To PointCount   ' XClear
         picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
      Next k
      picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
      
   Else
      If ShapeTool = [Paragram45] Then   ' XClear
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
      Else
         For k = 1 To PointCount - 1   ' XClear
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul  ' XClear
         Next k
      End If
   End If
      
   If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
   If xsto(2) = xsto(3) And ysto(2) = ysto(3) Then
      picDRAW.DrawMode = vbCopyPen
      DrawDot xsto(1), ysto(1), Cul
      SCOPY = True
      PixelSize = pxsvORG
      picDRAW.DrawWidth = PixelSize
      Exit Sub
   End If
   End If
   
   picDRAW.DrawMode = vbCopyPen
   
   Anti = 0
   If Not aAA Then Anti = 1
   
   ' DRAW
   Exten = 0
   If Not aRound Then Exten = 0.5
      
   If PointCount = 5 Then  ' Wedges, Frustrum, Triangle, Square, Paragram45
      picDRAW.DrawMode = vbCopyPen
      
'      If aAA Or Not aRound Then  ' AA or Square ends
         If ShapeTool = [Paragram45] Then
            ZDrawAALine xsto(1), ysto(1), xsto(3), ysto(3), Cul, 0, Anti
            ZDrawAALine xsto(3), ysto(3), xsto(2), ysto(2), Cul, 0, Anti
            ZDrawAALine xsto(2), ysto(2), xsto(4), ysto(4), Cul, 0, Anti
            ZDrawAALine xsto(4), ysto(4), xsto(5), ysto(5), Cul, 0, Anti
         Else
            For k = 1 To PointCount - 1    ' For Hexagon PointCount=8, ie k=2 to k+1=8
               ZDrawAALine xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), Cul, 0, Anti  '  Draw
            Next k
         End If
         
         Select Case ShapeTool
         Case [Frustrum]
            
            If PixelSize > 3 Then
               If Not aRound Then
                  BevelAdjust   ' Does pxsv = PixelSize
                  ' More !
                  Select Case pxsv
                  Case 8: PixelSize = PixelSize * 0.9
                  Case 10: PixelSize = PixelSize * 0.9
                  Case 11: PixelSize = PixelSize * 0.9
                  Case 12: PixelSize = PixelSize * 0.9
                  End Select
                  
                  BevelJoint picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), xsto(3), ysto(3), Cul, 0.5, Anti
                  BevelJoint picDRAW, xsto(2), ysto(2), xsto(3), ysto(3), xsto(4), ysto(4), Cul, 0.5, Anti
                  BevelJoint picDRAW, xsto(3), ysto(3), xsto(4), ysto(4), xsto(1), ysto(1), Cul, 0.5, Anti
                  BevelJoint picDRAW, xsto(4), ysto(4), xsto(1), ysto(1), xsto(2), ysto(2), Cul, 0.5, Anti
   
                  PixelSize = pxsv
               End If
            End If
            
         Case [Triangle]
            If Not aRound Then
               BevelAdjust
               If PixelSize > 5 Then
                  PixelSize = PixelSize * 0.8
               End If
               BevelJoint picDRAW, xsto(1), ysto(1), xsto(2), ysto(2), xsto(4), ysto(4), Cul, 0.5, Anti
               BevelJoint picDRAW, xsto(2), ysto(2), xsto(4), ysto(4), xsto(1), ysto(1), Cul, 0.5, Anti
               BevelJoint picDRAW, xsto(4), ysto(4), xsto(1), ysto(1), xsto(2), ysto(2), Cul, 0.5, Anti
               
               PixelSize = pxsv
            End If
         Case [FWedge], [BWedge]   ' Extend a line
         
            '     4--------3
            '    /         |
            '  1/__________2
            '
            ZDrawAALine xsto(2), ysto(2), xsto(3), ysto(3), Cul, Exten, Anti  '  Draw
            
            If PixelSize > 3 Then
            If Not aRound Then
               BevelAdjust
               BevelJoint picDRAW, xsto(2), ysto(2), xsto(1), ysto(1), xsto(4), ysto(4), Cul, 0.5, Anti
               BevelJoint picDRAW, xsto(1), ysto(1), xsto(4), ysto(4), xsto(3), ysto(3), Cul, 0.5, Anti
               PixelSize = pxsv
            End If
            End If
            
         Case [Square]  ' Extend 2 lines for square ends
            ZDrawAALine xsto(2), ysto(2), xsto(3), ysto(3), Cul, Exten, Anti    '  Draw
            ZDrawAALine xsto(4), ysto(4), xsto(5), ysto(5), Cul, Exten, Anti    '  Draw
         
         Case [Paragram45]
            '1(5)-----3
            '    \     \
            '     \     \
            '      4-----2
            If PixelSize > 3 Then
            If Not aRound Then
               BevelAdjust
               BevelJoint picDRAW, xsto(1), ysto(1), xsto(3), ysto(3), xsto(2), ysto(2), Cul, 0.5, Anti
               BevelJoint picDRAW, xsto(2), ysto(2), xsto(4), ysto(4), xsto(1), ysto(1), Cul, 0.5, Anti
               
               BevelJoint picDRAW, xsto(4), ysto(4), xsto(1), ysto(1), xsto(3), ysto(3), Cul, 0.5, Anti
               BevelJoint picDRAW, xsto(3), ysto(3), xsto(2), ysto(2), xsto(4), ysto(4), Cul, 0.5, Anti
               
               PixelSize = pxsv
            End If
            End If
         
         End Select
      
   ElseIf PointCount = 7 Then ' [Hexagon], [Hexagon45]
      picDRAW.DrawMode = vbCopyPen
      
'      If aAA Or Not aRound Then  ' AA or Square ends
         For k = 3 To PointCount   ' Draw
            ZDrawAALine xsto(k - 1), ysto(k - 1), xsto(k), ysto(k), Cul, 0, Anti
         Next k
         ZDrawAALine xsto(PointCount), ysto(PointCount), xsto(2), ysto(2), Cul, 0, Anti
      
         If PixelSize > 3 And PointCount >= 3 Then  ' Fill in
            If Not aRound Then
               svRound = aRound
               aRound = True
               For k = 2 To PointCount - 1
                  DrawDot xsto(k), ysto(k), Cul
               Next k
               aRound = svRound
            End If
         End If
      
   End If   ' If PointCount = 5 Then Else 8
   DISPLAY Form1, picDRAW
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub


Public Sub CompleteShape3D(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim k As Long
Dim xd(1 To 8) As Long, yd(1 To 8) As Long
Dim x3d(1 To 8) As Long, y3d(1 To 8) As Long
Dim sdx As Single, sdy As Single
Dim PointCount3D As Long
Dim xext1 As Long, yext1 As Long ' pt extended along 2 -> 2'
Dim xext2 As Long, yext2 As Long ' pt extended along 2 -> 2'
' Region markers
Dim tk(1 To 8) As Long

Dim Ang As Single
Dim scosA As Single, ssinA As Single
Dim hRgn As Long
Dim hRgn2 As Long
Dim Anti As Long

   If NClicks <> 4 Then Exit Sub
   aMouseDown = False
   
   If PointCount = 7 Then ' [Hexagon], [Hexagon45]
      For k = 3 To PointCount   ' XClear
         picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), XorCul
      Next k
      picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
      For k = 2 To 7 'PointCount - 1
         picDRAW.Line (xsto(k), ysto(k))-(xsto(13 + k), ysto(13 + k)), XorCul
      Next k
   
   Else
      If ShapeTool = [Paragram45] Then   ' XClear
         xsto(5) = xsto(1): ysto(5) = ysto(1)
         picDRAW.Line (xsto(1), ysto(1))-(xsto(3), ysto(3)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(2), ysto(2)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), XorCul
         
         picDRAW.Line (xsto(1), ysto(1))-(xsto(15), ysto(15)), XorCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(17), ysto(17)), XorCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(16), ysto(16)), XorCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(18), ysto(18)), XorCul
         
      ElseIf ShapeTool = [Triangle] Then   ' XClear
         For k = 1 To PointCount - 1   ' XClear
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul  ' XClear Shape
         Next k
         For k = 1 To 3
            picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul
         Next k
         
      Else
         For k = 1 To PointCount - 1   ' XClear
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul  ' XClear Shape
            picDRAW.Line (xsto(k), ysto(k))-(xsto(14 + k), ysto(14 + k)), XorCul ' XClear 3D lines
         Next k
      End If
   End If
   'picDRAW.Line (xsto(2), ysto(2))-(xsto(16), ysto(16)), XorCul  ' XClear
   
   
   picDRAW.DrawMode = vbCopyPen
   
   
   Anti = 0
   If Not aAA Then Anti = 1
   
   
   ' DRAW
   If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
   If xsto(2) = xsto(3) And ysto(2) = ysto(3) Then
      DrawDot xsto(1), ysto(1), Cul
      SCOPY = True
      PixelSize = pxsvORG
      picDRAW.DrawWidth = PixelSize
      Exit Sub
   End If
   End If
   
'[FWedge] = 0
            '     4--------3
            '    /         |
            '  1/__________2
            '               \
            '                (15)
'[BWedge]
            '   1---------2
            '    \        | \
            '    4\_______3
            '
'[Frustrum]
            '     4--------3
            '    /          \
            '  1/____________\2
            '                  \
'[Triangle]
            '     3
            '    / \
            '  1/___\2
            '         \
'[Square]
            '  4|---------|3
            '   |         |
            '  1|_________|2
            '               \
'[Paragram45]
            '   1-----3
            '    \     \
            '     \     \
            '      4-----2
            '             \
'[Hexagon]
            '  4----3
            '  /    \
            ' /      \
            ' \1     /2
            '  \    /  \
            '  5----6
   
   ' Recast points xysto() to xyd() & xy3D()
   ' and to Poly() & Poly2() & set regions hRgn(end cap1), hRgn2(end cap2).
   ' First get sdx & sdy from xysto(2) & xysto(16)
   sdx = xsto(16) - xsto(2)
   sdy = ysto(16) - ysto(2)
   
   Select Case ShapeTool
   Case [Hexagon], [Hexagon45]      'sto 2-7 to  d 1-6
      ReDim Poly(1 To 6)
      sdx = xsto(15) - xsto(2)
      sdy = ysto(15) - ysto(2)
      For k = 1 To 6
         xd(k) = xsto(k + 1): yd(k) = ysto(k + 1)
         x3d(k) = xd(k) + sdx: y3d(k) = yd(k) + sdy
         Poly(k).x = xd(k): Poly(k).y = yd(k)
      Next k
      PointCount3D = 6
   
   Case [Triangle]      ' 1-2-3
      ReDim Poly(1 To 3)
      ReDim Poly2(1 To 3)
      For k = 1 To 3
         xd(k) = xsto(k): yd(k) = ysto(k)
         x3d(k) = xd(k) + sdx: y3d(k) = yd(k) + sdy
         Poly(k).x = xd(k): Poly(k).y = yd(k)
         Poly2(k).x = x3d(k): Poly2(k).y = y3d(k)
      Next k
      PointCount3D = 3
      hRgn2 = CreatePolygonRgn(Poly2(1), PointCount3D, ALTERNATE)
   
   Case [Paragram45]    ' 1-3-2-4
      ReDim Poly(1 To 4)
      xd(1) = xsto(1): yd(1) = ysto(1)
      xd(2) = xsto(3): yd(2) = ysto(3)
      xd(3) = xsto(2): yd(3) = ysto(2)
      xd(4) = xsto(4): yd(4) = ysto(4)
      For k = 1 To 4
         x3d(k) = xd(k) + sdx: y3d(k) = yd(k) + sdy
         Poly(k).x = xd(k): Poly(k).y = yd(k)
      Next k
      PointCount3D = 4
   
   Case [BWedge], [FWedge], [Frustrum] ' 1-2-3-4
      ReDim Poly(1 To 4)
      ReDim Poly2(1 To 4)
      For k = 1 To 4
         xd(k) = xsto(k): yd(k) = ysto(k)
         x3d(k) = xd(k) + sdx: y3d(k) = yd(k) + sdy
         Poly(k).x = xd(k): Poly(k).y = yd(k)
         Poly2(k).x = x3d(k): Poly2(k).y = y3d(k)
      Next k
      PointCount3D = 4
      hRgn2 = CreatePolygonRgn(Poly2(1), PointCount3D, ALTERNATE)
   
   Case Else   ' 1-2-3-4  Square & Paragram45
      ReDim Poly(1 To 4)
      For k = 1 To 4
         xd(k) = xsto(k): yd(k) = ysto(k)
         x3d(k) = xd(k) + sdx: y3d(k) = yd(k) + sdy
         Poly(k).x = xd(k): Poly(k).y = yd(k)
      Next k
      PointCount3D = 4
   End Select
   
   ' Set region 1
   hRgn = CreatePolygonRgn(Poly(1), PointCount3D, ALTERNATE)
   
   ' DRAW   NO SQUARE BEVELS for 3D Shapes
   picDRAW.DrawMode = vbCopyPen
   
   svRound = aRound
   aRound = True
   
   Ang = zATan2(y3d(2) - yd(2), x3d(2) - xd(2))
   scosA = 4 * Cos(Ang)
   ssinA = 4 * Sin(Ang)
   
   Select Case ShapeTool
   Case [Hexagon], [Hexagon45]   '1-2-3-4-5-6
      For k = 1 To 5
         xext1 = xd(k) + scosA
         yext1 = yd(k) + ssinA
         xext2 = xd(k + 1) + scosA
         yext2 = yd(k + 1) + ssinA
         If PtInRegion(hRgn, xext1, yext1) = 0 Then   ' Outside region 1
         If PtInRegion(hRgn, xext2, yext2) = 0 Then   ' Outside region 1
            DrawPoly picDRAW, Cul, xd(k), yd(k), xd(k + 1), yd(k + 1), x3d(k + 1), y3d(k + 1), x3d(k), y3d(k), Anti
   If Not aAA Then Anti = 1

         End If
         End If
      Next k
      xext1 = xd(6) + scosA
      yext1 = yd(6) + ssinA
      xext2 = xd(1) + scosA
      yext2 = yd(1) + ssinA
      If PtInRegion(hRgn, xext1, yext1) = 0 Then
      If PtInRegion(hRgn, xext2, yext2) = 0 Then
         DrawPoly picDRAW, Cul, xd(6), yd(6), xd(1), yd(1), x3d(1), y3d(1), x3d(6), y3d(6), Anti
   If Not aAA Then Anti = 1

      End If
      End If
      
   Case [Triangle]      ' 1-2-3
      ' hRgn
      xext1 = xd(1) + scosA
      yext1 = yd(1) + ssinA
      tk(1) = PtInRegion(hRgn, xext1, yext1)
      xext1 = xd(2) + scosA
      yext1 = yd(2) + ssinA
      tk(2) = PtInRegion(hRgn, xext1, yext1)
      xext1 = xd(3) + scosA
      yext1 = yd(3) + ssinA
      tk(3) = PtInRegion(hRgn, xext1, yext1)
      If tk(1) = 0 And tk(2) = 0 And tk(3) = 0 Then  ' all points outside end cap1
         ' Draw 3 lines from end cap1 points
         xext1 = xd(1) + scosA
         yext1 = yd(1) + ssinA
         xext2 = xd(2) + scosA
         yext2 = yd(2) + ssinA
      ZDrawAALine xd(1), yd(1), x3d(1), y3d(1), Cul, 0, Anti
'         picDRAW.Line (xd(1), yd(1))-(x3d(1), y3d(1)), Cul
         
         xext1 = xd(2) + scosA
         yext1 = yd(2) + ssinA
         xext2 = xd(3) + scosA
         yext2 = yd(3) + ssinA
      ZDrawAALine xd(2), yd(2), x3d(2), y3d(2), Cul, 0, Anti
'         picDRAW.Line (xd(2), yd(2))-(x3d(2), y3d(2)), Cul
      
         xext1 = xd(3) + scosA
         yext1 = yd(3) + ssinA
         xext2 = xd(1) + scosA
         yext2 = yd(1) + ssinA
      ZDrawAALine xd(3), yd(3), x3d(3), y3d(3), Cul, 0, Anti
'         picDRAW.Line (xd(3), yd(3))-(x3d(3), y3d(3)), Cul
         
         'hRgn2
         xext1 = x3d(1) - scosA
         yext1 = y3d(1) - ssinA
         tk(4) = PtInRegion(hRgn2, xext1, yext1)
         xext1 = x3d(2) - scosA
         yext1 = y3d(2) - ssinA
         tk(5) = PtInRegion(hRgn2, xext1, yext1)
         xext1 = x3d(3) - scosA
         yext1 = y3d(3) - ssinA
         tk(6) = PtInRegion(hRgn2, xext1, yext1)
         
         ' Sort out which lines to draw onto end2 cap
         ' whichever point is inside hRgn2 draw lines from that to
         ' the other two
         ' t5 M-R (1)
         ' t6 C-G (2)
         ' t7 B-B (3)
         If tk(4) <> 0 Then      ' tk(4) inside region 2  draw 1-2 & 1-3
            DrawTriLines picDRAW, Cul, x3d(1), y3d(1), x3d(2), y3d(2), x3d(3), y3d(3), Anti
         ElseIf tk(5) <> 0 Then  ' tk(5) inside region 2  draw 2-1 & 2-3
            DrawTriLines picDRAW, Cul, x3d(2), y3d(2), x3d(1), y3d(1), x3d(3), y3d(3), Anti
         ElseIf tk(6) <> 0 Then  ' tk(6) inside region 2  draw 3-1 & 3-2
            DrawTriLines picDRAW, Cul, x3d(3), y3d(3), x3d(1), y3d(1), x3d(2), y3d(2), Anti
         End If
         
      Else ' at least one point inside end cap1 OK
         xext1 = xd(1) + scosA
         yext1 = yd(1) + ssinA
         xext2 = xd(2) + scosA
         yext2 = yd(2) + ssinA
         If PtInRegion(hRgn, xext1, yext1) = 0 Then   ' Outside region 1
         If PtInRegion(hRgn, xext2, yext2) = 0 Then   ' Outside region 1
            DrawPoly picDRAW, Cul, xd(1), yd(1), xd(2), yd(2), x3d(2), y3d(2), x3d(1), y3d(1), Anti
         End If
         End If
         
         xext1 = xd(2) + scosA
         yext1 = yd(2) + ssinA
         xext2 = xd(3) + scosA
         yext2 = yd(3) + ssinA
         If PtInRegion(hRgn, xext1, yext1) = 0 Then   ' Outside region 1
         If PtInRegion(hRgn, xext2, yext2) = 0 Then   ' Outside region 1
            DrawPoly picDRAW, Cul, xd(2), yd(2), xd(3), yd(3), x3d(3), y3d(3), x3d(2), y3d(2), Anti
         End If
         End If
         
         xext1 = xd(3) + scosA
         yext1 = yd(3) + ssinA
         xext2 = xd(1) + scosA
         yext2 = yd(1) + ssinA
         If PtInRegion(hRgn, xext1, yext1) = 0 Then   ' Outside region 1
         If PtInRegion(hRgn, xext2, yext2) = 0 Then   ' Outside region 1
            DrawPoly picDRAW, Cul, xd(3), yd(3), xd(1), yd(1), x3d(1), y3d(1), x3d(3), y3d(3), Anti
         End If
         End If
      End If
      
   Case [BWedge], [FWedge], [Frustrum]
      For k = 1 To 3
         xext1 = xd(k) + scosA
         yext1 = yd(k) + ssinA
         tk(k) = PtInRegion(hRgn, xext1, yext1) ' 1,2,3,4
      Next k
      xext1 = xd(4) + scosA
      yext1 = yd(4) + ssinA
      tk(4) = PtInRegion(hRgn, xext1, yext1)

      ' hRgn2
      For k = 1 To 3
         xext1 = x3d(k) - scosA
         yext1 = y3d(k) - ssinA
         tk(k + 4) = PtInRegion(hRgn2, xext1, yext1)  ' 5,6,7,8
      Next k
      xext1 = x3d(4) - scosA
      yext1 = y3d(4) - ssinA
      tk(8) = PtInRegion(hRgn2, xext1, yext1)
      
      If tk(1) = 0 And tk(2) = 0 And tk(3) = 0 And tk(4) = 0 Then   ' all points outside end cap1
         '    5-6, 6-7, 7-8, 8-5 If both outide end cap2 then skip
         'tk  1-2, 2-3, 3-4, 4-1
         If tk(5) <> 0 Or tk(6) <> 0 Then  ' One inside end cap2
            DrawPoly picDRAW, Cul, xd(1), yd(1), xd(2), yd(2), x3d(2), y3d(2), x3d(1), y3d(1), Anti
         End If
         If tk(6) <> 0 Or tk(7) <> 0 Then  ' One inside end cap2
            DrawPoly picDRAW, Cul, xd(2), yd(2), xd(3), yd(3), x3d(3), y3d(3), x3d(2), y3d(2), Anti
         End If
         If tk(7) <> 0 Or tk(8) <> 0 Then  ' One inside end cap2
            DrawPoly picDRAW, Cul, xd(3), yd(3), xd(4), yd(4), x3d(4), y3d(4), x3d(3), y3d(3), Anti
         End If
         If tk(8) <> 0 Or tk(5) <> 0 Then  ' One inside end cap2
            DrawPoly picDRAW, Cul, xd(4), yd(4), xd(1), yd(1), x3d(1), y3d(1), x3d(4), y3d(4), Anti
         End If
      
      Else ' at least one point inside end cap1 OK
         For k = 1 To 3
            xext1 = xd(k) + scosA
            yext1 = yd(k) + ssinA
            xext2 = xd(k + 1) + scosA
            yext2 = yd(k + 1) + ssinA
            tk(k) = PtInRegion(hRgn, xext1, yext1)
            tk(k + 4) = PtInRegion(hRgn, xext2, yext2)
            If PtInRegion(hRgn, xext1, yext1) = 0 Then   ' Outside region 1
            If PtInRegion(hRgn, xext2, yext2) = 0 Then   ' Outside region 1
               DrawPoly picDRAW, Cul, xd(k), yd(k), xd(k + 1), yd(k + 1), x3d(k + 1), y3d(k + 1), x3d(k), y3d(k), Anti
            End If
            End If
         Next k
         xext1 = xd(4) + scosA
         yext1 = yd(4) + ssinA
         xext2 = xd(1) + scosA
         yext2 = yd(1) + ssinA
         tk(4) = PtInRegion(hRgn, xext1, yext1)
         tk(8) = PtInRegion(hRgn, xext2, yext2)
         If PtInRegion(hRgn, xext1, yext1) = 0 Then   ' Outside region 1
         If PtInRegion(hRgn, xext2, yext2) = 0 Then   ' Outside region 1
            DrawPoly picDRAW, Cul, xd(4), yd(4), xd(1), yd(1), x3d(1), y3d(1), x3d(4), y3d(4), Anti
         End If
         End If
      End If
   
   Case [Square], [Paragram45]
      For k = 1 To 3
         xext1 = xd(k) + scosA
         yext1 = yd(k) + ssinA
         xext2 = xd(k + 1) + scosA
         yext2 = yd(k + 1) + ssinA
         If PtInRegion(hRgn, xext1, yext1) = 0 Then   ' Outside region 1
         If PtInRegion(hRgn, xext2, yext2) = 0 Then   ' Outside region 1
            DrawPoly picDRAW, Cul, xd(k), yd(k), xd(k + 1), yd(k + 1), x3d(k + 1), y3d(k + 1), x3d(k), y3d(k), Anti
         End If
         End If
      Next k
      xext1 = xd(4) + scosA
      yext1 = yd(4) + ssinA
      xext2 = xd(1) + scosA
      yext2 = yd(1) + ssinA
      If PtInRegion(hRgn, xext1, yext1) = 0 Then   ' Outside region 1
      If PtInRegion(hRgn, xext2, yext2) = 0 Then   ' Outside region 1
         DrawPoly picDRAW, Cul, xd(4), yd(4), xd(1), yd(1), x3d(1), y3d(1), x3d(4), y3d(4), Anti
      End If
      End If
   End Select
   
   For k = 1 To PointCount3D - 1  ' DRAW
      ZDrawAALine xd(k), yd(k), xd(k + 1), yd(k + 1), Cul, 0, Anti
   Next k
   ZDrawAALine xd(k), yd(k), xd(1), yd(1), Cul, 0, Anti
      
   DeleteObject hRgn ' This region always
   If hRgn2 <> 0 Then DeleteObject hRgn2   ' This region only for Triangle, Wedges & Frustrum
   
   DISPLAY Form1, picDRAW
   aRound = svRound
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub DrawTriLines(picDRAW As PictureBox, Cul As Long, _
      x1 As Long, y1 As Long, x2 As Long, y2 As Long, x3 As Long, y3 As Long, Optional Anti As Long = 0)
   ' For 3D Shape Triangle
   ZDrawAALine x1, y1, x2, y2, Cul, 0, Anti
   ZDrawAALine x1, y1, x3, y3, Cul, 0, Anti
End Sub

Public Sub DrawPoly(picDRAW As PictureBox, Cul As Long, _
      x1 As Long, y1 As Long, x2 As Long, y2 As Long, x3 As Long, y3 As Long, x4 As Long, y4 As Long, Optional Anti As Long = 0)
'For 3D Shapes
   ZDrawAALine x1, y1, x2, y2, Cul, 0, Anti
   ZDrawAALine x2, y2, x3, y3, Cul, 0, Anti
   ZDrawAALine x3, y3, x4, y4, Cul, 0, Anti
   ZDrawAALine x4, y4, x1, y1, Cul, 0, Anti
End Sub

Public Sub CompleteRadials(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim k As Long
Dim zk As Single
Dim zkup As Single
Dim xpr As Single, ypr As Single
Dim Anti As Long
' For Roses
Dim zzrad As Single, zzradup As Single
   If NClicks <> 3 Then Exit Sub
   aMouseDown = False
   Screen.MousePointer = vbHourglass
   
   Anti = 0
   If Not aAA Then Anti = 1
   
   Select Case RadialTool
   Case [Spokes], [CSpokes]
      
      ' XClear
      For k = 2 To PointCount
         picDRAW.Line (xsto(1), ysto(1))-(xsto(k), ysto(k)), XorCul
         'picDRAW.PSet (xsto(k), ysto(k)), XorCul
      Next k
      Select Case RadialTool
      Case [CSpokes]
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul ''''''''
      End Select
      
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      
      If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
         DrawDot xsto(1), ysto(1), Cul
         SCOPY = True
         PixelSize = pxsvORG
         picDRAW.DrawWidth = PixelSize
         Exit Sub
      End If
      
      For k = 2 To PointCount
         ZDrawAALine xsto(1), ysto(1), xsto(k), ysto(k), Cul, 0.5, Anti
         If PixelSize = 1 Then
            ZDrawAALine xsto(1), ysto(1), xsto(k), ysto(k), Cul, 0.5, 1
         End If
      Next k
      If RadialTool = [CSpokes] Then
         zradx = zrad + PixelSize / 2
         zrady = zrad + PixelSize / 2
         ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, 1
         End If
      End If
      
   Case [Stars], [CStars]
         ' XClear
         For k = 2 To PointCount - 1
            picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
         Next k
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
         If RadialTool = [CStars] Then
            picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul   ''''''''
         End If
      
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      
      For k = 2 To PointCount - 1 ' Draw
         ZDrawAALine xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), Cul, 0.1, Anti
         If PixelSize = 1 Then
            ZDrawAALine xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), Cul, 0.1, 1
         End If
      Next k
      ZDrawAALine xsto(PointCount), ysto(PointCount), xsto(2), ysto(2), Cul, 0.1, Anti
      If PixelSize = 1 Then
         ZDrawAALine xsto(PointCount), ysto(PointCount), xsto(2), ysto(2), Cul, 0.1, 1
      End If
      
      If RadialTool = [CStars] Then
         zradx = zrad + PixelSize / 2
         zrady = zrad + PixelSize / 2
         ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, 1
         End If
      End If
   
   Case [Circles]
      'XClear
      For k = 2 To PointCount
         picDRAW.Circle (xsto(k), ysto(k)), zrad2, XorCul
      Next k
      
      picDRAW.DrawMode = vbCopyPen
      ' DRAW
      For k = 2 To PointCount   ' Draw
         zradx = zrad2
         zrady = zrad2
         ZAACIRLIPSE xsto(k), ysto(k), zradx, zrady, Cul, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xsto(k), ysto(k), zradx, zrady, Cul, 0, 2 * pi#, 1
         End If
         'picDRAW.Circle (xsto(k), ysto(k)), zrad2, Cul
      Next k
   
   Case [Polygons], [CPolygons]
      ' XClear
      For k = 2 To PointCount - 1
         picDRAW.Line (xsto(k), ysto(k))-(xsto(k + 1), ysto(k + 1)), XorCul
      Next k
      picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), XorCul
      If RadialTool = [CPolygons] Then
         picDRAW.Circle (xsto(1), ysto(1)), zrad, XorCul ''''''''
      End If
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      
      For k = 2 To PointCount - 1 ' Draw
         ZDrawAALine xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), Cul, 0, Anti
         If PixelSize = 1 Then
            ZDrawAALine xsto(k), ysto(k), xsto(k + 1), ysto(k + 1), Cul, 0, 1
         End If
      Next k
      ZDrawAALine xsto(PointCount), ysto(PointCount), xsto(2), ysto(2), Cul, 0, Anti
      If PixelSize = 1 Then
         ZDrawAALine xsto(PointCount), ysto(PointCount), xsto(2), ysto(2), Cul, 0, 1
      End If
         If PixelSize > 3 Then
         If Not aRound Then
            BevelAdjust
            ' More !
            Select Case pxsv
            Case Is < 7
               PixelSize = PixelSize + 1
            End Select
            
            For k = 2 To PointCount - 2 ' Draw
               BevelJoint picDRAW, xsto(k), ysto(k), _
                                   xsto(k + 1), ysto(k + 1), _
                                   xsto(k + 2), ysto(k + 2), Cul, 0, Anti
            Next k
            BevelJoint picDRAW, xsto(PointCount), ysto(PointCount), _
                                xsto(2), ysto(2), _
                                xsto(3), ysto(3), Cul, 0, Anti


            BevelJoint picDRAW, xsto(PointCount - 1), ysto(PointCount - 1), _
                                xsto(PointCount), ysto(PointCount), _
                                xsto(2), ysto(2), Cul, 0, Anti
            PixelSize = pxsv
         End If
         End If
      
      If RadialTool = [CPolygons] Then
         zradx = zrad + PixelSize / 2
         zrady = zrad + PixelSize / 2
         ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xsto(1), ysto(1), zradx, zrady, Cul, 0, 2 * pi#, 1
         End If
      End If
   
   Case [Teeth]
      ' XClear
      DrawTeeth picDRAW, XorCul, 0, Anti
      LastTooth picDRAW, k, XorCul
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      If Not aAA Then
         DrawTeeth picDRAW, Cul, 1, Anti
         LastTooth picDRAW, k, Cul, 1, Anti
      Else   ' AA
         DrawTeeth picDRAW, Cul, 1, Anti
         LastTooth picDRAW, k, Cul, 1, Anti
      End If
   Case [OddRoses], [EvenRoses]
      ' XClear
      For zk = 0 To 360 Step 1
         zangle = zk * 180 / pi#
         zR = zrad * Cos(NumPoints * zangle)
         px = xsto(1) + zR * Cos(zangle)
         py = ysto(1) + zR * Sin(zangle)
         picDRAW.PSet (px, py), XorCul
      Next zk
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      zkup = pi#
      If RadialTool = [EvenRoses] Then zkup = 2 * pi#
      zangle = 0
      zR = zrad
      zzrad = zrad
      zzradup = zrad - PixelSize
      xpr = xsto(1) + zR
      ypr = ysto(1)
      pxsv = PixelSize
      PixelSize = 1
      picDRAW.DrawWidth = 2
      For zzrad = zrad To zzradup Step -1
         For zangle = 0 To zkup Step 0.02
            zR = zzrad * Cos(NumPoints * zangle)
            px = xsto(1) + zR * Cos(zangle)
            py = ysto(1) + zR * Sin(zangle)
            ZDrawAALine xpr, ypr, px, py, Cul, 0#, Anti    ' 0.25
            xpr = px
            ypr = py
         Next zangle
      Next zzrad
      PixelSize = pxsv
      picDRAW.DrawWidth = PixelSize
      
   Case [Hypocycloid]   ' RadialTool
      ' XClear
      For zk = 0 To 360 Step 1
         zangle = zk * 180 / pi#
         px = xsto(1) + zrad * Cos(zangle) ^ NumPoints
         py = ysto(1) + zrad * Sin(zangle) ^ NumPoints
         picDRAW.PSet (px, py), XorCul
      Next zk
      ' Draw
      picDRAW.DrawMode = vbCopyPen
      ' Draw
      svRound = aRound
      aRound = False
      xpr = xsto(1) + zrad
      ypr = ysto(1)
      For zangle = 0 To 2 * pi# Step 0.1
         px = xsto(1) + zrad * Cos(zangle) ^ NumPoints
         py = ysto(1) + zrad * Sin(zangle) ^ NumPoints
         If zangle > 0 And zangle <= 2 * pi# - 0.1 Then
            ZDrawAALine xpr, ypr, px, py, Cul, 0, Anti
            ' for HV lines line is draw up & down
               ' or left & right over same line so AA
            ' only on one side
        End If
            xpr = px
            ypr = py
      Next zangle
      aRound = svRound
   
   Case [Spiral]

      Screen.MousePointer = vbHourglass
      ' Clear
      DrawSpiral picDRAW
   
      PixelSize = pxsvORG
      picDRAW.DrawWidth = PixelSize

      picDRAW.DrawMode = vbCopyPen
      ' Draw
      xpr = xsto(1)
      ypr = ysto(1)
      zangle = NumPoints / 10
      aRound = True
      For zk = 0 To 90 Step zangle ' Draw 16 patterns
         zangle = zk * 180 / pi#
         zR = zrad * (zangle) / (5000)
         px = xsto(1) + zR * Cos(zangle)
         py = ysto(1) + zR * Sin(zangle)
         ZDrawAALine xpr, ypr, px, py, Cul, 0, Anti
         xpr = px
         ypr = py
      Next zk
      Screen.MousePointer = vbDefault
   
   End Select
   DISPLAY Form1, picDRAW
   SCOPY = True
Screen.MousePointer = vbDefault
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub RadialPreview(picPreview As PictureBox)
' Max 50 x 50
'Public pxsv As Long   ' Saving PixelSize
   
Dim k As Long
Dim pt As Long   ' PointCount
Dim px() As Long, py() As Long
Dim dx As Single, DY As Single
Dim zangle2 As Single
Dim zR As Single
Dim zk As Single
Dim pvx As Single, pvy As Single
Dim zrad2 As Single
' For Roses
Dim zzrad As Single, zzradup As Single

   pxsv = PixelSize
   pt = NumPoints + 1
   ReDim px(1 To pt), py(1 To pt)
   px(1) = 24
   py(1) = 24
   px(2) = 40
   py(2) = 40
   dx = px(2) - px(1)
   DY = py(2) - py(1)
   If pt > 1 Then zangle = 2 * pi# / (pt - 1)
   zrad = Sqr(dx * dx + DY * DY)
   
   picPreview.Picture = LoadPicture
   
   Select Case RadialTool
   Case [Spokes], [CSpokes]
      zrad = Sqr(dx * dx + DY * DY)
      zangle2 = zATan2(DY, dx)
      For k = 2 To pt   ' Draw
         px(k) = px(1) + zrad * Cos(zangle2)
         py(k) = py(1) + zrad * Sin(zangle2)
         picPreview.Line (px(1), py(1))-(px(k), py(k)), 0
         zangle2 = zangle2 + zangle
      Next k
      If RadialTool = [CSpokes] Then
         picPreview.Circle (px(1), py(1)), zrad, 0
      End If
   Case [Stars], [CStars]
      pt = 2 * NumPoints + 1
      zangle = 2 * pi# / (pt - 1)
      ReDim px(1 To pt), py(1 To pt)
      px(1) = 24
      py(1) = 24
      px(2) = 40
      py(2) = 40
      dx = (px(2) - px(1))
      DY = (py(2) - py(1))
      zrad = Sqr(dx * dx + DY * DY)
      zrad2 = zrad / 5
      zangle2 = zATan2(DY, dx)
      xsto(2) = px(1) + zrad * Cos(zangle2)
      ysto(2) = py(1) + zrad * Sin(zangle2)
      For k = 3 To pt ' Draw
         zangle2 = zangle2 + zangle
         If (k Mod 2) = 1 Then
            px(k) = px(1) + zrad2 * Cos(zangle2)
            py(k) = py(1) + zrad2 * Sin(zangle2)
         Else
            px(k) = px(1) + zrad * Cos(zangle2)
            py(k) = py(1) + zrad * Sin(zangle2)
         End If
         picPreview.Line (px(k - 1), py(k - 1))-(px(k), py(k)), 0
      Next k
      picPreview.Line (px(pt), py(pt))-(px(2), py(2)), 0
      If RadialTool = [CStars] Then
         picPreview.Circle (px(1), py(1)), zrad, 0
      End If
   Case [Circles]
      pt = 2 * NumPoints + 1
      ReDim px(1 To pt), py(1 To pt)
      px(1) = 24
      py(1) = 24
      px(2) = 36
      py(2) = 36
      dx = px(2) - px(1)
      DY = py(2) - py(1)
      zrad = Sqr(dx * dx + DY * DY)
      zrad2 = zrad / 5
      zangle2 = zATan2(DY, dx)
      For k = 2 To pt   ' Draw
         px(k) = px(1) + zrad * Cos(zangle2)
         py(k) = py(1) + zrad * Sin(zangle2)
         picPreview.Circle (px(k), py(k)), zrad2, 0
         zangle2 = zangle2 + zangle
      Next k
   Case [Polygons], [CPolygons]
      pt = NumPoints + 1
      ReDim px(1 To pt), py(1 To pt)
      If pt > 1 Then zangle = 2 * pi# / (pt - 1)
      px(1) = 24
      py(1) = 24
      px(2) = 36
      py(2) = 36
      dx = px(2) - px(1)
      DY = py(2) - py(1)
      zrad = Sqr(dx * dx + DY * DY)
      zangle2 = zATan2(DY, dx)
      px(2) = px(1) + zrad * Cos(zangle2)
      py(2) = py(1) + zrad * Sin(zangle2)
      For k = 3 To pt   ' Draw
         zangle2 = zangle2 + zangle
         px(k) = px(1) + zrad * Cos(zangle2)
         py(k) = py(1) + zrad * Sin(zangle2)
         picPreview.Line (px(k - 1), py(k - 1))-(px(k), py(k)), 0
      Next k
      picPreview.Line (px(pt), py(pt))-(px(2), py(2)), 0
      If RadialTool = [CPolygons] Then
         picPreview.Circle (px(1), py(1)), zrad, 0
      End If
   Case [Teeth]
      pt = 2 * NumPoints + 1
      ReDim px(1 To pt), py(1 To pt)
      If pt > 1 Then zangle = 2 * pi# / (pt - 1)
      px(1) = 24
      py(1) = 24
      px(2) = 36
      py(2) = 36
      dx = px(2) - px(1)
      DY = py(2) - py(1)
      zrad = Sqr(dx * dx + DY * DY)
      zangle2 = zATan2(DY, dx)
      px(2) = px(1) + zrad * Cos(zangle2)
      py(2) = py(1) + zrad * Sin(zangle2)
      For k = 3 To pt
         zangle2 = zangle2 + zangle
         px(k) = px(1) + zrad * Cos(zangle2)
         py(k) = py(1) + zrad * Sin(zangle2)
         If (k And 1) = 0 Then
            GetParallelCoords zrad / 4, px(k - 1), py(k - 1), px(k), py(k), _
                  ixa, iya, ixb, iyb
         
            picPreview.Line (px(k - 1), py(k - 1))-(ixa, iya), 0
            picPreview.Line (ixa, iya)-(ixb, iyb), XorCul
            picPreview.Line (ixb, iyb)-(px(k), py(k)), XorCul
         Else
            picPreview.Line (px(k - 1), py(k - 1))-(px(k), py(k)), 0
         End If
      Next k
      'LastTooth picPreview, k, 0
      If (k And 1) = 0 Then
            GetParallelCoords zrad / 4, px(pt), py(pt), px(2), py(2), _
                  ixa, iya, ixb, iyb
         picPreview.Line (px(pt), py(pt))-(ixa, iya), 0
         picPreview.Line (ixa, iya)-(ixb, iyb), 0
         picPreview.Line (ixb, iyb)-(px(2), py(2)), 0
      Else
         picPreview.Line (px(pt), py(pt))-(px(2), py(2)), 0
      End If
   Case [OddRoses], [EvenRoses]
      pt = NumPoints + 1
      ReDim px(1 To pt), py(1 To pt)
      If pt > 1 Then zangle = 2 * pi# / (pt - 1)
      px(1) = 24
      py(1) = 24
      px(2) = 36
      py(2) = 36
      dx = px(2) - px(1)
      DY = py(2) - py(1)
      zrad = Sqr(dx * dx + DY * DY)
      zzradup = zrad - 4
      For zzrad = zrad To zzradup Step -1
      For k = 0 To 360 Step 1 ' Draw
         zangle = k * 180 / pi#
         zR = zzrad * Cos(NumPoints * zangle)
         pvx = px(1) + zR * Cos(zangle)
         pvy = py(1) + zR * Sin(zangle)
         picPreview.PSet (pvx, pvy), 0
      Next k
      Next zzrad
   Case [Hypocycloid]
      pt = NumPoints + 1
      ReDim px(1 To pt), py(1 To pt)
      If pt > 1 Then zangle = 2 * pi# / (pt - 1)
      px(1) = 24
      py(1) = 24
      px(2) = 36
      py(2) = 36
      dx = px(2) - px(1)
      DY = py(2) - py(1)
      zrad = Sqr(dx * dx + DY * DY)
      For k = 0 To 360 Step 1 ' Clear
         zangle = k * 180 / pi#
         pvx = px(1) + zrad * Cos(zangle) ^ NumPoints
         pvy = py(1) + zrad * Sin(zangle) ^ NumPoints
         picPreview.PSet (pvx, pvy), 0
      Next k
   Case [Spiral]
      pvx = px(1)
      pvy = py(1)
      zangle = NumPoints / 10
      For zk = 0 To 90 Step zangle ' Draw 16 patterns
         zangle2 = zk * 180 / pi#
         zR = zrad * (zangle2) / (5000)
         dx = px(1) + zR * Cos(zangle2)
         DY = py(1) + zR * Sin(zangle2)
         picPreview.Line (pvx, pvy)-(dx, DY), 0
         pvx = dx
         pvy = DY
      Next zk
   
   End Select
   PixelSize = pxsv
End Sub

' Not used
'Private Sub Delayer(TDelay As Long)
'Dim t As Long
'   t = timeGetTime
'   Do: DoEvents
'   Loop Until (timeGetTime() - t >= TDelay Or (timeGetTime() - t) < 0)
'End Sub


Public Sub CompleteCircuits(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim Anti As Long

   If NClicks <> 3 Then Exit Sub
   aMouseDown = False
   ' XClear
   If CircuitTool = [Inductor] Then
      DrawCircuitComp picDRAW, x, y, XorCul, 0
   Else
      DrawCircuitComp picDRAW, xsto(PointCount), ysto(PointCount), XorCul, 0
   End If
   
   ' Draw
   picDRAW.DrawMode = vbCopyPen
      
   Anti = 0
   If Not aAA Then Anti = 1
      
   If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
      DrawDot xsto(1), ysto(1), Cul
      SCOPY = True
      PixelSize = pxsvORG
      picDRAW.DrawWidth = PixelSize
      Exit Sub
   End If
   
   If CircuitTool = [Inductor] Then
      DrawCircuitComp picDRAW, x, y, Cul, 1, Anti
   Else
      DrawCircuitComp picDRAW, xsto(PointCount), ysto(PointCount), Cul, 1, Anti ' Draw
   End If
   DISPLAY Form1, picDRAW
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub

Public Sub CompleteWaves(picDRAW As PictureBox, x As Single, y As Single, Cul As Long)
Dim Exten As Single
Dim Anti As Long
   If NClicks <> 3 Then Exit Sub
   
   Anti = 0
   If Not aAA Then Anti = 1
   
   Screen.MousePointer = vbHourglass
   Select Case WaveTool
   Case [SineWave], [Rectified]
      aMouseDown = False
      DrawSineWave picDRAW, XorCul, 0  ' Clear
      picDRAW.DrawMode = vbCopyPen
      
      If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
         DrawDot xsto(1), ysto(1), Cul
         SCOPY = True
         PixelSize = pxsvORG
         picDRAW.DrawWidth = PixelSize
         Screen.MousePointer = vbDefault
         Exit Sub
      End If
      
      DrawSineWave picDRAW, Cul, 1, Anti  ' Draw
      If aAA And aRound Then
         If PixelSize > 3 Then
         pxsv = PixelSize
         If PixelSize < 12 Then
         PixelSize = PixelSize + 1
         End If
         DrawDot xsto(1), ysto(1), Cul
         DrawDot xsto(2), ysto(2), Cul
         End If
      End If
      
   Case [SquareWave]
      aMouseDown = False
      DrawSquareWave picDRAW, XorCul   ' Clear
      picDRAW.DrawMode = vbCopyPen
      If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
         DrawDot xsto(1), ysto(1), Cul
         SCOPY = True
         PixelSize = pxsvORG
         picDRAW.DrawWidth = PixelSize
         Screen.MousePointer = vbDefault
         Exit Sub
      End If
      DrawSquareWave picDRAW, Cul, 1, Anti  ' Draw
   
   Case [Sawtooth1]
      aMouseDown = False
      DrawSawtooth1 picDRAW, XorCul   ' Clear
      picDRAW.DrawMode = vbCopyPen
      If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
         DrawDot xsto(1), ysto(1), Cul
         SCOPY = True
         PixelSize = pxsvORG
         picDRAW.DrawWidth = PixelSize
         Screen.MousePointer = vbDefault
         Exit Sub
      End If
      DrawSawtooth1 picDRAW, Cul, 1, Anti  ' Draw
   
   Case [Sawtooth2]
      aMouseDown = False
      DrawSawtooth2 picDRAW, XorCul   ' Clear
      picDRAW.DrawMode = vbCopyPen
      If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
         DrawDot xsto(1), ysto(1), Cul
         SCOPY = True
         PixelSize = pxsvORG
         picDRAW.DrawWidth = PixelSize
         Screen.MousePointer = vbDefault
         Exit Sub
      End If
      DrawSawtooth2 picDRAW, Cul, 1, Anti  ' Draw
      
   Case [Sawtooth3]
      aMouseDown = False
      DrawSawtooth3 picDRAW, XorCul   ' Clear
      picDRAW.DrawMode = vbCopyPen
      If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
         DrawDot xsto(1), ysto(1), Cul
         SCOPY = True
         PixelSize = pxsvORG
         picDRAW.DrawWidth = PixelSize
         Screen.MousePointer = vbDefault
         Exit Sub
      End If
      DrawSawtooth3 picDRAW, Cul, 1, Anti  ' Draw
      
   Case [CircWaves]
      aMouseDown = False
      DrawCircWaves picDRAW, XorCul   ' Clear
      picDRAW.DrawMode = vbCopyPen
      If xsto(1) = xsto(2) And ysto(1) = ysto(2) Then
         DrawDot xsto(1), ysto(1), Cul
         SCOPY = True
         PixelSize = pxsvORG
         picDRAW.DrawWidth = PixelSize
         Screen.MousePointer = vbDefault
         Exit Sub
      End If
      DrawCircWaves picDRAW, Cul, 1, Anti  ' Draw
   End Select
   
   If aJoinEndPoints Then
      Exten = 0
      If Not aRound Then Exten = 0.5
      pxsv = PixelSize
      If WaveTool = [CircWaves] Then
         If PixelSize >= 6 Then
            PixelSize = PixelSize - 2
         ElseIf PixelSize > 1 Then
            PixelSize = PixelSize - 1
         End If
      End If
      
      ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), Cul, Exten, Anti
   End If
   DISPLAY Form1, picDRAW
   Screen.MousePointer = vbDefault
   SCOPY = True
PixelSize = pxsvORG
picDRAW.DrawWidth = PixelSize
End Sub


'#### Drawing Extras ####################################################################

Public Sub AdjustShadeBoxPoints(x1 As Long, y1 As Long, x2 As Long, y2 As Long)
   Dim xys As Long
   If x1 > x2 Then
      xys = x1
      x1 = x2
      x2 = xys
   End If
   If y1 > y2 Then
      xys = y1
      y1 = y2
      y2 = xys
   End If

End Sub

Public Sub AdjustBoxPoints(x1 As Long, y1 As Long, x2 As Long, y2 As Long)
' Make x1,y1 TopLeft - x2,y2 bottom right
   Dim xys As Long
   If x1 > x2 Then
      If y1 > y2 Then
         xys = x1
         x1 = x2
         x2 = xys
         xys = y1
         y1 = y2
         y2 = xys
      Else  ' y1 < y2
         xys = x1
         x1 = x2
         x2 = xys
      End If
   
   Else   ' x1 < x2
      If y1 > y2 Then
         xys = y1
         y1 = y2
         y2 = xys
      Else   ' y1 < y2
         
      End If
   End If

End Sub

Public Sub GetColorSteps()
' All Public
' For Shading
   zRL = (LeftColor And &HFF&)
   zGL = (LeftColor And &HFF00&) / &H100&
   zBL = (LeftColor And &HFF0000) / &H10000
   zRR = (RightColor And &HFF&)
   zGR = (RightColor And &HFF00&) / &H100&
   zBR = (RightColor And &HFF0000) / &H10000
   zRIncr = (zRR - zRL) / NSteps
   zGIncr = (zGR - zGL) / NSteps
   zBIncr = (zBR - zBL) / NSteps
End Sub

Public Sub GetNextColors()
' All Public
' For shading
   zRL = zRL + zRIncr
   If zRL < 0 Then zRL = 0 Else If zRL > 255 Then zRL = 255
   zGL = zGL + zGIncr
   If zGL < 0 Then zGL = 0 Else If zGL > 255 Then zGL = 255
   zBL = zBL + zBIncr
   If zBL < 0 Then zBL = 0 Else If zBL > 255 Then zBL = 255
End Sub

Public Sub GetColorStepsReversed()
' All Public
' For Shading
   zRL = (RightColor And &HFF&)
   zGL = (RightColor And &HFF00&) / &H100&
   zBL = (RightColor And &HFF0000) / &H10000
   zRR = (LeftColor And &HFF&)
   zGR = (LeftColor And &HFF00&) / &H100&
   zBR = (LeftColor And &HFF0000) / &H10000
   zRIncr = (zRR - zRL) / NSteps
   zGIncr = (zGR - zGL) / NSteps
   zBIncr = (zBR - zBL) / NSteps
End Sub


Public Sub GetColorStepsReversed2()
' All Public
' For Shading
   zRL2 = (RightColor And &HFF&)
   zGL2 = (RightColor And &HFF00&) / &H100&
   zBL2 = (RightColor And &HFF0000) / &H10000
   zRR2 = (LeftColor And &HFF&)
   zGR2 = (LeftColor And &HFF00&) / &H100&
   zBR2 = (LeftColor And &HFF0000) / &H10000
   zRIncr2 = (zRR2 - zRL2) / NSteps
   zGIncr2 = (zGR2 - zGL2) / NSteps
   zBIncr2 = (zBR2 - zBL2) / NSteps
End Sub

Public Sub GetNextColors2()
' All Public
' For shading
   zRL2 = zRL2 + zRIncr2
   If zRL2 < 0 Then zRL2 = 0 Else If zRL2 > 255 Then zRL2 = 255
   zGL2 = zGL2 + zGIncr2
   If zGL2 < 0 Then zGL2 = 0 Else If zGL2 > 255 Then zGL2 = 255
   zBL2 = zBL2 + zBIncr2
   If zBL2 < 0 Then zBL2 = 0 Else If zBL2 > 255 Then zBL2 = 255
End Sub

Private Sub GetBoxMoveCoords()

' Called from Start,DrawMove and Complete Boxes.
' All Public

'   ' Outside drawn box
'   If xsto(2) > xsto(1) Then
'      If ysto(2) > ysto(1) Then   ' BR
'            ixa = xsto(1) - zspace
'            iya = ysto(1) - zspace
'            ixb = xsto(2) + zspace
'            iyb = ysto(2) + zspace
'      Else  ' ysto(2)< ysto(1)    ' TR
'            ixa = xsto(1) - zspace
'            iya = ysto(1) + zspace
'            ixb = xsto(2) + zspace
'            iyb = ysto(2) - zspace
'      End If
'   Else   ' xsto(2)<xsto(1)
'      If ysto(2) > ysto(1) Then   ' BL
'            ixa = xsto(1) + zspace
'            iya = ysto(1) - zspace
'            ixb = xsto(2) - zspace
'            iyb = ysto(2) + zspace
'      Else  ' ysto(2)< ysto(1)    ' TL
'            ixa = xsto(1) + zspace
'            iya = ysto(1) + zspace
'            ixb = xsto(2) - zspace
'            iyb = ysto(2) - zspace
'      End If
'   End If

   ' Inside drawn box
   If xsto(2) > xsto(1) Then
      If ysto(2) > ysto(1) Then   ' BR
            ixa = xsto(1) + zspace
            iya = ysto(1) + zspace
            ixb = xsto(2) - zspace
            iyb = ysto(2) - zspace
      Else  ' ysto(2)< ysto(1)    ' TR
            ixa = xsto(1) + zspace
            iya = ysto(1) - zspace
            ixb = xsto(2) - zspace
            iyb = ysto(2) + zspace
      End If
   Else   ' xsto(2)<xsto(1)
      If ysto(2) > ysto(1) Then   ' BL
            ixa = xsto(1) - zspace
            iya = ysto(1) + zspace
            ixb = xsto(2) + zspace
            iyb = ysto(2) - zspace
      Else  ' ysto(2)< ysto(1)    ' TL
            ixa = xsto(1) - zspace
            iya = ysto(1) - zspace
            ixb = xsto(2) + zspace
            iyb = ysto(2) + zspace
      End If
   End If

End Sub

Private Sub DrawDottedBox(PIC As PictureBox, Cul As Long)
   DrawDottedLine xsto(1), ysto(1), xsto(2), ysto(1), Cul
   DrawDottedLine xsto(2), ysto(1), xsto(2), ysto(2), Cul
   DrawDottedLine xsto(2), ysto(2), xsto(1), ysto(2), Cul
   DrawDottedLine xsto(1), ysto(2), xsto(1), ysto(1), Cul
End Sub

Private Sub DrawDottedLine(ByVal x1 As Long, ByVal y1 As Long, ByVal x2 As Long, ByVal y2 As Long, Cul As Long)
' Draw from x1,y1 to x2,y2  Integers since drawn using xsto(),ysto()
Dim spx As Long, spy As Long
Dim k As Long
Dim Dis As Single
Dim n As Long

Dim L As Long, p As Long

   LineLength = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
   L = LineLength
   p = PixelSize
   zangle = zATan2(y2 - y1, x2 - x1)
   cosA = Cos(zangle)
   sinA = Sin(zangle)
   If PixelSize <= 3 Then
      Dis = 2 * PixelSize
   Else
      Dis = 1.5 * PixelSize   ' approx 1.5
   End If
   NDots = LineLength / Dis   ' /
   n = 1
   If Tool = ALines Then n = 0
   For k = 0 To NDots - n
      spx = x1 + k * Dis * cosA
      spy = y1 + k * Dis * sinA
      DrawDot spx, spy, Cul
   Next k
End Sub

Public Sub DrawDot(ByVal px As Long, ByVal py As Long, Cul As Long, _
Optional PixMul As Single = 1)

Dim ixss As Single, iyss As Single  ' For square dots
Dim Length As Long, Offset As Long
Dim j As Long, k As Long, iy As Long
Dim ax1 As Long, ay1 As Long
Dim ax2 As Long, ay2 As Long
Dim Red As Byte, Green As Byte, Blue As Byte

  If Not aRound Then   ' SQUARE
      ixss = px - PixelSize \ 2  ' NB Integer division necessary !!
      iyss = py - PixelSize \ 2
      
      If Not aAA Then
         If PixelSize = 1 Then
            BresBoxFill ixss, iyss, ixss, iyss, Cul, 1, 1
         ElseIf PixelSize = 2 Then
            BresBoxFill ixss, iyss, ixss + 1, iyss, Cul, PixelSize, 1
         Else
            BresBoxFill ixss, iyss + 1, ixss + PixelSize - 1, iyss + PixelSize - 1, Cul, 1, 1
         End If
      Else ' AA & SQUARE
         If PixelSize = 1 Then
            BresBoxFill ixss, iyss, ixss, iyss, Cul, 1, 1
         ElseIf PixelSize = 2 Then
            BresBoxFill ixss, iyss + 1, ixss + PixelSize - 1, iyss + PixelSize - 1, Cul, 1.1
            BresBox ixss, iyss, ixss + PixelSize - 1, iyss + PixelSize - 1, Cul, 1  ' Does AA
         Else
            BresBoxFill ixss, iyss + 1, ixss + PixelSize - 1, iyss + PixelSize - 2, Cul, 1, 1
            BresBox ixss, iyss, ixss + PixelSize - 1, iyss + PixelSize - 1, Cul, 1  ' Does AA
         End If
      End If
         
   Else   ' ROUND
      
      k = (PixelSize \ 2)  ' To centre
      iy = py
      
      LngToRGB Cul, Red, Green, Blue
      
      For j = 1 To PixelSize
         Length = RA(PixelSize, j) \ 10       ' NB Integer division necessary !!
         Offset = RA(PixelSize, j) - (10 * Length)
         ax1 = px + (Offset) - k
         ay1 = iy - k + 1
         ax2 = px + (Offset) + (Length) - k - 1
         ay2 = iy - k + 1
         
         BresLine ax1, ay1, ax2, ay2, Red, Green, Blue, 0, 1
         If aAA Then ' AA to each side, not above and below
            AntiAlias picDATA(), CSng(ax1) - 0.5, ay1, Cul, 0, 0, 1 ' R
            AntiAlias picDATA(), CSng(ax2) + 0.5, ay2, Cul, 0, 0, 1 ' R
         End If
         
         iy = iy + 1
      Next j
   End If
End Sub

Private Sub DrawAngBox(picDRAW As PictureBox, DCul As Long, Optional Action As Long = 0)
', Optional x3 As Single, Optional y3 As Single)
   'BoxTool
   'Case [BAngBox], [AABAngBox]

' In with x/ysto(1) - x/ysto(2)  devel  x/ysto(3) - x/ysto(3)
'                 3
'                / \
'               /   \
'              1     4
'               \   /
'           zLen \ /  zD=zLen\2
'                 2
' Take box depth= zLen\2
Dim zD As Single, zLen As Single
Dim zdx As Single, zdy As Single
Dim HFrac As Single
Dim Exten As Single
      
' For [BAngBoxFilled] & [BAngBoxGradient]
Dim x1 As Single, y1 As Single
Dim x2 As Single, y2 As Single
Dim x3 As Single, y3 As Single
Dim x4 As Single, y4 As Single
Dim D As Single
Dim klo As Single, kup As Single, kstep As Single
Dim k As Single
      
   HFrac = NumPoints / 10  ' = H/W  ' 0.1 to 1 (square) H = HFRAC*W
   zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
   zLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
   zD = zLen * HFrac
   zdx = zD * Sin(zangle)
   zdy = zD * Cos(zangle)
   xsto(3) = xsto(1) + zdx
   ysto(3) = ysto(1) - zdy
   xsto(4) = xsto(2) + zdx
   ysto(4) = ysto(2) - zdy
   
' Action = 0 Clear or Draw, =1 AA
   
   If Action = 0 Then   'Clear
      picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
      picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), DCul
      picDRAW.Line (xsto(4), ysto(4))-(xsto(3), ysto(3)), DCul
      picDRAW.Line (xsto(3), ysto(3))-(xsto(1), ysto(1)), DCul
   Else  ' Action = 1 Final
      If (Not aAA And aRound) Or (Not aAA And Not aRound And PixelSize <= 3) Then
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten, 1
         ZDrawAALine xsto(2), ysto(2), xsto(4), ysto(4), DCul, Exten, 1
         ZDrawAALine xsto(4), ysto(4), xsto(3), ysto(3), DCul, Exten, 1
         ZDrawAALine xsto(3), ysto(3), xsto(1), ysto(1), DCul, Exten, 1
      
      Else  ' (AA & Round or Square)  OR Not AA & Square & Pixelsize > 3
         Exten = 0
         If Not aRound Then Exten = 0.5
         picDRAW.DrawWidth = 1
         picDRAW.DrawMode = vbCopyPen
         
         If BoxTool = [BAngBoxFilled] Then
               pxsv = PixelSize
               If PixelSize < 3 Then PixelSize = 3
         End If
         
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten
         ZDrawAALine xsto(2), ysto(2), xsto(4), ysto(4), DCul, Exten
         ZDrawAALine xsto(4), ysto(4), xsto(3), ysto(3), DCul, Exten
         ZDrawAALine xsto(3), ysto(3), xsto(1), ysto(1), DCul, Exten
         'DISPLAY Form1, picDRAW
         
         If BoxTool = [BAngBoxFilled] Then
            D = 0.75 'PixelSize
            x1 = xsto(1) + D * Cos(zangle)
            x1 = x1 + D * Sin(zangle)
            y1 = ysto(1) + D * Sin(zangle)
            y1 = y1 - D * Cos(zangle)
            
            x2 = xsto(2) - D * Cos(zangle)
            x2 = x2 + D * Sin(zangle)
            y2 = ysto(2) - D * Sin(zangle)
            y2 = y2 - D * Cos(zangle)
            zLen = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
            zD = zLen * HFrac - D / 2
            zdx = zD * Sin(zangle)
            zdy = zD * Cos(zangle)
            x3 = x1 + zdx
            y3 = y1 - zdy
            x4 = x2 + zdx
            y4 = y2 - zdy
            klo = 0: kup = zD: kstep = 1
            If zangle < 0 Then
               kup = 0: klo = zD: kstep = -1
            End If
            picDRAW.DrawWidth = 2
            'NSteps = kup - klo
            'GetColorSteps
            For k = klo To kup Step kstep
               GetParallelCoords CSng(k), x1, y1, x2, y2, _
                  ixa, iya, ixb, iyb ', cosA, sinA
               ZDrawAALine ixa, iya, ixb, iyb, DCul, Exten, 1
               'picDRAW.Line (ixa, iya)-(ixb, iyb), DCul
            Next k
            'DISPLAY Form1, picDRAW
            PixelSize = pxsv
         End If   ' If BoxTool = [BAngBoxFilled] Then
      
      End If
   End If   'If Action = 0 Then   'Clear
End Sub

Private Sub DrawFilledAngBox(picDRAW As PictureBox, DCul As Long)
' DRAW
' In with x/ysto(1) - x/ysto(2)  devel  x/ysto(3) - x/ysto(3)
'                 3
'                / \
'               /   \
'              1     4
'               \   /
'           zLen \ /  zD=zLen\2
'                 2
' Take box depth= zLen\2
Dim zD As Single, zLen As Single
Dim zdx As Single, zdy As Single
Dim k As Single
Dim HFrac As Single
Dim klo As Single, kup As Single, kstep As Single

' For AA rounded corners & PixelSize >= 4
Dim ixa1 As Single, iya1 As Single
Dim ixb1 As Single, iyb1 As Single
Dim ixa2 As Single, iya2 As Single
Dim ixb2 As Single, iyb2 As Single
Dim ixa3 As Single, iya3 As Single
Dim ixb3 As Single, iyb3 As Single
      
Dim xc1 As Single, yc1 As Single
Dim xc2 As Single, yc2 As Single
Dim xc3 As Single, yc3 As Single
Dim xc4 As Single, yc4 As Single

Dim ix1 As Single, iy1 As Single
Dim ix2 As Single, iy2 As Single
Dim ix3 As Single, iy3 As Single
Dim ix4 As Single, iy4 As Single

Dim Rad As Single
'Dim L As Single
Dim Exten As Single
      
   
   HFrac = NumPoints / 10
   zLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
   zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
   zD = zLen * HFrac
   klo = 0: kup = zD - 1: kstep = 1
   If zangle < 0 Then
      kup = 0: klo = zD: kstep = -1
   End If
   
If Not aAA Then
      If Not aRound Then
         picDRAW.DrawWidth = 2
      Else
         If PixelSize = 1 Then
            picDRAW.DrawWidth = 2
         Else
            picDRAW.DrawWidth = PixelSize
         End If
      End If
      PixelSize = 2
      If BoxTool = [BAngBoxFilled] Then
         For k = klo To kup Step kstep
            GetParallelCoords CSng(k), xsto(1), ysto(1), xsto(2), ysto(2), _
               ixa, iya, ixb, iyb ', cosA, sinA
            ZDrawAALine ixa, iya, ixb, iyb, DCul, Exten, 1
            'picDRAW.Line (ixa, iya)-(ixb, iyb), DCul
         Next k
      
      ElseIf BoxTool = [BAngBoxGradient] Then
         NSteps = Abs(kup - klo)
         If NSteps = 0 Then NSteps = 1
         GetColorSteps
         For k = klo To kup Step kstep
            GetParallelCoords CSng(k), xsto(1), ysto(1), xsto(2), ysto(2), _
               ixa, iya, ixb, iyb ', cosA, sinA
            ZDrawAALine ixa, iya, ixb, iyb, RGB(zRL, zGL, zBL), Exten, 1
            'picDRAW.Line (ixa, iya)-(ixb, iyb), RGB(zRL, zGL, zBL) '   DCul
            GetNextColors
         Next k
      End If
   
Else  ' AA
      pxsv = PixelSize
      If PixelSize = 1 Then PixelSize = 2
      If kstep = 0 Then
         PixelSize = pxsv
         Exit Sub
      End If
      
      ixa = xsto(1)
      iya = ysto(1)
      ixb = xsto(2)
      iyb = ysto(2)
      
      If BoxTool = [BAngBoxFilled] Then   ' AA
         PixelSize = 2
         If aRound Then
            ' Speeds up
            aAA = False
            For k = klo To kup Step kstep
               ZDrawAALine ixa, iya, ixb, iyb, DCul, Exten, 0
               'picDRAW.Line (ixa, iya)-(ixb, iyb), DCul
               GetParallelCoords CSng(k), xsto(1), ysto(1), xsto(2), ysto(2), _
                  ixa, iya, ixb, iyb  ', cosA, sinA
            Next k
            aAA = True
            DrawAngBox picDRAW, DCul, 1  ' Uses xysto(1/2) etc, Action = 1 to DRAW
         Else  ' Square
            ' Speeds up
            aAA = False
            For k = klo To kup Step kstep 'NSteps - 1
               picDRAW.DrawWidth = 2
               ZDrawAALine ixa, iya, ixb, iyb, DCul, Exten, 0
               'picDRAW.Line (ixa, iya)-(ixb, iyb), DCul
               GetParallelCoords CSng(k), xsto(1), ysto(1), xsto(2), ysto(2), _
                  ixa, iya, ixb, iyb  ', cosA, sinA
            Next k
            aAA = True
            PixelSize = 1
            DrawAngBox picDRAW, DCul, 1
         End If
         
      
      ElseIf BoxTool = [BAngBoxGradient] Then   ' AA
      
         
         ' Set up in DrawAngBox
         zD = zLen * HFrac ' = H
         zdx = zD * Sin(zangle)
         zdy = zD * Cos(zangle)
         xsto(3) = xsto(1) + zdx
         ysto(3) = ysto(1) - zdy
         xsto(4) = xsto(2) + zdx
         ysto(4) = ysto(2) - zdy
      
         If Not aRound Or (aRound And pxsv < 4) Then  ' Square [BAngBoxGradient]
                                                      ' or aRound pxsv < 4
            NSteps = Abs(kup - klo)
            If NSteps = 0 Then NSteps = 1
            GetColorSteps
            For k = klo To kup Step kstep 'NSteps - 1
               picDRAW.DrawWidth = 2
               ZDrawAALine ixa, iya, ixb, iyb, RGB(zRL, zGL, zBL), Exten, 0
               'picDRAW.Line (ixa, iya)-(ixb, iyb), RGB(zRL, zGL, zBL)
               picDRAW.DrawWidth = 1
               PixelSize = 2
               If k = klo Then
                  ZDrawAALine ixa, iya, ixb, iyb, RGB(zRL, zGL, zBL)
               End If
               AntiAlias picDATA(), ixa, iya, RGB(zRL, zGL, zBL), 5, 0, 1
               AntiAlias picDATA(), ixb, iyb, RGB(zRL, zGL, zBL), 5, 0, 1
               
               GetParallelCoords 1, ixa, iya, ixb, iyb, ixa, iya, ixb, iyb ', cosA, sinA
               GetNextColors
            Next k
            ZDrawAALine ixa, iya, ixb, iyb, RGB(zRL, zGL, zBL)
            AntiAlias picDATA(), ixa, iya, RGB(zRL, zGL, zBL), 5, 0, 1
            AntiAlias picDATA(), ixb, iyb, RGB(zRL, zGL, zBL), 5, 0, 1
         
         Else  ' AA Round corners   [BAngBoxGradient]  pxsv > = 4
            
      
            Rad = pxsv / 2
      
            ' 1st round
            xc1 = xsto(1)
            yc1 = ysto(1)
            xc2 = xsto(2)
            yc2 = ysto(2)
            zdx = Rad * Sin(zangle)
            zdy = Rad * Cos(zangle)
            ixa1 = xsto(1) - zdx
            iya1 = ysto(1) + zdy
            ixb1 = xsto(2) - zdx
            iyb1 = ysto(2) + zdy
                  
            ' Wider straight part
            zdx = Rad * Cos(zangle)
            zdy = Rad * Sin(zangle)
            ixa2 = xsto(1) - zdx
            iya2 = ysto(1) - zdy
            ixb2 = xsto(2) + zdx
            iyb2 = ysto(2) + zdy
                  
            ' 2nd round
            xc3 = xsto(3)
            yc3 = ysto(3)
            xc4 = xsto(4)
            yc4 = ysto(4)
            ixa3 = xsto(3)
            iya3 = ysto(3)
            ixb3 = xsto(4)
            iyb3 = ysto(4)
                  
            picDRAW.DrawWidth = 1
            PixelSize = 2 'pxsv
            zD = Sqr((xsto(3) - xsto(1)) ^ 2 + (ysto(3) - ysto(1)) ^ 2)
            NSteps = zD + (2 * Rad)
            If NSteps = 0 Then NSteps = 1
            GetColorSteps

            For k = 1 To Rad + zD + Rad
               
               If k <= Rad Then  ' 1st round
               
                GetLineCircPts ixa1, iya1, ixb1, iyb1, xc1, yc1, Rad, ix1, iy1, ix2, iy2, NegTest
                If NegTest > 0 Then
                  GetLineCircPts ixa1, iya1, ixb1, iyb1, xc2, yc2, Rad, ix3, iy3, ix4, iy4, NegTest
                  If NegTest > 0 Then
                     'picDRAW.DrawWidth = 2
                     'picDRAW.Line (ixa1, iya1)-(ixb1, iyb1), RGB(zRL, zGL, zBL)
                     PixelSize = 2
                     ZDrawAALine ixa1, iya1, ixb1, iyb1, RGB(zRL, zGL, zBL)
                     PixelSize = 2
                     ZDrawAALine ix1, iy1, ix2, iy2, RGB(zRL, zGL, zBL)
                     PixelSize = 2
                     ZDrawAALine ix3, iy3, ix4, iy4, RGB(zRL, zGL, zBL)
                  End If
               End If
               GetParallelCoords 1, ixa1, iya1, ixb1, iyb1, ixa1, iya1, ixb1, iyb1 ', cosA, sinA
      
               ElseIf k > Rad And k <= zD + Rad Then  ' Straight
                  picDRAW.DrawWidth = 2
                  'picDRAW.Line (ixa2, iya2)-(ixb2, iyb2), RGB(zRL, zGL, zBL)
                  PixelSize = 2
                  ZDrawAALine ixa2, iya2, ixb2, iyb2, RGB(zRL, zGL, zBL)
                  AntiAlias picDATA(), ixa2, iya2, RGB(zRL, zGL, zBL), 5, 0, 1
                  AntiAlias picDATA(), ixb2, iyb2, RGB(zRL, zGL, zBL), 5, 0, 1
                  
                  GetParallelCoords 1, ixa2, iya2, ixb2, iyb2, ixa2, iya2, ixb2, iyb2 ', cosA, sinA
                  If k >= zD And k <= (zD + Rad) Then
                     ZDrawAALine ixa2, iya2, ixb2, iyb2, RGB(zRL, zGL, zBL)
                  End If
               Else  ' 2nd round
                  
                  GetLineCircPts ixa3, iya3, ixb3, iyb3, xc3, yc3, Rad, ix1, iy1, ix2, iy2, NegTest
                  If NegTest > 0 Then
                     GetLineCircPts ixa3, iya3, ixb3, iyb3, xc4, yc4, Rad, ix3, iy3, ix4, iy4, NegTest
                     If NegTest > 0 Then
                        PixelSize = 2
                        picDRAW.DrawWidth = 2
                        'picDRAW.Line (ixa3, iya3)-(ixb3, iyb3), RGB(zRL, zGL, zBL)
                        ZDrawAALine ixa3, iya3, ixb3, iyb3, RGB(zRL, zGL, zBL)
                        PixelSize = 2
                        picDRAW.DrawWidth = 2
                        'picDRAW.Line (ix1, iy1)-(ix2, iy2), RGB(zRL, zGL, zBL)
                        ZDrawAALine ix1, iy1, ix2, iy2, RGB(zRL, zGL, zBL)
                        PixelSize = 2
                        picDRAW.DrawWidth = 2
                        'picDRAW.Line (ix3, iy3)-(ix4, iy4), RGB(zRL, zGL, zBL)
                        ZDrawAALine ix3, iy3, ix4, iy4, RGB(zRL, zGL, zBL)
                     End If
                  End If
                  GetParallelCoords 1, ixa3, iya3, ixb3, iyb3, ixa3, iya3, ixb3, iyb3 ', cosA, sinA
                  'picDRAW.Line (ixa3, iya3)-(ixb3, iyb3), RGB(zRL, zGL, zBL)
                  ZDrawAALine ixa3, iya3, ixb3, iyb3, RGB(zRL, zGL, zBL)
            
               End If
               
               GetNextColors
            Next k
         
         End If   ' If Not aRound Then   ' Square
      
      End If   ' ElseIf BoxTool = [BAngBoxGradient] Then
      
      PixelSize = pxsv
End If   ' AA
'DISPLAY Form1, picDRAW
End Sub


Private Sub DrawArrow(picDRAW As PictureBox, x As Long, y As Long, _
      ByVal xp As Single, ByVal yp As Single, DCul As Long, Optional aRRow As Boolean = True, _
      Optional Action As Long = 0, Optional Anti As Long = 0)
      
Dim xd1 As Single, yd1 As Single
Dim xd2 As Single, yd2 As Single
Dim HedLen As Single
Dim sdx As Single, sdy As Single
Dim LLen As Long
Dim Exten As Single

Dim xd As Single, yd As Single ' For box

'   Case [Arrow1] To [Arrow7]
' 1     -------->
' 2     >------->
' 3     <------->
' 4     -------|>

'              4
'      1------5|>2
'              3

'              4
' 5     |>----5|>2
'              3

' x/ysto(1)-x/yd2
'              4
'       |>1-- 5|>2
'              3
' x/ysto(1)-x/yd1

'           9
' 6    1---6|>7----2
'           8
'

' 7.
'       11  13
'         __     3
'        |  |    |\
'   1--10|  |15--5 2
'        |__|    |/
'       12  14   4

' Set at StartArrow
' Private zarrlen As Single
' Private zarrang As Single
' zarrlen = 10 + 3 * PixelSize
' zarrang = 0.333


   xsto(1) = x: ysto(1) = y
   xsto(2) = xp: ysto(2) = yp
   
   sdx = ysto(2) - ysto(1)
   sdy = xsto(2) - xsto(1)
   zangle = zATan2(sdx, sdy)
   LLen = Sqr(sdx * sdx + sdy * sdy)
   Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
   Form1.LabShowLen = Str$(LLen) & " pels"
   
   
   xd1 = zarrlen * Cos(zangle - zarrang)
   yd1 = zarrlen * Sin(zangle - zarrang)
   xd2 = zarrlen * Sin(pi# / 2 - zangle - zarrang)
   yd2 = zarrlen * Cos(pi# / 2 - zangle - zarrang)
   xsto(3) = xsto(2) - xd1
   ysto(3) = ysto(2) - yd1
   xsto(4) = xsto(2) - xd2
   ysto(4) = ysto(2) - yd2
   xsto(5) = (xsto(3) + xsto(4)) / 2
   ysto(5) = (ysto(3) + ysto(4)) / 2
   
   xsto(6) = (xsto(1) + xsto(2)) / 2
   ysto(6) = (ysto(1) + ysto(2)) / 2
   HedLen = Sqr((xsto(2) - xsto(5)) ^ 2 + (ysto(2) - ysto(5)) ^ 2)
   
   xsto(7) = xsto(6) + HedLen * Cos(zangle)
   ysto(7) = ysto(6) + HedLen * Sin(zangle)
   
   xsto(8) = xsto(7) - xd1
   ysto(8) = ysto(7) - yd1
   xsto(9) = xsto(7) - xd2
   ysto(9) = ysto(7) - yd2

   xsto(10) = xsto(6) - 2 * HedLen * Cos(zangle)
   ysto(10) = ysto(6) - 2 * HedLen * Sin(zangle)
   
   xd = 10 * Sin(zangle)
   yd = 10 * Cos(zangle)
   xsto(11) = xsto(10) + xd
   ysto(11) = ysto(10) - yd
   xsto(12) = xsto(10) - xd
   ysto(12) = ysto(10) + yd
   xsto(13) = xsto(6) + 2 * HedLen * Cos(zangle) + xd
   ysto(13) = ysto(6) + 2 * HedLen * Sin(zangle) - yd
   xsto(14) = xsto(6) + 2 * HedLen * Cos(zangle) - xd
   ysto(14) = ysto(6) + 2 * HedLen * Sin(zangle) + yd
   xsto(15) = (xsto(13) + xsto(14)) / 2
   ysto(15) = (ysto(13) + ysto(14)) / 2
      
' Action = 0 Clear or Draw, =1 AA
      
   If Action = 0 Then
      
      Select Case ArrowTool
      Case [Arrow1]  ' ------>
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(3), ysto(3)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), DCul
      Case [Arrow2]  ' >------->
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(3), ysto(3)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), DCul
         picDRAW.Line (xsto(1), ysto(1))-(xsto(1) - xd1, ysto(1) - yd1), DCul
         picDRAW.Line (xsto(1), ysto(1))-(xsto(1) - xd2, ysto(1) - yd2), DCul
      Case [Arrow3]  ' <---------->
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(3), ysto(3)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), DCul
         picDRAW.Line (xsto(1), ysto(1))-(xsto(1) + xd1, ysto(1) + yd1), DCul
         picDRAW.Line (xsto(1), ysto(1))-(xsto(1) + xd2, ysto(1) + yd2), DCul
      Case [Arrow4]  ' ------|>
         picDRAW.Line (xsto(1), ysto(1))-(xsto(5), ysto(5)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(3), ysto(3)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), DCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), DCul
      Case [Arrow5]   '|>------|>
         picDRAW.Line (xsto(1), ysto(1))-(xsto(5), ysto(5)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(3), ysto(3)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), DCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), DCul
         picDRAW.Line (xsto(1), ysto(1))-(xsto(1) - xd1, ysto(1) - yd1), DCul
         picDRAW.Line (xsto(1), ysto(1))-(xsto(1) - xd2, ysto(1) - yd2), DCul
         picDRAW.Line (xsto(1) - xd1, ysto(1) - yd1)-(xsto(1) - xd2, ysto(1) - yd2), DCul
      Case [Arrow6]  ' ----|>----
         picDRAW.Line (xsto(1), ysto(1))-(xsto(6), ysto(6)), DCul   ' ---
         picDRAW.Line (xsto(7), ysto(7))-(xsto(2), ysto(2)), DCul   ' ---  ---
         picDRAW.Line (xsto(7), ysto(7))-(xsto(8), ysto(8)), DCul   ' --- >---
         picDRAW.Line (xsto(7), ysto(7))-(xsto(9), ysto(9)), DCul
         picDRAW.Line (xsto(8), ysto(8))-(xsto(9), ysto(9)), DCul   ' ---|>---
      Case [Arrow7]  ' ---|   |--->
         picDRAW.Line (xsto(1), ysto(1))-(xsto(10), ysto(10)), DCul  ' ---
         picDRAW.Line (xsto(15), ysto(15))-(xsto(2), ysto(2)), DCul    ' ---   ---
         
         If aRRow Then
            picDRAW.Line (xsto(2), ysto(2))-(xsto(3), ysto(3)), DCul    ' ---   --->
            picDRAW.Line (xsto(2), ysto(2))-(xsto(4), ysto(4)), DCul
         End If
         
         picDRAW.Line (xsto(11), ysto(11))-(xsto(12), ysto(12)), DCul   '---|   --->
         picDRAW.Line (xsto(13), ysto(13))-(xsto(14), ysto(14)), DCul   '---|  |--->
         picDRAW.Line (xsto(11), ysto(11))-(xsto(13), ysto(13)), DCul   '    __
         picDRAW.Line (xsto(12), ysto(12))-(xsto(14), ysto(14)), DCul   '---|__|--->
      End Select
  
  Else ' AA or (not aAA and not aRound ie Square)
      
      Exten = 0
      'If Not aRound Then Exten = 0.5
      'BevelJoint picDRAW, x1, y1, x2, y2, x3, y3, LR
      
      Select Case ArrowTool
      Case [Arrow1]  ' ------>
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(3), ysto(3), DCul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(4), ysto(4), DCul, Exten, Anti
      Case [Arrow2]  ' >------->
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(3), ysto(3), DCul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(4), ysto(4), DCul, Exten, Anti
         ZDrawAALine xsto(1), ysto(1), xsto(1) - xd1, ysto(1) - yd1, DCul, Exten, Anti
         ZDrawAALine xsto(1), ysto(1), xsto(1) - xd2, ysto(1) - yd2, DCul, Exten, Anti
      Case [Arrow3]  ' <---------->
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(3), ysto(3), DCul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(4), ysto(4), DCul, Exten, Anti
         ZDrawAALine xsto(1), ysto(1), xsto(1) + xd1, ysto(1) + yd1, DCul, Exten, Anti
         ZDrawAALine xsto(1), ysto(1), xsto(1) + xd2, ysto(1) + yd2, DCul, Exten, Anti
      Case [Arrow4]  ' ------|>
         ZDrawAALine xsto(1), ysto(1), xsto(5), ysto(5), DCul, 0, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(3), ysto(3), DCul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(4), ysto(4), DCul, Exten, Anti
         ZDrawAALine xsto(3), ysto(3), xsto(4), ysto(4), DCul, Exten, Anti
         If Not aRound Then
            BevelAdjust
            BevelJoint picDRAW, xsto(3), ysto(3), xsto(4), ysto(4), xsto(2), ysto(2), DCul, 0.5, Anti
            BevelJoint picDRAW, xsto(4), ysto(4), xsto(3), ysto(3), xsto(2), ysto(2), DCul, 0.5, Anti
            BevelJoint picDRAW, xsto(4), ysto(4), xsto(2), ysto(2), xsto(3), ysto(3), DCul, 0.5, Anti
            PixelSize = pxsv
         End If
      
'     -xid2     4
' 5     |>1----5|>2
'     -xyd1     3

      Case [Arrow5]   '|>------|>
         ZDrawAALine xsto(1), ysto(1), xsto(5), ysto(5), DCul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(3), ysto(3), DCul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(4), ysto(4), DCul, Exten, Anti
         ZDrawAALine xsto(3), ysto(3), xsto(4), ysto(4), DCul, Exten, Anti
         If Not aRound Then
            ZDrawAALine xsto(1), ysto(1), xsto(1) - xd1, ysto(1) - yd1, DCul, 0.4, Anti
            ZDrawAALine xsto(1), ysto(1), xsto(1) - xd2, ysto(1) - yd2, DCul, 0.4, Anti
         Else
            ZDrawAALine xsto(1), ysto(1), xsto(1) - xd1, ysto(1) - yd1, DCul, 0, Anti
            ZDrawAALine xsto(1), ysto(1), xsto(1) - xd2, ysto(1) - yd2, DCul, 0, Anti
         End If
         If Not aRound And PixelSize <= 7 Then
            ZDrawAALine xsto(1) - xd1, ysto(1) - yd1, xsto(1) - xd2, ysto(1) - yd2, DCul, 0, Anti
         Else
            ZDrawAALine xsto(1) - xd1, ysto(1) - yd1, xsto(1) - xd2, ysto(1) - yd2, DCul, 0#, Anti
         End If
         If Not aRound Then
            BevelAdjust
            BevelJoint picDRAW, xsto(3), ysto(3), xsto(4), ysto(4), xsto(2), ysto(2), DCul, 0.5, Anti
            BevelJoint picDRAW, xsto(4), ysto(4), xsto(3), ysto(3), xsto(2), ysto(2), DCul, 0.5, Anti
            BevelJoint picDRAW, xsto(4), ysto(4), xsto(2), ysto(2), xsto(3), ysto(3), DCul, 0.5, Anti
            PixelSize = pxsv
         End If

'           9
' 6    1---6|>7----2
'           8
'
         
      Case [Arrow6]  ' ----|>----
         ZDrawAALine xsto(1), ysto(1), xsto(6), ysto(6), DCul, Exten, Anti  ' ---
         ZDrawAALine xsto(7), ysto(7), xsto(2), ysto(2), DCul, Exten, Anti  ' ---  ---
         If Not aRound Then   ' SQ
            ZDrawAALine xsto(7), ysto(7), xsto(8), ysto(8), DCul, 0.3, Anti         ' --- >---
            ZDrawAALine xsto(7), ysto(7), xsto(9), ysto(9), DCul, 0.3, Anti
         Else
            ZDrawAALine xsto(7), ysto(7), xsto(8), ysto(8), DCul, 0#, Anti         ' --- >---
            ZDrawAALine xsto(7), ysto(7), xsto(9), ysto(9), DCul, 0#, Anti
         End If
         If Not aRound And PixelSize <= 7 Then
            ZDrawAALine xsto(8), ysto(8), xsto(9), ysto(9), DCul, Exten, Anti  ' ---|>---
         Else
            ZDrawAALine xsto(8), ysto(8), xsto(9), ysto(9), DCul, 0, Anti
         End If
      
' 7.
'       11  13
'         __     3
'        |  |    |\
'   1--10|  |15--5 2
'        |__|    |/
'       12  14   4
      
      Case [Arrow7]  ' ---|   |--->
         ZDrawAALine xsto(1), ysto(1), xsto(10), ysto(10), DCul, Exten, Anti   ' ---
         ZDrawAALine xsto(15), ysto(15), xsto(2), ysto(2), DCul, Exten, Anti   ' ---   ---
         
         If aRRow Then
            ZDrawAALine xsto(2), ysto(2), xsto(3), ysto(3), DCul, Exten, Anti   ' ---   --->
            ZDrawAALine xsto(2), ysto(2), xsto(4), ysto(4), DCul, Exten, Anti
         End If
         
         ZDrawAALine xsto(11), ysto(11), xsto(12), ysto(12), DCul, Exten, Anti  '---|   --->
         ZDrawAALine xsto(13), ysto(13), xsto(14), ysto(14), DCul, Exten, Anti  '---|  |--->
         If Not aRound Then  ' SQ
            If PixelSize > 8 Then
               ZDrawAALine xsto(11), ysto(11), xsto(13), ysto(13), DCul, 1, Anti  '    __
               ZDrawAALine xsto(12), ysto(12), xsto(14), ysto(14), DCul, 1, Anti  '---|__|--->
            Else
               ZDrawAALine xsto(11), ysto(11), xsto(13), ysto(13), DCul, 0, Anti  '    __
               ZDrawAALine xsto(12), ysto(12), xsto(14), ysto(14), DCul, 0, Anti  '---|__|--->
            End If
         Else   ' Round
            If PixelSize > 8 Then
               ZDrawAALine xsto(11), ysto(11), xsto(13), ysto(13), DCul, 0, Anti '    __
               ZDrawAALine xsto(12), ysto(12), xsto(14), ysto(14), DCul, 0, Anti '---|__|--->
            Else
               ZDrawAALine xsto(11), ysto(11), xsto(13), ysto(13), DCul, 0, Anti '    __
               ZDrawAALine xsto(12), ysto(12), xsto(14), ysto(14), DCul, 0, Anti '---|__|--->
            End If
         End If
      End Select
   End If
   picDRAW.Refresh
End Sub

Private Sub DrawJunction(picDRAW As PictureBox, x1 As Long, y1 As Long, _
      ByVal x2 As Single, ByVal y2 As Single, DCul As Long, Optional Action As Long = 0, Optional Anti As Long = 0)
' Private ixa As Long, iya As Long, ixb As Long, iyb As Long
'   zJuncLen = 10  ' Default side length
'IN; 2 end points x1,y1 - x2,y2
Dim xh As Single, yh As Single
Dim zL As Single
Dim zdx1 As Single, zdy1 As Single
Dim zdx2 As Single, zdy2 As Single
Dim sdx As Single, sdy As Single
Dim LLen As Long
Dim Exten As Single


'        xsto(), ysto()
'         11|  |12
'           |  |
'           |  |
'          7|  |8
'3----------    ---------4
'
'1----------    ---------2
'          5|   |6
'           |   |
'           |   |
'          9|   |10

' Set junction TODO
'     -------          16-8,  16-4,  8-4
'            |
'             -------
'             -------
'            |
'     -------
' pxsv = PixelSize
  
   xsto(1) = x1: ysto(1) = y1
   xsto(2) = x2: ysto(2) = y2
   
   sdx = ysto(2) - ysto(1)
   sdy = xsto(2) - xsto(1)
   zangle = zATan2(sdx, sdy)
   LLen = Sqr(sdx * sdx + sdy * sdy)
   Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
   Form1.LabShowLen = Str$(LLen) & " pels"
   
   xh = (x1 + x2) / 2
   yh = (y1 + y2) / 2
   zL = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
   If zL > 0 Then ' Cint() allows accurate connecting ?
      zdx1 = CInt((1 - zspace / zL) * (xh - x1))
      zdy1 = CInt((1 - zspace / zL) * (yh - y1))
      zdx2 = CInt((1 + zspace / zL) * (xh - x1))
      zdy2 = CInt((1 + zspace / zL) * (yh - y1))
   End If
   GetParallelCoords zspace, xsto(1), ysto(1), xsto(2), ysto(2), ixa, iya, ixb, iyb
   xsto(3) = ixa: ysto(3) = iya
   xsto(4) = ixb: ysto(4) = iyb
   xsto(5) = xsto(1) + zdx1
   ysto(5) = ysto(1) + zdy1
   xsto(6) = xsto(1) + zdx2
   ysto(6) = ysto(1) + zdy2
   xsto(7) = xsto(3) + zdx1
   ysto(7) = ysto(3) + zdy1
   xsto(8) = xsto(3) + zdx2
   ysto(8) = ysto(3) + zdy2
   GetParallelCoords zspace + zJuncLen, xsto(1), ysto(1), xsto(2), ysto(2), ixa, iya, ixb, iyb
   xsto(11) = ixa + zdx1
   ysto(11) = iya + zdy1
   xsto(12) = ixa + zdx2
   ysto(12) = iya + zdy2
   GetParallelCoords zJuncLen, xsto(2), ysto(2), xsto(1), ysto(1), ixa, iya, ixb, iyb
   xsto(9) = ixb + zdx1
   ysto(9) = iyb + zdy1
   xsto(10) = ixb + zdx2
   ysto(10) = iyb + zdy2
   
' Action = 0 Clear or Draw, =1 AA
   
   If Action = 0 Then
      
      Select Case JunctionTool
      Case [JTee1]
         
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(7), ysto(7)), DCul
         picDRAW.Line (xsto(8), ysto(8))-(xsto(4), ysto(4)), DCul
         picDRAW.Line (xsto(7), ysto(7))-(xsto(11), ysto(11)), DCul
         picDRAW.Line (xsto(8), ysto(8))-(xsto(12), ysto(12)), DCul
      Case [JCross1]
         picDRAW.Line (xsto(1), ysto(1))-(xsto(5), ysto(5)), DCul
         picDRAW.Line (xsto(6), ysto(6))-(xsto(2), ysto(2)), DCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(7), ysto(7)), DCul
         picDRAW.Line (xsto(8), ysto(8))-(xsto(4), ysto(4)), DCul
         picDRAW.Line (xsto(7), ysto(7))-(xsto(11), ysto(11)), DCul
         picDRAW.Line (xsto(8), ysto(8))-(xsto(12), ysto(12)), DCul
         picDRAW.Line (xsto(5), ysto(5))-(xsto(9), ysto(9)), DCul
         picDRAW.Line (xsto(6), ysto(6))-(xsto(10), ysto(10)), DCul
      Case [JCorner1]
         picDRAW.Line (xsto(1), ysto(1))-(xsto(5), ysto(5)), DCul
         picDRAW.Line (xsto(5), ysto(5))-(xsto(9), ysto(9)), DCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(8), ysto(8)), DCul
         picDRAW.Line (xsto(8), ysto(8))-(xsto(10), ysto(10)), DCul
      Case [JLine]
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), DCul
      End Select
   
   Else  ' AA
      Exten = 0
      'If Not aRound Then 'Exten = 0.25
      '   BevelAdjust  ' Adjusts PixelSize
      'End If
      
      Select Case JunctionTool
      Case [JTee1]
'        xsto(), ysto()
'         11|  |12
'           |  |
'           |  |
'          7|  |8
'3----------    ---------4
'
'1-----------------------2
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten, Anti
         ZDrawAALine xsto(3), ysto(3), xsto(7), ysto(7), DCul, Exten, Anti
         ZDrawAALine xsto(8), ysto(8), xsto(4), ysto(4), DCul, Exten, Anti
         If Not aRound Then   ' SQ
            ZDrawAALine xsto(7), ysto(7), xsto(11), ysto(11), DCul, 0.4, Anti
            ZDrawAALine xsto(8), ysto(8), xsto(12), ysto(12), DCul, 0.4, Anti
         Else  ' Round
            ZDrawAALine xsto(7), ysto(7), xsto(11), ysto(11), DCul, 0, Anti
            ZDrawAALine xsto(8), ysto(8), xsto(12), ysto(12), DCul, 0, Anti
         End If
         
      Case [JCross1]
'        xsto(), ysto()
'         11|  |12
'           |  |
'           |  |
'          7|  |8
'3----------    ---------4
'
'1----------    ---------2
'          5|   |6
'           |   |
'           |   |
'          9|   |10
         ZDrawAALine xsto(1), ysto(1), xsto(5), ysto(5), DCul, Exten, Anti
         ZDrawAALine xsto(6), ysto(6), xsto(2), ysto(2), DCul, Exten, Anti
         ZDrawAALine xsto(3), ysto(3), xsto(7), ysto(7), DCul, Exten, Anti
         ZDrawAALine xsto(8), ysto(8), xsto(4), ysto(4), DCul, Exten, Anti
         If Not aRound Then
            Exten = 0.4
         Else
            Exten = 0
         End If
         ZDrawAALine xsto(7), ysto(7), xsto(11), ysto(11), DCul, Exten, Anti
         ZDrawAALine xsto(8), ysto(8), xsto(12), ysto(12), DCul, Exten, Anti
         ZDrawAALine xsto(5), ysto(5), xsto(9), ysto(9), DCul, Exten, Anti
         ZDrawAALine xsto(6), ysto(6), xsto(10), ysto(10), DCul, Exten, Anti
      Case [JCorner1]
'
'3--------------8
'               |
'1----------    |
'          5|   |
'           |   |
'           |   |
'          9|   |10
         If Not aRound Then
            Exten = 0.4
         Else
            Exten = 0
         End If
         ZDrawAALine xsto(1), ysto(1), xsto(5), ysto(5), DCul, Exten, Anti
         ZDrawAALine xsto(3), ysto(3), xsto(8), ysto(8), DCul, Exten, Anti
         Exten = 0
         ZDrawAALine xsto(5), ysto(5), xsto(9), ysto(9), DCul, Exten, Anti
         ZDrawAALine xsto(8), ysto(8), xsto(10), ysto(10), DCul, Exten, Anti
      Case [JLine]
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten, Anti
         ZDrawAALine xsto(3), ysto(3), xsto(4), ysto(4), DCul, Exten, Anti
      End Select
   End If
End Sub

Private Sub DrawCircuitComp(picDRAW As PictureBox, ByVal x As Single, ByVal y As Single, DCul As Long, _
         Optional Action As Long = 0, Optional Anti As Long = 0)
' X,Y end point
Dim k As Long

Dim zLen As Single
Dim zD As Single
Dim zdx As Single, zdy As Single
Dim zddx As Single, zddy As Single

Dim Exten As Single

' For Resistor & Box
Dim ppx As Single, ppy As Single
Dim ppx2 As Single, ppy2 As Single
Dim zx As Single, zy As Single

' For Inductor
Dim ncyc As Long
Dim zsp As Single
Dim drx As Single, dry As Single
Dim xR As Single, yR As Single
Dim xd As Single, yd As Single
Dim zhlen As Single
Dim zpistep As Single
Dim ztdis As Single
Dim zA As Single
Dim px As Single, py As Single
Dim XX As Single, YY As Single
Dim z As Single
Dim zt As Single
Dim x1 As Single, y1 As Single
Dim x2 As Single, y2 As Single
Dim zlo As Single, zhi As Single
Dim n As Long
' For battery
Dim TPixelsize As Long
' For Box
Dim bSize As Single
   
   zangle = zATan2(y - ysto(1), x - xsto(1))
   zLen = Sqr((x - xsto(1)) ^ 2 + (y - ysto(1)) ^ 2)
   If zLen = 0 Then zLen = 0.001
   
' Action = 0 Clear or Draw, =1 AA
   
   If Action = 0 Then 'Or (Not aAA And aRound) Then
   
      Select Case CircuitTool
                           '   /\      /\
      Case [Resistor]      '__/  \    /  \    __  eg 2 cycles
                           '      \  /    \  /
                           '       \/      \/
      
         zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
         zLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
         zD = zLen / (4 * NumPoints)
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         zddx = (zdx + zdy):      zddy = (zdy - zdx)
         px = xsto(1) + zdx:      py = ysto(1) + zdy
         picDRAW.Line (xsto(1), ysto(1))-(px, py), DCul
         For k = 2 To (4 * NumPoints) + 1 Step 4
            ' Draw a cycle
            zx = zddx:     zy = zddy
            ppx = px + zx: ppy = py + zy
            picDRAW.Line (px, py)-(ppx, ppy), DCul
            px = px + 2 * zdx
            py = py + 2 * zdy
            picDRAW.Line (ppx, ppy)-(px, py), DCul
            ' Swap & change sign
            zt = zx
            zx = -zy: zy = zt
            ppx = px + zx: ppy = py + zy
            picDRAW.Line (px, py)-(ppx, ppy), DCul
            px = px + 2 * zdx
            py = py + 2 * zdy
            picDRAW.Line (ppx, ppy)-(px, py), DCul
         Next k
         picDRAW.Line (px, py)-(px + zdx, py + zdy), DCul
      
      Case [Capacitor]
         zD = zLen / 3   ' ---|  |---
                                       ' zD  zD  zD
                                       
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         xsto(2) = xsto(1) + 1.5 * zdx:    ysto(2) = ysto(1) + 1.5 * zdy
         xsto(3) = xsto(2) + zdy \ 2: ysto(3) = ysto(2) - zdx \ 2
         xsto(4) = xsto(2) - zdy \ 2:  ysto(4) = ysto(2) + zdx \ 2
         xsto(7) = xsto(2) + 0.5 * zdx:    ysto(7) = ysto(2) + 0.5 * zdy
         xsto(5) = xsto(7) + zdy \ 2:   ysto(5) = ysto(7) - zdx \ 2
         xsto(6) = xsto(7) - zdy \ 2:   ysto(6) = ysto(7) + zdx \ 2
         xsto(8) = xsto(7) + zdx:     ysto(8) = ysto(7) + zdy
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), DCul
         picDRAW.Line (xsto(5), ysto(5))-(xsto(6), ysto(6)), DCul
         picDRAW.Line (xsto(7), ysto(7))-(xsto(8), ysto(8)), DCul
      
      Case [Inductor]  ' --OOOO--
         zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
         zLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
         If zLen = 0 Then zLen = 0.001
         x1 = xsto(1): y1 = ysto(1)
         x2 = xsto(2): y2 = ysto(2)

         ncyc = NumPoints
         zsp = zLen / (ncyc + 2)
         drx = zsp * Cos(zangle)
         dry = zsp * Sin(zangle)
         xR = x1 + drx         ' Helix radius ref pt
         yR = y1 + dry
         zhlen = (zLen - 2 * zsp)   ' Helix length
         zpistep = 0.3              ' Helix drawing step
         
         'ztdis        zpistep
         '-----  =  -------------   ' Helix travel distance
         'zhlen     2 * ncyc * pi#
         
         ztdis = zpistep * zhlen / (2 * ncyc * pi#)
         
         zA = zhlen / (2 * ncyc)    ' Helix amplitude
         px = xR - zA * Cos(zangle) ' Helix start point
         py = yR - zA * Sin(zangle)
         'picDRAW.Line (xsto(1), ysto(1))-(px, PY), DCul
         xd = px - x1: yd = py - y1
         
         pxsv = PixelSize
         PixelSize = 1
         picDRAW.DrawWidth = 1
         If pxsv > 2 Then picDRAW.DrawWidth = 2
         
         z = 0
         n = 0
         Do
            xR = x1 + drx       ' Helix radius ref pt
            yR = y1 + dry
            px = xR - zA * Cos(zangle) ' Helix start point
            py = yR - zA * Sin(zangle)
            z = 0
            zlo = zangle
            zhi = (zangle + 2 * ncyc * pi# + pi# - zpistep)
         
            For zt = zangle To (zangle + 2 * ncyc * pi# + pi#) Step zpistep
               XX = xR + Cos(zangle) * z - zA * Cos(zt)
               YY = yR + Sin(zangle) * z - zA * Sin(zt)
               picDRAW.Line (px, py)-(XX, YY), DCul
               z = z + ztdis
               px = XX
               py = YY
            Next zt
            x1 = x1 + Sin(zangle)
            y1 = y1 + Cos(zangle)
            n = n + 1
            If n >= pxsv Then Exit Do
         Loop
         PixelSize = pxsv
         picDRAW.DrawWidth = PixelSize
         'picDRAW.Line (XX, YY)-(XX + xd, YY + yd), DCul
      
      Case [Battery]
         zD = zLen / 3  '     |
                        ' ---||---
                        '     |
                        ' zD   zD
                                       
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         xsto(2) = xsto(1) + 1.5 * zdx: ysto(2) = ysto(1) + 1.5 * zdy
         xsto(3) = xsto(2) + zdy \ 2:   ysto(3) = ysto(2) - zdx \ 2
         xsto(4) = xsto(2) - zdy \ 2:   ysto(4) = ysto(2) + zdx \ 2
         xsto(7) = xsto(2) + 0.5 * zdx: ysto(7) = ysto(2) + 0.5 * zdy
         xsto(5) = xsto(7) + zdy:       ysto(5) = ysto(7) - zdx
         xsto(6) = xsto(7) - zdy:       ysto(6) = ysto(7) + zdx
         xsto(8) = xsto(7) + zdx:       ysto(8) = ysto(7) + zdy
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
   
         TPixelsize = 2 * PixelSize
         If TPixelsize > 7 Then TPixelsize = 7
         picDRAW.DrawWidth = TPixelsize
         picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), DCul
         picDRAW.DrawWidth = PixelSize
         picDRAW.Line (xsto(5), ysto(5))-(xsto(6), ysto(6)), DCul
         picDRAW.Line (xsto(7), ysto(7))-(xsto(8), ysto(8)), DCul
         picDRAW.Refresh
                                      
      Case [Switch]       '    /
         zD = zLen / 3    ' ---   ---
                          ' zD zD zD
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         xsto(2) = xsto(1) + zdx: ysto(2) = ysto(1) + zdy
         xsto(4) = xsto(2) + zdx: ysto(4) = ysto(2) + zdy
         xsto(3) = xsto(4) + zdy: ysto(3) = ysto(4) - zdx \ 2
         xsto(5) = xsto(4) + zdx: ysto(5) = ysto(4) + zdy
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
         picDRAW.Line (xsto(2), ysto(2))-(xsto(3), ysto(3)), DCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(5), ysto(5)), DCul
         
      Case [Earth]   '  |
                     '  | |
                     '--| | | .
                     '  | |
                     '  |
         ' In x/ysto(11) or end point
         xsto(11) = x
         ysto(11) = y
         zD = zLen / 5
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         xsto(2) = xsto(1) + zdx:      ysto(2) = ysto(1) + zdy
         xsto(3) = xsto(2) - 2 * zdy:  ysto(3) = ysto(2) + 2 * zdx
         xsto(4) = xsto(2) + 2 * zdy:  ysto(4) = ysto(2) - 2 * zdx
         px = xsto(1) + 2 * zdx:       py = ysto(1) + 2 * zdy
         xsto(5) = px - 1.5 * zdy:     ysto(5) = py + 1.5 * zdx
         xsto(6) = px + 1.5 * zdy:     ysto(6) = py - 1.5 * zdx
         px = xsto(1) + 3 * zdx:       py = ysto(1) + 3 * zdy
         xsto(7) = px - zdy:           ysto(7) = py + zdx
         xsto(8) = px + zdy:           ysto(8) = py - zdx
         px = xsto(1) + 4 * zdx:       py = ysto(1) + 4 * zdy
         xsto(9) = px - 0.5 * zdy:     ysto(9) = py + 0.5 * zdx
         xsto(10) = px + 0.5 * zdy:    ysto(10) = py - 0.5 * zdx
         
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), DCul
         picDRAW.Line (xsto(5), ysto(5))-(xsto(6), ysto(6)), DCul
         picDRAW.Line (xsto(7), ysto(7))-(xsto(8), ysto(8)), DCul
         picDRAW.Line (xsto(9), ysto(9))-(xsto(10), ysto(10)), DCul
         
         picDRAW.Line (px, py)-(xsto(11), ysto(11)), DCul
      
      Case [CBox]
         bSize = 0.25
         zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
         zLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
         zD = zLen / 3
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         zddx = (zdx + zdy):      zddy = (zdy - zdx)
         px = xsto(1) + zdx:      py = ysto(1) + zdy
         picDRAW.Line (xsto(1), ysto(1))-(px, py), DCul
         ' (1)----px,py
         ppx = px + bSize * zdy
         ppy = py - bSize * zdx
         ppx2 = px - bSize * zdy
         ppy2 = py + bSize * zdx
         picDRAW.Line (ppx, ppy)-(ppx2, ppy2), DCul
         '      |
         '(1)---
         '      |
         px = px + zdx
         py = py + zdy
         picDRAW.Line (px + bSize * zdy, py - bSize * zdx)- _
                      (px - bSize * zdy, py + bSize * zdx), DCul
         '      |    |
         '(1)---
         '      |    |
         picDRAW.Line (ppx, ppy)-(px + bSize * zdy, py - bSize * zdx), DCul
         picDRAW.Line (ppx2, ppy2)-(px - bSize * zdy, py + bSize * zdx), DCul
         '      ------
         '      |    |
         '(1)---
         '      |    |
         '      ------
         picDRAW.Line (px, py)-(xsto(2), ysto(2)), DCul
         '      ------
         '      |    |
         '(1)---      ---(2)
         '      |    |
         '      ------
      
      Case [CLine]
         picDRAW.Line (xsto(1), ysto(1))-(x, y), DCul
         picDRAW.PSet (x, y), DCul
      
      Case [CDiode]
         zD = zLen / 3  '    |\
                        ' ---| ----
                        '    |/
                        ' zD   zD
                                       
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         xsto(2) = xsto(1) + 1.5 * zdx: ysto(2) = ysto(1) + 1.5 * zdy
         xsto(3) = xsto(2) + zdy \ 2:   ysto(3) = ysto(2) - zdx \ 2
         xsto(4) = xsto(2) - zdy \ 2:   ysto(4) = ysto(2) + zdx \ 2
         xsto(7) = xsto(2) + 0.5 * zdx: ysto(7) = ysto(2) + 0.5 * zdy
         xsto(5) = xsto(7) + zdy \ 2:     ysto(5) = ysto(7) - zdx \ 2
         xsto(6) = xsto(7) - zdy \ 2:     ysto(6) = ysto(7) + zdx \ 2
         xsto(8) = xsto(7) + zdx:       ysto(8) = ysto(7) + zdy
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(2)), DCul
         
         picDRAW.Line (xsto(3), ysto(3))-(xsto(4), ysto(4)), DCul
         picDRAW.Line (xsto(3), ysto(3))-(xsto(7), ysto(7)), DCul
         picDRAW.Line (xsto(4), ysto(4))-(xsto(7), ysto(7)), DCul
         picDRAW.Line (xsto(5), ysto(5))-(xsto(6), ysto(6)), DCul
         picDRAW.Line (xsto(7), ysto(7))-(xsto(8), ysto(8)), DCul
         picDRAW.Refresh
      End Select
   
   Else   ' DRAW
         
      Screen.MousePointer = vbHourglass
      GetExtension Exten
      
      Select Case CircuitTool
      Case [Resistor]
      
         zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
         zLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
         zD = zLen / (4 * NumPoints)
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         zddx = (zdx + zdy):      zddy = (zdy - zdx)
         px = xsto(1) + zdx:      py = ysto(1) + zdy
         ZDrawAALine xsto(1), ysto(1), px, py, DCul, 0.1, Anti
         For k = 2 To (4 * NumPoints) + 1 Step 4
            ' Draw a cycle
            zx = zddx:     zy = zddy
            ppx = px + zx: ppy = py + zy
            ZDrawAALine px, py, ppx, ppy, DCul, Exten, Anti
            px = px + 2 * zdx
            py = py + 2 * zdy
            ZDrawAALine ppx, ppy, px, py, DCul, Exten, Anti
            ' Swap & change sign
            zt = zx
            zx = -zy: zy = zt
            ppx = px + zx: ppy = py + zy
            ZDrawAALine px, py, ppx, ppy, DCul, Exten, Anti
            px = px + 2 * zdx
            py = py + 2 * zdy
            ZDrawAALine ppx, ppy, px, py, DCul, Exten, Anti
         Next k
         ZDrawAALine px, py, px + zdx, py + zdy, DCul, 0.1, Anti  'Exten
      
      Case [Capacitor]
         zD = zLen / 3   ' ---|  |---
                                       ' zD  zD  zD
                                       
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         xsto(2) = xsto(1) + 1.5 * zdx:    ysto(2) = ysto(1) + 1.5 * zdy
         xsto(3) = xsto(2) + zdy \ 2: ysto(3) = ysto(2) - zdx \ 2
         xsto(4) = xsto(2) - zdy \ 2:  ysto(4) = ysto(2) + zdx \ 2
         xsto(7) = xsto(2) + 0.5 * zdx:    ysto(7) = ysto(2) + 0.5 * zdy
         xsto(5) = xsto(7) + zdy \ 2:   ysto(5) = ysto(7) - zdx \ 2
         xsto(6) = xsto(7) - zdy \ 2:   ysto(6) = ysto(7) + zdx \ 2
         xsto(8) = xsto(7) + zdx:     ysto(8) = ysto(7) + zdy
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten
         ZDrawAALine xsto(3), ysto(3), xsto(4), ysto(4), DCul, Exten
         ZDrawAALine xsto(5), ysto(5), xsto(6), ysto(6), DCul, Exten
         ZDrawAALine xsto(7), ysto(7), xsto(8), ysto(8), DCul, Exten
      
      Case [Inductor]  ' --OOOO--
         zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
         zLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
         If zLen = 0 Then zLen = 0.001
      
         x1 = xsto(1): y1 = ysto(1)
         x2 = xsto(2): y2 = ysto(2)
         
         ncyc = NumPoints
         zsp = zLen / (ncyc + 2)
         drx = zsp * Cos(zangle)
         dry = zsp * Sin(zangle)

         xR = x1 + drx       ' Helix radius ref pt
         yR = y1 + dry
         zhlen = (zLen - 2 * zsp)   ' Helix length
         zpistep = 0.1 '3              ' Helix drawing step
         
         'ztdis        zpistep
         '-----  =  -------------   ' Helix travel distance
         
         'zhlen     2 * ncyc * pi#
         
         ztdis = zpistep * zhlen / (2 * ncyc * pi#)
         zA = zhlen / (2 * ncyc)    ' Helix amplitude
         px = xR - zA * Cos(zangle) ' Helix start point
         py = yR - zA * Sin(zangle)

         xd = px - x1: yd = py - y1  ' for last end line

         'pxsv = PixelSize
         'PixelSize = 1
         'picDRAW.DrawWidth = 1
         'If pxsv > 2 Then picDRAW.DrawWidth = 2
         
         svRound = aRound
         aRound = True

         n = 0
         Do
            xR = x1 + drx       ' Helix radius ref pt
            yR = y1 + dry
            px = xR - zA * Cos(zangle) ' Helix start point
            py = yR - zA * Sin(zangle)
            z = 0
            zlo = zangle
            zhi = (zangle + 2 * ncyc * pi# + pi# - zpistep)
            For zt = zlo To zhi Step zpistep
               XX = xR + Cos(zangle) * z - zA * Cos(zt)
               YY = yR + Sin(zangle) * z - zA * Sin(zt)
               If zt >= zhi Then  'Helix end
                   Exit For
               Else   ' Main Helix
                     ZDrawAALine px, py, XX, YY, DCul, 0#, Anti   'Exten .5
                     If PixelSize = 1 Then
                        ZDrawAALine px, py, XX, YY, DCul, 0#, 1
                     End If
                     'If pxsv <= 3 Then
                     '   picDRAW.Line (px, py)-(XX, YY), DCul
                     'Else  ' pxsv>3
                     '   If aAA = False Then
                     '      picDRAW.DrawWidth = 2
                     '      picDRAW.Line (px, py)-(XX, YY), DCul
                     '   End If
                     'End If
               End If
               z = z + ztdis
               px = XX
               py = YY
            Next zt
            x1 = x1 + Sin(zangle)
            y1 = y1 + Cos(zangle)
            n = n + 1
            If n >= pxsv Then Exit Do
         Loop
         PixelSize = pxsv
         aRound = svRound
         picDRAW.DrawWidth = PixelSize

      
      Case [Battery]
         zD = zLen / 3  '     |
                        ' ---||---
                        '     |
                        ' zD   zD
                                       
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         xsto(2) = xsto(1) + 1.5 * zdx: ysto(2) = ysto(1) + 1.5 * zdy
         xsto(3) = xsto(2) + zdy \ 2:   ysto(3) = ysto(2) - zdx \ 2
         xsto(4) = xsto(2) - zdy \ 2:   ysto(4) = ysto(2) + zdx \ 2
         xsto(7) = xsto(2) + 0.5 * zdx: ysto(7) = ysto(2) + 0.5 * zdy
         xsto(5) = xsto(7) + zdy:       ysto(5) = ysto(7) - zdx
         xsto(6) = xsto(7) - zdy:       ysto(6) = ysto(7) + zdx
         xsto(8) = xsto(7) + zdx:       ysto(8) = ysto(7) + zdy
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten, Anti
   
         TPixelsize = 2 * PixelSize
         If TPixelsize > 7 Then TPixelsize = 7
         picDRAW.DrawWidth = TPixelsize
         ZDrawAALine xsto(3), ysto(3), xsto(4), ysto(4), DCul, Exten, Anti
         picDRAW.DrawWidth = PixelSize
         ZDrawAALine xsto(5), ysto(5), xsto(6), ysto(6), DCul, Exten, Anti
         ZDrawAALine xsto(7), ysto(7), xsto(8), ysto(8), DCul, Exten, Anti
         picDRAW.Refresh
                                      
      Case [Switch]       '    /
         zD = zLen / 3    ' ---   ---
                          ' zD zD zD
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         xsto(2) = xsto(1) + zdx: ysto(2) = ysto(1) + zdy
         xsto(4) = xsto(2) + zdx: ysto(4) = ysto(2) + zdy
         xsto(3) = xsto(4) + zdy: ysto(3) = ysto(4) - zdx \ 2
         xsto(5) = xsto(4) + zdx: ysto(5) = ysto(4) + zdy
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten, Anti
         ZDrawAALine xsto(2), ysto(2), xsto(3), ysto(3), DCul, Exten, Anti
         ZDrawAALine xsto(4), ysto(4), xsto(5), ysto(5), DCul, Exten, Anti
         
      Case [Earth]   '  |
                     '  | |
                     '--| | | .
                     '  | |
                     '  |
         ' In x/ysto(11) or end point
         xsto(11) = x
         ysto(11) = y
         zD = zLen / 5
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         xsto(2) = xsto(1) + zdx:      ysto(2) = ysto(1) + zdy
         xsto(3) = xsto(2) - 2 * zdy:  ysto(3) = ysto(2) + 2 * zdx
         xsto(4) = xsto(2) + 2 * zdy:  ysto(4) = ysto(2) - 2 * zdx
         px = xsto(1) + 2 * zdx:       py = ysto(1) + 2 * zdy
         xsto(5) = px - 1.5 * zdy:     ysto(5) = py + 1.5 * zdx
         xsto(6) = px + 1.5 * zdy:     ysto(6) = py - 1.5 * zdx
         px = xsto(1) + 3 * zdx:       py = ysto(1) + 3 * zdy
         xsto(7) = px - zdy:           ysto(7) = py + zdx
         xsto(8) = px + zdy:           ysto(8) = py - zdx
         px = xsto(1) + 4 * zdx:       py = ysto(1) + 4 * zdy
         xsto(9) = px - 0.5 * zdy:     ysto(9) = py + 0.5 * zdx
         xsto(10) = px + 0.5 * zdy:    ysto(10) = py - 0.5 * zdx
         
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten, Anti
         ZDrawAALine xsto(3), ysto(3), xsto(4), ysto(4), DCul, Exten, Anti
         ZDrawAALine xsto(5), ysto(5), xsto(6), ysto(6), DCul, Exten, Anti
         ZDrawAALine xsto(7), ysto(7), xsto(8), ysto(8), DCul, Exten, Anti
         ZDrawAALine xsto(9), ysto(9), xsto(10), ysto(10), DCul, Exten, Anti
         
         ZDrawAALine px, py, xsto(11), ysto(11), DCul, Exten, Anti
      
      Case [CBox]
         bSize = 0.25
         zangle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
         zLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
         zD = zLen / 3
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         zddx = (zdx + zdy):      zddy = (zdy - zdx)
         px = xsto(1) + zdx:      py = ysto(1) + zdy
         ZDrawAALine xsto(1), ysto(1), px, py, DCul, Exten
         ' (1)----px,py
         ppx = px + bSize * zdy
         ppy = py - bSize * zdx
         ppx2 = px - bSize * zdy
         ppy2 = py + bSize * zdx
         ZDrawAALine ppx, ppy, ppx2, ppy2, DCul, Exten, Anti
         '      |
         '(1)---
         '      |
         px = px + zdx
         py = py + zdy
         ZDrawAALine px + bSize * zdy, py - bSize * zdx, px - bSize * zdy, py + bSize * zdx, DCul, Exten, Anti
         '      |    |
         '(1)---
         '      |    |
         ZDrawAALine ppx, ppy, px + bSize * zdy, py - bSize * zdx, DCul, Exten, Anti
         ZDrawAALine ppx2, ppy2, px - bSize * zdy, py + bSize * zdx, DCul, Exten, Anti
         '      ------
         '      |    |
         '(1)---
         '      |    |
         '      ------
         ZDrawAALine px, py, xsto(2), ysto(2), DCul, Exten, Anti
         '      ------
         '      |    |
         '(1)---      ---(2)
         '      |    |
         '      ------
      
      Case [CLine]
         ZDrawAALine xsto(1), ysto(1), x, y, DCul, Exten, Anti
         'picDRAW.PSet (x, y), DCul
      
      Case [CDiode]
         zD = zLen / 3  '    |\
                        ' ---| ----
                        '    |/
                        ' zD   zD
                                                
         If PixelSize > 7 And Not aRound Then Exten = Exten * 0.75
         
                                       
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         zdx = zD * Cos(zangle):  zdy = zD * Sin(zangle)
         xsto(2) = xsto(1) + 1.5 * zdx: ysto(2) = ysto(1) + 1.5 * zdy
         xsto(3) = xsto(2) + zdy \ 2:   ysto(3) = ysto(2) - zdx \ 2
         xsto(4) = xsto(2) - zdy \ 2:   ysto(4) = ysto(2) + zdx \ 2
         xsto(7) = xsto(2) + 0.5 * zdx: ysto(7) = ysto(2) + 0.5 * zdy
         xsto(5) = xsto(7) + zdy \ 2:     ysto(5) = ysto(7) - zdx \ 2
         xsto(6) = xsto(7) - zdy \ 2:     ysto(6) = ysto(7) + zdx \ 2
         xsto(8) = xsto(7) + zdx:       ysto(8) = ysto(7) + zdy
         ZDrawAALine xsto(1), ysto(1), xsto(2), ysto(2), DCul, Exten, Anti
         
         ZDrawAALine xsto(3), ysto(3), xsto(4), ysto(4), DCul, Exten, Anti
         ZDrawAALine xsto(3), ysto(3), xsto(7), ysto(7), DCul
         ZDrawAALine xsto(4), ysto(4), xsto(7), ysto(7), DCul
         ZDrawAALine xsto(5), ysto(5), xsto(6), ysto(6), DCul, Exten, Anti
         ZDrawAALine xsto(7), ysto(7), xsto(8), ysto(8), DCul, Exten, Anti
         picDRAW.Refresh
      End Select
      
      
   End If
   Screen.MousePointer = vbDefault
End Sub

Private Sub DrawTeeth(picDRAW As PictureBox, Cul As Long, Optional Action As Long = 0, Optional Anti As Long = 0)
' Action = 0 Clear or Draw, =1 AA
Dim k As Long
Dim Exten As Single
   If Action = 0 Then
      For k = 3 To PointCount ' Clear/Draw
         If (k And 1) = 0 Then
            GetParallelCoords zrad / 4, xsto(k - 1), ysto(k - 1), xsto(k), ysto(k), _
                  ixa, iya, ixb, iyb
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(ixa, iya), Cul
            picDRAW.Line (ixa, iya)-(ixb, iyb), Cul
            picDRAW.Line (ixb, iyb)-(xsto(k), ysto(k)), Cul
         Else
            picDRAW.Line (xsto(k - 1), ysto(k - 1))-(xsto(k), ysto(k)), Cul
         End If
      Next k
   Else
      ' Draw
      GetExtension Exten
      Exten = Exten * 0.5
         
      For k = 3 To PointCount ' AA Draw
         If (k And 1) = 0 Then
            GetParallelCoords zrad / 4, xsto(k - 1), ysto(k - 1), xsto(k), ysto(k), _
                  ixa, iya, ixb, iyb
            ZDrawAALine xsto(k - 1), ysto(k - 1), ixa, iya, Cul, Exten, Anti
            ZDrawAALine ixa, iya, ixb, iyb, Cul, 2.5 * Exten, Anti
            ZDrawAALine ixb, iyb, xsto(k), ysto(k), Cul, Exten, Anti
         Else
            ZDrawAALine xsto(k - 1), ysto(k - 1), xsto(k), ysto(k), Cul, Exten, Anti
         End If
      Next k
   End If
End Sub

Private Sub LastTooth(picDRAW As PictureBox, k As Long, Cul As Long, Optional Action As Long = 0, Optional Anti As Long = 0)
' Action = 0 Clear or Draw, =1 AA
Dim Exten As Single
   If Action = 0 Then
      If (k And 1) = 0 Then
            GetParallelCoords zrad / 4, xsto(PointCount), ysto(PointCount), xsto(2), ysto(2), _
                  ixa, iya, ixb, iyb
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(ixa, iya), Cul
         picDRAW.Line (ixa, iya)-(ixb, iyb), Cul
         picDRAW.Line (ixb, iyb)-(xsto(2), ysto(2)), Cul
      Else
         picDRAW.Line (xsto(PointCount), ysto(PointCount))-(xsto(2), ysto(2)), Cul
      End If
   Else
      ' DRAW
      GetExtension Exten
      
      If (k And 1) = 0 Then
            GetParallelCoords zrad / 4, xsto(PointCount), ysto(PointCount), xsto(2), ysto(2), _
                  ixa, iya, ixb, iyb
         ZDrawAALine xsto(PointCount), ysto(PointCount), ixa, iya, Cul, Exten, Anti
         ZDrawAALine ixa, iya, ixb, iyb, Cul, Exten, Anti
         ZDrawAALine ixb, iyb, xsto(2), ysto(2), Cul, Exten, Anti
      Else
         ZDrawAALine xsto(PointCount), ysto(PointCount), xsto(2), ysto(2), Cul, Exten, Anti
      End If
   End If
End Sub

Public Sub MarkRegion(xp As Long, yp As Long)
'MarkRegion picDATAREG(), xDat, yDat  : Only called from Form1.DoShadeFills
' Marks region from xp,yp where color is that at picDATAREG(-,xp,yp)

'Public bMark() As Byte  ' Marked region
'' Region extent
'Public xmax As Long, xmin As Long
'Public ymax As Long, ymin As Long
'Private xpts() As Long, ypts() As Long
'Private bPattern() As Byte


Dim CulP As Long
Dim NotCulP2 As Long
Dim n As Long

   ReDim picDATAREG(0 To 3, 0 To FullW - 1, 0 To FullH - 1)
   picDATAREG() = picDATA()
   ReDim xpts(0 To FullW * FullH - 1), ypts(0 To FullW * FullH - 1)
   
   ReDim bMark(0 To FullW - 1, 0 To FullH - 1)
   ReDim bPattern(0 To 31, 0 To 31)
   FillMemory bPattern(0, 0), 1024, 1
   
   CulP = RGB(picDATAREG(2, xp, yp), picDATAREG(1, xp, yp), picDATAREG(0, xp, yp))
   NotCulP2 = picDATAREG(2, xp, yp) + 1
   If NotCulP2 > 255 Then NotCulP2 = 0
   ' For grading
   xmin = 10000
   xmax = -10000
   ymin = 10000
   ymax = -10000
   n = 0
   Do
      PatternFill xp, yp, CulP, n, NotCulP2 ' 0,0
      xp = xp + 1
      If xp < FullW Then    ' 1,0
         PatternFill xp, yp, CulP, n, NotCulP2
      End If
      yp = yp + 1
      xp = xp - 1
      If yp < FullH Then
      If xp >= 0 Then   ' 0,1
         PatternFill xp, yp, CulP, n, NotCulP2
      End If
      End If
      xp = xp - 1
      yp = yp - 1
      If xp >= 0 Then
      If yp >= 0 Then   ' -1,0
         PatternFill xp, yp, CulP, n, NotCulP2
      End If
      End If
      yp = yp - 1
      xp = xp + 1
      If yp >= 0 Then
      If xp < FullW Then    ' 0,-1
         PatternFill xp, yp, CulP, n, NotCulP2
      End If
      End If
      xp = xpts(n)
      yp = ypts(n)
      n = n - 1
   Loop Until n = 0
End Sub

Private Sub PatternFill(xp As Long, yp As Long, CulP As Long, n As Long, NotCulP2 As Long)
Dim Cul As Long
Dim ix As Long, iy As Long
   Cul = RGB(picDATAREG(2, xp, yp), picDATAREG(1, xp, yp), picDATAREG(0, xp, yp))
   If Cul = CulP Then
      bMark(xp, yp) = 1
      n = n + 1
      xpts(n) = xp
      ypts(n) = yp
      ix = (xp Mod 32)
      iy = (yp Mod 32)
      ' For grading
      If xp < xmin Then xmin = xp
      If xp > xmax Then xmax = xp
      If yp < ymin Then ymin = yp
      If yp > ymax Then ymax = yp
      If bPattern(ix, iy) = 1 Then
         picDATAREG(0, xp, yp) = 0
         picDATAREG(1, xp, yp) = 0
         picDATAREG(2, xp, yp) = CByte(NotCulP2)
      End If
   End If
End Sub

Private Sub DoPolyShading(picDRAW As PictureBox, x As Single, y As Single, Optional Anti As Long = 0)
' Shading frustrum
Dim k As Long
Dim ix1 As Long, iy1 As Long
Dim zL1 As Single, zL2 As Single  ' end lengths
Dim za1 As Single, zA2 As Single  ' end angles
Dim xd1 As Single, yd1 As Single  ' end1 unit dx,dy steps
Dim xd2 As Single, yd2 As Single  ' end2 adjusted dx,dy unit steps
Dim pxi As Long, pyi As Long  ' Previous Intersection point

Dim zn As Single
Dim zSteps As Single
Dim dx1 As Single, dy1 As Single

Dim sik As Single
Dim zD As Single ', zLen As Single
Dim zdx As Single, zdy As Single
Dim CA As Single, SA As Single
Dim rmd As Single  ' End semi rad-dis

'   .....................  L     |
'   ......>>>>...........      zspace
'   .....................  R     |

   
   Screen.MousePointer = vbHourglass
   
   ' Parallel points
   ReDim pxstoL(PointCount) As Long, pystoL(PointCount) As Long
   ReDim pxstoR(PointCount) As Long, pystoR(PointCount) As Long
   
   'zspace = 2 * PixelSize
   zspace = PixelSize
   GetIntersections    ' Generates points either side of x/ysto()
   
   ' Shift coords to last cursor position
   For k = 1 To PointCount
      zD = 0.5 * Sqr((pxstoL(k) - pxstoR(k)) ^ 2 + (pystoL(k) - pystoR(k)) ^ 2)
      za1 = zATan2(pystoL(k) - pystoR(k), pxstoL(k) - pxstoR(k))
      dx1 = zD * Cos(za1)
      dy1 = zD * Sin(za1)
      pxstoL(k) = pxstoL(k) - dx1
      pystoL(k) = pystoL(k) - dy1
      pxstoR(k) = pxstoR(k) - dx1
      pystoR(k) = pystoR(k) - dy1
   Next k
   
   If aAA Then Screen.MousePointer = vbHourglass
   
   For k = 2 To PointCount
      zL1 = Sqr((pxstoL(k - 1) - pxstoR(k - 1)) ^ 2 + (pystoL(k - 1) - pystoR(k - 1)) ^ 2) '+ PixelSize
      If zL1 = 0 Then
         zL1 = 1
      End If
      If zL1 > 1000 Then zL1 = 1000
      zL2 = Sqr((pxstoL(k) - pxstoR(k)) ^ 2 + (pystoL(k) - pystoR(k)) ^ 2) '+ PixelSize
      If zL2 = 0 Then
         zL2 = 1
      End If
      If zL2 > 1000 Then zL2 = 1000
      za1 = zATan2(pystoL(k - 1) - pystoR(k - 1), pxstoL(k - 1) - pxstoR(k - 1))
      zA2 = zATan2(pystoL(k) - pystoR(k), pxstoL(k) - pxstoR(k))
      yd1 = Sin(za1)
      xd1 = Cos(za1)
      yd2 = (Sin(zA2) * zL2 / zL1)
      xd2 = (Cos(zA2) * zL2 / zL1)
'      NSteps = zL1
      NSteps = 50
      zSteps = zL1 / NSteps

      GetColorSteps
      
      If k = 2 Or k = PointCount Then
      If PixelSize >= 3 And aRound Then  ' To vary length at ends
         If k = 2 Then
            zangle = zATan2(pystoL(k) - pystoL(k - 1), pxstoL(k) - pxstoL(k - 1))
         Else
            zangle = zATan2(pystoL(k) - pystoL(k - 1), pxstoL(k) - pxstoL(k - 1))
         End If
         CA = Cos(zangle)
         SA = Sin(zangle)
         sik = 2 * PixelSize / (zL1 - 1)
      End If
      End If
      
      For zn = 0 To zL1 - 1 Step zSteps
         pxi = pxstoL(k - 1) - zn * xd1
         pyi = pystoL(k - 1) - zn * yd1
         ix1 = pxstoL(k) - zn * xd2
         iy1 = pystoL(k) - zn * yd2
          
         If k = 2 Or k = PointCount Then
         If PixelSize >= 3 And aRound Then   ' Vary length at ends for round end
            'zD = Sqr((pxsv) ^ 2 - (pxsv - k * sik) ^ 2)
            ' radius of semi-circle = pxsv &
            ' distance from edge = k*sik
            rmd = (2 * PixelSize - (zn * sik))
            If rmd < 0 Then rmd = 0
            zD = Sqr(zn * sik * rmd)
            zdx = zD * CA
            zdy = zD * SA
            If k = 2 Then
               pxi = pxi - zdx
               pyi = pyi - zdy
               If PointCount = 2 Then
                  ix1 = ix1 + zdx
                  iy1 = iy1 + zdy
               End If
            Else
               ix1 = ix1 + zdx
               iy1 = iy1 + zdy
            End If
         End If
         End If
         
         pxsv = PixelSize
         PixelSize = 2
         ZDrawAALine pxi, pyi, ix1, iy1, RGB(zRL, zGL, zBL), 0.5, Anti
         PixelSize = pxsv
          GetNextColors
      Next zn
   Next k
   Screen.MousePointer = Default
End Sub

Private Sub DrawSelectBoundary()
Dim k As Long
'Public xmax As Long, xmin As Long
'Public ymax As Long, ymin As Long
   xmin = xsto(1)
   xmax = xsto(2)
   If xmax < xmin Then
      xmin = xsto(2)
      xmax = xsto(1)
   End If
   ymin = ysto(1)
   ymax = ysto(2)
   If ymax < ymin Then
      ymin = ysto(2)
      ymax = ysto(1)
   End If
   For k = 0 To 1
      With Form1
         .SHP(k).Left = xmin '- 1
         .SHP(k).TOp = ymin '- 1
         .SHP(k).Width = xmax - xmin + 1
         .SHP(k).Height = ymax - ymin + 1
         .SHP(k).Visible = True
      End With
   Next k
End Sub

Private Sub FillInBricks(picDRAW As PictureBox, Cul As Long)
'Public xmax As Long, xmin As Long  '
'Public ymax As Long, ymin As Long  ' Defines the Selection box
'Public Cul As Long
'Public Cul2 As Long
'Private ixa As Long, iya As Long, ixb As Long, iyb As Long
' NPt, HAR from Brick scrollbars
' NPt = NumPoints
'Dim ixlo As Long, ixhi As Long

Dim iyhi As Long
   
   Cul2 = RightColor
   If StartClick = vbRightButton Then Cul2 = LeftColor

   NPt = NumPoints
   BresBox xmin - 1, ymin, xmax + 1, ymax, Cul, PixelSize
   BresBoxFill xmin - 1, ymin, xmax + 1, ymax, Cul2, 0, 1
   
   'picDRAW.Line (xmin - 1, ymin)-(xmax + 1, ymax), RightColor, BF
   'picDRAW.Line (xmin - 1, ymin)-(xmax + 1, ymax), Cul, B
   
   For iya = ymin To ymax Step 2 * HAR
      ZDrawAALine xmin, iya, xmax, iya, Cul, 0, 1
      'picDRAW.Line (xmin, iya)-(xmax, iya), Cul ' horz
   Next iya
   
   For iya = ymin To ymax Step 4 * HAR
   For ixa = xmin To xmax Step 2 * NPt
      iyhi = iya + 2 * HAR
      If iyhi > ymax Then iyhi = ymax
      ZDrawAALine ixa, iya, ixa, iyhi, Cul, 0, 1
      'picDRAW.Line (ixa, iya)-(ixa, iyhi), Cul  ' vert
   Next ixa
   Next iya
   
   For iya = ymin + 2 * HAR To ymax Step 4 * HAR
   For ixa = xmin + NPt To xmax Step 2 * NPt
      iyhi = iya + 2 * HAR
      If iyhi > ymax Then iyhi = ymax
      ZDrawAALine ixa, iya, ixa, iyhi, Cul, 0, 1
      'picDRAW.Line (ixa, iya)-(ixa, iyhi), Cul  ' vert
   Next ixa
   Next iya
   DISPLAY Form1, picDRAW
   
End Sub

Private Sub DrawPartBox(picDRAW As PictureBox, DCul As Long, Optional Action As Long = 0)
' Action = 0 Clear or Draw, =1 AA

' x/ysto(1)  \ x/ysto(2)
Dim x1 As Long, y1 As Long
Dim x2 As Long, y2 As Long
Dim BW As Long, BH As Long
' Rounded rectangle Arcs
Dim BRad As Long
Dim PS As Long
Dim svAA As Boolean
Dim svRound As Boolean
Dim Exten As Single
Dim Anti As Long
   
   PS = PixelSize
   svRound = aRound
   svAA = aAA

' Action = 0 Clear or Draw, =1 AA
   
   If Action = 0 Then   ' CLEAR
  
      Select Case BoxTool
      Case [BoxTop]
         'picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(1)), DCul   ' Top
         picDRAW.Line (xsto(1), ysto(2))-(xsto(2), ysto(2)), DCul   ' Bottom
         picDRAW.Line (xsto(2), ysto(2))-(xsto(2), ysto(1)), DCul   ' Right
         picDRAW.Line (xsto(1), ysto(2))-(xsto(1), ysto(1)), DCul   ' Left
      
      Case [BoxRight]
         picDRAW.Line (xsto(1), ysto(1))-(xsto(2), ysto(1)), DCul   ' Top
         picDRAW.Line (xsto(1), ysto(2))-(xsto(2), ysto(2)), DCul   ' Bottom
         'picDRAW.Line (xsto(2), ysto(2))-(xsto(2), ysto(1)), DCul   ' Right
         picDRAW.Line (xsto(1), ysto(2))-(xsto(1), ysto(1)), DCul   ' Left
      
      Case BoxRounded
         'Could use RoundRect API but this is more fun!
         x1 = xsto(1): x2 = xsto(2)
         y1 = ysto(1): y2 = ysto(2)
         ' Make x1,y1 TopLeft - x2,y2 bottom right
         AdjustBoxPoints x1, y1, x2, y2
         BW = x2 - x1
         BH = y2 - y1
         If BW > BH Then
            BRad = BH \ 12
         Else
            BRad = BW \ 12
         End If
         ' With adjustments to plug gaps. No rounding for small BW,BH ie BRad = 0
         picDRAW.Line (x1 + BRad, y1)-(x2 - BRad + 1, y1), DCul ' Top
         picDRAW.Line (x1 + BRad, y2)-(x2 - BRad, y2), DCul     ' Bottom
         picDRAW.Line (x1, y2 - BRad)-(x1, y1 + BRad - 1), DCul ' Left
         picDRAW.Line (x2, y2 - BRad)-(x2, y1 + BRad - 1), DCul ' Right
         
         picDRAW.Circle (x1 + BRad, y1 + BRad), BRad, DCul, pi# / 2, pi# + 0.2     ' TL
         picDRAW.Circle (x2 - BRad, y1 + BRad), BRad, DCul, 0, pi# / 2             ' TR
         picDRAW.Circle (x1 + BRad, y2 - BRad), BRad, DCul, pi#, 3 * pi# / 2 + 0.2 ' BL
         picDRAW.Circle (x2 - BRad, y2 - BRad), BRad, DCul, 3 * pi# / 2, 2 * pi#   ' BR
      End Select
      
   Else  ' Action = 1 DRAW
      Anti = 0
      If Not aAA Then Anti = 1
      
      x1 = xsto(1): x2 = xsto(2)
      y1 = ysto(1): y2 = ysto(2)
      ' Make x1,y1 TopLeft - x2,y2 bottom right
      AdjustBoxPoints x1, y1, x2, y2

      ' ZDrawAALine  x1, y1, x2, y2, DCul, DoEnds, Extend, Anti
      ' Extend = 0(Default) no extension, = Frac  extend by Extend * PixelSize
      ' Anti   = 0(default) do AA,                  = 1 no AA (just SetPixel)
         
      GetExtension Exten
      
      
      Select Case BoxTool
      Case [BoxTop]  ' Upright
            If ysto(2) > ysto(1) Then  ' |_|   Bottom line
               ZDrawAALine x1, y2 + 1, x2, y2 + 1, DCul, Exten, Anti
            Else  ' |~|    ' Top line
                ZDrawAALine x1, y1 + 1, x2, y1 + 1, DCul, Exten, Anti
            End If
            If ysto(2) > ysto(1) Then  ' |_|   Bottom line
               ZDrawAALine x2, y2, x2, y1, DCul, Exten, Anti  ' Right
               ZDrawAALine x1, y2, x1, y1, DCul, Exten, Anti  ' Left
            Else  ' |~|    ' Top line
               ZDrawAALine x2, y2 + 1, x2, y1 + 1, DCul, Exten, Anti ' Right
               ZDrawAALine x1, y2 + 1, x1, y1 + 1, DCul, Exten, Anti ' Left
            End If
      Case [BoxRight]   ' Sideways
            If xsto(2) >= xsto(1) Then   ' |=  LEFT
               ZDrawAALine x1, y1 + 1, x1, y2 + 1, DCul, Exten, Anti
            Else  ' =|   RIGHT
               ZDrawAALine x2, y1 + 1, x2, y2 + 1, DCul, Exten, Anti
            End If
            ZDrawAALine x1, y1 + 1, x2, y1 + 1, DCul, Exten, Anti ' Top
            ZDrawAALine x1, y2 + 1, x2, y2 + 1, DCul, Exten, Anti ' Bottom
         
      Case BoxRounded

         ' AA  Square or Round
         aRound = False   ' ie All Square
         
         ' Rounded done here with:-
         ' ZSetArcAngles
         ' ZAACIRLIPSE

         x1 = xsto(1): x2 = xsto(2)
         y1 = ysto(1): y2 = ysto(2)
         AdjustBoxPoints x1, y1, x2, y2
                    
         y1 = y1 + 1
         y2 = y2 + 1
                    
         BW = x2 - x1
         BH = y2 - y1
         If BW > BH Then
            BRad = BH \ 12
         Else
            BRad = BW \ 12
         End If
         If BRad = 0 Then BRad = 1

         If PixelSize > 2 And (PixelSize And 1) = 0 Then
            ZDrawAALine x1 + BRad, y1, x2 - BRad, y1, DCul, 0, Anti      ' Top
         Else
            ZDrawAALine x1 + BRad, y1, x2 - BRad, y1, DCul, 0, Anti    ' Top
         End If
         If PixelSize <= 2 Then
            ZDrawAALine x1 + BRad, y2, x2 - BRad, y2, DCul, 0, Anti     ' Bottom
         Else
            ZDrawAALine x1 + BRad, y2, x2 - BRad, y2, DCul, 0, Anti    ' Bottom
         End If
         
         If PixelSize <= 2 Then
           ZDrawAALine x1, y1 + BRad, x1, y2 - BRad, DCul, 0, Anti      ' Left
         Else
            ZDrawAALine x1, y1 + BRad, x1, y2 - BRad, DCul, 0, Anti    ' Left
         End If
         
         If PixelSize > 2 And (PixelSize And 1) = 0 Then
            ZDrawAALine x2, y1 + BRad, x2, y2 - BRad, DCul, 0, Anti      ' Right
         Else
            ZDrawAALine x2, y1 + BRad, x2, y2 - BRad, DCul, 0, Anti    ' Right
         End If
         
         zradx = BRad
         zrady = BRad
         If PixelSize > 2 Then
            zradx = BRad + 1
            zrady = BRad + 1
         End If
         
         ' Set ArcStep value to avoid gaps  NB << 1 deg!
         Select Case PixelSize
         Case 1
            ArcStep = 2 / zradx
            If zrady > zradx Then ArcStep = 2 / zrady
         Case 2 To 7
            ArcStep = 1 / zradx
            If zrady > zradx Then ArcStep = 1 / zrady
         Case 8 To 12
            ArcStep = 1 / (2 * zradx)
            If zrady > zradx Then ArcStep = 1 / (2 * zrady)
         Case Else
            ArcStep = 0.002
         End Select
         
         ' Actual Pixelsize odd OK
         PixelSize = PS + 1 ' Odd To better match sides
         ArcTool = 0 ' TL
         ZSetArcAngles
         If (PixelSize And 1) = 1 Then  ' Actual Pixelsize even
            zradx = zradx
            zrady = zrady - 1
         End If
         ZAACIRLIPSE x1 + BRad, y1 + BRad, zradx, zrady, DCul, zAS, zAE, Anti
         
         ArcTool = 2 ' TR
         ZSetArcAngles
         If (PixelSize And 1) = 1 Then  ' Actual Pixelsize even
            zradx = zradx - 1
            zrady = zrady
         End If
         ZAACIRLIPSE x2 - BRad, y1 + BRad, zradx, zrady, DCul, zAS, zAE, Anti
         
         ArcTool = 1 ' BL
         ZSetArcAngles
         If (PixelSize And 1) = 1 Then  ' Actual Pixelsize even
            ZAACIRLIPSE x1 + BRad - 1, y2 - BRad + 1, zradx, zrady, DCul, zAS, zAE, Anti
         Else
            ZAACIRLIPSE x1 + BRad, y2 - BRad, zradx, zrady, DCul, zAS, zAE, Anti
         End If
         
         ArcTool = 3 ' BR
         ZSetArcAngles
         If (PixelSize And 1) = 1 Then  ' Actual Pixelsize even
            zradx = zradx
            zrady = zrady + 1
         End If
         ZAACIRLIPSE x2 - BRad, y2 - BRad, zradx, zrady, DCul, zAS, zAE, Anti
         
      End Select
   
   End If   ' If Action = 0 Then
   aAA = svAA
   aRound = svRound
   PixelSize = PS
End Sub


'Private Sub AdjustBucketDot(x1 As Long, y1 As Long, x2 As Long, y2 As Long, TB As Long)
'' Not used
'' TB=0 |_|
'If TB = 0 Then
'   If (PixelSize And 1) = 0 Then   ' Even
'      x1 = x1 - PixelSize / 2 + 1
'      x2 = x2 - PixelSize / 2 + 1
'   Else   ' Odd
'      x1 = x1 - PixelSize / 2 + 0.5
'      x2 = x2 - PixelSize / 2 + 0.5
'   End If
'Else  '|=
'   If (PixelSize And 1) = 0 Then   ' Even
'      y1 = y1 - PixelSize / 2 + 1
'      y2 = y2 - PixelSize / 2 + 1
'   Else   ' Odd
'      y1 = y1 - PixelSize / 2 + 0.5
'      y2 = y2 - PixelSize / 2 + 0.5
'   End If
'
'End If
'End Sub

Private Sub DrawSpiral(picDRAW As PictureBox)
' For Spirals
' Public zrad
Dim zk As Single
Dim xpr As Single, ypr As Single
   xpr = xsto(1)
   ypr = ysto(1)
   zangle = NumPoints / 10
   For zk = 0 To 90 Step zangle ' Draw 16 patterns
      zangle = zk * 180 / pi#
      zR = zrad * (zangle) / (5000)
      px = xsto(1) + zR * Cos(zangle)
      py = ysto(1) + zR * Sin(zangle)
      picDRAW.Line (xpr, ypr)-(px, py), XorCul
      xpr = px
      ypr = py
   Next zk
End Sub

Private Sub DrawSineWave(picDRAW As PictureBox, DCul As Long, Optional Action As Long = 0, Optional Anti As Long = 0)
' Public xsto(1), ysto(1), xsto(2), ysto(2),
' DCul = XorCul or Cul
' Public NCycles, Amp
Dim TheLen As Single
Dim Freq As Single
Dim x As Single, y As Single
Dim XX As Single, YY As Single
Dim px As Single, py As Single
Dim r As Single
Dim Angle As Single
Dim Ang As Single
Dim zA As Single
Dim Exten As Single
Dim DL As Single

   If NCycles = 0 Then NCycles = 1
   TheLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
   If TheLen = 0 Then Exit Sub
   
   
   Angle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
   Freq = Abs(NCycles) / TheLen
   
   px = xsto(1)
   py = ysto(1)
   zA = 2 * pi# * Freq
   
' Action = 0 Clear or Draw, =1 AA
   
   If Action = 0 Then

      picDRAW.PSet (px, py), DCul
      For x = 0 To TheLen Step 0.5
            If WaveTool = [SineWave] Then
               y = -Amp * Sin(zA * x)
            ElseIf WaveTool = [Rectified] Then
               y = -Amp * Abs(Sin(zA * x))  ' Rectifies
            End If
            r = Sqr(x * x + y * y)
            Ang = zATan2(y, x) + Angle
            XX = xsto(1) + r * Cos(Ang)
            YY = ysto(1) + r * Sin(Ang)
            picDRAW.Line (px, py)-(XX, YY), DCul
            px = XX
            py = YY
      Next x
   Else  ' AA or Not AA but Square ends
      svRound = aRound
      aRound = False
      GetExtension Exten
      Exten = Exten * 0.25
      DL = 0
      For x = 1 To TheLen Step 0.5   ' 1 to avoid bump on first point
            If WaveTool = [SineWave] Then
               y = -Amp * Sin(zA * x)
            ElseIf WaveTool = [Rectified] Then
               y = -Amp * Abs(Sin(zA * x))  ' Rectifies
            End If
            r = Sqr(x * x + y * y)
            Ang = zATan2(y, x) + Angle
            XX = xsto(1) + r * Cos(Ang)
            YY = ysto(1) + r * Sin(Ang)
            
            DL = DL + (px - XX) * (px - XX) _
                 + (py - YY) * (py - YY)
            If DL >= 1 Then
               ZDrawAALine px, py, XX, YY, DCul, Exten, Anti
               DL = 0
               px = XX
               py = YY
            End If
            'ZDrawAALine  px, py, XX, YY, DCul, Exten
'            px = XX
'            py = YY
      Next x
      
      'If Not aRound Then
      
      'End If
      
      
      aRound = svRound
   End If
End Sub

Private Sub DrawSquareWave(picDRAW As PictureBox, DCul As Long, Optional Action As Long = 0, Optional Anti As Long = 0)
' Public NCycles, Amp

'  2---3
'  |   |
'  |   |    6
' 1    |   |
'      |   |
'      4---5

Dim x1 As Single, y1 As Single
Dim x2 As Single, y2 As Single
Dim x3 As Single, y3 As Single
Dim x4 As Single, y4 As Single
Dim x5 As Single, y5 As Single
Dim x6 As Single, y6 As Single
Dim TheLen As Single
Dim zD As Single
Dim Angle As Single
Dim cosA As Single
Dim sinA As Single
Dim k As Long
Dim Exten As Single
   If NCycles = 0 Then NCycles = 1
   TheLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
   If TheLen = 0 Then Exit Sub
   Angle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
   cosA = Cos(Angle)
   sinA = Sin(Angle)
   zD = TheLen / (2 * NCycles)
   
   px = xsto(1)   ' 1
   py = ysto(1)
   
   x1 = xsto(1)   ' 1
   y1 = ysto(1)

' Action = 0 Clear or Draw, =1 AA
     
   If Action = 0 Then
   
   
      For k = 1 To NCycles
         x2 = x1 + Amp * sinA
         y2 = y1 - Amp * cosA
         picDRAW.Line (x1, y1)-(x2, y2), DCul   ' 1-2
         x3 = x2 + zD * cosA
         y3 = y2 + zD * sinA
         picDRAW.Line (x2, y2)-(x3, y3), DCul   ' 2-3
         x4 = x3 - 2 * Amp * sinA
         y4 = y3 + 2 * Amp * cosA
         picDRAW.Line (x3, y3)-(x4, y4), DCul   ' 3-4
         x5 = x4 + zD * cosA
         y5 = y4 + zD * sinA
         picDRAW.Line (x4, y4)-(x5, y5), DCul   ' 4-5
         x6 = xsto(1) + 2 * k * zD * cosA
         y6 = ysto(1) + 2 * k * zD * sinA
         picDRAW.Line (x5, y5)-(x6, y6), DCul   ' 5-6
         x1 = x6
         y1 = y6
      Next k
   
   Else  ' DRAW
      GetExtension Exten
      'If Not aRound Then Exten = Exten * 0.5
      For k = 1 To NCycles
         x2 = x1 + Amp * sinA
         y2 = y1 - Amp * cosA
         ZDrawAALine x1, y1, x2, y2, DCul, Exten, Anti ' 1-2
         x3 = x2 + zD * cosA
         y3 = y2 + zD * sinA
         ZDrawAALine x2, y2, x3, y3, DCul, Exten, Anti  ' 2-3
         x4 = x3 - 2 * Amp * sinA
         y4 = y3 + 2 * Amp * cosA
         ZDrawAALine x3, y3, x4, y4, DCul, Exten, Anti  ' 3-4
         x5 = x4 + zD * cosA
         y5 = y4 + zD * sinA
         ZDrawAALine x4, y4, x5, y5, DCul, Exten, Anti  ' 4-5
         x6 = xsto(1) + 2 * k * zD * cosA
         y6 = ysto(1) + 2 * k * zD * sinA
         ZDrawAALine x5, y5, x6, y6, DCul, Exten, Anti  ' 5-6
         x1 = x6
         y1 = y6
      Next k
   End If
End Sub


Private Sub DrawSawtooth1(picDRAW As PictureBox, DCul As Long, Optional Action As Long = 0, Optional Anti As Long = 0)
' Sawtooth 1

'      2
'      /\
'     /  \     4
'    /    \    /
'    1     \  /
'           \/
'           3

' Public NCycles, Amp
Dim x1 As Single, y1 As Single
Dim x2 As Single, y2 As Single
Dim x3 As Single, y3 As Single
Dim x4 As Single, y4 As Single
Dim TheLen As Single
Dim zD As Single
Dim Angle As Single
Dim cosA As Single
Dim sinA As Single
Dim k As Long
Dim Exten As Single
   If NCycles = 0 Then NCycles = 1
   TheLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
   If TheLen = 0 Then Exit Sub
   Angle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
   cosA = Cos(Angle)
   sinA = Sin(Angle)
   zD = TheLen / (2 * NCycles)
   x1 = xsto(1)   ' 1
   y1 = ysto(1)
   
   ' Action = 0 Clear or Draw, =1 AA
   
   If Action = 0 Then
   
      For k = 1 To NCycles
         x2 = x1 + (zD / 2) * cosA + Amp * sinA
         y2 = y1 + (zD / 2) * sinA - Amp * cosA
         picDRAW.Line (x1, y1)-(x2, y2), DCul   ' 1-2
         x3 = x1 + (3 * zD / 2) * cosA - Amp * sinA
         y3 = y1 + (3 * zD / 2) * sinA + Amp * cosA
         
         picDRAW.Line (x2, y2)-(x3, y3), DCul   ' 2-3
         x4 = xsto(1) + 2 * k * zD * cosA
         y4 = ysto(1) + 2 * k * zD * sinA
         picDRAW.Line (x3, y3)-(x4, y4), DCul   ' 3-4
         x1 = x4
         y1 = y4
      Next k
   Else  ' DRAW
      pxsv = PixelSize
      GetExtension Exten
      If Not aRound Then Exten = Exten * 0.5
      For k = 1 To NCycles
         x2 = x1 + (zD / 2) * cosA + Amp * sinA
         y2 = y1 + (zD / 2) * sinA - Amp * cosA
         ZDrawAALine x1, y1, x2, y2, DCul  ' 1-2
         x3 = x1 + (3 * zD / 2) * cosA - Amp * sinA
         y3 = y1 + (3 * zD / 2) * sinA + Amp * cosA
         
         ZDrawAALine x2, y2, x3, y3, DCul, 0, Anti ' 2-3
         
         If PixelSize > 3 Then
            If Not aRound Then
               BevelAdjust
               BevelJoint picDRAW, x1, y1, x2, y2, x3, y3, DCul, 0.4, Anti ' xys Byval
               PixelSize = pxsv
            End If
         End If
         x4 = xsto(1) + 2 * k * zD * cosA
         y4 = ysto(1) + 2 * k * zD * sinA
         ZDrawAALine x3, y3, x4, y4, DCul, 0, Anti    ' 3-4
         
         If PixelSize > 3 Then
            If Not aRound Then
               BevelAdjust
               BevelJoint picDRAW, x2, y2, x3, y3, x4, y4, DCul, 0.4, Anti ' xys Byval
               PixelSize = pxsv
            End If
         End If
         
         x1 = x4
         y1 = y4
      Next k
   End If

'BevelJoint picDRAW, x1, y1, x2, y2, x3, y3, Cul  ' xys Byval


End Sub

Private Sub DrawSawtooth2(picDRAW As PictureBox, DCul As Long, Optional Action As Long = 0, Optional Anti As Long = 0)
' Sawtooth 2

'       2
'      /|
'     / |   4
'    /  |  /
'   1   | /
'       |/
'       3

' Public NCycles, Amp
Dim x1 As Single, y1 As Single
Dim x2 As Single, y2 As Single
Dim x3 As Single, y3 As Single
Dim x4 As Single, y4 As Single

Dim TheLen As Single
Dim zD As Single
Dim Angle As Single
Dim cosA As Single
Dim sinA As Single
Dim k As Long
Dim Exten As Single
   If NCycles = 0 Then NCycles = 1
   TheLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
   If TheLen = 0 Then Exit Sub
   Angle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
   cosA = Cos(Angle)
   sinA = Sin(Angle)
   zD = TheLen / (2 * NCycles)
   x1 = xsto(1)   ' 1
   y1 = ysto(1)
   
' Action = 0 Clear or Draw, =1 AA
   
   If Action = 0 Then
   
      For k = 1 To NCycles
         x2 = x1 + zD * cosA + Amp * sinA
         y2 = y1 + zD * sinA - Amp * cosA
         picDRAW.Line (x1, y1)-(x2, y2), DCul   ' 1-2
         x3 = x1 + zD * cosA - Amp * sinA
         y3 = y1 + zD * sinA + Amp * cosA
         picDRAW.Line (x2, y2)-(x3, y3), DCul   ' 2-3
         x4 = xsto(1) + 2 * k * zD * cosA
         y4 = ysto(1) + 2 * k * zD * sinA
         picDRAW.Line (x3, y3)-(x4, y4), DCul   ' 3-4
         x1 = x4
         y1 = y4
      Next k
   Else  ' DRAW
      pxsv = PixelSize
      GetExtension Exten
      If Not aRound Then Exten = Exten * 0.5 '0.7
      For k = 1 To NCycles
         x2 = x1 + zD * cosA + Amp * sinA
         y2 = y1 + zD * sinA - Amp * cosA
         ZDrawAALine x1, y1, x2, y2, DCul, Exten, Anti ' 1- 2
         x3 = x1 + zD * cosA - Amp * sinA
         y3 = y1 + zD * sinA + Amp * cosA
         ZDrawAALine x2, y2, x3, y3, DCul, Exten, Anti ' 2-3
         If PixelSize > 3 Then
            If Not aRound Then
               BevelAdjust
               Select Case pxsv
               Case 3 To 4, 6 To 7
                  PixelSize = PixelSize
               Case 5
                  PixelSize = PixelSize + 1
               Case 8
                  PixelSize = PixelSize
               Case 10
                  PixelSize = PixelSize * 1.1
               Case 11, 12
                  PixelSize = PixelSize * 1.1 + 1
               Case Else
                  PixelSize = PixelSize
               End Select
               BevelJoint picDRAW, x1, y1, x2, y2, x3, y3, DCul, 0.4, Anti ' xys Byval
               PixelSize = pxsv
            End If
         End If
         x4 = xsto(1) + 2 * k * zD * cosA
         y4 = ysto(1) + 2 * k * zD * sinA
         ZDrawAALine x3, y3, x4, y4, DCul, Exten, Anti ' 3-4
         If PixelSize > 3 Then
            If Not aRound Then
               BevelAdjust
               Select Case pxsv
               Case 3 To 4, 6 To 7
                  PixelSize = PixelSize
               Case 5
                  PixelSize = PixelSize + 1
               Case 8
                  PixelSize = PixelSize
               Case 10
                  PixelSize = PixelSize * 1.1
               Case 11, 12
                  PixelSize = PixelSize * 1.1 + 1
               Case Else
                  PixelSize = PixelSize
               End Select
               BevelJoint picDRAW, x2, y2, x3, y3, x4, y4, DCul, 0.4, Anti ' xys Byval
               PixelSize = pxsv
            End If
         End If
         x1 = x4
         y1 = y4
      Next k
   End If
End Sub

Private Sub DrawSawtooth3(picDRAW As PictureBox, DCul As Long, Optional Action As Long = 0, Optional Anti As Long = 0)
' Sawtooth 3

' 2
' |\
' | \   4
' |  \  |
' 1   \ |
'      \|
'       3
' Public NCycles, Amp

Dim x1 As Single, y1 As Single
Dim x2 As Single, y2 As Single
Dim x3 As Single, y3 As Single
Dim x4 As Single, y4 As Single

Dim TheLen As Single
Dim zD As Single
Dim Angle As Single
Dim cosA As Single
Dim sinA As Single
Dim k As Long
Dim Exten As Single
   If NCycles = 0 Then NCycles = 1
   TheLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
   If TheLen = 0 Then Exit Sub
   Angle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
   cosA = Cos(Angle)
   sinA = Sin(Angle)
   zD = TheLen / (2 * NCycles)
   x1 = xsto(1)   ' 1
   y1 = ysto(1)
   
' Action = 0 Clear or Draw, =1 AA
   
   If Action = 0 Then
   
      For k = 1 To NCycles
         x2 = x1 + Amp * sinA
         y2 = y1 - Amp * cosA
         picDRAW.Line (x1, y1)-(x2, y2), DCul   ' 1-2
         x3 = xsto(1) + 2 * k * zD * cosA
         y3 = ysto(1) + 2 * k * zD * sinA
         x4 = x3 - Amp * sinA
         y4 = y3 + Amp * cosA
         picDRAW.Line (x2, y2)-(x4, y4), DCul   ' 2-3
         picDRAW.Line (x3, y3)-(x4, y4), DCul   ' 3-4
         x1 = x3
         y1 = y3
      Next k
   Else   ' DRAW
      GetExtension Exten
      If Not aRound Then Exten = Exten * 0.7
      
Exten = 0
      
      For k = 1 To NCycles
         x2 = x1 + Amp * sinA
         y2 = y1 - Amp * cosA
         ZDrawAALine x1, y1, x2, y2, DCul, Exten, Anti 'Cul   ' 1-2
         x3 = xsto(1) + 2 * k * zD * cosA
         y3 = ysto(1) + 2 * k * zD * sinA
         x4 = x3 - Amp * sinA
         y4 = y3 + Amp * cosA
         ZDrawAALine x2, y2, x4, y4, DCul, Exten, Anti  ' 2-3
         ZDrawAALine x3, y3, x4, y4, DCul, Exten, Anti  ' 3-4
         If PixelSize > 3 Then
            If Not aRound Then
               BevelAdjust
               Select Case pxsv
               Case 3 To 4, 6 To 7
                  PixelSize = PixelSize
               Case 5
                  PixelSize = PixelSize + 1
               Case 8
                  PixelSize = PixelSize
               Case 10, 11
                  PixelSize = PixelSize * 0.8
               Case Else
                  PixelSize = PixelSize
               End Select
               BevelJoint picDRAW, x1, y1, x2, y2, x4, y4, DCul, 0.4, Anti ' xys Byval
               BevelJoint picDRAW, x2, y2, x4, y4, x3, y3, DCul, 0.4, Anti  ' xys Byval
               PixelSize = pxsv
            End If
         End If
         x1 = x3
         y1 = y3
      Next k
   End If
End Sub

Private Sub DrawCircWaves(picDRAW As PictureBox, DCul As Long, Optional Action As Long = 0, Optional Anti As Long = 0)
' Joined Circles to fit line
'  __  __
' /  \/  \/
' |  ||  |
' \__/\__/\
'

' Public NCycles   ' Amp not used
' Radius set by
Dim TheLen As Single
Dim xc As Single, yc As Single
Dim Radius As Single
Dim Angle As Single
Dim cosA As Single
Dim sinA As Single
Dim k As Long
   If NCycles = 0 Then NCycles = 1
   TheLen = Sqr((xsto(2) - xsto(1)) ^ 2 + (ysto(2) - ysto(1)) ^ 2)
   If TheLen = 0 Then Exit Sub
   Angle = zATan2(ysto(2) - ysto(1), xsto(2) - xsto(1))
   cosA = Cos(Angle)
   sinA = Sin(Angle)
   Radius = TheLen / (2 * NCycles)
   xc = xsto(1) + Radius * cosA
   yc = ysto(1) + Radius * sinA

' Action = 0 Clear or Draw, =1 AA
      
   If Action = 0 Then
      
      For k = 1 To NCycles
         picDRAW.Circle (xc, yc), Radius, DCul  ' 2
         xc = xc + 2 * Radius * cosA
         yc = yc + 2 * Radius * sinA
      Next k
   Else   'DRAW
      For k = 1 To NCycles
         zradx = Radius
         zrady = Radius
         ZAACIRLIPSE xc, yc, zradx, zrady, DCul, 0, 2 * pi#, Anti
         If PixelSize = 1 Then
            ZAACIRLIPSE xc, yc, zradx, zrady, DCul, 0, 2 * pi#, 1
         End If
         xc = xc + 2 * Radius * cosA
         yc = yc + 2 * Radius * sinA
      Next k
   End If
End Sub


Private Sub GetExtension(Exten As Single)
   Exten = 0
   If Not aRound Then
      Select Case PixelSize
      Case 1: Exten = 0.4
      Case 2: Exten = 0.2
      Case 3: Exten = 0.27
      Case 4: Exten = 0.3
      Case 5: Exten = 0.4
      Case 6: Exten = 0.3
      Case 7: Exten = 0.4
      Case 8: Exten = 0.4
      Case 9: Exten = 0.4
      Case 10: Exten = 0.4
      Case 11: Exten = 0.4
      Case 12: Exten = 0.4
      End Select
   End If
End Sub

Private Sub ShowAngleLen(x As Single, y As Single)
Dim sdx As Single, sdy As Single
Dim LLen As Long
   If NClicks = 1 Then
      sdx = x - CSng(xsto(1))
      sdy = y - CSng(ysto(1))
      'zangle = zATan2(sdy, sdx)
      zangle = zAng(sdy, sdx)
      LLen = Sqr(sdx * sdx + sdy * sdy)
   Else
      sdx = ysto(2) - ysto(1)
      sdy = xsto(2) - xsto(1)
      'zangle = zATan2(sdx, sdy)
      zangle = zAng(sdx, sdy)
      LLen = Sqr(sdx * sdx + sdy * sdy)
   End If
   Form1.LabShowAngle = Format(Str$(zangle * 180 / pi#), "##0.0") & " deg"
   Form1.LabShowLen = Str$(LLen) & " pels"
End Sub

