Skip to content

Commit

Permalink
Back to the old distance measurement
Browse files Browse the repository at this point in the history
The optimized version wasn't always faster, and was missing a whole branch for
the case zeros > 0 and ones < 0. Big oops!
  • Loading branch information
ezheidtmann committed May 3, 2015
1 parent c54a282 commit 2583281
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 85 deletions.
41 changes: 3 additions & 38 deletions lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ def __init__(self, instructions):

self.instructions = instructions

def _distance_to_slow(self, other):
return max(abs(other.start[0] - self.end[0]), abs(other.start[1] - self.end[1]))

def distance_to(self, other):
"""
Compute distance between two glyphs (other.start - self.end)
Expand All @@ -58,42 +55,10 @@ def distance_to(self, other):
to move betwen points is proportional to the greatest distance each
servo has to move.
"""
# Optimized equivalent to:
#
#

zeros = other.start[0] - self.end[0]
ones = other.start[1] - self.end[1]
if zeros < 0:
if ones < 0:
if zeros < ones:
return -zeros
return -ones
if -zeros < ones:
return ones
return -zeros

if zeros < ones:
return ones
return zeros

def _distance_to_if_other_reversed_slow(self, other):
return max(abs(other.end[0] - self.end[0]), abs(other.end[1] - self.end[1]))
return max(abs(other.start[0] - self.end[0]), abs(other.start[1] - self.end[1]))

def distance_to_if_other_reversed(self, other):
zeros = other.end[0] - self.end[0]
ones = other.end[1] - self.end[1]
if zeros < 0:
if ones < 0:
if zeros < ones:
return -zeros
return -ones
if -zeros < ones:
return ones
return -zeros

if zeros < ones:
return ones
return zeros
return max(abs(other.end[0] - self.end[0]), abs(other.end[1] - self.end[1]))

def _reversed_instructions(self):
"""
Expand Down
47 changes: 0 additions & 47 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,52 +264,5 @@ def test_three_continuing_glyphs(self):
"C17,5700,3600,2,END\n"
"C14,END")

def test_distance_to(self):
one = self.makeGlyphFromText("""
C17,5782,3576,2,END
C13,END
C17,5700,3600,2,END
C14,END
""")

two = self.makeGlyphFromText("""
C17,5838,3619,2,END
C13,END
C17,5782,3576,2,END
C14,END
""")

three = self.makeGlyphFromText("""
C17,5701,3619,2,END
C13,END
C17,5782,3576,2,END
C14,END
""")

# one -> two == max(5838-5700, 3619-3600)
# one -> two_reversed == max(...

self.assertEqual(one.distance_to(two), one._distance_to_slow(two))
self.assertEqual(two.distance_to(one), two._distance_to_slow(one))
self.assertEqual(one.distance_to_if_other_reversed(two),
one._distance_to_if_other_reversed_slow(two))
self.assertEqual(two.distance_to_if_other_reversed(one),
two._distance_to_if_other_reversed_slow(one))

self.assertEqual(three.distance_to(two), three._distance_to_slow(two))
self.assertEqual(two.distance_to(three), two._distance_to_slow(three))
self.assertEqual(three.distance_to_if_other_reversed(two),
three._distance_to_if_other_reversed_slow(two))
self.assertEqual(two.distance_to_if_other_reversed(three),
two._distance_to_if_other_reversed_slow(three))

self.assertEqual(one.distance_to(three), one._distance_to_slow(three))
self.assertEqual(three.distance_to(one), three._distance_to_slow(one))
self.assertEqual(one.distance_to_if_other_reversed(three),
one._distance_to_if_other_reversed_slow(three))
self.assertEqual(three.distance_to_if_other_reversed(one),
three._distance_to_if_other_reversed_slow(one))


if __name__ == '__main__':
unittest.main()

0 comments on commit 2583281

Please sign in to comment.