Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
On Exit question
#13
(06-02-2023, 12:28 PM)Dimster Wrote: Hi Terry - hope I'm not taking this tread down a different road but on that Option_Explicit, do you find it highlights an "undefined variable" error when the variable is just a local loop control type. I don't DIM or DIM SHARED those loop control variables.

Ie

For XYZ = 1 to 10
 XYZ$ = str$(XYZ)
 FileName$ = "MyFileNumber"
 File$ = FileName$ + LTrim$(XYZ$)
 Print File$
Next XYZ

In this case I'm just trying to add a number to the file name and XYZ does not need to be Dimensioned as I believe it's a Single by default. But I take your suggestion as a very good one to debug for a "Duplicate Definition".

I'm under the opinion that all variables should be declared. OPTION _EXPLICIT in your code will require this for every single variable. By declaring all variables you get the added benefit of having a detailed listing of your variables followed by a REM statement after each explaining its use:

Code: (Select All)
    DIM lineStart AS Type_Vector2 ' start vector of line (x,y)
    DIM lineEnd AS Type_Vector2 '   end   vector of line (x,y)
    DIM dx AS SINGLE '              run  (delta in the x direction)
    DIM dy AS SINGLE '              rise (delta in the y direction)
    DIM m AS SINGLE '               slope (rise over run)
    DIM b AS SINGLE '               the y intercept

This is a habit, in my opinion, that every programmer should get in to. It GREATLY helps when debugging your own code or asking someone else to have a look at it for help. The above code snippet contains the variables I use in a function that detects a point on a line. Imagine trying to debug this code without declaring the variables and having a description associated with them. Here is the entire function :

Code: (Select All)
FUNCTION PointOnLine (TestPoint AS Type_Vector2, __line2D AS Type_Line2D)

    ' based on the Slope Intercept Form of the equation of a straight line
    '
    '
    '  |                                       S = Line Start = (0,1)
    '  |                             (8,5)     E = Line End   = (8,5)
    ' 5+                             __ù       Need to get values of this formula: y = m * x + b
    '  |                           _-          Solve for m:
    '  |                        _--              - dy = Ey - Sy = 5 - 1 = 4
    ' 4+                     __-                 - dx = Ex - Sx = 8 - 0 = 8
    '  |                   _-                           dy     4     1
    '  |      P         _--                      - m = ---- = --- = --- (m solved)
    ' 3+      ù      __-                                dx     8     2
    '  |    (2,3)  _-                          Solve for b:
    '  |        _--                              - b = y - mx  (plug in x (0) and y (1) from line start)
    ' 2+     __-                                            1
    '  |   _-                                    - b = 1 - --- * 0 = 1 - 0 = 1 (b solved)
    '  |_--                                                 2
    ' 1ù                                       Plug in values from Px along with solved m and b to compare y result with Py
    '(0,1)                                              1
    '  |                                         - y = --- * x + 1 = y = .5 * 2 + 1 = y = 2 (FALSE) 2 is not equal to Py (3)
    '  +---+---+---+---+---+---+---+---+---             2
    ' 0    1   2   3   4   5   6   7   8
    '

    DIM lineStart AS Type_Vector2 ' start vector of line (x,y)
    DIM lineEnd AS Type_Vector2 '   end   vector of line (x,y)
    DIM dx AS SINGLE '              run  (delta in the x direction)
    DIM dy AS SINGLE '              rise (delta in the y direction)
    DIM m AS SINGLE '               slope (rise over run)
    DIM b AS SINGLE '               the y intercept

    PointOnLine = 0 '                                            assume point not on line
    lineStart = __line2D.from '                                  get line start vector (x,y)
    lineEnd = __line2D.too '                                     get line end   vector (x,y)
    dy = lineEnd.y - lineStart.y '                               calculate rise
    dx = lineEnd.x - lineStart.x '                               calculate run
    IF dx = 0 THEN '                                             vertical line? (avoid divide by 0)
        IF TestPoint.x = lineStart.x THEN '                      yes, do x values match?
            PointOnLine = -1 '                                   yes, must be on the line
            EXIT FUNCTION '                                      leave
        END IF
    END IF
    m = dy / dx '                                                calculate slope
    b = lineStart.y - (m * lineStart.x) '                        calculate y intercept
    IF TestPoint.y = m * TestPoint.x + b THEN PointOnLine = -1 ' point on line if y = mx + b

END FUNCTION
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply


