Толщина круга

This commit is contained in:
2026-02-26 00:32:59 +03:00
parent f8731bde87
commit faf79367f6
2 changed files with 14 additions and 4 deletions

View File

@@ -6,14 +6,24 @@ const Color = @import("dvui").Color;
const Object = Document.Object;
const default_stroke: Color.PMA = .{ .r = 0, .g = 0, .b = 0, .a = 255 };
const default_thickness: f32 = 2.0;
/// Эллипс с центром (0,0) и полуосями radii (обводка).
/// Эллипс с центром (0,0) и полуосями radii (обводка с учётом thickness).
pub fn draw(ctx: *DrawContext, obj: *const Object) void {
const r_prop = obj.getProperty(.radii) orelse return;
const rx = r_prop.radii.x;
const ry = r_prop.radii.y;
if (rx <= 0 or ry <= 0) return;
const stroke = if (obj.getProperty(.stroke_rgba)) |s| pipeline.rgbaToPma(s.stroke_rgba) else default_stroke;
const thickness = if (obj.getProperty(.thickness)) |t| t.thickness else default_thickness;
// Полуширина обводки в нормализованных единицах (d = (x/rx)² + (y/ry)², граница при d=1).
const min_r = @min(rx, ry);
const half_norm = thickness / (2.0 * min_r);
const inner = @max(0.0, 1.0 - half_norm);
const outer = 1.0 + half_norm;
const d_inner_sq = inner * inner;
const d_outer_sq = outer * outer;
const corners = [_]struct { x: f32, y: f32 }{
.{ .x = -rx, .y = -ry },
@@ -51,7 +61,7 @@ pub fn draw(ctx: *DrawContext, obj: *const Object) void {
const nx = loc.x / rx;
const ny = loc.y / ry;
const d = nx * nx + ny * ny;
if (d >= 0.9 and d <= 1.1) {
if (d >= d_inner_sq and d <= d_outer_sq) {
ctx.blendPixelAtBuffer(@intCast(bx), @intCast(by), stroke);
}
}