1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
--[[ Lua Poly Drawing functions
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2017 William Wilgus
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
]]
--[[ Exposed Functions
_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
-- Internal Constants
local rocklib_image = getmetatable(rb.lcd_framebuffer())
local BSAND = 8 -- blits color to dst if src <> 0
local _NIL = nil -- nil placeholder
local _abs = math.abs
local _clear = rocklib_image.clear
local _copy = rocklib_image.copy
local _line = rocklib_image.line
local _newimg = rb.new_image
local flood_fill
-- draws a non-filled figure based on points in t-points
local function polyline(img, x, y, t_pts, color, bClosed, bClip, scale_x, scale_y, b_size_only)
local draw_fn = _line
if b_size_only == true then draw_fn = function() end end
scale_x = scale_x or 1
scale_y = scale_y or 1
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_pts[1] * scale_x, t_pts[2] * scale_y}
else
pt_first_last = {t_pts[len - 1] * scale_x, t_pts[len] * scale_y}
end
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
draw_fn(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_pts
_poly.polygon = function(img, x, y, t_pts, color, fillcolor, bClip, scale_x, scale_y, b_size_only)
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 and b_size_only ~= true then
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
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)
local fill_img = _newimg(w + 3, h + 3)
_clear(fill_img, 0x1)
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)
end
return polyline(img, x, y, t_pts, color, true, bClip, scale_x, scale_y, b_size_only)
end
-- expose internal functions to the outside through _poly table
_poly.polyline = polyline
end
return _poly
|