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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Fourex
Citizen
Posts: 53
Joined: Tue Nov 10, 2009 2:33 am
Location: Earth

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

Post 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?
User avatar
Fourex
Citizen
Posts: 53
Joined: Tue Nov 10, 2009 2:33 am
Location: Earth

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

Post 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
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

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

Post 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
User avatar
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)

Post 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()?
Help us help you: attach a .love.
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

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

Post by napco »

Doesn't math.min() take only 2 arguments?
User avatar
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)

Post by bartbes »

math.min (x, ···)
No..
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

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

Post by TechnoCat »

He is looking for the min-value and the index of the min-value in the table.
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

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

Post by napco »

Ah, ok... Wow, i didn't know about math.min! I always use it with 2 args...
User avatar
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)

Post 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
Help us help you: attach a .love.
User avatar
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)

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 58 guests