Esempio n. 1
0
 def get_hex(self, position):
     """Return coordinates of a Hex from the given pixel position
     """
     size = self.size()
     x = size.width() // 2
     y = size.height() // 2
     hexgrid = hexutil.HexGrid(10)
     return hexgrid.hex_at_coordinate(position.x() - x, position.y() - y)
Esempio n. 2
0
    def draw_areas(self):
        """Draw areas in the game board
        """
        if self.game.draw_battle:
            self.game.draw_battle = False
        size = self.size()
        x = size.width()
        y = size.height()

        hexgrid = hexutil.HexGrid(10)

        self.qp.setPen(Qt.NoPen)
        self.qp.translate(x // 2, y // 2)

        for k, area in self.board.areas.items():
            lines = []
            first_hex = True

            color = player_color(area.get_owner_name())
            if self.activated_area_name == int(k):
                color = (170 + color[0] // 3, 170 + color[1] // 3, 170 + color[2] // 3)
            self.qp.setBrush(QColor(*color))
            self.qp.setPen(Qt.NoPen)
            for h in area.get_hexes():
                polygon = QPolygon([QPoint(*corner) for corner in hexgrid.corners(h)])
                self.qp.drawPolygon(polygon)

                if first_hex:
                    self.qp.save()
                    rect = QRectF(*hexgrid.bounding_box(h))
                    self.qp.setBrush(QColor(0, 0, 0))
                    self.qp.setPen(self.pen)
                    self.qp.setFont(self.font)
                    self.qp.setRenderHint(QPainter.TextAntialiasing)

                    self.qp.drawText(rect, Qt.AlignCenter, self.area_text_fn(area))
                    first_hex = False
                    self.qp.restore()

                for n in h.neighbours():
                    if n not in area.get_hexes():
                        line = []
                        for corner in hexgrid.corners(h):
                            if corner in hexgrid.corners(n):
                                line.append(corner)
                        lines.append(line)

            self.qp.save()
            pen = QPen()
            pen.setWidth(3)
            self.qp.setPen(pen)
            self.qp.setBrush(QColor())
            self.qp.setRenderHint(QPainter.Antialiasing)
            for line in lines:
                self.qp.drawLine(line[0][0], line[0][1], line[1][0], line[1][1])
            self.qp.restore()
Esempio n. 3
0
 def test_hexes_in_rectangle(self):
     hg = hexutil.HexGrid(32)
     self.assertEqual(
         list(hg.hexes_in_rectangle(hg.bounding_box(hexutil.origin))), [
             hexutil.Hex(-1, -1),
             hexutil.Hex(1, -1),
             hexutil.Hex(-2, 0),
             hexutil.Hex(0, 0),
             hexutil.Hex(-1, 1),
             hexutil.Hex(1, 1)
         ])
Esempio n. 4
0
 def test_hex_at_coordinates(self):
     hg = hexutil.HexGrid(32)
     data = [((0, 0), hexutil.Hex(0, 0)), ((33, 16), hexutil.Hex(2, 0)),
             ((30, 20), hexutil.Hex(1, 1))]
     for fx in (-1, 1):
         for fy in (-1, 1):
             for pixel, hex in data:
                 x, y = pixel
                 pixel = (fx * x, fy * y)
                 x, y = hex
                 hex = hexutil.Hex(fx * x, fy * y)
                 self.assertEqual(hg.hex_at_coordinate(*pixel), hex, pixel)
Esempio n. 5
0
    def __init__(self, *args, **kws):
        super().__init__(*args, **kws)
        self.setMouseTracking(True)  # we want to receive mouseMoveEvents

        self.level = Level(500)
        self.player = hexutil.origin
        self.hexgrid = hexutil.HexGrid(24)

        # initialize GUI objects needed for painting
        self.font = QtGui.QFont("Helvetica", 20)
        self.font.setStyleHint(QtGui.QFont.SansSerif)
        self.pen = QtGui.QPen()
        self.pen.setWidth(2)
        self.select_brush = QtGui.QBrush(QtGui.QColor(127, 127, 255, 127))
        self.unseen_brush = QtGui.QBrush(QtGui.QColor(0, 0, 0, 127))

        self.update_fov()
Esempio n. 6
0
 def test_bounding_box(self):
     hg = hexutil.HexGrid(32)
     self.assertEqual(hg.bounding_box(hexutil.Hex(0, 2)),
                      hexutil.Rectangle(-32, 72, 64, 72))
Esempio n. 7
0
 def test_center(self):
     hg = hexutil.HexGrid(32)
     self.assertEqual(hg.center(hexutil.Hex(1, 1)), (32, 54))
Esempio n. 8
0
 def test_corners(self):
     self.assertEqual(
         hexutil.HexGrid(32).corners(hexutil.Hex(1, 1)), [(64, 72),
                                                          (32, 90), (0, 72),
                                                          (0, 36), (32, 18),
                                                          (64, 36)])
Esempio n. 9
0
 def test_height(self):
     self.assertEqual(hexutil.HexGrid(32).height, 18)
Esempio n. 10
0
import plotly.graph_objects as go
import pyproj
import random
from shapely import affinity
from shapely.geometry import LineString, Point, Polygon, MultiPolygon
from shapely.ops import unary_union
# from sklearn import manifold

path = "/home/michal/dev/cartograms/cartogram/"

# set up hexagons grid
# https://en.wikipedia.org/wiki/Hexagon
R = math.sqrt(2 / 3 / math.sqrt(3))
r = math.cos(math.pi / 6) * R
# https://github.com/stephanh42/hexutil
hexgrid = hexutil.HexGrid(width=r, height=R / 2)


# loss function - distances of the corners and center from polygon and continent
# @h center of hexagon Point
# @polygon the Polygon
def _loss(h, polygon):
    loss = 0
    corners = hexgrid.corners(h)
    for corner in corners:
        p = Point(corner)
        loss += p.distance(polygon)
        loss += p.distance(continent['multipolygon'])
    loss += Point(hexgrid.center(h)).distance(polygon)
    return loss