PDA

View Full Version : How do you programmtically determine regions of a world map?



SpiceWeasel
26th November 2015, 02:46
I am trying to figure out how to build a game like Risk. What I am focusing on now is just the map, setting it up in a scene, visualizing in a view, and using the mouse to click on regions and have it print out what region was clicked on.

My question is, how to make a regions of a map (like the world map -- not simple rectangles) identifiable to the program?

anda_skoa
26th November 2015, 09:15
QPolygon or QPolygonF perhaps?

Cheers,
_

SpiceWeasel
27th November 2015, 01:40
Thanks, I've read about those classes but not sure how to apply it to the image of a world map (ala Risk). Even making the problem simpler, say using an image of the US states map, how would you create those polygons? Is their a class or tool that does that?

Oh, and BTW, Happy Thanksgiving to all!

d_stranz
27th November 2015, 18:52
You might try this: I googled for "US map state outlines" and got to the d-maps.com site (http://d-maps.com/index.php?lang=en), and specifically to a page with a blank map of the US state outlines (http://d-maps.com/carte.php?num_car=5187&lang=en). In there you will see a downloadable map in SVG format (which is XML for graphics). If you look at the XML source, you will see 50 or so "<path>" elements, each of which contains the polygon coordinates for a state (plus the outlines for parts of Mexico and Canada).

You'll need to read some SVG documentation to figure out how to decode the SVG, and you'll probably have to do some figuring out of which polygon is which, but that will give you the dimensional data you need to construct QPolygonF instances for each state.

Start with the US, and before long, you'll be able to rule the world :-)

SpiceWeasel
27th November 2015, 21:40
Thanks very much!

I've started looking over the SVG file format and the QGraphicsSvgItem class. The SVG file format is pretty cool!

d_stranz
27th November 2015, 22:10
QGraphicsSvgItem

Yes, I'd forgotten about that. You might be able to simply take apart the map SVG file and create separate SVG files that you can use to create a QGraphicsSvgItem for each region. That would fit nicely into a Graphics / View architecture for the game board and player pieces.

QGraphicsSvgItem is a bit restrictive, though. If you're designing a Risk-like game, you might, for example, like to be able to change the color of a region depending on which player has control of it. You can't do that with the SVG item - its properties and appearance are hard-coded into the SVG file it uses as a source. Reading the SVG as XML instead, and creating a QGraphicsPolygonItem from the path coordinates will give you greater control over dynamic properties.

SpiceWeasel
1st December 2015, 22:02
Thanks again, anda_skoa and d_stranz!

Because of your input, I managed to create an application showing a map of the US! I used QGraphicsScene with QGraphicsPolygonItems which I create from data that I load from an SVG file. There are some minor issues with the map -- the tool to convert a <path> elements to <polygon> elements (https://betravis.github.io/shape-tools/path-to-polygon/) did not create a perfect reproduction. I need to spend some time looking into that.

The coolest thing about doing this was actually having to delve into the SVG format itself. Who knew you could represents graphics by textually enumerating a path? I've included the code, just in case someone else is interested in this.

jefftee
4th December 2015, 02:07
Who knew you could represents graphics by textually enumerating a path?
The "V" in SVG stands for vector... :) That's the entire reason SVG files are resizable up or down w/o losing resolution and why it's the chosen file format many applications where something needs to be drawn, etc.