lua optimize poly_draw add draw_number, poly_points modules

Change-Id: Id36e765f18234f5a4f3092d090c0adffa3da1612
This commit is contained in:
William Wilgus 2019-08-26 22:17:33 -05:00
parent 9f551b09f6
commit b99d4d7fa9
4 changed files with 297 additions and 43 deletions

View file

@ -25,7 +25,11 @@
_poly.polygon
_poly.polyline
]]
--[[
every 2 elements in t_pts is an x, y coord pair
p[?] = {x,y,x,y,x,y}
lines get drawn between the coords
]]
if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
local _poly = {} do
@ -39,66 +43,83 @@ local _poly = {} do
local _copy = rocklib_image.copy
local _line = rocklib_image.line
local _newimg = rb.new_image
local flood_fill = require("draw_floodfill")
local flood_fill
-- draws a non-filled figure based on points in t-points
local function polyline(img, x, y, t_points, color, bClosed, bClip)
if #t_points < 2 then error("not enough points", 3) end
local function polyline(img, x, y, t_pts, color, bClosed, bClip, scale_x, scale_y)
scale_x = scale_x or 1
scale_y = scale_y or 1
local pt_first_last
local pt_first_last, pt1, pt2
local max_x, max_y = 0, 0
local len = #t_pts
if len < 4 then error("not enough points", 3) end
if bClosed then
pt_first_last = t_points[1]
pt_first_last = {t_pts[1] * scale_x, t_pts[2] * scale_y}
else
pt_first_last = t_points[#t_points]
pt_first_last = {t_pts[len - 1] * scale_x, t_pts[len] * scale_y}
end
for i = 1, #t_points, 1 do
local pt1 = t_points[i]
local pt2 = t_points[i + 1] or pt_first_last-- first and last point
pt2 = {t_pts[1] * scale_x, t_pts[2] * scale_y}
for i = 3, len + 2, 2 do
pt1 = pt2
if t_pts[i + 1] == nil then
pt2 = pt_first_last
else
pt2 = {t_pts[i] * scale_x, t_pts[i + 1] * scale_y}
end-- first and last point
_line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip)
if pt1[1] > max_x then max_x = pt1[1] end
if pt1[2] > max_y then max_y = pt1[2] end
end
if pt2[1] > max_x then max_x = pt2[1] end
if pt2[2] > max_y then max_y = pt2[2] end
return max_x + x, max_y + y
end
-- draws a closed figure based on points in t_points
_poly.polygon = function(img, x, y, t_points, color, fillcolor, bClip)
if #t_points < 2 then error("not enough points", 3) end
-- draws a closed figure based on points in t_pts
_poly.polygon = function(img, x, y, t_pts, color, fillcolor, bClip, scale_x, scale_y)
scale_x = scale_x or 1
scale_y = scale_y or 1
if #t_pts < 2 then error("not enough points", 3) end
if fillcolor then
local x_min, x_max = 0, 0
local y_min, y_max = 0, 0
local w, h = 0, 0
-- find boundries of polygon
for i = 1, #t_points, 1 do
local pt = t_points[i]
if pt[1] < x_min then x_min = pt[1] end
if pt[1] > x_max then x_max = pt[1] end
if pt[2] < y_min then y_min = pt[2] end
if pt[2] > y_max then y_max = pt[2] end
end
w = _abs(x_max) + _abs(x_min)
h = _abs(y_max) + _abs(y_min)
x_min = x_min - 2 -- leave a border to use flood_fill
y_min = y_min - 2
flood_fill = flood_fill or require("draw_floodfill")
local x_min, x_max = 0, 0
local y_min, y_max = 0, 0
local w, h = 0, 0
local pt1, pt2
-- find boundries of polygon
for i = 1, #t_pts, 2 do
if t_pts[i] < x_min then x_min = t_pts[i] end
if t_pts[i] > x_max then x_max = t_pts[i] end
local fill_img = _newimg(w + 3, h + 3)
_clear(fill_img, 0x1)
if t_pts[i+1] < y_min then y_min = t_pts[i+1] end
if t_pts[i+1] > y_max then y_max = t_pts[i+1] end
end
x_max = x_max * scale_x
x_min = x_min * scale_x
y_max = y_max * scale_y
y_min = y_min * scale_y
w = _abs(x_max) + _abs(x_min)
h = _abs(y_max) + _abs(y_min)
x_min = -(x_min - 2) -- leave a border to use flood_fill
y_min = -(y_min - 2)
for i = 1, #t_points, 1 do
local pt1 = t_points[i]
local pt2 = t_points[i + 1] or t_points[1]-- first and last point
_line(fill_img, pt1[1] - x_min, pt1[2] - y_min,
pt2[1]- x_min, pt2[2] - y_min, 0)
local fill_img = _newimg(w + 3, h + 3)
_clear(fill_img, 0x1)
end
polyline(fill_img, x_min, y_min, t_pts,
0x0, true, bClip, scale_x, scale_y)
-- flood the outside of the figure with 0 the inside will be fillcolor
flood_fill(fill_img, fill_img:width(), fill_img:height() , 0x1, 0x0)
_copy(img, fill_img, x - 1, y - 1, _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor)
_copy(img, fill_img, x - 1, y - 1,
_NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor)
end
polyline(img, x, y, t_points, color, true, bClip)
polyline(img, x, y, t_pts, color, true, bClip, scale_x, scale_y)
end
-- expose internal functions to the outside through _poly table