How do I find the minimum value out of a list of arguments, and WHICH argument it is. As far as I know math.min only does the first thing. Is there anything easier than writing out a huge tree of "if x < y then; if x < z then; if x < a then; ect, ect"? Knowing specifically which value is very important. (And I'm comparing a whole ton of variables, which change a lot, so I won't be able to tell just by what the value is.
Whoa, while writing this I thought of a pretty good way to do it, by doing a "<" test with the math.min result and each of the arguments. This is still pretty awkward, so does anyone have any better methods?
Finding the minimum value... (not math.min)
Re: Finding the minimum value... (not math.min)
Uhh... Just in case anyone was wondering I finished the script I was using this for. It was for a collision between two rectangles. It finds the position for the "A" rectangle that is closest to the mouse, but not intersecting the "Z" rectangle. Ax, Ay and Zx, Zy are the positions of the rectangles. Arx, Ary and Zrx, Zry are the "radii" of the rectangles. (half the width, height) "A" follows the mouse, and "Z" stays still. You can use this code as-is, because there are no other image or conf. files, just the graphics.point and graphics.rectangle functions. You can also edit the "r"s to whatever you want, and it works just fine. (which was the point)
If anyone has a more efficient way of doing this, feel free to edit/use this. Just tell me how you did it, so I can use it!
If anyone has a more efficient way of doing this, feel free to edit/use this. Just tell me how you did it, so I can use it!
Code: Select all
function love.load()
Ax, Ay = 100, 100
Zx, Zy = 300, 300
Arx, Ary = 10, 10
Zrx, Zry = 300, 10
end
function love.update(dt)
Ax, Ay = love.mouse.getPosition()
--This is where the meat is:
if Ax+Arx >= Zx-Zrx and Ay+Ary >= Zy-Zry and Ax-Arx <= Zx+Zrx and Ay-Ary <= Zy+Zry then
hit = true
minim = math.min(math.abs((Zy+Zry)-(Ay-Ary)), math.abs((Ay+Ary)-(Zy-Zry)), math.abs((Zx+Zrx)-(Ax-Arx)), math.abs((Ax+Arx)-(Zx-Zrx)))
if minim == math.abs((Zy+Zry)-(Ay-Ary)) then Ay = Ay + ((Zy+Zry)-(Ay-Ary))
elseif minim == math.abs((Ay+Ary)-(Zy-Zry)) then Ay = Ay - ((Ay+Ary)-(Zy-Zry))
elseif minim == math.abs((Zx+Zrx)-(Ax-Arx)) then Ax = Ax + ((Zx+Zrx)-(Ax-Arx))
elseif minim == math.abs((Ax+Arx)-(Zx-Zrx)) then Ax = Ax - ((Ax+Arx)-(Zx-Zrx))
end
else
hit = false
minim = 0
end
end
function love.draw()
love.graphics.rectangle( "line", Ax-Arx, Ay-Ary, Arx*2, Ary*2 )
love.graphics.point(Ax, Ay)
love.graphics.rectangle( "line", Zx-Zrx, Zy-Zry, Zrx*2, Zry*2 )
love.graphics.point(Zx, Zy)
if minim == 0 then
love.graphics.print("none", 10, 25)
else
love.graphics.print(minim, 10, 25)
end
if hit==true then
love.graphics.print("true", 10, 10)
else
love.graphics.print("false", 10, 10)
end
end
Re: Finding the minimum value... (not math.min)
If you are looking for a function thad does the work for you, than this could be a solution:
Code: Select all
function get_minumum(...)
table.sort(arg, function(a, b) return a < b end)
return arg[1]
end
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Finding the minimum value... (not math.min)
But isn't that effectively the same as math.min()?napco wrote:If you are looking for a function thad does the work for you, than this could be a solution:Code: Select all
function get_minumum(...) table.sort(arg, function(a, b) return a < b end) return arg[1] end
Help us help you: attach a .love.
Re: Finding the minimum value... (not math.min)
Doesn't math.min() take only 2 arguments?
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Finding the minimum value... (not math.min)
No..math.min (x, ···)
- TechnoCat
- Inner party member
- Posts: 1612
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Finding the minimum value... (not math.min)
He is looking for the min-value and the index of the min-value in the table.
Re: Finding the minimum value... (not math.min)
Ah, ok... Wow, i didn't know about math.min! I always use it with 2 args...
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Finding the minimum value... (not math.min)
How I would do that is something like this:TechnoCat wrote:He is looking for the min-value and the index of the min-value in the table.
Code: Select all
local sorted = {}
for k,v in pairs(tableYouWantSorted) do
table.insert(sorted, {k = k, v = v}
end
table.sort(sorted, function (a,b) return a.v < b.v end)
--sorted[1].k is the index of the min-value
--sorted[1].v is the min-value
Help us help you: attach a .love.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Finding the minimum value... (not math.min)
Well, that's easy:
Code: Select all
function min(t)
local min, mini = math.huge, 0
for i, v in pairs(t) do
if v < min then
min = v
mini = i
end
end
return mini, min
end
Who is online
Users browsing this forum: No registered users and 1 guest