ref: a1eb886641fb413b2c2a30681a3f693fdaeb7fc6
parent: 4576943d222da4b6a95c898eea1a38d7e610393a
author: phil9 <[email protected]>
date: Wed Nov 30 02:19:12 EST 2022
implement more shapes square(x, y, w) rect(x, y, w, h) circle(x, y, r) ellipse(x, y, a, b) arc(x, y, a, b, c, d)
--- a/README.md
+++ b/README.md
@@ -12,14 +12,22 @@
Usage:
------
A slug program is written in lua and composed of two functions:
-- setup (not mandatory) which is called before the main draw loop.
-- draw which is called in a loop until program exits.
+- `setup()` (not mandatory) which is called before the main draw loop.
+- `draw()` which is called in a loop until program exits.
slug provides the following functions:
-- background(color) paint the background using given color (see colors)
-- stroke(color) change the shapes stroke color
-- strokeWidth(n) change the width of strokes
-- line(x1, y1, x2, y2) draw a line between points (x1,y1) and (x2, y2)
+- `background(color)` paint the background using given color (see colors)
+- `noStroke()` disable drawing shapes outline
+- `stroke(color)` change the shapes outline color
+- `strokeWidth(n)` change the width of outlines
+- `noFill()` disable filling shapes
+- `fill(color)` change the shapes fill color
+- `line(x1, y1, x2, y2)` draw a line between points (x1,y1) and (x2, y2)
+- `square(x, y, w)` draw a square with a top left corner at point (x,y) and size w
+- `rect(x, y, w, h)` draw a rectangle with a top left corner at point (x,y), width w and height h
+- `circle(x, y, r)` draw a circle centered on point (x,y) and a radius r
+- `ellipse(x, y, a, b)` draw an ellipse centered on point (x,y) with an horizontal radius of a and a vertical radius of b
+- `arc(x, y, a, b, c, d)` draw an arc centered on point (x,y) with an horizontal radius of a and a vertical radius of b. arc is like ellipse, but draws only that portion of the ellipse starting at angle c and extending through an angle of d
colors:
slug currently uses a 216 color palette based on web safe color palette.
--- a/slug.c
+++ b/slug.c
@@ -136,6 +136,102 @@
return 0;
}
+int
+csquare(lua_State *L)
+{
+ Rectangle r;
+ Point p1, p2;
+ int x, y, w;
+
+ x = luaL_checkinteger(L, 1);
+ y = luaL_checkinteger(L, 2);
+ w = luaL_checkinteger(L, 3);
+ p1 = addpt(screen->r.min, Pt(x, y));
+ p2 = addpt(p1, Pt(w, w));
+ r = Rpt(p1, p2);
+ if(!nofill)
+ draw(screen, r, fill, nil, ZP);
+ if(!nostroke)
+ border(screen, r, strokewidth, stroke, ZP);
+ return 0;
+}
+
+int
+crect(lua_State *L)
+{
+ Rectangle r;
+ Point p1, p2;
+ int x, y, w, h;
+
+ x = luaL_checkinteger(L, 1);
+ y = luaL_checkinteger(L, 2);
+ w = luaL_checkinteger(L, 3);
+ h = luaL_checkinteger(L, 4);
+ p1 = addpt(screen->r.min, Pt(x, y));
+ p2 = addpt(p1, Pt(w, h));
+ r = Rpt(p1, p2);
+ if(!nofill)
+ draw(screen, r, fill, nil, ZP);
+ if(!nostroke)
+ border(screen, r, strokewidth, stroke, ZP);
+ return 0;
+}
+
+int
+ccircle(lua_State *L)
+{
+ Point p;
+ int x, y, a;
+
+ x = luaL_checkinteger(L, 1);
+ y = luaL_checkinteger(L, 2);
+ a = luaL_checkinteger(L, 3);
+ p = addpt(screen->r.min, Pt(x, y));
+ if(!nofill)
+ fillellipse(screen, p, a, a, fill, ZP);
+ if(!nostroke)
+ ellipse(screen, p, a, a, strokewidth, stroke, ZP);
+ return 0;
+}
+
+int
+cellipse(lua_State *L)
+{
+ Point p;
+ int x, y, a, b;
+
+ x = luaL_checkinteger(L, 1);
+ y = luaL_checkinteger(L, 2);
+ a = luaL_checkinteger(L, 3);
+ b = luaL_checkinteger(L, 4);
+ p = addpt(screen->r.min, Pt(x, y));
+ if(!nofill)
+ fillellipse(screen, p, a, b, fill, ZP);
+ if(!nostroke)
+ ellipse(screen, p, a, b, strokewidth, stroke, ZP);
+ return 0;
+}
+
+int
+carc(lua_State *L)
+{
+ Point p;
+ int x, y, a, b, c, d;
+
+ x = luaL_checkinteger(L, 1);
+ y = luaL_checkinteger(L, 2);
+ a = luaL_checkinteger(L, 3);
+ b = luaL_checkinteger(L, 4);
+ c = luaL_checkinteger(L, 5);
+ d = luaL_checkinteger(L, 6);
+ p = addpt(screen->r.min, Pt(x, y));
+ if(!nofill)
+ fillarc(screen, p, a, b, fill, ZP, c, d);
+ if(!nostroke)
+ arc(screen, p, a, b, strokewidth, stroke, ZP, c, d);
+ return 0;
+}
+
void
initcontext(void)
{
@@ -163,6 +259,11 @@
registerfunc("noFill", cnofill);
registerfunc("fill", cfill);
registerfunc("line", cline);
+ registerfunc("square", csquare);
+ registerfunc("rect", crect);
+ registerfunc("circle", ccircle);
+ registerfunc("ellipse", cellipse);
+ registerfunc("arc", carc);
}
void