HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Find minimal value?

01-31-2010, 05:43 PM#1
rednek
Let we say I have 10 integer variables, each of them has random number between 0 and 100.
Now, how can I find which one of them has lowest value? Min function World Editor has can compare only two variables (as far as I know)

Or

I'm doing this, because I'm trying to find closest unit to a certain point. If there's any clear way to find which one is it, well, I'm all ears :)
01-31-2010, 05:49 PM#2
Kueken
For the first one, use a sort algorithm, or simply loop though the numbers and get the lowest one by multiple comparisons

For the second one, pick all units and compare their distances like mentioned
01-31-2010, 06:24 PM#3
rednek
Thanks for the reply, could you be more specific please?

The number of variables is unknown to me, might be more than 10. I can't imagine how to sucesfully compare them, without having it to be ridiculously long.
What is this sorting algoritm you speak of, and how do I accomplish it?
01-31-2010, 06:48 PM#4
TriggerHappy
Should output the lowest #. Not tested (or syntax checked).

Collapse JASS:
local integer i = 1
local integer lowest = yourArray[0]
local integer index  = 0
loop
    exitwhen i > 10
    if (yourArray[i] < lowest) then
        set lowest = yourArray[i]
        set index  = i
    endif
    set i = i + 1
endloop
call BJDebugMsg("yourArray["+I2S(index)+"] has the lowest value, " + I2S(lowest))
01-31-2010, 06:53 PM#5
moyack
Collapse JASS:
globals
   integer array A
   integer Size = 9 // for values in the array with index between 0 - 9
endglobals

function Min10 takes nothing returns integer
   local integer i = 0
   local integer r = A[0]
   local integer a = 0
   loop
      exitwhen i == Size
      set a = Min(A[i], A[i+1])
      if a < r then
         set r = a
      endif
      set i = i+1
   endloop
   return r
endfunction
01-31-2010, 06:56 PM#6
Anitarf
If you want to find the nearest unit to a point, PruneGroup can help.
01-31-2010, 07:04 PM#7
Michael Peppers
Quote:
Originally Posted by Anitarf
If you want to find the nearest unit to a point, PruneGroup can help.
...or my FindUnits =P
01-31-2010, 07:53 PM#8
Themerion
Quote:
I'm trying to find closest unit to a certain point.

Here's an outline for how it's done. It's made to be simple, read comment on (meaningless? :P) optimizations below.

Trigger:
Set CertainPoint = (Center of (Playable Map Area))
Set TempReal = 500
Set TempUnit = No Unit
Collapse Unit Group - Pick All units in 500 from CertainPoint
Collapse Loop
Collapse If
Collapse Conditions
Distance Between (Certain Point) and (Position of (Picked Unit)) less than TempReal
Collapse Actions
Set TempUnit = (Picked Unit)
Set TempReal = Distance Between (Certain Point) and (Position of (Picked Unit))

Collapse if TempUnit not equal to No Unit
--- Voila, the closest unit was TempUnit ---

Once you understand this outline, you should probably optimize it by storing "Distance Between..." as a variable (so you won't have to check multiple times for the same unit). You should also take care of the point-leaks, and the unit group leak.
01-31-2010, 10:06 PM#9
rednek
Whoa, thanks for all the replies, I went with the moyack's one for finding lowest value, and it works perfectly (thanks man)
After that, Themerion's trigger was clear as day to me. I'm glad you posted it :P
So, yeah, thanks again ^^
02-01-2010, 01:06 PM#10
Anitarf
Quote:
Originally Posted by rednek
Whoa, thanks for all the replies, I went with the moyack's one for finding lowest value, and it works perfectly
It does? It doesn't even compile for me.
02-01-2010, 01:37 PM#11
moyack
Quote:
Originally Posted by Anitarf
It does? It doesn't even compile for me.
Man, you suck at coding.... :P
02-01-2010, 05:05 PM#12
rednek
Quote:
Originally Posted by Anitarf
It does? It doesn't even compile for me.

Perhaps, I didn't exactly copy'n'paste it, just saw how it's done and then did it by memory.