#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL v1_x AS DOUBLE
    LOCAL v1_y AS DOUBLE
    LOCAL v1_z AS DOUBLE
    LOCAL v2_x AS DOUBLE
    LOCAL v2_y AS DOUBLE
    LOCAL v2_z AS DOUBLE
    LOCAL r_x AS DOUBLE
    LOCAL r_y AS DOUBLE
    LOCAL r_z AS DOUBLE
    LOCAL dot_val AS DOUBLE
    LOCAL len1 AS DOUBLE
    LOCAL angle_cos AS DOUBLE
    LOCAL rx_90 AS DOUBLE
    LOCAL ry_90 AS DOUBLE
    LOCAL rz_90 AS DOUBLE
    LOCAL ra_x AS DOUBLE
    LOCAL ra_y AS DOUBLE
    LOCAL ra_z AS DOUBLE
    LOCAL temp_str AS STRING

    PRINT "3D Vector Operations"
    PRINT "===================="
    PRINT ""

    v1_x = 1.0
    v1_y = 2.0
    v1_z = 3.0
    v2_x = 4.0
    v2_y = 5.0
    v2_z = 6.0

    ' Add
    r_x = v1_x + v2_x
    r_y = v1_y + v2_y
    r_z = v1_z + v2_z
    PRINT "Add: (1,2,3)+(4,5,6) = (" + STR$(r_x) + "," + STR$(r_y) + "," + STR$(r_z) + ")"

    ' Subtract
    r_x = v1_x - v2_x
    r_y = v1_y - v2_y
    r_z = v1_z - v2_z
    PRINT "Sub: (1,2,3)-(4,5,6) = (" + STR$(r_x) + "," + STR$(r_y) + "," + STR$(r_z) + ")"

    ' Dot product
    dot_val = v1_x * v2_x + v1_y * v2_y + v1_z * v2_z
    PRINT "Dot product = " + STR$(dot_val)

    ' Cross product
    r_x = v1_y * v2_z - v1_z * v2_y
    r_y = v1_z * v2_x - v1_x * v2_z
    r_z = v1_x * v2_y - v1_y * v2_x
    PRINT "Cross: (1,2,3)x(4,5,6) = (" + STR$(r_x) + "," + STR$(r_y) + "," + STR$(r_z) + ")"

    ' Length
    len1 = SQR(v1_x * v1_x + v1_y * v1_y + v1_z * v1_z)
    PRINT "Length(1,2,3) = " + STR$(len1)

    ' Normalize
    IF len1 > 0 THEN
        r_x = v1_x / len1
        r_y = v1_y / len1
        r_z = v1_z / len1
        PRINT "Normalized = (" + STR$(r_x) + "," + STR$(r_y) + "," + STR$(r_z) + ")"
    END IF

    ' Rotate around X axis (90 degrees)
    angle_cos = 0.0
    rx_90 = v1_x
    ry_90 = v1_y * angle_cos - v1_z * 1.0
    rz_90 = v1_y * 1.0 + v1_z * angle_cos
    PRINT "RotateX 90: (" + STR$(rx_90) + "," + STR$(ry_90) + "," + STR$(rz_90) + ")"

    ' Rotate around Y axis (90 degrees)
    rx_90 = v1_x * angle_cos + v1_z * 1.0
    ry_90 = v1_y
    rz_90 = -v1_x * 1.0 + v1_z * angle_cos
    PRINT "RotateY 90: (" + STR$(rx_90) + "," + STR$(ry_90) + "," + STR$(rz_90) + ")"

    ' Rotate around Z axis (90 degrees)
    rx_90 = v1_x * angle_cos - v1_y * 1.0
    ry_90 = v1_x * 1.0 + v1_y * angle_cos
    rz_90 = v1_z
    PRINT "RotateZ 90: (" + STR$(rx_90) + "," + STR$(ry_90) + "," + STR$(rz_90) + ")"

    ' More test vectors
    v1_x = 0.0
    v1_y = 1.0
    v1_z = 0.0
    v2_x = 1.0
    v2_y = 0.0
    v2_z = 0.0

    r_x = v1_y * v2_z - v1_z * v2_y
    r_y = v1_z * v2_x - v1_x * v2_z
    r_z = v1_x * v2_y - v1_y * v2_x
    PRINT ""
    PRINT "Cross (0,1,0)x(1,0,0) = (" + STR$(r_x) + "," + STR$(r_y) + "," + STR$(r_z) + ")"

    ' Triple product
    dot_val = r_x * 0.0 + r_y * 1.0 + r_z * 0.0
    PRINT "Scalar triple product = " + STR$(dot_val)

    PRINT ""
    PRINT "3D vector operations complete."

    FUNCTION = 0
END FUNCTION
