def test_polyline_get_Iz(): pl = PolyLine([(0, 3), (10, 3), (10, 0), (0, 0)], copy_data=True) for angle in np.random.uniform(-180, 180, 1000): pl1 = pl.add_vec(Vec2(10, 20)) plt = pl1.transform(Mat2.from_angle(angle, True)) cm = plt.get_center_mass() assert plt.get_Iz(cm) == approx((10**2 + 3**2) / 12)
def get_standart(cls, w: float, h: float, n: int, m: int): obs = [ PolyLine( [Vec2(0, 0), Vec2(w, 0), Vec2(w, h), Vec2(0, h)], enclosed=True), PolyLine([ Vec2(-10, -10), Vec2(w + 10, -10), Vec2(10 + w, 10 + h), Vec2(-10, 10 + h) ], enclosed=True) ] w1, h1 = w / n, h / m r = min(w1, h1) * 0.3 for i in range(n): for j in range(m): x = w1 / 2 + w1 * i + np.random.uniform(-0.1, 0.1) * w1 y = h1 / 2 + h1 * j + np.random.uniform(-0.1, 0.1) * h1 if (i == 0 and j == 0): pos_1 = Vec2(x, y) continue if (i == (n - 1) and j == (m - 1)): pos_2 = Vec2(x, y) continue ri = r * np.random.uniform(0.9, 1.1) nn = np.random.randint(3, 6) dx = w1 * np.random.uniform(0.02, 0.1) dy = h1 * np.random.uniform(0.02, 0.1) obs.append(rnd_polygon(x, y, ri, nn, dx, dy)) return cls(obs, pos_1, pos_2)
def rnd_polygon(x, y, r, n, dx, dy, angle0=None): v = Vec2(r, 0) v.rotate_(angle0 if angle0 else np.random.uniform(0, 360), degrees=True) vs = [ v.rotate(i * (360 / n), degrees=True) + rnd_vec(x, y, dx, dy) for i in range(n) ] return PolyLine(vs, enclosed=True)
def get_vis_polygon(self, pos: Vec2, r: float, alpha: float, thetta: float) -> PolyLine: n_rays = 10 angles = np.linspace(-thetta + alpha, thetta + alpha, n_rays) p2s = [ Vec2(r, 0).rotate(angle, degrees=True).add(pos) for angle in angles ] ps = [pos.copy()] for p2 in p2s: p1, pi = self.intersected_segment(pos, p2) ps.append(pi) return PolyLine(ps)
def test_PolyLine_is_in2(): pl = PolyLine([(-10, 1), (1, 0), (-10, -1)], copy_data=True) assert pl.is_in(Vec2(0, 0))
def test_polyline_is_selfintersect1(): pl = PolyLine([(0, 3), (10, 3), (10, 0), (0, 0)], copy_data=True) assert not pl.is_selfintersect()
def test_PolyLine_is_in(): pl = PolyLine([(0, 0), (1, 0), (0, 1)], copy_data=True) assert pl.is_in(Vec2(0.5, 0.1))
def test_polyline_area5(): pl = PolyLine([(1, 1), (2, 1), (1, 3)], copy_data=True) assert pl.get_area() == approx(1)
def test_polyline_area4(): pl = PolyLine([(1, 2), (11, 2), (10, 0), (0, 0)], copy_data=True) for angle in np.random.uniform(-180, 180, 1000): pl1 = pl.add_vec(Vec2(10, 20)) plt = pl1.transform(Mat2.from_angle(angle, True)) assert plt.get_area() == approx(20)
def test_polyline_area3(): a = 1000 pl = PolyLine([(1 + a, 2 + a), (11 + a, 2 + a), (10 + a, 0 + a), (0 + a, 0 + a)], copy_data=True) assert pl.get_area() == approx(20)
def test_polyline_area2(): pl = PolyLine([(1, 2), (11, 2), (10, 0), (0, 0)], copy_data=True) assert pl.get_area() == approx(20)
def test_plyline_get_center_mass1(): a = 1000 pl = PolyLine([(1 + a, 2 + a), (11 + a, 2 + a), (10 + a, 0 + a), (0 + a, 0 + a)], copy_data=True) assert pl.get_center_mass() == (5.5 + a, 1 + a)