home *** CD-ROM | disk | FTP | other *** search
- VERSION 4.00
- Begin VB.Form LeastSquareForm
- Caption = "Least Squares"
- ClientHeight = 5310
- ClientLeft = 2085
- ClientTop = 900
- ClientWidth = 4830
- Height = 6000
- Left = 2025
- LinkTopic = "Form1"
- ScaleHeight = 354
- ScaleMode = 3 'Pixel
- ScaleWidth = 322
- Top = 270
- Width = 4950
- Begin VB.CommandButton CmdGo
- Caption = "Go"
- Default = -1 'True
- Enabled = 0 'False
- Height = 375
- Left = 2040
- TabIndex = 1
- Top = 4920
- Width = 615
- End
- Begin VB.PictureBox Canvas
- AutoRedraw = -1 'True
- Height = 4815
- Left = 0
- ScaleHeight = 317
- ScaleMode = 3 'Pixel
- ScaleWidth = 317
- TabIndex = 0
- Top = 0
- Width = 4815
- End
- Begin VB.Menu mnuFile
- Caption = "&File"
- Begin VB.Menu mnuFileExit
- Caption = "E&xit"
- End
- End
- Attribute VB_Name = "LeastSquareForm"
- Attribute VB_Creatable = False
- Attribute VB_Exposed = False
- Option Explicit
- Dim NumPts As Integer
- Dim PtX() As Single
- Dim PtY() As Single
- ' ************************************************
- ' Compute the m and b values for the least squares
- ' line.
- ' ************************************************
- Sub GetLeastSquaresValues(num As Integer, X() As Single, Y() As Single, mvalue As Single, bvalue As Single)
- Dim A As Single
- Dim B As Single
- Dim C As Single
- Dim D As Single
- Dim i As Integer
- ' Compute the sums.
- For i = 1 To NumPts
- A = A + PtX(i) * PtX(i)
- B = B + PtX(i)
- C = C + PtX(i) * PtY(i)
- D = D + PtY(i)
- Next i
- mvalue = (B * D - C * NumPts) / (B * B - A * NumPts)
- bvalue = (B * C - A * D) / (B * B - A * NumPts)
- End Sub
- ' ************************************************
- ' Add this point to the list of points.
- ' ************************************************
- Private Sub Canvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
- Const GAP = 2
- ' If this is the first point, erase the screen.
- If NumPts < 1 Then Canvas.Cls
- ' Record the new point.
- NumPts = NumPts + 1
- ReDim Preserve PtX(1 To NumPts)
- ReDim Preserve PtY(1 To NumPts)
- PtX(NumPts) = X
- PtY(NumPts) = Y
- ' Display the point.
- Canvas.Line (X - GAP, Y - GAP)-(X + GAP, Y + GAP), , BF
- ' If NumPts >= 2, enable the Go button.
- If NumPts >= 2 Then CmdGo.Enabled = True
- End Sub
- ' ************************************************
- ' Draw the least squares fit curve.
- ' ************************************************
- Private Sub CmdGo_Click()
- CmdGo.Enabled = False
- DrawCurve
- ' Prepare to get a new set of points.
- NumPts = 0
- End Sub
- ' ************************************************
- ' Draw the least squares line.
- ' ************************************************
- Sub DrawCurve()
- Dim mvalue As Single
- Dim bvalue As Single
- Dim x1 As Single
- Dim x2 As Single
- Dim y1 As Single
- Dim y2 As Single
- Dim i As Integer
- ' Get the m and b values for the line.
- GetLeastSquaresValues NumPts, PtX, PtY, mvalue, bvalue
- ' Find the minimum and maximum X values.
- x1 = PtX(1) ' This will be the minimum X value.
- x2 = x1 ' This will be the maximum X value.
- For i = 2 To NumPts
- If x1 > PtX(i) Then x1 = PtX(i)
- If x2 < PtX(i) Then x2 = PtX(i)
- Next i
- ' Draw the line.
- y1 = mvalue * x1 + bvalue
- y2 = mvalue * x2 + bvalue
- Canvas.Line (x1, y1)-(x2, y2)
- End Sub
- Private Sub mnuFileExit_Click()
- Unload Me
- End Sub
-