#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL v1_x AS DOUBLE
    LOCAL v1_y AS DOUBLE
    LOCAL v2_x AS DOUBLE
    LOCAL v2_y AS DOUBLE
    LOCAL r_x AS DOUBLE
    LOCAL r_y AS DOUBLE
    LOCAL dot_val AS DOUBLE
    LOCAL cross_val AS DOUBLE
    LOCAL len1 AS DOUBLE
    LOCAL len2 AS DOUBLE
    LOCAL norm_x AS DOUBLE
    LOCAL norm_y AS DOUBLE
    LOCAL temp_str AS STRING

    PRINT "2D Vector Operations"
    PRINT "===================="
    PRINT ""

    v1_x = 3.0
    v1_y = 4.0
    v2_x = 1.0
    v2_y = 2.0

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

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

    ' Scale
    r_x = v1_x * 2.0
    r_y = v1_y * 2.0
    PRINT "Scale: (3,4) * 2 = (" + STR$(r_x) + ", " + STR$(r_y) + ")"

    ' Dot product
    dot_val = v1_x * v2_x + v1_y * v2_y
    PRINT "Dot: (3,4) . (1,2) = " + STR$(dot_val)

    ' Cross product (2D scalar)
    cross_val = v1_x * v2_y - v1_y * v2_x
    PRINT "Cross: (3,4) x (1,2) = " + STR$(cross_val)

    ' Length
    len1 = SQR(v1_x * v1_x + v1_y * v1_y)
    len2 = SQR(v2_x * v2_x + v2_y * v2_y)
    PRINT "Length(3,4) = " + STR$(len1)
    PRINT "Length(1,2) = " + STR$(len2)

    ' Normalize
    IF len1 > 0 THEN
        norm_x = v1_x / len1
        norm_y = v1_y / len1
        PRINT "Normalize(3,4) = (" + STR$(norm_x) + ", " + STR$(norm_y) + ")"
    END IF

    ' Distance between two vectors
    r_x = v1_x - v2_x
    r_y = v1_y - v2_y
    len1 = SQR(r_x * r_x + r_y * r_y)
    PRINT "Distance between (3,4) and (1,2) = " + STR$(len1)

    ' Angle between vectors
    dot_val = 3.0 * 1.0 + 4.0 * 2.0
    len1 = SQR(9.0 + 16.0)
    len2 = SQR(1.0 + 4.0)
    IF len1 > 0 AND len2 > 0 THEN
        dot_val = dot_val / (len1 * len2)
        PRINT "Angle between: cos(theta) = " + STR$(dot_val)
    END IF

    ' Projection of v1 onto v2
    dot_val = 3.0 * 1.0 + 4.0 * 2.0
    len2 = 1.0 + 4.0
    IF len2 > 0 THEN
        dot_val = dot_val / len2
        r_x = dot_val * 1.0
        r_y = dot_val * 2.0
        PRINT "Projection of (3,4) onto (1,2) = (" + STR$(r_x) + ", " + STR$(r_y) + ")"
    END IF

    ' Perpendicular vector
    r_x = -v1_y
    r_y = v1_x
    PRINT "Perpendicular to (3,4) = (" + STR$(r_x) + ", " + STR$(r_y) + ")"

    ' Linear interpolation
    r_x = v1_x + 0.5 * (v2_x - v1_x)
    r_y = v1_y + 0.5 * (v2_y - v1_y)
    PRINT "Lerp(3,4)->(1,2) at t=0.5 = (" + STR$(r_x) + ", " + STR$(r_y) + ")"

    ' Reflection
    ' Reflect v1 across normal (0,1)
    dot_val = 3.0 * 0.0 + 4.0 * 1.0
    r_x = v1_x - 2.0 * dot_val * 0.0
    r_y = v1_y - 2.0 * dot_val * 1.0
    PRINT "Reflect (3,4) across (0,1) = (" + STR$(r_x) + ", " + STR$(r_y) + ")"

    PRINT ""
    PRINT "Vector 2D operations complete."

    FUNCTION = 0
END FUNCTION
