From 2fe3f34effacc6efccf1fcf9cccee723fa66bdf1 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Tue, 10 Dec 2019 10:52:57 +0000 Subject: [PATCH] Graphics: new g.fillPoly to handle irregular polygons - fix previous issues with duplicate points --- ChangeLog | 1 + libs/graphics/graphics.c | 5 +- tests/test_graphics_fillPoly2.js | 131 +++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 tests/test_graphics_fillPoly2.js diff --git a/ChangeLog b/ChangeLog index 88bb042fa..6acfee12e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -57,6 +57,7 @@ Bangle.js: add 'null' LCD mode to stop apps from drawing Bangle.js: add Bangle.getLCDMode Bangle.js: skip firmware version checks to save some bytes in bootloader + Graphics: new g.fillPoly to handle irregular polygons 2v04 : Allow \1..\9 escape codes in RegExp ESP8266: reading storage is not working for boot from user2 (fix #1507) diff --git a/libs/graphics/graphics.c b/libs/graphics/graphics.c index 408a45f7d..c1ca1da3d 100644 --- a/libs/graphics/graphics.c +++ b/libs/graphics/graphics.c @@ -445,8 +445,9 @@ void graphicsFillPoly(JsGraphics *gfx, int points, short *vertices) { // work out all the times lines cross the scanline j = points-1; for (i=0;i=y) || - (v[j].y<=y && v[i].y>=y)) { + if ((y==miny && (v[i].y==y || v[j].y==y)) || // special-case top line + (v[i].y=y) || + (v[j].y=y)) { if (crosscnt < MAX_CROSSES) { int l = v[j].y - v[i].y; if (l) { // don't do horiz lines - rely on the ends of the lines that join onto them diff --git a/tests/test_graphics_fillPoly2.js b/tests/test_graphics_fillPoly2.js new file mode 100644 index 000000000..76c1e89ec --- /dev/null +++ b/tests/test_graphics_fillPoly2.js @@ -0,0 +1,131 @@ +var g = Graphics.createArrayBuffer(100,64,8); +g.dump = _=>{ + var s = ""; + var b = new Uint8Array(g.buffer); + var n = 0; + for (var y=0;y{ + print("`"+g.dump()+"`"); +} +var ok = true; +function SHOULD_BE(a) { + var b = g.dump(); + if (a!=b) { + console.log("GOT :"+b+"\nSHOULD BE:"+a+"\n================"); + ok = false; + } +} + +g.clear(); +// test code from http://forum.espruino.com/conversations/341492/#comment15018442 +var vs2 = function(x,y,x2,y2,p) { // vertices for beveled, chk box like shape + // 4 'beveled corners' = 8 corners by 0|p inset combinations for x/y corners + return [x,y+p, x+p,y, x2-p,y, x2,y+p, x2,y2-p, x2-p,y2, x+p,y2, x,y2-p]; }; +var c0 = 0, c1 = 255; +g.clear(); +g.setColor(c0); +g.fillRect(0,0,127,63); // canvas (color 0) +`beveled squares and rectangles drawn and filled with polygon (color 0)` +g.setColor(c1); +g.fillRect(8,8,40,53); // frame filled (color 1) +g.setColor(c0); +g.drawPoly(vs2(10,10,18,18,1),1); // 9x 9 draw beveled [1] for x and y +g.drawPoly(vs2(21,10,38,18,1),1); // 9x19 draw beveled [1] for x and y +g.fillPoly(vs2(10,21,18,29,1),1); // 9x 9 fill beveled [1] for x and y +g.fillPoly(vs2(21,21,38,29,1),1); // 9x19 fill beveled [1] for x and y +g.drawPoly(vs2(10,32,18,40,2),1); // 9x 9 draw beveled [2] for x and y +g.drawPoly(vs2(21,32,38,40,2),1); // 9x19 draw beveled [2] for x and y +g.fillPoly(vs2(10,43,18,51,2),1); // 9x 9 fill beveled [2] for x and y +g.fillPoly(vs2(21,43,38,51,2),1); // 9x19 fill beveled [2] for x and y +`scalable beveled squares w/ borders done with two stacked, filled polygons` +g.setColor(c1); +g.fillRect(45,8,57,53); // frame filled (color 1) +g.setColor(c0); +g.fillPoly(vs2(47,21,55,29,1),1); // bot bev'd [1] square separate (color 0) +g.fillPoly(vs2(47,43,55,51,1),2); // bot bev'd [2] square separate (color 0) +g.setColor(c1); +g.drawRect(62,8,74,53); // frame hallow (0 from canvas) +g.setColor(c1); +g.fillPoly(vs2(65,22,71,28,1),1); // top bev'd [1] square separate (color 1) +g.fillPoly(vs2(65,44,71,50,1),2); // top bev'd [2] square separate (color 1) +g.setColor(c1); +g.fillRect(79,8,91,53); // frame filled (color 1) +g.setColor(c0); +g.fillPoly(vs2(81,21,89,29,1),1); // bot bev'd [1] square combined (color 0) +g.fillPoly(vs2(81,43,89,51,1),2); // bot bev'd [2] square combined (color 0) +g.setColor(c1); +g.fillPoly(vs2(82,22,88,28,1),1); // top bev'd [1] square combined (color 1) +g.fillPoly(vs2(82,44,88,50,1),2); // top bev'd [2] square combined (color 1) +SHOULD_BE(` +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +........#################################....#############....#############....#############........ +........#################################....#############....#...........#....#############........ +........###.......####................###....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........###.......####................###....#############....#...........#....#############........ +........#################################....#############....#...........#....#############........ +........#################################....#############....#...........#....#############........ +........###.......####................###....###.......###....#...........#....###.......###........ +........##.........##..................##....##.........##....#...#####...#....##..#####..##........ +........##.........##..................##....##.........##....#..#######..#....##.#######.##........ +........##.........##..................##....##.........##....#..#######..#....##.#######.##........ +........##.........##..................##....##.........##....#..#######..#....##.#######.##........ +........##.........##..................##....##.........##....#..#######..#....##.#######.##........ +........##.........##..................##....##.........##....#..#######..#....##.#######.##........ +........##.........##..................##....##.........##....#...#####...#....##..#####..##........ +........###.......####................###....###.......###....#...........#....###.......###........ +........#################################....#############....#...........#....#############........ +........#################################....#############....#...........#....#############........ +........####.....######..............####....#############....#...........#....#############........ +........###.#####.####.##############.###....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........##.#######.##.################.##....#############....#...........#....#############........ +........###.#####.####.##############.###....#############....#...........#....#############........ +........####.....######..............####....#############....#...........#....#############........ +........#################################....#############....#...........#....#############........ +........#################################....#############....#...........#....#############........ +........####.....######..............####....###.......###....#...........#....###.......###........ +........###.......####................###....##.........##....#...#####...#....##..#####..##........ +........##.........##..................##....##.........##....#..#######..#....##.#######.##........ +........##.........##..................##....##.........##....#..#######..#....##.#######.##........ +........##.........##..................##....##.........##....#..#######..#....##.#######.##........ +........##.........##..................##....##.........##....#..#######..#....##.#######.##........ +........##.........##..................##....##.........##....#..#######..#....##.#######.##........ +........###.......####................###....##.........##....#...#####...#....##..#####..##........ +........####.....######..............####....###.......###....#...........#....###.......###........ +........#################################....#############....#...........#....#############........ +........#################################....#############....#############....#############........ +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +.................................................................................................... +....................................................................................................`); + +result = ok;