New topic: Near Objetcs
<http://forums.realsoftware.com/viewtopic.php?t=25241> Page 1 of 1 [ 1 post ] Previous topic | Next topic Author Message Amando Post subject: Near ObjetcsPosted: Fri Nov 14, 2008 4:40 pm Joined: Tue Jan 31, 2006 5:22 am Posts: 47 I have to say I am BAD at maths, and that's not good on some kind of applications. I am willing to find a methods that find "Nearest Units" among one. The closer will get priotity, but I can't find the way I will like to do it. So I ask you help. I have an array of Map of (X,Y) coords, and would like an algorithm to make it as is 3333333 3222223 3211123 321X123 3211123 3222223 3333333 Let's Day X is positioned on coords X=10 and Y = 10, and need to find near units looking firstly at 1, then just looking at 2 coords, and finally 3 coords. This is a very consuming method, but I have no clue of how to achieve it. I made a code that look's for all coords, but it's not efficent and sorry to have so bad maths skills. Here goes the code: dim pos as WorldPos = lpUnit.WorldPos dim rango,x,y as Integer Dim FoundUnits() as Unit dim counter as UInt8 = 0 Dim found as new Dictionary for rango = 0 to nRange - 1 for x = pos.x - nRange to Pos.x + rango for y = pos.y - nRange to pos.y + rango if y < 0 then y = 0 if y > Zone_Heigth then Y = Zone_Heigth if x < 0 then x = 0 if x > Zone_Width then x = Zone_Width // Convertimos Int32 en UInt32 dim ux,uy as UInt32 ux = x uy = y // TODO: Esto hacerlo por una rutina mejor y mas limpia, de momento lo meto todo en un // diccionario para evitar duplicados // Chequeamos que estemos dentro del rango if not CheckZoneOutBounds(ux,uy) then if Grid(GetZoneCoordsOffset(ux,uy)) <> nil then Dim fUnit as Unit = Grid(GetZoneCoordsOffset(ux,uy)) // CHAPUZA: Lo meto en un diccionario para evitar duplicados if not found.HasKey(GetZoneCoordsOffset(ux,uy)) then // Si no debe incluir a la unidad que lanza la accion (no incluirse a si mismo) if not bIncludeme then if fUnit <> lpUnit then // Incluye a todas las unidades, exceptuando a quien lo llama FoundUnits.Append Grid(GetZoneCoordsOffset(ux,uy)) Found.Value(GetZoneCoordsOffset(ux,uy))=1 end if else // Incluye a todas las unidades, incluyendo al caller del metodo FoundUnits.Append Grid(GetZoneCoordsOffset(ux,uy)) Found.Value(GetZoneCoordsOffset(ux,uy))=1 end if end if end if end if next next next Return(FoundUnits) I had to use a dictionary to avoid duplicated on the found Units. Do you find an intelligent way to do it? Regards, Amando _________________ ¡No lo intentes! Hazlo o no lo hagas, pero no lo intentes. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 1 post ]
-- Over 900 classes with 18000 functions in one REALbasic plug-in. The Monkeybread Software Realbasic Plugin v8.1. <http://www.monkeybreadsoftware.de/realbasic/plugins.shtml> [email protected]
