summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/include_lua/poly_points.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/include_lua/poly_points.lua')
-rw-r--r--apps/plugins/lua/include_lua/poly_points.lua118
1 files changed, 118 insertions, 0 deletions
diff --git a/apps/plugins/lua/include_lua/poly_points.lua b/apps/plugins/lua/include_lua/poly_points.lua
new file mode 100644
index 0000000000..d313710e5c
--- /dev/null
+++ b/apps/plugins/lua/include_lua/poly_points.lua
@@ -0,0 +1,118 @@
1--PolyPoints.lua
2--[[
3/***************************************************************************
4 * __________ __ ___.
5 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
6 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
7 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
8 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * \/ \/ \/ \/ \/
10 * $Id$
11 *
12 * Copyright (C) 2017 William Wilgus
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23]]
24
25--------------------------------------------------------------------------------
26
27--------------------------------------------------------------------------------
28-- decodes ascii string back to t_pts
29function points_from_ascii(encstr, scale)
30 scale = scale or 1
31 local t_pts = {}
32 encstr = encstr or "00000008"
33 local chroffset = tonumber(string.sub(encstr, 1, 3)) or 0
34 local chrlen = tonumber(string.sub(encstr, 4, 8)) or 0
35
36 if string.len(encstr) ~= chrlen then
37 error("Invalid Points String" .. string.len(encstr), 2)
38 end
39
40 for i = 9, string.len(encstr) - 1, 2 do
41 local x = string.byte(encstr, i, i) - chroffset
42 local y = string.byte(encstr, i + 1, i + 1) - chroffset
43 t_pts[#t_pts + 1] = x * scale
44 t_pts[#t_pts + 1] = y * scale
45 end
46 return t_pts
47end
48--------------------------------------------------------------------------------
49-- encodes t_pts as a ascii string non print chars are excluded so
50-- size is limited to approx ~ 90x90
51function points_to_ascii(t_pts)
52 if not t_pts then return "" end
53 local chroffset = 33
54 local maxoffset = 126 - 33
55 local t_enc = {[1] = string.format("%03d%05d", chroffset, #t_pts + 8)}
56 local max_n, min_n = 0, 0
57 for i = 1, #t_pts, 2 do
58 if t_pts[i] > max_n then max_n = t_pts[i] end
59 if t_pts[i] < min_n then min_n = t_pts[i] end
60 if t_pts[i+1] > max_n then max_n = t_pts[i+1] end
61 if t_pts[i+1] < min_n then min_n = t_pts[i+1] end
62 if max_n > maxoffset or min_n < 0 then break; end
63 t_enc[#t_enc + 1] = string.char(t_pts[i] + chroffset)
64 t_enc[#t_enc + 1] = string.char(t_pts[i+1] + chroffset)
65 end
66 if min_n >= 0 and (max_n - min_n) <= maxoffset then
67 return table.concat(t_enc)
68 else
69 return "00000008"
70 end
71end
72--------------------------------------------------------------------------------
73-- scales t_pts by percentage (x/y)
74function points_scale_pct(t_pts, x_pct, y_pct)
75 for i = 1, #t_pts, 2 do
76 local t_pt = {t_pts[i], t_pts[i + 1]}
77 t_pt = t_pt or {0, 0}
78 t_pts[i] = (t_pt[1] * x_pct) / 100
79 t_pts[i+1] = (t_pt[2] * y_pct) / 100
80 end
81 return t_pts
82end
83--------------------------------------------------------------------------------
84-- scales t_pts by (x/y)
85function points_scale(t_pts, width, height)
86 local max_x, max_y = 0, 0
87 for i = 1, #t_pts, 2 do
88 if t_pts[i] > max_x then max_x = t_pts[i] end
89 if t_pts[i+1] > max_y then max_y = t_pts[i+1] end
90 end
91
92 local x_pct = (width * 100) / max_x
93 local y_pct = (height * 100) / max_y
94
95 return points_scale_pct(t_pts, x_pct, y_pct)
96end
97--------------------------------------------------------------------------------
98--[[
99 function scaleup(t_pts, scale_x, scale_y)
100 local t_coord
101 for key,value in pairs(t_pts) do
102 t_coord = t_pts[key]
103 t_coord[1] = t_coord[1] * scale_x
104 t_coord[2] = t_coord[2] * scale_y
105 t_pts[key] = t_coord
106 end
107 end
108
109 function scaledn(t_pts, scale_x, scale_y)
110 local t_coord
111 for key,value in pairs(t_pts) do
112 t_coord = t_pts[key]
113 t_coord[1] = t_coord[1] / scale_x
114 t_coord[2] = t_coord[2] / scale_y
115 t_pts[key] = t_coord
116 end
117 end
118]]