VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Draws a curve from a random array of 3-9 coordinates. (Press numbers 3-9 for the size of the array

by Zeev Rotshtein (1 Submission)
Category: Graphics
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Thu 11th August 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Draws a curve from a random array of 3-9 coordinates. (Press numbers 3-9 for the size of the array and Enter to draw).

API Declarations



Option Explicit

Const STEP = 100

Public Enum SplineSegment
SplineMiddleSegment = &H0
SplineFirstSegment = &H1
SplineLastSegment = &H2
SplineSingleSegment = &H3
End Enum

Public Type Coordinate
X As Double
Y As Double
End Type

Public Sub HalfSpline(Board As Form, Point1 As Coordinate, Point2 As Coordinate, Point3 As Coordinate, Optional Flag As SplineSegment = SplineMiddleSegment)
Dim p1 As Coordinate
Dim p3 As Coordinate

If (Flag And SplineFirstSegment) Then
p1.X = Point1.X
p1.Y = Point1.Y
Else
p1.X = (Point1.X + Point2.X) / 2
p1.Y = (Point1.Y + Point2.Y) / 2
End If

If (Flag And SplineLastSegment) Then
p3.X = Point3.X
p3.Y = Point3.Y
Else
p3.X = (Point3.X + Point2.X) / 2
p3.Y = (Point3.Y + Point2.Y) / 2
End If

SingleSpline Board, p1, Point2, p3
End Sub

Public Sub SingleSpline(Board As Form, Point1 As Coordinate, Point2 As Coordinate, Point3 As Coordinate)
Dim i As Integer

Dim CurrX1 As Double
Dim CurrY1 As Double
Dim CurrX2 As Double
Dim CurrY2 As Double

Dim StepX1 As Double
Dim StepY1 As Double
Dim StepX2 As Double
Dim StepY2 As Double

Dim DX As Double
Dim DY As Double
Dim LastDX As Double
Dim LastDy As Double

CurrX1 = Point1.X
CurrY1 = Point1.Y
CurrX2 = Point2.X
CurrY2 = Point2.Y

StepX1 = (Point2.X - Point1.X) / STEP
StepY1 = (Point2.Y - Point1.Y) / STEP
StepX2 = (Point3.X - CurrX2) / STEP
StepY2 = (Point3.Y - CurrY2) / STEP

LastDX = CurrX1
LastDy = CurrY1

For i = 0 To STEP
DX = ((CurrX2 - CurrX1) * (i / STEP)) + CurrX1
DY = ((CurrY2 - CurrY1) * (i / STEP)) + CurrY1
Board.Line (DX, DY)-(LastDX, LastDy)

LastDX = DX
LastDy = DY

CurrX1 = CurrX1 + StepX1
CurrY1 = CurrY1 + StepY1
CurrX2 = CurrX2 + StepX2
CurrY2 = CurrY2 + StepY2
Next i
End Sub

Rate Draws a curve from a random array of 3-9 coordinates. (Press numbers 3-9 for the size of the array




Option Explicit
Option Base 1

Dim Points As Integer

Const FRAME_WIDTH = 300
Const FRAME_HEIGHT = 500

Const WELCOME_STR = "Welcome. Hit the ENTER!"
Const TITLE_STR = "Spline Maker"
Const DEF_POINTS = 5

Dim P() As Coordinate

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then Form_Paint
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    Dim Num As Integer
    
    Num = KeyAscii - Asc("0")
    
    If (Num > 2 And Num < 10) Then
        Points = Num
        WriteOnForm Points & " points"
    End If
End Sub

Private Sub Form_Load()
    Points = DEF_POINTS
    WriteOnForm WELCOME_STR & vbTab & "[" & Points & " points]"
End Sub

Private Sub Form_Paint()
    Dim i As Integer
    
    Randomize
    Cls
    
    ReDim P(Points)
    
    For i = 1 To Points
        P(i).X = ScaleLeft + Rnd * (ScaleWidth - FRAME_WIDTH)
        P(i).Y = ScaleTop + Rnd * (ScaleHeight - FRAME_HEIGHT)
        ForeColor = vbBlack
        Line (P(i).X - 50, P(i).Y - 50)-(P(i).X + 50, P(i).Y + 50)
        Line (P(i).X - 50, P(i).Y + 50)-(P(i).X + 50, P(i).Y - 50)
        CurrentX = P(i).X + 50
        CurrentY = P(i).Y + 50
        Print Str(i) 'Chr((Asc("A") - 1) + i)
        If i > 1 Then
            Line (P(i).X, P(i).Y)-(P(i - 1).X, P(i - 1).Y), vbGreen
        End If
    Next i
    
    ForeColor = vbRed
    
    DrawSpline
End Sub

Sub DrawSpline()
    Dim i As Integer
    
    For i = 2 To Points - 1
        HalfSpline Me, P(i - 1), P(i), P(i + 1), (-(i = 2)) + (-2 * (i = Points - 1))
    Next i
End Sub

Sub WriteOnForm(Text As String)
    Cls
    CurrentX = (ScaleLeft + ScaleWidth - TextWidth(Text)) / 2
    CurrentY = (ScaleTop + ScaleHeight - TextHeight(Text)) / 2
    Print Text
End Sub

Download this snippet    Add to My Saved Code

Draws a curve from a random array of 3-9 coordinates. (Press numbers 3-9 for the size of the array Comments

No comments have been posted about Draws a curve from a random array of 3-9 coordinates. (Press numbers 3-9 for the size of the array . Why not be the first to post a comment about Draws a curve from a random array of 3-9 coordinates. (Press numbers 3-9 for the size of the array .

Post your comment

Subject:
Message:
0/1000 characters