LoginLogin

Vector2

Root / Programming Questions / [.]

KHypnoCreated:
I want a way for ONE variable to have 2 values. (x and y) How do I do that?

DIM VEC[2]
VEC[0] = x
VEC[1] = y

DIM VEC[2]
VEC[0] = x
VEC[1] = y
lol yes Is there another way?

It's usually better to just use 2 variables though

If you're okay with only having 16 bits of information, you could probably use bit shifting
VAR VEC2 = 0
' To set values:
X = 5
Y = 10
VEC = VEC AND ((X AND &HFFFF) << 16) AND (Y AND &HFFFF)
' To get values:
VEC2X = VEC >> 16 AND &HFFFF
VEC2Y = VEC AND &HFFFF
(tihs might not work, I haven't used SB in a long time)

psst, this is what DEFY's TOPOINT() does though it's written kinda poorly at the moment. tell you what, I'll rewrite today

Here are the new POINT functions.
COMMON DEF TOPOINT(X%,Y%)
  RETURN (X% AND &HFFFF) OR  (Y% AND &HFFFF<<16)
END

COMMON DEF GETX(P%)
  RETURN P%<<16>>16
END

COMMON DEF GETY(P%)
  RETURN P%>>16
END
If you wanna have a laugh, look at how insanely long wrote the original functions in comparison.
Spoiler
COMMON DEF TOPOINT(XI,YI)
VAR X=XI
VAR Y=YI
VAR XS,YS,R,S

IF !CLASSIFY(X) THEN
X=CAP(ROUND(X),-&H7FFF,&H7FFF)
XS=SGN(X)==-1
X=ABS(X)
ELSE
X=0:XS=1
ENDIF

IF !CLASSIFY(Y) THEN
Y=CAP(ROUND(Y),-&H7FFF,&H7FFF)
YS=SGN(Y)==-1
Y=ABS(Y)
ELSE
Y=0:YS=1
ENDIF

R=0
FOR S=0 TO 14
R=SETBIT(R,S,READBIT(X,S))
R=SETBIT(R,S+16,READBIT(Y,S))
NEXT
R=SETBIT(R,15,XS)
R=SETBIT(R,31,YS)
RETURN R
END



COMMON DEF GETX(P)
VAR X,XS,S
X=0
FOR S=0 TO 14
X=SETBIT(X,S,READBIT(P,S))
NEXT
XS=READBIT(P,15)
IF XS THEN X=X*-1
IF XS AND !X THEN
RETURN NAN()
ELSE
RETURN X
ENDIF
END

COMMON DEF GETY(P)
VAR Y,YS,S
Y=0
FOR S=0 TO 14
Y=SETBIT(Y,S,READBIT(P,S+16))
NEXT
YS=READBIT(P,31)
IF YS THEN Y=Y*-1
IF YS AND !Y THEN
RETURN NAN()
ELSE
RETURN Y
ENDIF
END

DIM VEC[2]
VEC[0] = x
VEC[1] = y
Ill use this

If you're okay with only having 16 bits of information, you could probably use bit shifting
VAR VEC2 = 0
' To set values:
X = 5
Y = 10
VEC = VEC AND ((X AND &HFFFF) << 16) AND (Y AND &HFFFF)
' To get values:
VEC2X = VEC >> 16 AND &HFFFF
VEC2Y = VEC AND &HFFFF
(tihs might not work, I haven't used SB in a long time)
what is bit shifting

yeah I prefer data arrays DIM THISARR[377,582,562] (probably out of memory lol)