def new_origin_x(cls, width: float, length: float) -> "DirectedRectangle": """Creates a new rect, centered at origin, headed in direction of the X axis.""" center = Point(0, 0) top_right = center + Vec2(length / 2, width / 2) shape = Polygon.from_points([ top_right, top_right - Vec2(0, width), top_right - Vec2(length, width), top_right - Vec2(length, 0), ]) return cls(Ray(center, Vec2(1, 0)), shape)
from timeit import timeit from planar import Vec2 from planar.polygon import Polygon times = 500 def rand_pt(span=10): return Vec2(random() * span - 0, random() * span - 0.5) pts = [Vec2(i, random() * 10.0 + 5.001) for i in range(359)] for sides in [4, 5, 6, 7, 8, 9, 10, 20, 40, 80, 160, 320, 640]: angles = sorted(set(random() * 360.0 for i in range(sides))) if random() > 0.5: angles.reverse() poly = Polygon((Vec2.polar(a, 5) for a in angles)) assert poly.is_convex tangents = poly._pt_tangents cvx_tangents = poly.tangents_to_point for pt in pts: assert not poly.contains_point(pt) tans = tangents(pt) cvx_tans = cvx_tangents(pt) assert tans == cvx_tans, (tans, cvx_tans, sides, pt, list(poly)) def null(): pt_tangents = poly._pt_tangents for pt in pts: pass
import itertools def rand_pt(span=10): return Vec2(random() * span - 0.5, random() * span - 0.5) def rand_pts(count, span=10): return [rand_pt(span) for i in range(count)] polys = [] # Regular polygons regulars = [ Polygon.regular(i * 2 + 3, 10, angle=random() * 360.0) for i in range(20) ] stars = [ Polygon.star(i + 2, 5, random() * 10, angle=random() * 360.0) for i in range(20) ] rands = [Polygon(rand_pts(i * 2 + 4)) for i in range(100)] pts = [rand_pt(20) for i in range(20000)] ins = 0 # confirm that the algorithms agree for poly in itertools.chain(regulars, stars): crossing_test = poly._pnp_crossing_test winding_test = poly._pnp_winding_test contains_test = poly.contains_point
from planar.polygon import Polygon times = 500 def rand_pt(span=10): return Vec2(random() * span - 0, random() * span - 0.5) pts = [Vec2(i, random() * 10.0 + 5.001) for i in range(359)] for sides in [4, 5, 6, 7, 8, 9, 10, 20, 40, 80, 160, 320, 640]: angles = sorted(set(random() * 360.0 for i in range(sides))) if random() > 0.5: angles.reverse() poly = Polygon((Vec2.polar(a, 5) for a in angles)) assert poly.is_convex tangents = poly._pt_tangents cvx_tangents = poly.tangents_to_point for pt in pts: assert not poly.contains_point(pt) tans = tangents(pt) cvx_tans = cvx_tangents(pt) assert tans == cvx_tans, (tans, cvx_tans, sides, pt, list(poly)) def null(): pt_tangents = poly._pt_tangents for pt in pts: pass