def main(): house = Point(10, 9) print('House is at X:', house.x) print('House is at Y:', house.y) work = Point(5, 2) print('House to work distance:', house.distance_to(work)) city = Rectangle(Point(5, 5), 10, 10) print('City corner is:', city.bottom_left_corner) print('City width:', city.w) print('City height:', city.h) print('House in city:', city.contains(house)) print('Work in city:', city.contains(work)) city_center = city.find_center() print('City center:', city_center) cats_house = Point(10, 9) identical_city = Rectangle(Point(5, 5), 10, 10) different_city = Rectangle(Point(13, 13), 10, 10) print("My cat's house and mine are equal:", house == cats_house) print('Two identical cities are equal:', city == identical_city) print('Two different cities are equal:', city == different_city) house.move_by(1, -1) print('After moving my house:', house)
def range(self, rectangle: Rectangle, node: Node) -> List[Point]: if not node.rectangle.intersects(rectangle): return [] points = [] if rectangle.contains(node.point): points.append(node.point) if node.left.__class__ != EmptyNode: points += self.range(rectangle, node.left) if node.right.__class__ != EmptyNode: points += self.range(rectangle, node.right) return points
def box_density(self, radius, box: Rectangle): bigger_box = Rectangle(box.px - radius, box.py - radius, box.qx + radius, box.qy + radius) my_polys = PolygonSet() for s in self.polys: for p in s.points: if bigger_box.contains(p): my_polys.add(s, allow_intersecting=True) times = 2000 bad = 0 for i in range(times): p = box.rand_point() if my_polys.intersect_with_circle(p, radius): bad += 1 return bad / times
def top_edge_windows(self): h, w = self.cropped_image_size frame_rect = Rectangle(pos=(0, 0), size=(w, h)) ws = 16 * 4 // self.scale left_edge_ws = 48 * 4 // self.scale result = [] x1, x2 = left_edge_ws, w - ws - left_edge_ws x, y = x1, 4 * 4 // self.scale while x <= x2: window = Rectangle(pos=(x, y), size=ws) assert frame_rect.contains(window) result.append(window) x += ws / 2 return result
def left_edge_windows(self): result = [] h, w = self.cropped_image_size frame_rect = Rectangle(pos=(0, 0), size=(w, h)) for width, height, x, y in (48, 48, 0, 16), (32, 32, 0, 0), (32, 32, 8, 0), (32, 32, 16, 0), (32, 32, 0, 8), (32, 32, 8, 8), (32, 32, 16, 8): window = Rectangle(pos=(x, y), size=(width, height)) * 4 // self.scale assert frame_rect.contains(window) result.append(window) return result
from point import Point from rectangle import Rectangle p2 = Point(100, 80) box_2 = Rectangle(p2, 5, 10) print(f'box_2: {box_2}') p2.x = 110 print(f'box_2: {box_2}') print(f'Dist from (0,0): {p2.distance_from_origin()}') box_2.grow(100, 100) print(f'box_2: {box_2}') box_2.move(100, 100) print(f'box_2: {box_2}') print(f'Area: {box_2.area()}') print(f'Perimeter: {box_2.perimeter()}') box_2.flip() print(f'box_2: {box_2}') p3 = Point(200, 180) box_3 = Rectangle(p3, 5, 10) p4 = Point(700, 700) box_4 = Rectangle(p4, 5, 10) print(f'box4: {box_4}') print("Box 2 contains P2: ", box_2.contains(p2)) print("Box 2 contains P3: ", box_2.contains(p3)) print("Box 2 overlaps with Box3: ", box_2.overlap(box_3)) print(box_2.overlap(box_4))
def test_contains(self): rectangle = Rectangle(-1, 1, -1, 1) self.assertTrue(rectangle.contains(Point(0, 0))) self.assertFalse(rectangle.contains(Point(10, 0)))