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.lua107
1 files changed, 64 insertions, 43 deletions
diff --git a/apps/plugins/lua/include_lua/draw_poly.lua b/apps/plugins/lua/include_lua/draw_poly.lua
index dc2783898a..23e02d0955 100644
--- a/apps/plugins/lua/include_lua/draw_poly.lua
+++ b/apps/plugins/lua/include_lua/draw_poly.lua
@@ -25,7 +25,11 @@
25 _poly.polygon 25 _poly.polygon
26 _poly.polyline 26 _poly.polyline
27]] 27]]
28 28--[[
29 every 2 elements in t_pts is an x, y coord pair
30 p[?] = {x,y,x,y,x,y}
31 lines get drawn between the coords
32]]
29if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end 33if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
30 34
31local _poly = {} do 35local _poly = {} do
@@ -39,66 +43,83 @@ local _poly = {} do
39 local _copy = rocklib_image.copy 43 local _copy = rocklib_image.copy
40 local _line = rocklib_image.line 44 local _line = rocklib_image.line
41 local _newimg = rb.new_image 45 local _newimg = rb.new_image
42 local flood_fill = require("draw_floodfill") 46 local flood_fill
43 47
44 -- draws a non-filled figure based on points in t-points 48 -- draws a non-filled figure based on points in t-points
45 local function polyline(img, x, y, t_points, color, bClosed, bClip) 49 local function polyline(img, x, y, t_pts, color, bClosed, bClip, scale_x, scale_y)
46 if #t_points < 2 then error("not enough points", 3) end 50 scale_x = scale_x or 1
51 scale_y = scale_y or 1
47 52
48 local pt_first_last 53 local pt_first_last, pt1, pt2
54 local max_x, max_y = 0, 0
55 local len = #t_pts
56 if len < 4 then error("not enough points", 3) end
49 57
50 if bClosed then 58 if bClosed then
51 pt_first_last = t_points[1] 59 pt_first_last = {t_pts[1] * scale_x, t_pts[2] * scale_y}
52 else 60 else
53 pt_first_last = t_points[#t_points] 61 pt_first_last = {t_pts[len - 1] * scale_x, t_pts[len] * scale_y}
54 end 62 end
55 63
56 for i = 1, #t_points, 1 do 64 pt2 = {t_pts[1] * scale_x, t_pts[2] * scale_y}
57 local pt1 = t_points[i] 65 for i = 3, len + 2, 2 do
58 66 pt1 = pt2
59 local pt2 = t_points[i + 1] or pt_first_last-- first and last point 67 if t_pts[i + 1] == nil then
68 pt2 = pt_first_last
69 else
70 pt2 = {t_pts[i] * scale_x, t_pts[i + 1] * scale_y}
71 end-- first and last point
60 72
61 _line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip) 73 _line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip)
74 if pt1[1] > max_x then max_x = pt1[1] end
75 if pt1[2] > max_y then max_y = pt1[2] end
62 end 76 end
63 77 if pt2[1] > max_x then max_x = pt2[1] end
78 if pt2[2] > max_y then max_y = pt2[2] end
79 return max_x + x, max_y + y
64 end 80 end
65 81
66 -- draws a closed figure based on points in t_points 82 -- draws a closed figure based on points in t_pts
67 _poly.polygon = function(img, x, y, t_points, color, fillcolor, bClip) 83 _poly.polygon = function(img, x, y, t_pts, color, fillcolor, bClip, scale_x, scale_y)
68 if #t_points < 2 then error("not enough points", 3) end 84 scale_x = scale_x or 1
85 scale_y = scale_y or 1
86 if #t_pts < 2 then error("not enough points", 3) end
69 87
70 if fillcolor then 88 if fillcolor then
71 local x_min, x_max = 0, 0 89 flood_fill = flood_fill or require("draw_floodfill")
72 local y_min, y_max = 0, 0 90 local x_min, x_max = 0, 0
73 local w, h = 0, 0 91 local y_min, y_max = 0, 0
74 -- find boundries of polygon 92 local w, h = 0, 0
75 for i = 1, #t_points, 1 do 93 local pt1, pt2
76 local pt = t_points[i] 94 -- find boundries of polygon
77 if pt[1] < x_min then x_min = pt[1] end 95 for i = 1, #t_pts, 2 do
78 if pt[1] > x_max then x_max = pt[1] end 96 if t_pts[i] < x_min then x_min = t_pts[i] end
79 if pt[2] < y_min then y_min = pt[2] end 97 if t_pts[i] > x_max then x_max = t_pts[i] end
80 if pt[2] > y_max then y_max = pt[2] end 98
81 end 99 if t_pts[i+1] < y_min then y_min = t_pts[i+1] end
82 w = _abs(x_max) + _abs(x_min) 100 if t_pts[i+1] > y_max then y_max = t_pts[i+1] end
83 h = _abs(y_max) + _abs(y_min) 101 end
84 x_min = x_min - 2 -- leave a border to use flood_fill 102 x_max = x_max * scale_x
85 y_min = y_min - 2 103 x_min = x_min * scale_x
86 104 y_max = y_max * scale_y
87 local fill_img = _newimg(w + 3, h + 3) 105 y_min = y_min * scale_y
88 _clear(fill_img, 0x1) 106 w = _abs(x_max) + _abs(x_min)
89 107 h = _abs(y_max) + _abs(y_min)
90 for i = 1, #t_points, 1 do 108 x_min = -(x_min - 2) -- leave a border to use flood_fill
91 local pt1 = t_points[i] 109 y_min = -(y_min - 2)
92 local pt2 = t_points[i + 1] or t_points[1]-- first and last point 110
93 _line(fill_img, pt1[1] - x_min, pt1[2] - y_min, 111 local fill_img = _newimg(w + 3, h + 3)
94 pt2[1]- x_min, pt2[2] - y_min, 0) 112 _clear(fill_img, 0x1)
95 113
96 end 114 polyline(fill_img, x_min, y_min, t_pts,
115 0x0, true, bClip, scale_x, scale_y)
116 -- flood the outside of the figure with 0 the inside will be fillcolor
97 flood_fill(fill_img, fill_img:width(), fill_img:height() , 0x1, 0x0) 117 flood_fill(fill_img, fill_img:width(), fill_img:height() , 0x1, 0x0)
98 _copy(img, fill_img, x - 1, y - 1, _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor) 118 _copy(img, fill_img, x - 1, y - 1,
119 _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor)
99 end 120 end
100 121
101 polyline(img, x, y, t_points, color, true, bClip) 122 polyline(img, x, y, t_pts, color, true, bClip, scale_x, scale_y)
102 end 123 end
103 124
104 -- expose internal functions to the outside through _poly table 125 -- expose internal functions to the outside through _poly table