' Coordinate Point — 2D point and vector operations using parallel arrays
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL pointCount AS LONG
    LOCAL x_coords(1000) AS LONG
    LOCAL y_coords(1000) AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL n AS LONG
    LOCAL dx AS LONG
    LOCAL dy AS LONG
    LOCAL distanceSq AS LONG
    LOCAL distance AS LONG
    LOCAL sumX AS LONG
    LOCAL sumY AS LONG
    LOCAL avgX AS LONG
    LOCAL avgY AS LONG
    LOCAL dotProd AS LONG
    LOCAL crossProd AS LONG
    LOCAL minDistIdx AS LONG
    LOCAL minDistVal AS LONG
    LOCAL testCount AS LONG
    LOCAL passCount AS LONG
    LOCAL failCount AS LONG
    LOCAL lineStr AS STRING
    LOCAL coordStr AS STRING
    testCount = 0
    passCount = 0
    failCount = 0
    PRINT "============================================"
    PRINT "       COORDINATE POINT SYSTEM"
    PRINT "============================================"
    PRINT ""
    PRINT "Creating a set of 2D points"
    PRINT ""
    pointCount = 20
    FOR i = 0 TO pointCount - 1
        x_coords(i) = (i * 7 + 3) MOD 50
        y_coords(i) = (i * 13 + 7) MOD 40
    NEXT i
    FOR i = 0 TO pointCount - 1
        coordStr = "(" + STR$(x_coords(i)) + ", " + STR$(y_coords(i)) + ")"
        PRINT " Point " + STR$(i) + ": " + coordStr
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "      DISTANCE CALCULATIONS"
    PRINT "============================================"
    PRINT ""
    PRINT "Distances from origin (0,0):"
    PRINT ""
    FOR i = 0 TO pointCount - 1
        dx = x_coords(i)
        dy = y_coords(i)
        IF dx < 0 THEN
            dx = 0 - dx
        END IF
        IF dy < 0 THEN
            dy = 0 - dy
        END IF
        distanceSq = dx * dx + dy * dy
        distance = 0
        n = 0
        DO WHILE n * n <= distanceSq
            distance = n
            n = n + 1
        LOOP
        PRINT " d(0," + STR$(i) + ") = " + STR$(distance)
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "      DISTANCE BETWEEN POINTS"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO 4
        FOR j = 0 TO 4
            dx = x_coords(i) - x_coords(j)
            dy = y_coords(i) - y_coords(j)
            IF dx < 0 THEN
                dx = 0 - dx
            END IF
            IF dy < 0 THEN
                dy = 0 - dy
            END IF
            distanceSq = dx * dx + dy * dy
            distance = 0
            n = 0
            DO WHILE n * n <= distanceSq
                distance = n
                n = n + 1
            LOOP
            PRINT " d(P" + STR$(i) + ",P" + STR$(j) + ") = " + STR$(distance)
        NEXT j
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "      CENTROID (average of points)"
    PRINT "============================================"
    PRINT ""
    sumX = 0
    sumY = 0
    FOR i = 0 TO pointCount - 1
        sumX = sumX + x_coords(i)
        sumY = sumY + y_coords(i)
    NEXT i
    avgX = sumX / pointCount
    avgY = sumY / pointCount
    PRINT " Centroid: (" + STR$(avgX) + ", " + STR$(avgY) + ")"
    PRINT ""
    PRINT "============================================"
    PRINT "      NEAREST NEIGHBOR"
    PRINT "============================================"
    PRINT ""
    LOCAL targetX AS LONG
    LOCAL targetY AS LONG
    targetX = 25
    targetY = 20
    minDistIdx = 0
    dx = x_coords(0) - targetX
    dy = y_coords(0) - targetY
    IF dx < 0 THEN
        dx = 0 - dx
    END IF
    IF dy < 0 THEN
        dy = 0 - dy
    END IF
    minDistVal = dx * dx + dy * dy
    FOR i = 1 TO pointCount - 1
        dx = x_coords(i) - targetX
        dy = y_coords(i) - targetY
        IF dx < 0 THEN
            dx = 0 - dx
        END IF
        IF dy < 0 THEN
            dy = 0 - dy
        END IF
        distanceSq = dx * dx + dy * dy
        IF distanceSq < minDistVal THEN
            minDistVal = distanceSq
            minDistIdx = i
        END IF
    NEXT i
    PRINT " Nearest to (" + STR$(targetX) + ", " + STR$(targetY) + "):"
    PRINT " Point " + STR$(minDistIdx) + " at (" + STR$(x_coords(minDistIdx)) + ", " + STR$(y_coords(minDistIdx)) + ")"
    PRINT ""
    PRINT "============================================"
    PRINT "      DOT PRODUCT"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO 4
        FOR j = 0 TO 4
            dotProd = x_coords(i) * x_coords(j) + y_coords(i) * y_coords(j)
            PRINT " P" + STR$(i) + " . P" + STR$(j) + " = " + STR$(dotProd)
        NEXT j
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "      TRANSLATION (shift all points)"
    PRINT "============================================"
    PRINT ""
    LOCAL shiftX AS LONG
    LOCAL shiftY AS LONG
    shiftX = 10
    shiftY = 5
    PRINT " Translating by (" + STR$(shiftX) + ", " + STR$(shiftY) + ")"
    PRINT ""
    FOR i = 0 TO pointCount - 1
        x_coords(i) = x_coords(i) + shiftX
        y_coords(i) = y_coords(i) + shiftY
    NEXT i
    FOR i = 0 TO pointCount - 1
        coordStr = "(" + STR$(x_coords(i)) + ", " + STR$(y_coords(i)) + ")"
        PRINT "  Point " + STR$(i) + ": " + coordStr
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "      BOUNDING BOX"
    PRINT "============================================"
    PRINT ""
    LOCAL minX AS LONG
    LOCAL minY AS LONG
    LOCAL maxX AS LONG
    LOCAL maxY AS LONG
    minX = x_coords(0)
    maxX = x_coords(0)
    minY = y_coords(0)
    maxY = y_coords(0)
    FOR i = 1 TO pointCount - 1
        IF x_coords(i) < minX THEN
            minX = x_coords(i)
        END IF
        IF x_coords(i) > maxX THEN
            maxX = x_coords(i)
        END IF
        IF y_coords(i) < minY THEN
            minY = y_coords(i)
        END IF
        IF y_coords(i) > maxY THEN
            maxY = y_coords(i)
        END IF
    NEXT i
    PRINT " Bounding box: (" + STR$(minX) + ", " + STR$(minY) + ") to (" + STR$(maxX) + ", " + STR$(maxY) + ")"
    dx = maxX - minX
    dy = maxY - minY
    IF dx < 0 THEN
        dx = 0 - dx
    END IF
    IF dy < 0 THEN
        dy = 0 - dy
    END IF
    PRINT " Width: " + STR$(dx) + ", Height: " + STR$(dy)
    PRINT ""
    PRINT "Coordinate operations complete"
    FUNCTION = 0
END FUNCTION
