Vector2
Root / Programming Questions / [.]
KHypnoCreated:
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)
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 ENDIf 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
If you're okay with only having 16 bits of information, you could probably use bit shiftingwhat is bit shiftingVAR 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)