ekumenlabs / terminus

Library to create cities and terrains
Apache License 2.0
56 stars 29 forks source link

Make arc merge commutative #244

Open jazminlaila opened 7 years ago

jazminlaila commented 7 years ago

can_be_merged_with's implementation could be replaced by:

def can_be_merged_with(self, other):
        if self.__class__ is other.__class__ and \
           self.center_point().almost_equal_to(other.center_point(), 5) and \
           self.radius() == other.radius():
            return (self.end_heading() == other.start_heading() and
                    self.end_point().almost_equal_to(other.start_point(), 5)) or \
                (other.end_heading() == self.start_heading() and
                    other.end_point().almost_equal_to(self.start_point(), 5))

In that case, merge's implementation should be changed to merge in one order or the other depending on which order they can be appended one to the other.

Here are some tests:

def test_can_be_merged_with(self):
        arc1 = Arc(Point(1, 0), 90, 1, 90)
        arc2 = Arc(Point(0, 1), 180, 1, 90)
        arc3 = Arc(Point(0, 1), 0, 1, -90)
        arc4 = Arc(Point(-1, 0), 270, 1, 90)
        self.assertTrue(arc1.can_be_merged_with(arc2))
        self.assertTrue(arc2.can_be_merged_with(arc1))
        self.assertFalse(arc1.can_be_merged_with(arc3))
        self.assertFalse(arc1.can_be_merged_with(arc4))