Skip to content

Instantly share code, notes, and snippets.

@WetDesertRock
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WetDesertRock/7b877047035b286a4ca6 to your computer and use it in GitHub Desktop.
Save WetDesertRock/7b877047035b286a4ca6 to your computer and use it in GitHub Desktop.
Triangles - Small library to draw delaunay triangle geometry on LOVE.
--
-- triangles
--
-- The MIT License (MIT)
--
-- Copyright (c) 2014, DesertRock
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
--
-- Notice that this license does not apply to the dependancy on the delaunay
-- library made by Yonaba that can be found here: tps://github.com/Yonaba/delaunay
--
-- To use, put the delaunay.lua file in a folder along with this file. You then
-- can do a `Triangles = require("triangles")`.
-- To create a triangles object, you do Triangles:new(). After that you do
-- Triangles:generate(width,height,cellsize,cellvariance). If you want to color
-- the triangles, do :colorize(lines). If lines is true, it will draw outlines.
-- Look at main.lua below for an example.
local Delaunay = require( ... .. '.delaunay') -- https://github.com/Yonaba/delaunay
--Stolen from: https://github.com/EmmanuelOga/columns/blob/master/utils/color.lua
local function hslToRgb(h, s, l, a)
local r, g, b
if s == 0 then
r, g, b = l, l, l -- achromatic
else
function hue2rgb(p, q, t)
if t < 0 then t = t + 1 end
if t > 1 then t = t - 1 end
if t < 1/6 then return p + (q - p) * 6 * t end
if t < 1/2 then return q end
if t < 2/3 then return p + (q - p) * (2/3 - t) * 6 end
return p
end
local q
if l < 0.5 then q = l * (1 + s) else q = l + s - l * s end
local p = 2 * l - q
r = hue2rgb(p, q, h + 1/3)
g = hue2rgb(p, q, h)
b = hue2rgb(p, q, h - 1/3)
end
return {r * 255, g * 255, b * 255, a * 255}
end
local Triangles = {}
Triangles.__index = Triangles
function Triangles:new(i)
local o = {}
o = {}
o.points = {}
o.triangles = nil
o.cs = 100
o.cp = 15
o.noffx = 0
o.noffy = 0
o.freq = 1000
o.saturation = 0.5
o.lightness = 0.5
o.linecolor = {0,0,0}
o.hueoff = 0
o.huemult = 1
setmetatable(o, self)
return o
end
function Triangles:generate(w,h,csize,padding)
self.points = {}
self.cs = csize or 100
self.cp = padding or 15
local cellrows = 1+ w/self.cs
local cellcols = 1+ h/self.cs
for x=-1,cellrows do
for y=-1,cellcols do
local xx = x*self.cs + love.math.random(self.cp*2) - self.cp
local yy = y*self.cs + love.math.random(self.cp*2) - self.cp
table.insert(self.points,Delaunay.Point(xx,yy))
end
end
self.noffx,self.noffy = love.math.random(10000),love.math.random(10000)
self.triangles = Delaunay.triangulate(unpack(self.points))
for _,tri in ipairs(self.triangles) do
tri.color = {100,100,100}
end
return self
end
function Triangles:getColor(x,y)
local hue = love.math.noise( self.noffx+x/self.freq,self.noffy+y/self.freq )*self.huemult + self.hueoff
while hue > 1.0 do
hue = hue-1
end
while hue < 0 do
hue = hue + 1
end
return hslToRgb(hue,self.saturation,self.lightness,1)
end
function Triangles:colorize()
for _,tri in ipairs(self.triangles) do
tri.color = self:getColor(tri:getCenter())
end
return self
end
function Triangles:draw(lines)
for _,tri in ipairs(self.triangles) do
love.graphics.setColor(unpack(tri.color))
love.graphics.polygon( "fill", tri.p1.x,tri.p1.y,tri.p2.x,tri.p2.y,tri.p3.x,tri.p3.y)
if lines then
love.graphics.setColor(unpack(self.linecolor))
love.graphics.polygon( "line", tri.p1.x,tri.p1.y,tri.p2.x,tri.p2.y,tri.p3.x,tri.p3.y)
end
end
end
return Triangles
Triangles = require("triangles")
function love.load()
for i=0,20 do
love.math.random();love.math.random();love.math.random();love.math.random()
end
love.graphics.setBackgroundColor( 220, 220, 220 )
tris = Triangles:new():generate(love.graphics.getWidth(),love.graphics.getHeight(),30,10)
-- tris:colorize()
tris.lightness = 0.5
tris.saturation = 0.3
tris.hueoff = love.math.random()
tris.huemult = 0.3
end
function love.update(dt)
-- tris.noffx = tris.noffx+dt/4
-- tris.noffy = tris.noffx+dt/20
tris.hueoff = tris.hueoff+dt/20
tris:colorize()
end
function love.draw()
tris:draw()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment