use crate::solver::Region;
use crate::style::Color;
pub trait SurfacePainter {
fn bounds(&self) -> Region;
fn clip(&self) -> Region;
fn blend_pixel(&mut self, px: i16, py: i16, color: Color, alpha: u8);
fn draw_text(&mut self, bounds: Region, text: &str, color: Color, font_size: u8);
}
pub const COS_Q10: [i16; 91] = [
1024, 1024, 1023, 1023, 1022, 1020, 1018, 1016, 1014, 1011, 1008, 1005, 1002, 998, 994, 989,
984, 979, 974, 968, 962, 956, 949, 943, 935, 928, 920, 912, 904, 896, 887, 878, 868, 859, 849,
839, 828, 818, 807, 796, 784, 773, 761, 749, 737, 724, 711, 698, 685, 672, 658, 644, 630, 616,
602, 587, 573, 558, 543, 527, 512, 496, 481, 465, 449, 433, 416, 400, 384, 367, 350, 333, 316,
299, 282, 265, 248, 230, 213, 195, 178, 160, 143, 125, 107, 89, 71, 54, 36, 18, 0,
];
#[inline(always)]
pub fn sin_cos_deg10(deg10: i32) -> (i32, i32) {
let mut d = deg10 % 3600;
if d < 0 {
d += 3600;
}
let deg = (d / 10) as usize;
let frac = d % 10;
let cos_interp = |a: usize, frac_part: i32| -> i32 {
let q = (a / 90) % 4;
let idx = a % 90;
let lookup = |ang: usize| -> i32 {
match q {
0 => COS_Q10[ang] as i32,
1 => -(COS_Q10[90 - ang] as i32),
2 => -(COS_Q10[ang] as i32),
_ => COS_Q10[90 - ang] as i32,
}
};
let c0 = lookup(idx);
if frac_part == 0 {
return c0;
}
let next_a = (a + 1) % 360;
let nq = (next_a / 90) % 4;
let nidx = next_a % 90;
let c1 = match nq {
0 => COS_Q10[nidx] as i32,
1 => -(COS_Q10[90 - nidx] as i32),
2 => -(COS_Q10[nidx] as i32),
_ => COS_Q10[90 - nidx] as i32,
};
c0 + ((c1 - c0) * frac_part) / 10
};
let cos_v = cos_interp(deg, frac);
let sin_deg = (deg + 270) % 360;
let sin_v = cos_interp(sin_deg, frac);
(sin_v, cos_v)
}
#[inline(always)]
pub fn dir_from_deg(deg10: u16) -> (i32, i32) {
let (sin_v, cos_v) = sin_cos_deg10(deg10 as i32);
(sin_v, -cos_v)
}
#[inline(always)]
pub fn isqrt_i32(n: i32) -> i32 {
if n <= 0 {
return 0;
}
let mut x = n as u32;
let mut res = 0u32;
let mut bit = 1u32 << 30;
while bit > x {
bit >>= 2;
}
while bit != 0 {
if x >= res + bit {
x -= res + bit;
res = (res >> 1) + bit;
} else {
res >>= 1;
}
bit >>= 2;
}
res as i32
}
#[inline(always)]
pub const fn isqrt_i64(n: i64) -> i32 {
if n <= 0 {
return 0;
}
let mut x = n as u64;
let mut res = 0u64;
let mut bit = 1u64 << 62;
while bit > x {
bit >>= 2;
}
while bit != 0 {
if x >= res + bit {
x -= res + bit;
res = (res >> 1) + bit;
} else {
res >>= 1;
}
bit >>= 2;
}
if res > i32::MAX as u64 {
i32::MAX
} else {
res as i32
}
}
#[inline(always)]
pub fn dist_to_segment(px: i32, py: i32, ax: i32, ay: i32, bx: i32, by: i32) -> i32 {
let vx = (bx - ax) as i64;
let vy = (by - ay) as i64;
let wx = (px - ax) as i64;
let wy = (py - ay) as i64;
let c1 = wx * vx + wy * vy;
if c1 <= 0 {
return isqrt_i64(wx * wx + wy * wy);
}
let c2 = vx * vx + vy * vy;
if c2 <= c1 {
let dx = (px - bx) as i64;
let dy = (py - by) as i64;
return isqrt_i64(dx * dx + dy * dy);
}
let proj_x = (ax as i64) + (vx * c1) / c2;
let proj_y = (ay as i64) + (vy * c1) / c2;
let dx = (px as i64) - proj_x;
let dy = (py as i64) - proj_y;
isqrt_i64(dx * dx + dy * dy)
}
#[inline(always)]
pub fn norm_sdf_to_alpha(sd_norm: i32, inv_px_norm_8: i32) -> u8 {
let band = inv_px_norm_8.max(4);
if sd_norm <= -band {
255
} else if sd_norm >= band {
0
} else {
(((band - sd_norm) * 255) / (band * 2)) as u8
}
}
#[inline(always)]
pub fn blend_color(dst: Color, src: Color, alpha: u8) -> Color {
if alpha == 0 {
return dst;
}
if alpha == 255 {
return src;
}
let a = alpha as u16;
let inv = 255 - a;
Color::rgb(
((src.r() as u16 * a + dst.r() as u16 * inv) / 255) as u8,
((src.g() as u16 * a + dst.g() as u16 * inv) / 255) as u8,
((src.b() as u16 * a + dst.b() as u16 * inv) / 255) as u8,
)
}
#[inline(always)]
pub fn dist_to_arc(
nx: i32,
ny: i32,
r_dist: i32,
r_arc: i32,
half_thick: i32,
n0_x: i32,
n0_y: i32,
cos_half_span_q10: i32,
cap_a: (i32, i32),
cap_b: (i32, i32),
) -> i32 {
if r_dist == 0 {
return r_arc;
}
let dot_q10 = (nx * n0_x + ny * n0_y) / r_dist;
if dot_q10 >= cos_half_span_q10 {
(r_dist - r_arc).abs() - half_thick
} else {
let da = isqrt_i32((nx - cap_a.0) * (nx - cap_a.0) + (ny - cap_a.1) * (ny - cap_a.1));
let db = isqrt_i32((nx - cap_b.0) * (nx - cap_b.0) + (ny - cap_b.1) * (ny - cap_b.1));
da.min(db) - half_thick
}
}
#[inline(always)]
pub fn sdf_uppercase_char(ch: u8, lx: i32, ly: i32) -> i32 {
if lx.abs() > 26 || ly.abs() > 34 {
return 1000;
}
let w = 4;
match ch {
b'O' => {
let cy = ly.clamp(-9, 9);
(isqrt_i32(lx * lx + (ly - cy) * (ly - cy)) - 13).abs() - w
}
b'P' => {
let d1 = dist_to_segment(lx, ly, -12, -22, -12, 22);
let d2 = dist_to_segment(lx, ly, -12, -22, 1, -22);
let d3 = dist_to_segment(lx, ly, -12, 1, 1, 1);
let d_loop = if lx >= 1 {
(isqrt_i32((lx - 1) * (lx - 1) + (ly + 10) * (ly + 10)) - 11).abs()
} else {
1000
};
d1.min(d2).min(d3).min(d_loop) - w
}
b'R' => {
let dp = sdf_uppercase_char(b'P', lx, ly) + w;
let d_leg = dist_to_segment(lx, ly, 0, 1, 13, 22);
dp.min(d_leg) - w
}
b'T' => {
let d1 = dist_to_segment(lx, ly, -14, -22, 14, -22);
let d2 = dist_to_segment(lx, ly, 0, -22, 0, 22);
d1.min(d2) - w
}
b'E' => {
let d1 = dist_to_segment(lx, ly, -12, -22, -12, 22);
let d2 = dist_to_segment(lx, ly, -12, -22, 12, -22);
let d3 = dist_to_segment(lx, ly, -12, 0, 9, 0);
let d4 = dist_to_segment(lx, ly, -12, 22, 12, 22);
d1.min(d2).min(d3).min(d4) - w
}
b'M' => {
let d1 = dist_to_segment(lx, ly, -14, 22, -13, -22);
let d2 = dist_to_segment(lx, ly, -13, -22, 0, 10);
let d3 = dist_to_segment(lx, ly, 0, 10, 13, -22);
let d4 = dist_to_segment(lx, ly, 13, -22, 14, 22);
d1.min(d2).min(d3).min(d4) - w
}
b'A' => {
let d1 = dist_to_segment(lx, ly, -14, 22, 0, -22);
let d2 = dist_to_segment(lx, ly, 0, -22, 14, 22);
let d3 = dist_to_segment(lx, ly, -9, 7, 9, 7);
d1.min(d2).min(d3) - w
}
b'G' => {
let cy = ly.clamp(-9, 9);
let d_ring = if lx > 3 && (-8..=3).contains(&ly) {
1000
} else {
(isqrt_i32(lx * lx + (ly - cy) * (ly - cy)) - 13).abs()
};
let d_bar = dist_to_segment(lx, ly, 2, 3, 13, 3);
let d_stem = dist_to_segment(lx, ly, 13, 3, 13, 16);
d_ring.min(d_bar).min(d_stem) - w
}
b'S' => {
let d1 = dist_to_segment(lx, ly, 11, -18, -6, -22);
let d2 = dist_to_segment(lx, ly, -6, -22, -12, -12);
let d3 = dist_to_segment(lx, ly, -12, -12, 12, 8);
let d4 = dist_to_segment(lx, ly, 12, 8, 8, 21);
let d5 = dist_to_segment(lx, ly, 8, 21, -11, 18);
d1.min(d2).min(d3).min(d4).min(d5) - w
}
_ => 1000,
}
}