Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilip Withnall <philip@tecnocode.co.uk>2013-08-19 21:11:56 (GMT)
committer Philip Withnall <philip@tecnocode.co.uk>2013-08-19 21:11:56 (GMT)
commit51385bb99f2659c94213e65a476baf84628685d3 (patch)
tree3b74073d9ac65a1f6109a86a76cecb9058347733
parentcc64775b91e7a9b9c05d82b5d3fada147999dd5f (diff)
Tidy up cell indexing throughout the code
-rwxr-xr-xPascalTriangle.activity/pascaltriangle.py78
1 files changed, 38 insertions, 40 deletions
diff --git a/PascalTriangle.activity/pascaltriangle.py b/PascalTriangle.activity/pascaltriangle.py
index 9a70ee4..d779432 100755
--- a/PascalTriangle.activity/pascaltriangle.py
+++ b/PascalTriangle.activity/pascaltriangle.py
@@ -128,6 +128,13 @@ class PascalTriangleActivity(activity.Activity):
self._drawing_area.queue_draw()
+ def _update_current_cell(self, index):
+ if index != self._current_cell:
+ self._current_cell = index
+ self._current_cell_text = ''
+ widget.queue_draw()
+
+
"""
Calculate the number of cells in the triangle. This is the Nth triangle
number, where N is the triangle_size. The formula for this is 1/2*N*(N+1).
@@ -157,7 +164,10 @@ class PascalTriangleActivity(activity.Activity):
are both 0-based. This is equivalent to calculating the Binomial coefficient
of (row choose column).
"""
- def _calculate_pascal_number(self, row, column):
+ def _calculate_pascal_number(self, index):
+ row = index[0]
+ column = index[1]
+
num = math.factorial(row)
denom = math.factorial(column) * math.factorial(row - column)
return num / denom
@@ -183,27 +193,20 @@ class PascalTriangleActivity(activity.Activity):
for row_index in range(self._triangle_size):
row_order = row_index + 1
for column_index in range(row_order):
+ index = (row_index, column_index)
(cell_x, cell_y) = self._calculate_cell_position(base_width,
- cell_width, cell_height,
- row_index, column_index)
+ cell_width, cell_height, index)
actual_radius_sq = (cell_x - event.x) ** 2 + \
(cell_y - event.y) ** 2
if actual_radius_sq <= radius ** 2:
- # Found the cell. Update the current cell and queue a
- # redraw.
- self._current_cell = (row_index, column_index)
- self._current_cell_text = ''
- widget.queue_draw()
-
+ # Found the cell.
+ self._update_current_cell(index)
return True
- # No cell found? Clear the current cell and queue a redraw.
- self._current_cell = (-1, -1)
- self._current_cell_text = ''
- widget.queue_draw()
-
+ # No cell found? Clear the current cell.
+ self._update_current_cell((-1, -1))
return True
@@ -275,50 +278,49 @@ class PascalTriangleActivity(activity.Activity):
for row_index in range(self._triangle_size):
row_order = row_index + 1
for column_index in range(row_order):
+ index = (row_index, column_index)
+
# Calculate the cell position.
(cell_x, cell_y) = self._calculate_cell_position(base_width,
- cell_width, cell_height,
- row_index, column_index)
+ cell_width, cell_height, index)
# Move to the cell position and draw the cell.
ctx.move_to(cell_x, cell_y)
- self._draw_cell(ctx,
- row_index, column_index,
- cell_width, cell_height)
+ self._draw_cell(ctx, index, cell_width, cell_height)
return True
def _calculate_cell_position(self, base_width, cell_width,
- cell_height, row_index, column_index):
+ cell_height, index):
# Calculate the cell position. Add an offset every odd row so
# the triangle is balanced. Each row is only 2/3 of cell_height
# because the hexagons interlock as they tesselate.
cell_x = self._padding + \
- base_width / 2.0 - (cell_width / 2.0 * row_index) + \
- cell_width * column_index
+ base_width / 2.0 - (cell_width / 2.0 * index[0]) + \
+ cell_width * index[1]
cell_y = self._padding + cell_height / 2.0 + \
- (cell_height * row_index * (2.0 / 3.0))
+ (cell_height * index[0] * (2.0 / 3.0))
return (cell_x, cell_y)
- def _get_cell_background(self, row_index, column_index):
- if (row_index, column_index) == self._current_cell:
+ def _get_cell_background(self, index):
+ if index == self._current_cell:
return cairo.SolidPattern(0.541, 0.886, 0.204) # green
elif self._show_hints and self._current_cell != (-1, -1) and \
(self._current_cell[1] == 0 or \
self._current_cell[1] == self._current_cell[0]) and \
- (column_index == 0 or column_index == row_index):
+ (index[1] == 0 or index[1] == index[0]):
return cairo.SolidPattern(0.447, 0.624, 0.812) # blue
- elif self._show_hints and row_index == self._current_cell[0] - 1 and \
- (column_index == self._current_cell[1] - 1 or \
- column_index == self._current_cell[1]):
+ elif self._show_hints and index[0] == self._current_cell[0] - 1 and \
+ (index[1] == self._current_cell[1] - 1 or \
+ index[1] == self._current_cell[1]):
return cairo.SolidPattern(0.988, 0.914, 0.310) # yellow
else:
return cairo.SolidPattern(1.0, 1.0, 1.0) # white
- def _draw_cell(self, ctx, row_index, column_index, cell_width, cell_height):
+ def _draw_cell(self, ctx, index, cell_width, cell_height):
centre = ctx.get_current_point()
# Draw the cell outline as a hexagon and fill it.
@@ -333,17 +335,16 @@ class PascalTriangleActivity(activity.Activity):
ctx.set_source_rgb(0.0, 0.0, 0.0)
ctx.stroke_preserve()
- ctx.set_source(self._get_cell_background(row_index, column_index))
+ ctx.set_source(self._get_cell_background(index))
ctx.fill()
# Write its number if it's a non-empty cell. If it's an empty cell,
# write a question mark unless it's the selected cell.
cell_text = None
- if not (row_index, column_index) in self._blank_cells:
- cell_text = str(self._calculate_pascal_number(row_index,
- column_index))
+ if not index in self._blank_cells:
+ cell_text = str(self._calculate_pascal_number(index))
ctx.set_source_rgb(0.0, 0.0, 0.0) # black
- elif (row_index, column_index) != self._current_cell:
+ elif index != self._current_cell:
# TRANS: This is the text shown in cells which haven't yet
# been filled in by the user.
cell_text = _('?')
@@ -371,13 +372,10 @@ class PascalTriangleActivity(activity.Activity):
def _check_current_cell_text(self, widget):
# Check whether the answer is correct. If so, change the cell to be
# uneditable.
- expected_num = self._calculate_pascal_number(self._current_cell[0],
- self._current_cell[1])
+ expected_num = self._calculate_pascal_number(self._current_cell)
if int(self._current_cell_text) == expected_num:
self._blank_cells.remove(self._current_cell)
- self._current_cell = (-1, -1)
- self._current_cell_text = ''
- widget.queue_draw()
+ self._update_current_cell((-1, -1))
# Check whether all blank cells have been filled.
if len(self._blank_cells) == 0: