summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/include_lua/draw_poly.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/include_lua/draw_poly.lua')
-rw-r--r--apps/plugins/lua/include_lua/draw_poly.lua103
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
29if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
30
31local _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
102end
103return _poly