# CelerisLab/preprocess.py

import math
import numpy as np
from typing import Tuple

FLUID = 0b00000001
SOLID = 0b00000010
GAS = 0b00000100
INTERFACE = 0b00001000
SENSOR = 0b00010000


def find_circle_intersection(x, y, x_neb, y_neb, xc, yc, r0):
    dx, dy = x_neb - x, y_neb - y
    a = dx ** 2 + dy ** 2
    b = 2 * (dx * (x - xc) + dy * (y - yc))
    c = (x - xc) ** 2 + (y - yc) ** 2 - r0 ** 2
    det = b ** 2 - 4 * a * c

    if det < 0:
        return None

    t1 = (-b + math.sqrt(det)) / (2 * a)
    t2 = (-b - math.sqrt(det)) / (2 * a)

    if 0 <= t1 <= 1:
        return x + t1 * dx, y + t1 * dy
    elif 0 <= t2 <= 1:
        return x + t2 * dx, y + t2 * dy
    else:
        return None 
    
def find_sensor_area(radius):
    area = 0
    for i in range(np.floor(-radius), np.ceil(radius)):
        for j in range(np.floor(-radius), np.ceil(radius)):
            if i ** 2 + j ** 2 <= radius ** 2:
                area += 1
    return area