How do I find adjacent paths in an SVG file?

I am trying to make an interactive card game (like Risk) in which I have many SVG paths that are similar to this one: Example SVG Map

So when player1 clicks on player2's territory to attack him, the clicked territory must have a border with player1's territory. Another attack is invalid.

I thought to store all paths and adjacent paths in a multidimensional array like this:

path[0] -> path[0][0] path[0][1] path[0][2]
path[1] -> path[1][0] path[1][1]

      

But since my map is huge, it's really inefficient for me to do this. Any suggestions on how to solve this problem?

+3


source to share


2 answers


create a table of countries

Containing contiguity information, each country usually has up to 7 neighbors, unless you have small states like the Vatican, Luxembourg, ... So, for a unified map subdivision, you can use a static number of neighbors. Something like that:

  • int map[countries_max][neighbors_max];

Splitting the map evenly

There are more ways to get information about the adjacency. For an evenly split map, you can calculate the midpoint of the border path, which is usually near the center of the country. Take each country point and find the nearest countries (points). If the distance is less, then list the country code in the list of neighbors. The triangle can be around the size of the country (you need 2 cracks per axis to separate the rectangles).



general map division

You need to check if any part of a country's border path is near / parallel to any part of another country's border. If yes, then save the country index in the list of neighbors. This is only doable if you have border paths.

general division of the map (no border paths)

If your map is raster or vector, but not in the form of closed polygons / paths per country (for example, you can use split paths), then the above approaches are not suitable. When you attack, you know the start and end position of the attack so that you cast a line from start to finish (DDA or Bresenham) and count how many boundaries you cross (counting color edges). If the counter is 1, valid. This will have false negatives if you throw your attack over the same border multiple times.

+2


source


I think the idea of ​​using an array for this kind of thing is the wrong way to follow.

You should try to create class Map

and all functions within this class will represent territory and all players will be objects of this class. By doing this, you can handle the situation in an easier way.



For example: to find out if player1 got that part of the map, you only need to see if the function player1.Brazil

(where Brazil

is the function inside class Map

the player1 object) already has the flag = True. This flag will be there boolean

, and whenever you get this territory, you must change this flag to True.

So by comparing both, player 1 and player 2 are functioning with a simple operator if

, you can create adjacent paths that follow the rules of the game.

+1


source







All Articles