Messages In This Thread
On Exit question - by NasaCow - 06-02-2023, 03:50 AM
RE: On Exit question - by mnrvovrfc - 06-02-2023, 05:29 AM
RE: On Exit question - by NasaCow - 06-02-2023, 06:00 AM
RE: On Exit question - by mnrvovrfc - 06-02-2023, 08:59 AM
RE: On Exit question - by Dimster - 06-02-2023, 11:19 AM
RE: On Exit question - by TerryRitchie - 06-02-2023, 11:27 AM
RE: On Exit question - by Dimster - 06-02-2023, 12:28 PM
RE: On Exit question - by Kernelpanic - 06-02-2023, 01:54 PM
RE: On Exit question - by TerryRitchie - 06-02-2023, 05:33 PM
RE: On Exit question - by bplus - 06-02-2023, 01:28 PM
RE: On Exit question - by bplus - 06-02-2023, 02:21 PM
RE: On Exit question - by Kernelpanic - 06-02-2023, 08:23 PM
RE: On Exit question - by bplus - 06-02-2023, 02:36 PM
RE: On Exit question - by bplus - 06-02-2023, 03:14 PM
RE: On Exit question - by bplus - 06-02-2023, 08:19 PM
RE: On Exit question - by bplus - 06-02-2023, 08:27 PM
RE: On Exit question - by Kernelpanic - 06-02-2023, 08:40 PM
RE: On Exit question - by Dimster - 06-02-2023, 09:33 PM
RE: On Exit question - by mnrvovrfc - 06-02-2023, 09:44 PM
RE: On Exit question - by TerryRitchie - 06-02-2023, 10:52 PM
RE: On Exit question - by bplus - 06-02-2023, 10:59 PM
RE: On Exit question - by TerryRitchie - 06-02-2023, 11:12 PM
RE: On Exit question - by bplus - 06-02-2023, 11:21 PM
RE: On Exit question - by Dimster - 06-03-2023, 01:29 PM
RE: On Exit question - by SMcNeill - 06-03-2023, 01:51 PM
RE: On Exit question - by Dimster - 06-03-2023, 03:10 PM
RE: On Exit question - by TerryRitchie - 06-03-2023, 07:21 PM
RE: On Exit question - by Kernelpanic - 06-03-2023, 03:26 PM
RE: On Exit question - by NasaCow - 06-04-2023, 03:03 AM
RE: On Exit question - by bplus - 06-04-2023, 03:20 PM
RE: On Exit question - by TempodiBasic - 06-04-2023, 04:05 PM
RE: On Exit question - by TempodiBasic - 06-04-2023, 05:38 PM
RE: On Exit question - by TerryRitchie - 06-04-2023, 08:35 PM
RE: On Exit question - by TempodiBasic - 06-04-2023, 10:51 PM
RE: On Exit question - by NasaCow - 06-04-2023, 11:59 PM
RE: On Exit question - by TerryRitchie - 06-05-2023, 12:31 AM
RE: On Exit question - by NasaCow - 06-05-2023, 12:50 AM
RE: On Exit question - by SMcNeill - 06-05-2023, 09:04 PM
RE: On Exit question - by bplus - 06-05-2023, 09:09 PM
RE: On Exit question - by SMcNeill - 06-05-2023, 09:17 PM
RE: On Exit question - by SMcNeill - 06-07-2023, 01:38 AM
RE: On Exit question - by TempodiBasic - 06-07-2023, 09:08 AM
RE: On Exit question - by TempodiBasic - 06-07-2023, 09:31 AM
RE: On Exit question - by NasaCow - 06-08-2023, 12:05 AM
RE: On Exit question - by mnrvovrfc - 06-09-2023, 09:31 PM
RE: On Exit question - by TempodiBasic - 06-09-2023, 09:14 PM
RE: On Exit question - by bplus - 06-10-2023, 05:24 PM
RE: On Exit question - by TempodiBasic - 06-17-2023, 12:18 AM
RE: On Exit question - by bplus - 06-17-2023, 02:29 AM
RE: On Exit question - by GareBear - 06-17-2023, 01:07 AM
RE: On Exit question - by TempodiBasic - 06-22-2023, 12:01 PM
RE: On Exit question - by bplus - 06-22-2023, 12:22 PM
RE: On Exit question - by TempodiBasic - 06-22-2023, 07:45 PM



Users browsing this thread: 14 Guest(s)