!  RADAR.BAS
    !  Hobby BASIC Interpreter
    !  Part of the Hobby BASIC examples collection.
    !   This program draws a continuously rotating radar sweep line in the console window.
    !   The line rotates around the center of the window with a slightly smaller radius
    !   to avoid touching the edges. Each full rotation, a new random color is selected.
    !   Floating-point arithmetic (FPU) is used to compute sine, cosine, and rotation steps.
    title "Implements a rotating radar sweep with dynamic color changes"
    view 11
    screen 80, 30, 0
    cursor 0
    color 0, 7
    cls
    ! Get console device context
    syscall "Kernel32.dll", "GetConsoleWindow"
    hwnd = V0
    syscall "User32.dll", "GetDC", hwnd
    hdc = V0
    ! Get window dimensions
    dim rect[4].ZERO
    syscall "User32.dll", "GetWindowRect", hwnd, byref rect[]
    width = rect[2] - rect[0]
    height = rect[3] - rect[1]
    !  Center coordinates and radius
    cx$ = FROUND(FDIV(width, 2), "NEAREST")        ! X center
    cy$ = FROUND(FDIV(height, 2), "NEAREST")       ! Y center
    radius$ = FROUND(FMUL(FDIV(height, 2), "0.8"),"INT")  ! slightly smaller radius
    angle$ = "0.0"       ! current rotation angle in radians
    delta$ = "0.1"       ! rotation increment per frame
    col = 0xFFFFFF       ! initial pen color (white)
    do
        ! Pick new random color after full rotation
        if FCOMP(angle$, "6.283185") > 0
            angle$ = FSUB(angle$, "6.283185")
            col = VAL("0x" + GEN(6,"0123456789ABCDEF"))
        endif
        ! Create pen and select it for drawing
        thick = 2
        syscall "Gdi32.dll", "CreatePen", 0, thick, col
        hpen = V0
        syscall "Gdi32.dll", "SelectObject", hdc, hpen
        ! Compute end point of radar line
        x$ = FADD(cx$, FMUL(radius$, FCOS(angle$)))
        y$ = FADD(cy$, FMUL(radius$, FSIN(angle$)))
        ! Draw radar line from center to computed end point
        x = VAL(FROUND(cx$, "INT"))
        y = VAL(FROUND(cy$, "INT"))
        syscall "Gdi32.dll", "MoveToEx", hdc, x, y,  0
        x = VAL(FROUND(x$, "INT"))
        y = VAL(FROUND(y$, "INT"))
        syscall "Gdi32.dll", "LineTo", hdc, x, y
        wait 10
        angle$ = FADD(angle$, delta$)   ! increment angle
    until KEY(27)
    ! Release DC and exit
    syscall "User32.dll", "ReleaseDC", hwnd, hdc
    cls : end