Graphics: new g.fillPoly to handle irregular polygons - fix previous issues with duplicate points

This commit is contained in:
Gordon Williams 2019-12-10 10:52:57 +00:00
parent a5116134d9
commit 2fe3f34eff
3 changed files with 135 additions and 2 deletions

View File

@ -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)

View File

@ -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<points;i++) {
if ((v[i].y<=y && v[j].y>=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) ||
(v[j].y<y && v[i].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

View File

@ -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<g.getHeight();y++) {
s+="\n";
for (var x=0;x<g.getWidth();x++)
s+=".#"[b[n++]?1:0];
}
return s;
}
g.print = _=>{
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;