Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Declaring Functions AS TYPEs
#5
I feel your pain. Working with vector types just screams having a way to directly functionalize their manipulation. I resort to the somewhat clumsy approach of passing a variable back from a sub. The main issue being keeping track of which variable is the desired result. I use the first parameter as a return. Then I have to decide if I want to preserve original operands or overwrite them. At least it can be done in a fairly concise manner.

I imagine that since there are no rules against multi-dimensional (4+ element) vectors, there is simply no point in trying to satisfy all possibilities. So we'll just have to do the typical hacks.

Code: (Select All)
TYPE V3
    x AS SINGLE
    y AS SINGLE
    z AS SINGLE
END TYPE
DIM AS V3 A, B, C
A.x = 3: A.y = 7: A.z = -4
B.x = -13: B.y = 5: B.z = 9
'If I need to keep the original state, then I copy the first parameter prior to the call
C = A: R3_Add C, B, 1
Pvec "A", A
PRINT "plus"
Pvec "B", B
PRINT "equals"
Pvec "C", C
PRINT "________________________________"
C = A: R3_Add C, B, -1
Pvec "A", A
PRINT "minus"
Pvec "B", B
PRINT "equals"
Pvec "C", C
PRINT "________________________________"
'If I don't need the original, I'll just overwrite it.
Pvec "A", A
PRINT "plus"
R3_Add A, B, 1
Pvec "B", B
PRINT "equals"
Pvec "A", A
PRINT "________________________________"
'an inversion by manipulating the scalar
Pvec "A", A
PRINT "inverted"
R3_Add A, A, -2
Pvec "A", A

END
SUB R3_Add (re AS V3, se AS V3, scalar AS INTEGER)
    re.x = re.x + se.x * scalar
    re.y = re.y + se.y * scalar
    re.z = re.z + se.z * scalar
END SUB
SUB Pvec (s AS STRING, v AS V3)
    PRINT "Vector "; s; ":  <"; v.x; ", "; v.y; ", "; v.z; ">"
END SUB
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply


Messages In This Thread
Declaring Functions AS TYPEs - by TerryRitchie - 08-16-2023, 02:26 AM
RE: Declaring Functions AS TYPEs - by dbox - 08-16-2023, 02:56 AM
RE: Declaring Functions AS TYPEs - by mnrvovrfc - 08-16-2023, 03:08 AM
RE: Declaring Functions AS TYPEs - by OldMoses - 08-16-2023, 11:53 AM
RE: Declaring Functions AS TYPEs - by mnrvovrfc - 08-16-2023, 12:33 PM
RE: Declaring Functions AS TYPEs - by a740g - 08-16-2023, 12:38 PM
RE: Declaring Functions AS TYPEs - by bplus - 08-16-2023, 02:38 PM
RE: Declaring Functions AS TYPEs - by Kernelpanic - 08-16-2023, 04:14 PM
RE: Declaring Functions AS TYPEs - by grymmjack - 08-16-2023, 09:50 PM
RE: Declaring Functions AS TYPEs - by mnrvovrfc - 08-16-2023, 11:25 PM
RE: Declaring Functions AS TYPEs - by bplus - 08-17-2023, 12:41 AM



Users browsing this thread: 8 Guest(s)