Discord
Login
Community
DARK THEME

Question

How to know if one of the classes in a list are the same x coordinate as another class in the same list

If there are few objects and the search is performed rarely, searching the list is acceptable.

ObjGraph =class
  constructor = function( x, y, id )
    this.x = x
    this.y = y
    this.id = id
  end
end

testCoord = function( list , objGraph )
  local  test = false
  for obj in list 
    if  (obj != objGraph) 
    //  (obj.id != objGraph.id )
    and (obj.x == objGraph.x ) 
    and (obj.y == objGraph.y ) 
    then
         test = true
         break
       end
  end
  return test
end

init = function()
  list = []
  obj1 = new ObjGraph( 1, 2, 1 )
  list.push( obj1 )
  print( " one object = " + testCoord( list, obj1 ) + " correct result = 0 ")
  obj2 = new ObjGraph( 2, 2, 2 )
  list.push( obj2 )
  print( " two object = " + testCoord( list, obj1 ) + " correct result = 0 ")  
  obj3 = new ObjGraph( 2, 2, 3 )
  list.push( obj2 )
  print( " three object = " + testCoord( list, obj3 ) + " correct result = 1 ")  
end

If you have several thousand items on the list, you need to provide an additional hundred to do it effectively. Otherwise, the program will search all the time.

n MicroStudio you have a Map object that provides a TileMap for displaying levels. But it can also be used to store information about the occupancy of a given coordinate.

map = new Map( 100, 100, 10, 10 )
map.set( 2, 2, sprite_name )
map.get( 2, 2 )

If you don't want to use Maps and you have a large number of objects to check, you should consider QuadTree or HashList.

https://microstudio.dev/i/Loginus/quadtree/ - press the mouse to change the testing mode

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community