diff options
author | William Wilgus <me.theuser@yahoo.com> | 2019-08-04 09:48:09 -0500 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2019-08-04 16:57:02 +0200 |
commit | f85df30e3e7da55c6880a321b3d8dc737f7af5b2 (patch) | |
tree | c778d743feef43162f53df9c1b642912822f664e /apps/plugins/lua/include_lua/draw_poly.lua | |
parent | 4209c097705ce474f955bf1d73ca6ccf1ac6786a (diff) | |
download | rockbox-f85df30e3e7da55c6880a321b3d8dc737f7af5b2.tar.gz rockbox-f85df30e3e7da55c6880a321b3d8dc737f7af5b2.zip |
lua add rlimg.lua example script split large includes to separate files
Change-Id: I67cac5bc4ce5525ab30abf9443f6cc1a33190512
Diffstat (limited to 'apps/plugins/lua/include_lua/draw_poly.lua')
-rw-r--r-- | apps/plugins/lua/include_lua/draw_poly.lua | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/apps/plugins/lua/include_lua/draw_poly.lua b/apps/plugins/lua/include_lua/draw_poly.lua new file mode 100644 index 0000000000..fd76a582b1 --- /dev/null +++ b/apps/plugins/lua/include_lua/draw_poly.lua | |||
@@ -0,0 +1,103 @@ | |||
1 | --[[ Lua Poly Drawing functions | ||
2 | /*************************************************************************** | ||
3 | * __________ __ ___. | ||
4 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
5 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
6 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
7 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
8 | * \/ \/ \/ \/ \/ | ||
9 | * $Id$ | ||
10 | * | ||
11 | * Copyright (C) 2017 William Wilgus | ||
12 | * | ||
13 | * This program is free software; you can redistribute it and/or | ||
14 | * modify it under the terms of the GNU General Public License | ||
15 | * as published by the Free Software Foundation; either version 2 | ||
16 | * of the License, or (at your option) any later version. | ||
17 | * | ||
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
19 | * KIND, either express or implied. | ||
20 | * | ||
21 | ****************************************************************************/ | ||
22 | ]] | ||
23 | |||
24 | --[[ Exposed Functions | ||
25 | _poly.polygon | ||
26 | _poly.polyline | ||
27 | ]] | ||
28 | |||
29 | if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end | ||
30 | |||
31 | local _poly = {} do | ||
32 | local BSAND = 8 -- blits color to dst if src <> 0 | ||
33 | local _NIL = nil -- nil placeholder | ||
34 | |||
35 | local _abs = math.abs | ||
36 | local _copy = rocklib_image.copy | ||
37 | local _line = rocklib_image.line | ||
38 | local flood_fill = require("draw_floodfill") | ||
39 | |||
40 | -- draws a non-filled figure based on points in t-points | ||
41 | local function polyline(img, x, y, t_points, color, bClosed, bClip) | ||
42 | if #t_points < 2 then error("not enough points", 3) end | ||
43 | |||
44 | local pt_first_last | ||
45 | |||
46 | if bClosed then | ||
47 | pt_first_last = t_points[1] | ||
48 | else | ||
49 | pt_first_last = t_points[#t_points] | ||
50 | end | ||
51 | |||
52 | for i = 1, #t_points, 1 do | ||
53 | local pt1 = t_points[i] | ||
54 | |||
55 | local pt2 = t_points[i + 1] or pt_first_last-- first and last point | ||
56 | |||
57 | _line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip) | ||
58 | end | ||
59 | |||
60 | end | ||
61 | |||
62 | -- draws a closed figure based on points in t_points | ||
63 | _poly.polygon = function(img, x, y, t_points, color, fillcolor, bClip) | ||
64 | if #t_points < 2 then error("not enough points", 3) end | ||
65 | |||
66 | if fillcolor then | ||
67 | local x_min, x_max = 0, 0 | ||
68 | local y_min, y_max = 0, 0 | ||
69 | local w, h = 0, 0 | ||
70 | -- find boundries of polygon | ||
71 | for i = 1, #t_points, 1 do | ||
72 | local pt = t_points[i] | ||
73 | if pt[1] < x_min then x_min = pt[1] end | ||
74 | if pt[1] > x_max then x_max = pt[1] end | ||
75 | if pt[2] < y_min then y_min = pt[2] end | ||
76 | if pt[2] > y_max then y_max = pt[2] end | ||
77 | end | ||
78 | w = _abs(x_max) + _abs(x_min) | ||
79 | h = _abs(y_max) + _abs(y_min) | ||
80 | x_min = x_min - 2 -- leave a border to use flood_fill | ||
81 | y_min = y_min - 2 | ||
82 | |||
83 | local fill_img = _newimg(w + 3, h + 3) | ||
84 | _clear(fill_img, 0x1) | ||
85 | |||
86 | for i = 1, #t_points, 1 do | ||
87 | local pt1 = t_points[i] | ||
88 | local pt2 = t_points[i + 1] or t_points[1]-- first and last point | ||
89 | _line(fill_img, pt1[1] - x_min, pt1[2] - y_min, | ||
90 | pt2[1]- x_min, pt2[2] - y_min, 0) | ||
91 | |||
92 | end | ||
93 | flood_fill(fill_img, fill_img:width(), fill_img:height() , 0x1, 0x0) | ||
94 | _copy(img, fill_img, x - 1, y - 1, _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor) | ||
95 | end | ||
96 | |||
97 | polyline(img, x, y, t_points, color, true, bClip) | ||
98 | end | ||
99 | |||
100 | -- expose internal functions to the outside through _poly table | ||
101 | _poly.polyline = polyline | ||
102 | end | ||
103 | return _poly | ||