| 12-02-2009, 03:40 AM | #1 |
I have a bunch of unit and I want to find a center-point for them then find the distance from the center-point to all the units. How do you find this center-point? |
| 12-02-2009, 03:49 AM | #2 |
Average the x/y coordinates of all of the units. That will give you the central point between all of them. |
| 12-02-2009, 08:02 AM | #3 |
you need square average coorditates, not just summ\number |
| 12-02-2009, 06:42 PM | #4 |
I don't know if you want GUI or JASS, but basically you could have a Unit Group containing the units, and then you could have variables called, say, UnitGroupX (real starting at 0.0), UnitGroupY (real starting at 0.0), and UnitGroupCenter (point/location). For each unit in the group you can get its X coordinate and add it to UnitGroupX and then get its Y coordinate and add it UnitGroupY. After you've gone through all units in the unit group you can then divide both variables by the number of units in the unit group. Lastly, set UnitGroupCenter = (UnitGroupX, UnitGroupY). Next, you can get the distance from each unit in the group to this new UnitGroupCenter point. You can use JASS:function DistanceBetweenPoints takes location locA, location locB returns real local real dx = GetLocationX(locB) - GetLocationX(locA) local real dy = GetLocationY(locB) - GetLocationY(locA) return SquareRoot(dx * dx + dy * dy) endfunction In GUI I'm not sure if this function is in there somewhere...? so you might have to duplicate it yourself. |
| 12-03-2009, 03:29 AM | #5 |
If you don't want to use locations, you could use a function like this: JASS:function DistanceBetweenPointsEx takes real x1, real y1, real x2, real y2 returns real local real dx = x2 - x1 local real dy = y2 - y1 return SquareRoot( dx * dx + dy * dy ) endfunction Its the exact same thing except it uses coordinates (x, y) instead of a location. |
| 12-03-2009, 04:06 AM | #6 |
Thank you everyone. @Fluff Thank you for the explanation and I will never use GUI again. It's messy, you can't tell what some variables are, let alone what trigger is being used sometime. Once upon a time I helped people with GUI problems on the forum but I can't stand to look at it anymore. Oh the messes I've made with GUI. @TheKid JASS:function DistanceBetweenPointsEx takes real x1, real y1, real x2, real y2 returns real return SquareRoot((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) endfunction |
| 12-03-2009, 06:34 AM | #7 |
I just wanted to make sure you saw the similarity. Its been clarified that you don't need the clarification, haha. |
| 12-07-2009, 05:31 AM | #8 |
I was wrong with wanting you know the distance it seems. I wanted to know each units x/y point in relation to the center of them all. I made a trigger and tested it but when I try to recreate the exact units they are not in the exact same position, just slightly off. Is there something wrong with my code? JASS:library Relations initializer init globals private real CenterX private real CenterY private unit array U private real array X private real array Y private integer Count = 0 endglobals private function Conditions takes nothing returns boolean return true endfunction private function Store takes nothing returns nothing set Count = Count + 1 set U[Count] = GetEnumUnit() set X[Count] = GetUnitX(GetEnumUnit()) set Y[Count] = GetUnitY(GetEnumUnit()) endfunction private function Actions takes nothing returns nothing local integer i = 0 //Finds and stores all the unit x/y points to globals variables local group g = CreateGroup() call GroupEnumUnitsInRect(g, GetWorldBounds(), Condition(function Conditions)) call ForGroup(g, function Store) //Finds the center; adds all unit x/y values together and divids it set CenterX = 0 //Starting value of zero set CenterY = 0 loop //adds values together set i = i + 1 set CenterX = CenterX + X[i] set CenterY = CenterY + Y[i] exitwhen i == Count endloop set CenterX = CenterX / Count //Divids for center x/y point set CenterY = CenterY / Count call BJDebugMsg("Center Point X-" + R2S(CenterX)) call BJDebugMsg("Center Point Y-" + R2S(CenterY)) //Displays all the units x/y points in relation to the center point set i = 0 loop set i = i + 1 call BJDebugMsg("Name-" + GetUnitName(U[i])) call BJDebugMsg("X-" + R2S(GetUnitX(U[i]) - CenterX)) call BJDebugMsg("Y-" + R2S(GetUnitY(U[i]) - CenterY)) exitwhen i == Count endloop endfunction private function init takes nothing returns nothing local trigger t = CreateTrigger() call TriggerRegisterTimerEvent(t, 1, false) call TriggerAddAction(t, function Actions) endfunction endlibrary |
