' Geometry shapes: point, line, circle, rectangle with area/perimeter calculations
#COMPILE EXE
#DIM ALL

TYPE Point2D
    x AS DOUBLE
    y AS DOUBLE
END TYPE

TYPE Line2D
    x1 AS DOUBLE
    y1 AS DOUBLE
    x2 AS DOUBLE
    y2 AS DOUBLE
END TYPE

TYPE Circle2D
    cx AS DOUBLE
    cy AS DOUBLE
    radius AS DOUBLE
END TYPE

TYPE Rectangle2D
    x AS DOUBLE
    y AS DOUBLE
    w AS DOUBLE
    h AS DOUBLE
END TYPE

FUNCTION PBMAIN() AS LONG
    LOCAL p1  AS Point2D
    LOCAL p2  AS Point2D
    LOCAL p3  AS Point2D
    LOCAL p4  AS Point2D
    LOCAL line1 AS Line2D
    LOCAL c1  AS Circle2D
    LOCAL c2  AS Circle2D
    LOCAL r1 AS Rectangle2D
    LOCAL dx  AS DOUBLE
    LOCAL dy  AS DOUBLE
    LOCAL dist  AS DOUBLE
    LOCAL perimeter  AS DOUBLE
    LOCAL area  AS DOUBLE
    LOCAL lineLen  AS DOUBLE
    LOCAL slope  AS DOUBLE
    LOCAL intersectX  AS DOUBLE
    LOCAL intersectY  AS DOUBLE
    LOCAL rectPerim  AS DOUBLE
    LOCAL rectArea  AS DOUBLE
    LOCAL circleCirc  AS DOUBLE
    LOCAL circleArea  AS DOUBLE
    LOCAL overlap AS LONG

    PRINT "=== Geometry Shapes ==="
    PRINT

    p1.x = 0.0
    p1.y = 0.0
    p2.x = 3.0
    p2.y = 4.0
    PRINT "Point 1: (" & FORMAT$(p1.x) & ", " & FORMAT$(p1.y) & ")"
    PRINT "Point 2: (" & FORMAT$(p2.x) & ", " & FORMAT$(p2.y) & ")"
    dx = p2.x - p1.x
    dy = p2.y - p1.y
    dist = SQR(dx * dx + dy * dy)
    PRINT "Distance between points: " & FORMAT$(dist)
    PRINT

    line1.x1 = 1.0
    line1.y1 = 1.0
    line1.x2 = 5.0
    line1.y2 = 5.0
    PRINT "Line: (" & FORMAT$(line1.x1) & "," & FORMAT$(line1.y1) & ") to (" & FORMAT$(line1.x2) & "," & FORMAT$(line1.y2) & ")"
    dx = line1.x2 - line1.x1
    dy = line1.y2 - line1.y1
    lineLen = SQR(dx * dx + dy * dy)
    PRINT "Line length: " & FORMAT$(lineLen)
    IF dx > 0.0 OR dx < 0.0 THEN
        slope = dy / dx
        PRINT "Slope: " & FORMAT$(slope)
    END IF
    PRINT

    c1.cx = 0.0
    c1.cy = 0.0
    c1.radius = 5.0
    PRINT "Circle 1: center(" & FORMAT$(c1.cx) & "," & FORMAT$(c1.cy) & "), radius " & FORMAT$(c1.radius)
    circleCirc = 2.0 * 3.14159265 * c1.radius
    circleArea = 3.14159265 * c1.radius * c1.radius
    PRINT "  Circumference: " & FORMAT$(circleCirc)
    PRINT "  Area: " & FORMAT$(circleArea)
    PRINT

    c2.cx = 3.0
    c2.cy = 4.0
    c2.radius = 3.0
    PRINT "Circle 2: center(" & FORMAT$(c2.cx) & "," & FORMAT$(c2.cy) & "), radius " & FORMAT$(c2.radius)
    dx = c2.cx - c1.cx
    dy = c2.cy - c1.cy
    dist = SQR(dx * dx + dy * dy)
    IF dist <= c1.radius + c2.radius THEN
        overlap = 1
        PRINT "  Circles OVERLAP (distance: " & FORMAT$(dist) & ")"
    ELSE
        overlap = 0
        PRINT "  Circles do NOT overlap (distance: " & FORMAT$(dist) & ")"
    END IF
    PRINT

    r1.x = 2.0
    r1.y = 3.0
    r1.w = 8.0
    r1.h = 5.0
    PRINT "Rectangle: (" & FORMAT$(r1.x) & "," & FORMAT$(r1.y) & "), " & FORMAT$(r1.w) & " x " & FORMAT$(r1.h)
    rectPerim = 2.0 * (r1.w + r1.h)
    rectArea = r1.w * r1.h
    PRINT "  Perimeter: " & FORMAT$(rectPerim)
    PRINT "  Area: " & FORMAT$(rectArea)
    PRINT

    PRINT "--- 3D distance via repeated 2D ---"
    p3.x = 1.0
    p3.y = 2.0
    p4.x = 4.0
    p4.y = 6.0
    dx = p4.x - p3.x
    dy = p4.y - p3.y
    dist = SQR(dx * dx + dy * dy)
    PRINT "Distance P3(" & FORMAT$(p3.x) & "," & FORMAT$(p3.y) & ") to P4(" & FORMAT$(p4.x) & "," & FORMAT$(p4.y) & "): " & FORMAT$(dist)
    PRINT

    PRINT "--- Midpoint calculation ---"
    p3.x = (line1.x1 + line1.x2) / 2.0
    p3.y = (line1.y1 + line1.y2) / 2.0
    PRINT "Midpoint of line: (" & FORMAT$(p3.x) & ", " & FORMAT$(p3.y) & ")"
    PRINT

    PRINT "--- Point-in-circle test ---"
    p3.x = 2.0
    p3.y = 0.0
    dx = p3.x - c1.cx
    dy = p3.y - c1.cy
    dist = SQR(dx * dx + dy * dy)
    IF dist <= c1.radius THEN
        PRINT "Point (2,0) is INSIDE circle 1"
    ELSE
        PRINT "Point (2,0) is OUTSIDE circle 1"
    END IF
    PRINT

    PRINT "--- Point-in-rectangle test ---"
    p3.x = 5.0
    p3.y = 4.0
    IF p3.x >= r1.x AND p3.x <= r1.x + r1.w AND p3.y >= r1.y AND p3.y <= r1.y + r1.h THEN
        PRINT "Point (5,4) is INSIDE rectangle"
    ELSE
        PRINT "Point (5,4) is OUTSIDE rectangle"
    END IF
    PRINT

    PRINT "SIZEOF(Point2D):" & STR$(SIZEOF(p1))
    PRINT "SIZEOF(Circle2D):" & STR$(SIZEOF(c1))
    PRINT "SIZEOF(Rectangle2D):" & STR$(SIZEOF(r1))
    PRINT "Geometry calculations complete."

    FUNCTION = 0
END FUNCTION
