Drawing joined parallel line segments
Could some one please show me the code to do the calculation that make the
open lines intersect one another so that the object makes a closed shape.
What I am trying to do is to make a few joined “parallel” lines that create a closed shape
Using the line method.
To see more of what I am talking about please create this project and see the drawing.
You just need a form and a picture box then paste the code.
The parallel lines will constantly be a set distance apart for each segment of the shape.
The angle of each segment, or rectangle, will vary according to the application I will be using it for.
The ends that do not join another segment will always be drawn at a 90 degree angle from the
base line (the red line) .
I would appreciate any help anyone could give me with any or all of this code.
Thank you……
If you would, please…..
Create a form and with the name Form1
Place a Picture box control on the form and name it Picture1
Copy and paste the code and all the properties will be set with the code.
Dim pi As Double
Dim kX1, kY1, kX2, kY2 As Double 'Red line cords.
Dim kLineX, kLineY, kHypot As Double 'Imaginary triangle sides
Dim kAngle As Double 'The angle in relation of the X and Y cords.
Dim fLine As Double 'The height of the rectangle
Dim eLine As Double 'Length of line a (Imaginary base line along the X plane )
Dim dLine As Double 'Length of line b ( Imaginary base line along the Y plane )
Dim kX3, kY3, kX4, kY4 As Double 'Cords. for the parallel blue line
Dim DrawLeftEndDlosed As Boolean 'If not draw the right end closed
Private Sub Form_Load()
Call SetupFormAndPictureBoxSoUDontHaveTo
'------------------------------------
'Cords. for the first rectangle which will
'Appear on the left side of the screen
'-------------------------------------
'cords. of the red line to be drawn first
'These cords. can be change to draw other rectangles
DrawLeftEndDlosed = True
kX1 = 118 'x1 'These cords. can be used
kY1 = 187 'Y1 'to plug in mouse events
kX2 = 201 'X2
kY2 = 104 'Y2
Call kAngleFunc
DrawLeftEndDlosed = False
'------------------------------------
'Cords. for the second rectangle which will
'appear on the right side of the screen
'-------------------------------------
'cords. of the red line to be drawn first
'These cords. can be change to draw other rectangles
kX1 = 201 'x1
kY1 = 104 'Y1
kX2 = 334 'X2
kY2 = 157 'Y2
Call kAngleFunc
End Sub
Sub kAngleFunc() 'My angle finding function
Dim kArccos As Double
Dim kRatio As Double 'The ratio between imaginary
'base line(along the X plane)and the
'Hypotenuse( the red line that was actually drawn with
'the mouse ) ( Base/Drawn Line )
fLine = 12 'set the height of the rectangle
pi = 3.14159265358979
kLineX = kX2 - kX1 'Imaginary line along the X plane
kLineY = kY2 - kY1 'Imaginary line along the Y plane
'Calculate the length of the red line that was
'Actulay drawn ( Hypotenuse )
kHypot = Sqr((kLineX * kLineX) + (kLineY * kLineY))
kRatio = kLineX / kHypot 'Get the ratio between the
'Imaginary line along the X plane and
'The red line that was drawn
'If the red line is being drawn in an upward direction
If kY1 > kY2 Then
'Calculate the "Angle" using
'the ( inverse cosine, aka arccos )
kArccos = (Atn(-kRatio / Sqr(-kRatio * kRatio + 1)) + 2 * Atn(1)) * 180 / pi
End If
'If the red line is being drawn in an downward direction
If kY1 < kY2 Then
'Calculate the "Angle" using
'the ( inverse cosine, aka arccos )
'and change it to a negative
'Example: If the line is drawn down and to the
'left, show the angle as a negative
kArccos = (-1) * (Atn(-kRatio / Sqr(-kRatio * kRatio + 1)) + 2 * Atn(1)) * 180 / pi
End If
kAngle = kArccos 'Pass the kArcoss Function's
'answer ( number in degrees )to the public variable kAngle
'so that the whole program can see
Picture1.Line (kX1, kY1)-(kX2, kY2), vbRed
dLine = Sin(kAngle * pi / 180) * fLine
eLine = Cos(kAngle * pi / 180) * fLine
kX3 = kX1 - dLine
kY3 = kY1 - eLine
kX4 = kX2 - dLine
kY4 = kY2 - eLine
If DrawLeftEndDlosed = True Then 'Rectangle on left side of screen
Picture1.Line (kX1, kY1)-(kX3, kY3), RGB(50, 75, 255)
Picture1.Line (kX3, kY3)-(kX4, kY4), RGB(50, 75, 255)
End If
If DrawLeftEndDlosed = False Then 'Rectangle on right side of screen
Picture1.Line (kX2, kY2)-(kX4, kY4), RGB(50, 75, 255)
Picture1.Line (kX3, kY3)-(kX4, kY4), RGB(50, 75, 255)
End If
End Sub
Sub SetupFormAndPictureBoxSoUDontHaveTo()
Form1.WindowState = 2
Picture1.AutoRedraw = True
Picture1.BackColor = &H80000009
Picture1.BorderStyle = 1
Picture1.ScaleMode = 3
Picture1.Height = 4860
Picture1.Left = 30
Picture1.Top = 105
Picture1.Width = 5880
End Sub