From 2b9f4dba36ac8c855c183f1d1db2a5a5a641cbae Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 16 Jul 2016 17:39:03 -0400 Subject: [PATCH] Added even_r_coords and cube_coords to HexMapCell. That is, hexagons now know they're coordinates. --- main.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 4f66fca..d7f27f5 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ from kivy.uix.label import Label from kivy.uix.floatlayout import FloatLayout from kivy.graphics import Color, Ellipse, Line import kivy.utils +from math import ceil MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) @@ -60,15 +61,40 @@ class HexMapCell(Label): def __init__(self, row=0, col=0, **kwargs): super(HexMapCell, self).__init__(**kwargs) self.coords = MapCoords(row, col) + ## set the cube coordinates of the hexagon + ## as [x, y, z] + self.cube_coords = self.even_r_to_cube(self.coords.row / 3, self.coords.col / 2) + + + def even_r_to_cube(self, row, col): + '''compute cube coordinates from even-r hex coordinates''' + x = int(col - ceil(float(row)/2)) + z = row + y = - x - z + return([x, y, z]) + + def cube_to_even_r(self, x, y, z): + row = int(x + ceil(z / 2)) + col = z + return ([row, col]) + + @property + def even_r_coords(self): + '''return even-r coordinates of the hexagon.''' + return(self.cube_to_even_r(*self.cube_coords)) def coordinate_text(self): return '({}, {})'.format(self.coords.row, self.coords.col) - def map_coordinate_text(self): - return '[{}, {}]'.format(self.coords.row / 3, self.coords.col / 2) + def even_r_coordinate_text(self): + return '{}'.format(self.even_r_coords) + + def cube_coordinate_text(self): + x, y, z = self.cube_coords + return '{}\n{}\n{}'.format(x, y, z) def map_display_text(self): - return "{}\n{}".format(self.coordinate_text(), self.map_coordinate_text()) + return "{}\n{}".format(self.even_r_coordinate_text(), self.cube_coordinate_text()) def update_pos(self, instance, value): # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.