Thursday, December 4, 2008

Country Colours

After drawing the country borders, my next task I wanted to do was have each country it's own colour. Because while Cornflour Blue worked for water, it just wasn't working for the land.

Now I had all the vertex points making up the shapes of the countries, but in order to color them in, it wasn't as simple and clicking flood fill in paint.

Each shape had to be converted into triangles, as a triangle is simple for a graphics card to draw. Any shape can be converted into triangles, and there are a few different methods to do this.

Photobucket

I downloaded a triangulation class from http://www.xnawiki.com/index.php?title=Polygon_Triangulation
How it works it is goes through each set of 3 points in the polygon and checks if the triangle is what is called an 'ear'. If it is, it can be removed from the polygon and set aside. And the process is continued on the now smaller polygon.

This process will continue, removing triangles until only 1 triangles is left, then we know have a list of triangles which we can draw.

However there were a few problems with this code I downloaded, at one point my visual studio kept freezing up, turns out it was stuck in an infinite loop. The reason was I was sending an extra vertex point (In the shapefile, the first and last point of the polygon is the same, completing the shape. The triangulation does not accept this extra point).

While I caused it by sending the wrong data, it not very good of the original programmer to have the possibility of an infinite loop in their programming (They used a 'Do' loop while the polygon has more then 3 points, so if the data is incorrect and no points get removed, it would loop forever).
If it were me, I would have done dome kind of checking to make sure the process is working, and break out of the loop if it's not.

Anyway, I got it working, here is the result;
Photobucket
North Africa

Photobucket
Central America

The shapefile came in two resolutions, i'm using the low resolution one here.