06-02-2023, 05:33 PM
(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