Page 1 of 2

Finding the minimum value... (not math.min)

Posted: Mon Jan 11, 2010 12:19 am
by Fourex
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?

Re: Finding the minimum value... (not math.min)

Posted: Mon Jan 11, 2010 5:43 am
by Fourex
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! :nyu:

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)

Posted: Thu Jan 14, 2010 10:53 am
by napco
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

Re: Finding the minimum value... (not math.min)

Posted: Thu Jan 14, 2010 11:29 am
by Robin
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
But isn't that effectively the same as math.min()?

Re: Finding the minimum value... (not math.min)

Posted: Thu Jan 14, 2010 11:43 am
by napco
Doesn't math.min() take only 2 arguments?

Re: Finding the minimum value... (not math.min)

Posted: Thu Jan 14, 2010 12:17 pm
by bartbes
math.min (x, ···)
No..

Re: Finding the minimum value... (not math.min)

Posted: Thu Jan 14, 2010 2:08 pm
by TechnoCat
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)

Posted: Thu Jan 14, 2010 2:13 pm
by napco
Ah, ok... Wow, i didn't know about math.min! I always use it with 2 args...

Re: Finding the minimum value... (not math.min)

Posted: Thu Jan 14, 2010 4:39 pm
by Robin
TechnoCat wrote:He is looking for the min-value and the index of the min-value in the table.
How I would do that is something like this:

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

Re: Finding the minimum value... (not math.min)

Posted: Thu Jan 14, 2010 6:01 pm
by bartbes
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