-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
106 lines (92 loc) · 2.89 KB
/
Copy pathmain.lua
File metadata and controls
106 lines (92 loc) · 2.89 KB
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
--[[
luapoly by David Gayerie
]]--
require "circular_table"
local new_poly = require "poly"
local msg = ""
local shape = {}
local function parse_args(args)
local vertices_chain={}
if args[2] then
local f = io.open(args[2])
if io.type(f) == "file" then
for line in f:lines() do
for t in string.gmatch(line, "([%d%.]+)[,%w]?") do
table.insert(vertices_chain, tonumber(t))
end
end
f:close()
else
for t in string.gmatch(args[2], "([%d%.]+)[,]?") do
table.insert(vertices_chain, tonumber(t))
end
end
end
return vertices_chain
end
function love.load(args)
love.window.setTitle("LuaPoly")
love.graphics.setBackgroundColor(250,250,250)
love.graphics.setColor(60,60,60)
shape.poly = new_poly(parse_args(args))
end
function love.mousepressed(x, y, button)
if button == 1 and shape.is_cw_winding == nil then
shape.poly:push_coord(x, y)
elseif button == 2 then
shape.poly:close()
if shape.poly:is_closed() then
shape.is_convex = shape.poly:is_convex()
shape.is_cw_winding = shape.poly:is_cw()
shape.triangles = shape.poly:get_triangles()
print("triangulation of (".. table.concat(shape.poly, ",")..")")
for _,triangle in ipairs(shape.triangles) do
print("("..table.concat(triangle,",")..")")
end
end
end
end
function love.keypressed(key, isrepeat)
if key == "backspace" or key == "delete" then
shape.poly:pop_coord()
shape.is_convex = nil
shape.is_cw_winding = nil
shape.triangles = nil
end
end
function love.update(dt)
if shape.is_cw_winding == nil then
msg = "Add points (left click)"
if shape.poly:get_coord_count() > 2 then
msg = msg .. " or close the polygon (right click to close)"
end
else
msg = shape.is_convex and "Convex polygon" or "Concave polygon"
msg = msg .. (shape.is_cw_winding and " (clockwise winding)" or " (counterclockwise winding)")
end
end
local color_components = to_circular_table{255,0,0, 125, 125, 0, 80, 175, 0}
function love.draw()
local color = {love.graphics.getColor()}
if shape.triangles then
local color_index=1
for _,triangle in ipairs(shape.triangles) do
love.graphics.setColor(color_components[color_index], color_components[color_index+1], color_components[color_index+2], 150)
color_index = color_index + 1
local x1, y1 = shape.poly:get_coord(triangle[1])
local x2, y2 = shape.poly:get_coord(triangle[2])
local x3, y3 = shape.poly:get_coord(triangle[3])
love.graphics.polygon("fill", x1, y1, x2, y2, x3, y3)
end
end
love.graphics.setColor(0, 0, 0)
local coord_count = shape.poly:get_coord_count()
love.graphics.setPointSize(3)
if coord_count == 1 then
love.graphics.points(shape.poly:get_coord(1))
elseif coord_count > 1 then
love.graphics.line(shape.poly)
end
love.graphics.print(msg, 10, 10)
love.graphics.setColor(color)
end