From 5333dc18382ecb0a2286712718ac3b4225fedf64 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 15 Apr 2024 21:40:17 +0100 Subject: implement stroke operation --- drawing/drawing.ha | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'drawing/drawing.ha') diff --git a/drawing/drawing.ha b/drawing/drawing.ha index 063577d..a7d9e1f 100644 --- a/drawing/drawing.ha +++ b/drawing/drawing.ha @@ -1,8 +1,7 @@ -use sdl2; use fmt; use io; use endian; -use math::random; +use math; export def CHUNKSIZE = 512; @@ -53,6 +52,8 @@ export fn outline_picture(pic: *picture) void = { fn min(a: i32, b: i32) i32 = if (a 0) 1 else 0; +fn abs(x: i32) i32 = if (x<0) -x else x; // Draws a circle onto the picture, at given position radius color // Clips at the boundaries of the picture to avoid overflow. @@ -98,6 +99,23 @@ export fn circle_hollow(picture: *picture, c: pos, r: i32, color: u32) void = { }; }; }; + +export fn stroke(picture: *picture, c0: pos, c1: pos, r: i32, color: u32) void = { + const dx = c1.0 - c0.0; + const dy = c1.1 - c0.1; + const count = (abs(dx)+abs(dy))/r; + const sx = dx: f64 / (count-1): f64; + const sy = dy: f64 / (count-1): f64; + + for (let i = 0i32; i < count; i += 1) { + const cx = c0.0 + math::roundf64(i:f64*sx):i32; + const cy = c0.1 + math::roundf64(i:f64*sy):i32; + + circle(picture, (cx, cy), r, color); + }; +}; + +// ehh should check bounding box instead of doing all pictures maybe export fn perform(pictures: []picture, op: op) void = { match (op) { case let o: op_circle => @@ -109,5 +127,12 @@ export fn perform(pictures: []picture, op: op) void = { (x - pic.world_pos.0, y - pic.world_pos.1): pos; circle(pic, pos_within_pic, r: i32, c); }; + case let s: op_stroke => + const col = s.color & 0xffffff; + for (const pic &.. pictures) { + const c0 = (s.pos0.0 - pic.world_pos.0, s.pos0.1 - pic.world_pos.1); + const c1 = (s.pos1.0 - pic.world_pos.0, s.pos1.1 - pic.world_pos.1); + stroke(pic, c0, c1, s.radius: i32, col); + }; }; }; -- cgit v1.2.3