aboutsummaryrefslogtreecommitdiff
path: root/drawing/drawing.ha
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-04-15 21:40:17 +0100
committerubq323 <ubq323@ubq323.website>2024-04-15 21:40:17 +0100
commit5333dc18382ecb0a2286712718ac3b4225fedf64 (patch)
tree4d1541a9c59c12bf59e552c49f425055f7671593 /drawing/drawing.ha
parentd3eaceebe43cbb6b85125f73fbef3cf2241fe452 (diff)
implement stroke operation
Diffstat (limited to 'drawing/drawing.ha')
-rw-r--r--drawing/drawing.ha29
1 files changed, 27 insertions, 2 deletions
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<b) a else b;
fn max(a: i32, b: i32) i32 = if (a<b) b else a;
+fn sign(x: i32) i32 = if (x < 0) -1 else if (x > 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);
+ };
};
};