Skip to content

Commit

Permalink
Fixed issue #1
Browse files Browse the repository at this point in the history
Scaling of the circle was not corect when the target enclosure was not centered on (0, 0).
  • Loading branch information
elmotec committed Apr 27, 2019
1 parent 24c170b commit f68f69d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
10 changes: 6 additions & 4 deletions circlify.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,13 @@ def scale(circles, enclosure, target):
"""
r = target.r / enclosure.r
dx = target.x - enclosure.x
dy = target.y - enclosure.y
t_x, t_y = target.x, target.y
e_x, e_y = enclosure.x, enclosure.y
scaled = []
for circle in circles:
x_c, y_c, r_c = circle
scaled.append(Circle((x_c + dx) * r, (y_c + dy) * r, r_c * r))
c_x, c_y, c_r = circle
scaled.append(Circle((c_x - e_x) * r + t_x,
(c_y - e_y) * r + t_y, c_r * r))
return scaled


Expand All @@ -347,6 +348,7 @@ def enclose(circles):
return e


# FIXME: remove with_enclosure because it complicates the interface.
def circlify(data, target_enclosure=None, with_enclosure=False):
"""Pack and enclose circles whose radius is linked to the input data.
Expand Down
36 changes: 35 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import unittest

from circlify import Circle, circlify
from circlify import Circle, circlify, scale


# Set this variable to True to get a display of the layout (req matlplotlib)
Expand Down Expand Up @@ -137,6 +137,40 @@ def test_circlify(self):
self.assertEqual(actual, expected)


class EnclosureScalingTestCase(unittest.TestCase):
"""Test scale function"""

def test_simple_zoom(self):
"""Trivial zoom test when the enclosure is the same as the circle."""
input = Circle(0, 0, 0.5)
target = Circle(0, 0, 1.0)
actual = scale([input], input, target)
self.assertEqual([target], actual)

def test_simple_zoom_off_center(self):
"""Zoom test with off center circle equal to enclosure."""
input = Circle(0.5, 0.5, 0.5)
target = Circle(0.5, 0.5, 1.0)
actual = scale([input], input, target)
self.assertEqual([target], actual)

def test_simple_zoom_and_translation(self):
"""Pan and zoom test with off center circle equal to enclosure."""
input = Circle(0.5, 0.5, 0.5)
target = Circle(-0.5, 0, 1.0)
actual = scale([input], input, target)
self.assertEqual([target], actual)

def test_zoom_with_enclosure(self):
"""Zoom test with off center circle and difference enclosure"""
input = Circle(1.0, 0.0, 1.0)
enclosure = Circle(0.0, 0.0, 2.0)
target = Circle(0.0, 0.0, 1.0)
actual = scale([input], enclosure, target)
expected = Circle(0.5, 0.0, 0.5)
self.assertEqual([expected], actual)


if __name__ == '__main__':
import logging
logging.basicConfig(level='INFO')
Expand Down

0 comments on commit f68f69d

Please sign in to comment.