A few snippets

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

A few snippets

Post by BlackBulletIV »

Here's three snippets I've put out as gists recently; thought I'd share them as they might be useful to someone.

Linear radial gradient (top two functions taken from Ammo):

Code: Select all

local function scale(x, min1, max1, min2, max2)
  return min2 + ((x - min1) / (max1 - min1)) * (max2 - min2)
end

local function distance(x1, y1, x2, y2)
  return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
end

function radialGradient(radius)
  local data = love.image.newImageData(radius * 2, radius * 2)
  
  data:mapPixel(function(x, y)
    local dist = distance(radius, radius, x, y)
    return 0, 0, 0, (dist <= radius and scale(dist, 0, radius, 255, 0) or 0)
  end)
  
  return love.graphics.newImage(data)
end
Random tune made out of sine waves:

Code: Select all

local samples = 100000
local data = love.sound.newSoundData(samples)
local noteChange = 10000
local note = 200
local change = 50
local minimum = 100

for i = 0, samples * 2 - 1 do
  if i % noteChange == 0 then
    local factor = -2 + math.random(0, 4)
    if note <= minimum then factor = 1 end
    note = note + change * factor
  end
  
  data:setSample(i, math.sin(i % note / note / (math.pi * 2)))
end
Hz frequencies of the notes from eight octaves in a Lua table:

Code: Select all

-- converted from http://www.seventhstring.com/resources/notefrequencies.html
notes = {
  [0] = { ["c"] = 16.35, ["c#"] = 17.32, ["d"] = 18.35, ["d#"] = 19.45, ["e"] = 20.60, ["f"] = 21.83, ["f#"] = 23.12, ["g"] = 24.50, ["g#"] = 25.96, ["a"] = 27.50, ["a#"] = 29.14, ["b"] = 30.87, },
  [1] = { ["c"] = 32.70, ["c#"] = 34.65, ["d"] = 36.71, ["d#"] = 38.89, ["e"] = 41.20, ["f"] = 43.65, ["f#"] = 46.25, ["g"] = 49.00, ["g#"] = 51.91, ["a"] = 55.00, ["a#"] = 58.27, ["b"] = 61.74, },
  [2] = { ["c"] = 65.41, ["c#"] = 69.30, ["d"] = 73.42, ["d#"] = 77.78, ["e"] = 82.41, ["f"] = 87.31, ["f#"] = 92.50, ["g"] = 98.00, ["g#"] = 103.8, ["a"] = 110.0, ["a#"] = 116.5, ["b"] = 123.5, },
  [3] = { ["c"] = 130.8, ["c#"] = 138.6, ["d"] = 146.8, ["d#"] = 155.6, ["e"] = 164.8, ["f"] = 174.6, ["f#"] = 185.0, ["g"] = 196.0, ["g#"] = 207.7, ["a"] = 220.0, ["a#"] = 233.1, ["b"] = 246.9, },
  [4] = { ["c"] = 261.6, ["c#"] = 277.2, ["d"] = 293.7, ["d#"] = 311.1, ["e"] = 329.6, ["f"] = 349.2, ["f#"] = 370.0, ["g"] = 392.0, ["g#"] = 415.3, ["a"] = 440.0, ["a#"] = 466.2, ["b"] = 493.9, },
  [5] = { ["c"] = 523.3, ["c#"] = 554.4, ["d"] = 587.3, ["d#"] = 622.3, ["e"] = 659.3, ["f"] = 698.5, ["f#"] = 740.0, ["g"] = 784.0, ["g#"] = 830.6, ["a"] = 880.0, ["a#"] = 932.3, ["b"] = 987.8, },
  [6] = { ["c"] = 1047, ["c#"] = 1109, ["d"] = 1175, ["d#"] = 1245, ["e"] = 1319, ["f"] = 1397, ["f#"] = 1480, ["g"] = 1568, ["g#"] = 1661, ["a"] = 1760, ["a#"] = 1865, ["b"] = 1976, },
  [7] = { ["c"] = 2093, ["c#"] = 2217, ["d"] = 2349, ["d#"] = 2489, ["e"] = 2637, ["f"] = 2794, ["f#"] = 2960, ["g"] = 3136, ["g#"] = 3322, ["a"] = 3520, ["a#"] = 3729, ["b"] = 3951, },
  [8] = { ["c"] = 4186, ["c#"] = 4435, ["d"] = 4699, ["d#"] = 4978, ["e"] = 5274, ["f"] = 5588, ["f#"] = 5920, ["g"] = 6272, ["g#"] = 6645, ["a"] = 7040, ["a#"] = 7459, ["b"] = 7902, },
}
By the way, was there some kind of previous topic for posting snippets like these?
User avatar
Nsmurf
Party member
Posts: 191
Joined: Fri Jul 27, 2012 1:58 am
Location: West coast.

Re: A few snippets

Post by Nsmurf »

OBEY!!!
My Blog
UE0gbWUgd2l0aCB0aGUgd29yZCAnSE1TRycgYXMgdGhlIHN1YmplY3Q=
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: A few snippets

Post by qaisjp »

... a wiki page!

math.sin is rather interesting :D
Lua is not an acronym.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: A few snippets

Post by BlackBulletIV »

I guess I could add the first two to the wiki... but I'm not sure whether I can be bothered. :P
qaisjp wrote:math.sin is rather interesting :D
Yeah, sines are pretty amazing.
User avatar
bartoleo
Party member
Posts: 118
Joined: Wed Jul 14, 2010 10:57 am
Location: Savigliano

Re: A few snippets

Post by bartoleo »

qaisjp wrote:... a wiki page!

math.sin is rather interesting :D
in Italian sin is "seno"... "seno" in english translates as boobs

so math.boobs ... very interesting!!
Bartoleo
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: A few snippets

Post by Roland_Yonaba »

bartoleo wrote:in Italian sin is "seno"... "seno" in english translates as boobs
so math.boobs ... very interesting!!
Oh my .... :shock:
@Nixola: Please, eat that guy...!
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: A few snippets

Post by Nixola »

Why? It's true, in Italian sine and boobs are both called "seno" (but 'boobs' has many different translations)

And cosine is "coseno"... Co-boobs?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: A few snippets

Post by kikito »

If you really want boobs, then you need exp, not sine.

Now, to be back on topic: are the previous snippets being used somewhere, appart from ammo? I was intrigued when I saw them popping out on my github dashboard.
When I write def I mean function.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: A few snippets

Post by Roland_Yonaba »

Nixola wrote:Why? It's true,
I'm not wondering about the truthfulness of this.
Just thinking that whoever who says math.boobs shall be eaten.
And that's your call. :crazy:

EDIT: I've been Kikito'ed
User avatar
paritybit
Citizen
Posts: 53
Joined: Thu Sep 06, 2012 3:52 am

Re: A few snippets

Post by paritybit »

kikito wrote:If you really want boobs, then you need exp, not sine.
Somebody spent a really long time to work that out.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], MrFariator and 160 guests