From 5993ea8f6fd4316aee313e1e6fdae2dc7fbab5df Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Wed, 3 Jun 2026 12:49:35 +0530 Subject: Add circle example --- examples/circle.sql | 18 ++++++++++++++++++ examples/gradient.sql | 8 ++++---- justfile | 3 +-- setup.sql | 7 +++++++ 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 examples/circle.sql diff --git a/examples/circle.sql b/examples/circle.sql new file mode 100644 index 0000000..46874cc --- /dev/null +++ b/examples/circle.sql @@ -0,0 +1,18 @@ +DELETE FROM images WHERE id = 'circle'; +INSERT INTO images (id, width, height) VALUES ('circle', 200, 200) RETURNING id; + +WITH RECURSIVE + image AS (SELECT * FROM images WHERE id = 'circle'), + circle AS (SELECT width/2 AS cx, height/2 AS cy, 80 AS radius FROM image), + horizontal(x) AS + (SELECT width FROM image UNION ALL SELECT x - 1 FROM horizontal WHERE x > 1), + vertical(y) AS + (SELECT height FROM image UNION ALL SELECT y - 1 FROM vertical WHERE y > 1), + _pixels(x, y, r, g, b) AS (SELECT + x, y, + 150, + 255 * MAX(0, MIN(1, (SELECT POW(x - cx, 2) + POW(y - cy, 2) - POW(radius, 2) FROM circle))), + 150 + FROM vertical, horizontal + ) +INSERT INTO pixels (image_id, x, y, r, g, b) SELECT 'circle', x, y, r, g, b FROM _pixels; diff --git a/examples/gradient.sql b/examples/gradient.sql index 9aa9010..f04cdad 100644 --- a/examples/gradient.sql +++ b/examples/gradient.sql @@ -1,12 +1,12 @@ +DELETE FROM images WHERE id = 'gradient'; INSERT INTO images (id, width, height) VALUES ('gradient', 200, 200) RETURNING id; WITH RECURSIVE - width AS (SELECT width FROM images WHERE id = 'gradient'), - height AS (SELECT height FROM images WHERE id = 'gradient'), + image AS (SELECT * FROM images WHERE id = 'gradient'), horizontal(x) AS - (SELECT * FROM width UNION ALL SELECT x - 1 FROM horizontal WHERE x > 1), + (SELECT width FROM image UNION ALL SELECT x - 1 FROM horizontal WHERE x > 1), vertical(y) AS - (SELECT * FROM height UNION ALL SELECT y - 1 FROM vertical WHERE y > 1), + (SELECT height FROM image UNION ALL SELECT y - 1 FROM vertical WHERE y > 1), _pixels(x, y, r, g, b) AS (SELECT x, y, mod(x, 255), mod(y, 255), 100 FROM vertical, horizontal) INSERT INTO pixels (image_id, x, y, r, g, b) SELECT 'gradient', x, y, r, g, b FROM _pixels; diff --git a/justfile b/justfile index b67c6f8..be55afb 100644 --- a/justfile +++ b/justfile @@ -12,8 +12,7 @@ image file *args: just show-image "$image_id" show-image image_id: - #!/usr/bin/env sh - echo "Displaying $image_id" 1>&2 + echo "Displaying {{image_id}}" 1>&2 ./image.sh {{image_id}} | magick display - repl *args: diff --git a/setup.sql b/setup.sql index d5d8ce2..7b6732e 100644 --- a/setup.sql +++ b/setup.sql @@ -18,3 +18,10 @@ CREATE TABLE pixels ( FOREIGN KEY(image_id) REFERENCES images(id), UNIQUE(image_id, x, y) ); + +CREATE TRIGGER delete_pixels_when_image_deleted +AFTER DELETE ON images +WHEN (SELECT COUNT(*) FROM images WHERE id = OLD.id) = 0 +BEGIN + DELETE FROM pixels WHERE image_id = OLD.id; +END; -- cgit v1.3.1