#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL p1_x AS DOUBLE
    LOCAL p1_y AS DOUBLE
    LOCAL p2_x AS DOUBLE
    LOCAL p2_y AS DOUBLE
    LOCAL p3_x AS DOUBLE
    LOCAL p3_y AS DOUBLE
    LOCAL p4_x AS DOUBLE
    LOCAL p4_y AS DOUBLE
    LOCAL test_x AS DOUBLE
    LOCAL test_y AS DOUBLE
    LOCAL intersect_x AS DOUBLE
    LOCAL intersect_y AS DOUBLE
    LOCAL denom AS DOUBLE
    LOCAL t_val AS DOUBLE
    LOCAL u_val AS DOUBLE
    LOCAL dist_val AS DOUBLE
    LOCAL dot_val AS DOUBLE
    LOCAL inside_flag AS LONG
    LOCAL vertices_x(10) AS DOUBLE
    LOCAL vertices_y(10) AS DOUBLE
    LOCAL vert_count AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL cross_val AS DOUBLE
    LOCAL temp_str AS STRING

    PRINT "2D Geometry Operations"
    PRINT "======================"
    PRINT ""

    ' Line intersection
    p1_x = 0.0
    p1_y = 0.0
    p2_x = 4.0
    p2_y = 4.0
    p3_x = 0.0
    p3_y = 4.0
    p4_x = 4.0
    p4_y = 0.0

    denom = (p1_x - p2_x) * (p3_y - p4_y) - (p1_y - p2_y) * (p3_x - p4_x)
    IF denom <> 0 THEN
        t_val = ((p1_x - p3_x) * (p3_y - p4_y) - (p1_y - p3_y) * (p3_x - p4_x)) / denom
        u_val = ((p1_x - p3_x) * (p1_y - p2_y) - (p1_y - p3_y) * (p1_x - p2_x)) / denom

        intersect_x = p1_x + t_val * (p2_x - p1_x)
        intersect_y = p1_y + t_val * (p2_y - p1_y)

        IF t_val >= 0 AND t_val <= 1 AND u_val >= 0 AND u_val <= 1 THEN
            PRINT "Line intersection at (" + STR$(intersect_x) + ", " + STR$(intersect_y) + ")"
        ELSE
            PRINT "Lines intersect at (" + STR$(intersect_x) + ", " + STR$(intersect_y) + ") (DOUBLE)"
        END IF
    ELSE
        PRINT "Lines are parallel"
    END IF

    ' Point-in-polygon test (triangle)
    vert_count = 3
    vertices_x(0) = 0.0
    vertices_y(0) = 0.0
    vertices_x(1) = 5.0
    vertices_y(1) = 0.0
    vertices_x(2) = 2.5
    vertices_y(2) = 5.0

    test_x = 2.5
    test_y = 1.0
    inside_flag = 0
    i = 0
    WHILE i < vert_count
        j = i + 1
        IF j >= vert_count THEN j = 0
        IF vertices_y(i) > test_y AND vertices_y(j) <= test_y THEN
            denom = vertices_y(j) - vertices_y(i)
            IF denom <> 0 THEN
                t_val = (test_y - vertices_y(i)) / denom
                IF test_x < vertices_x(i) + t_val * (vertices_x(j) - vertices_x(i)) THEN
                    inside_flag = 1 - inside_flag
                END IF
            END IF
        ELSEIF vertices_y(j) > test_y AND vertices_y(i) <= test_y THEN
            denom = vertices_y(j) - vertices_y(i)
            IF denom <> 0 THEN
                t_val = (test_y - vertices_y(i)) / denom
                IF test_x < vertices_x(i) + t_val * (vertices_x(j) - vertices_x(i)) THEN
                    inside_flag = 1 - inside_flag
                END IF
            END IF
        END IF
        i = i + 1
    WEND

    IF inside_flag = 1 THEN
        PRINT "Point (2.5,1.0) is INSIDE triangle"
    ELSE
        PRINT "Point (2.5,1.0) is OUTSIDE triangle"
    END IF

    test_x = 10.0
    test_y = 10.0
    inside_flag = 0
    i = 0
    WHILE i < vert_count
        j = i + 1
        IF j >= vert_count THEN j = 0
        IF vertices_y(i) > test_y AND vertices_y(j) <= test_y THEN
            denom = vertices_y(j) - vertices_y(i)
            IF denom <> 0 THEN
                t_val = (test_y - vertices_y(i)) / denom
                IF test_x < vertices_x(i) + t_val * (vertices_x(j) - vertices_x(i)) THEN
                    inside_flag = 1 - inside_flag
                END IF
            END IF
        ELSEIF vertices_y(j) > test_y AND vertices_y(i) <= test_y THEN
            denom = vertices_y(j) - vertices_y(i)
            IF denom <> 0 THEN
                t_val = (test_y - vertices_y(i)) / denom
                IF test_x < vertices_x(i) + t_val * (vertices_x(j) - vertices_x(i)) THEN
                    inside_flag = 1 - inside_flag
                END IF
            END IF
        END IF
        i = i + 1
    WEND

    IF inside_flag = 1 THEN
        PRINT "Point (10,10) is INSIDE triangle"
    ELSE
        PRINT "Point (10,10) is OUTSIDE triangle"
    END IF

    ' Distance from point to line
    p1_x = 0.0
    p1_y = 0.0
    p2_x = 4.0
    p2_y = 0.0
    test_x = 2.0
    test_y = 3.0

    denom = SQR((p2_x - p1_x) * (p2_x - p1_x) + (p2_y - p1_y) * (p2_y - p1_y))
    IF denom > 0 THEN
        dist_val = ((p2_x - p1_x) * (p1_y - test_y) - (p1_x - test_x) * (p2_y - p1_y)) / denom
        IF dist_val < 0 THEN
    dist_val = 0 - dist_val
END IF
        PRINT "Distance from (2,3) to line (0,0)-(4,0) = " + STR$(dist_val)
    END IF

    ' Closest point on segment
    p1_x = 0.0
    p1_y = 0.0
    p2_x = 4.0
    p2_y = 0.0
    test_x = 5.0
    test_y = 3.0

    denom = ((p2_x - p1_x) * (p2_x - p1_x) + (p2_y - p1_y) * (p2_y - p1_y))
    IF denom > 0 THEN
        t_val = ((test_x - p1_x) * (p2_x - p1_x) + (test_y - p1_y) * (p2_y - p1_y)) / denom
        IF t_val < 0 THEN t_val = 0
        IF t_val > 1 THEN t_val = 1
        intersect_x = p1_x + t_val * (p2_x - p1_x)
        intersect_y = p1_y + t_val * (p2_y - p1_y)
        PRINT "Closest point on segment = (" + STR$(intersect_x) + ", " + STR$(intersect_y) + ")"
        dist_val = SQR((test_x - intersect_x) * (test_x - intersect_x) + (test_y - intersect_y) * (test_y - intersect_y))
        PRINT "Distance = " + STR$(dist_val)
    END IF

    PRINT ""
    PRINT "2D geometry operations complete."

    FUNCTION = 0
END FUNCTION
