Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_merger_rectangulartile.py
diff options
context:
space:
mode:
Diffstat (limited to 'ep_merger_rectangulartile.py')
-rw-r--r--ep_merger_rectangulartile.py112
1 files changed, 112 insertions, 0 deletions
diff --git a/ep_merger_rectangulartile.py b/ep_merger_rectangulartile.py
new file mode 100644
index 0000000..1db1736
--- /dev/null
+++ b/ep_merger_rectangulartile.py
@@ -0,0 +1,112 @@
+# coding: UTF8
+# Copyright 2009 Thomas Jourdan
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import random
+import ka_random
+import model_allele
+
+class RectangularTileMerger(model_allele.Allele):
+ """RectangularTileMerger: Reverse the layer horizontally or vertically.
+ inv: self.xTiles > 0
+ inv: self.yTiles > 0
+ """
+
+ def __init__(self, trunk):
+ """Constructor for a flip merger."""
+ super(RectangularTileMerger, self).__init__(trunk)
+ self.xTiles = 1
+ self.yTiles = 1
+
+ def __eq__(self, other):
+ """Equality based on fliping horizontally or vertically."""
+ equal = isinstance(other, RectangularTileMerger) \
+ and self.xTiles == other.xTiles \
+ and self.yTiles == other.yTiles
+ return equal
+
+ def randomize(self):
+ """Randomize the layers components."""
+ self.xTiles = random.randint(2, 4)
+ self.yTiles = random.randint(2, 4)
+
+ def mutate(self):
+ """Make small random changes to the layers components."""
+ if ka_random.is_mutating():
+ self.xTiles += random.randint(-1, 1)
+ self.xTiles = 1 if self.xTiles < 1 else self.xTiles
+ if ka_random.is_mutating():
+ self.yTiles += random.randint(-1, 1)
+ self.yTiles = 1 if self.yTiles < 1 else self.yTiles
+
+ def shuffle(self):
+ """Shuffle similar componets."""
+ pass
+
+ def crossingover(self, other):
+ """
+ pre: isinstance(other, RectangularTileMerger)
+ pre: isinstance(self, RectangularTileMerger)
+ # check for distinct references, needs to copy content, not references
+ post: __return__ is not self
+ post: __return__ is not other
+ """
+ new_one = RectangularTileMerger(self.get_trunk())
+ cross_sequence = ka_random.crossing_sequence(2)
+ new_one.xTiles = self.xTiles if cross_sequence[0] else other.xTiles
+ new_one.yTiles = self.yTiles if cross_sequence[1] else other.yTiles
+ return new_one
+
+ def draw_single_layer(self, single_layer, mask_layer, ctx, width, height):
+ """
+ pre: single_layer is not None
+ pre: ctx is not None
+ pre: width > 0
+ pre: height > 0
+ pre: width == height
+ """
+ # repeat painting layer
+ dx, dy = 1.0 / self.xTiles, 1.0 / self.yTiles
+ for tx in range(self.xTiles):
+ for ty in range(self.yTiles):
+ ctx.save()
+ ctx.translate((tx-0.5*self.xTiles)*dx+0.5*dx, \
+ (ty-0.5*self.yTiles)*dy+0.5*dy)
+ ctx.scale(dx, dy)
+ ctx.save()
+ single_layer.draw(ctx, width, height)
+ ctx.restore()
+ ctx.restore()
+
+ def explain(self):
+ """
+ post len(__return__) == 3
+ """
+ return u'Rectangular tile merger: %d*x, %d*y' \
+ % (self.xTiles, self.yTiles), \
+ None, \
+ None
+
+ def copy(self):
+ """A copy constructor.
+ post: isinstance(__return__, RectangularTileMerger)
+ # check for distinct references, needs to copy content, not references
+ post: __return__ is not self
+ """
+ new_one = RectangularTileMerger(self.get_trunk())
+ new_one.xTiles = self.xTiles
+ new_one.yTiles = self.yTiles
+ return new_one