def __new__(cls, p1, p2, **kwargs): if not isinstance(p1, Point) or not isinstance(p2, Point): raise TypeError("%s.__new__ requires Point instances" % cls.__name__) if p1 == p2: raise RuntimeError("%s.__new__ requires two distinct points" % cls.__name__) return GeometryEntity.__new__(cls, p1, p2, **kwargs)
def __new__(cls, center=None, hradius=None, vradius=None, eccentricity=None, **kwargs): hradius = sympify(hradius) vradius = sympify(vradius) eccentricity = sympify(eccentricity) if len(filter(None, (hradius, vradius, eccentricity))) != 2: raise ValueError, 'Exactly two arguments between "hradius", '\ '"vradius", and "eccentricity" must be not None."' if eccentricity is not None: if hradius is None: hradius = vradius / sqrt(1 - eccentricity**2) elif vradius is None: vradius = hradius * sqrt(1 - eccentricity**2) else: if hradius is None and vradius is None: raise ValueError("At least two arguments between hradius, " "vradius and eccentricity must not be none.") if center is None: center = Point(0, 0) if not isinstance(center, Point): raise TypeError("center must be a Point") if hradius == vradius: return Circle(center, hradius, **kwargs) return GeometryEntity.__new__(cls, center, hradius, vradius, **kwargs)
def __new__(cls, function, limits): fun = sympify(function) if not fun: raise GeometryError("%s.__new__ don't know how to handle" % cls.__name__); if not isinstance(limits, (list, tuple)) or len(limits) != 3: raise ValueError("Limits argument has wrong syntax"); return GeometryEntity.__new__(cls, fun, limits)
def __new__(cls, center=None, hradius=None, vradius=None, eccentricity=None, **kwargs): hradius = sympify(hradius) vradius = sympify(vradius) eccentricity = sympify(eccentricity) if center is None: center = Point(0, 0) else: center = Point(center) if len(filter(None, (hradius, vradius, eccentricity))) != 2: raise ValueError('Exactly two arguments of "hradius", '\ '"vradius", and "eccentricity" must not be None."') if eccentricity is not None: if hradius is None: hradius = vradius / sqrt(1 - eccentricity**2) elif vradius is None: vradius = hradius * sqrt(1 - eccentricity**2) if hradius == vradius: return Circle(center, hradius, **kwargs) return GeometryEntity.__new__(cls, center, hradius, vradius, **kwargs)
def __new__(cls, *args, **kwargs): if len(args) != 3: raise GeometryError("Triangle.__new__ requires three points") vertices = [Point(a) for a in args] # remove consecutive duplicates nodup = [] for p in vertices: if nodup and p == nodup[-1]: continue nodup.append(p) if len(nodup) > 1 and nodup[-1] == nodup[0]: nodup.pop() # last point was same as first # remove collinear points i = -3 while i < len(nodup) - 3 and len(nodup) > 2: a, b, c = sorted([nodup[i], nodup[i + 1], nodup[i + 2]]) if Point.is_collinear(a, b, c): nodup[i] = a nodup[i + 1] = None nodup.pop(i + 1) i += 1 vertices = filter(lambda x: x is not None, nodup) if len(vertices) == 3: return GeometryEntity.__new__(cls, *vertices, **kwargs) elif len(vertices) == 2: return Segment(*vertices, **kwargs) else: return Point(*vertices, **kwargs)
def __new__( cls, center=None, hradius=None, vradius=None, eccentricity=None, **kwargs): hradius = sympify(hradius) vradius = sympify(vradius) eccentricity = sympify(eccentricity) if center is None: center = Point(0, 0) else: center = Point(center) if len(filter(None, (hradius, vradius, eccentricity))) != 2: raise ValueError('Exactly two arguments of "hradius", ' '"vradius", and "eccentricity" must not be None."') if eccentricity is not None: if hradius is None: hradius = vradius / sqrt(1 - eccentricity**2) elif vradius is None: vradius = hradius * sqrt(1 - eccentricity**2) if hradius == vradius: return Circle(center, hradius, **kwargs) return GeometryEntity.__new__(cls, center, hradius, vradius, **kwargs)
def __new__(cls, function, limits): fun = sympify(function) if not ordered_iter(fun) or len(fun) != 2: raise ValueError("Function argument should be (x(t), y(t)) but got %s" % str(function)) if not ordered_iter(limits) or len(limits) != 3: raise ValueError("Limit argument should be (t, tmin, tmax) but got %s" % str(limits)) return GeometryEntity.__new__(cls, tuple(sympify(fun)), tuple(sympify(limits)))
def __new__(cls, p1, p2, **kwargs): p1 = Point(p1) p2 = Point(p2) if p1 == p2: # Rolygon returns lower priority classes...should LinearEntity, too? return p1 # raise ValueError("%s.__new__ requires two unique Points." % cls.__name__) return GeometryEntity.__new__(cls, p1, p2, **kwargs)
def __new__(cls, center, hradius, vradius, **kwargs): hradius = sympify(hradius) vradius = sympify(vradius) if not isinstance(center, Point): raise TypeError("center must be be a Point") if hradius == vradius: return Circle(center, hradius, **kwargs) return GeometryEntity.__new__(cls, center, hradius, vradius, **kwargs)
def __new__(cls, *args, **kwargs): if isinstance(args[0], (tuple, list, set)): coords = tuple([sympify(x) for x in args[0]]) else: coords = tuple([sympify(x) for x in args]) if len(coords) != 2: raise NotImplementedError("Only two dimensional points currently supported") return GeometryEntity.__new__(cls, *coords)
def __new__(cls, *args, **kwargs): vertices = GeometryEntity.extract_entities(args, remove_duplicates=False) if len(vertices) != 3: raise GeometryError("Triangle.__new__ requires three points") for p in vertices: if not isinstance(p, Point): raise GeometryError("Triangle.__new__ requires three points") return GeometryEntity.__new__(cls, *vertices, **kwargs)
def __new__(self, c, r, n, **kwargs): r = sympify(r) if not isinstance(c, Point): raise GeometryError("RegularPolygon.__new__ requires c to be a Point instance") if not isinstance(r, Basic): raise GeometryError("RegularPolygon.__new__ requires r to be a number or Basic instance") if n < 3: raise GeometryError("RegularPolygon.__new__ requires n >= 3") obj = GeometryEntity.__new__(self, c, r, n, **kwargs) return obj
def __new__(cls, function, limits): fun = sympify(function) if not is_sequence(fun) or len(fun) != 2: raise ValueError( "Function argument should be (x(t), y(t)) but got %s" % str(function)) if not is_sequence(limits) or len(limits) != 3: raise ValueError( "Limit argument should be (t, tmin, tmax) but got %s" % str(limits)) return GeometryEntity.__new__(cls, tuple(sympify(fun)), tuple(sympify(limits)))
def __new__(cls, *args, **kwargs): if iterable(args[0]): coords = Tuple(*args[0]) elif isinstance(args[0], Point): coords = args[0].args else: coords = Tuple(*args) if len(coords) != 2: raise NotImplementedError("Only two dimensional points currently supported") return GeometryEntity.__new__(cls, *coords)
def __new__(cls, *args, **kwargs): if iterable(args[0]): coords = Tuple(*args[0]) elif isinstance(args[0], Point): coords = args[0].args else: coords = Tuple(*args) if len(coords) != 2: raise NotImplementedError( "Only two dimensional points currently supported") return GeometryEntity.__new__(cls, *coords)
def __new__(self, c, r, n, rot=0, **kwargs): r, n, rot = sympify([r, n, rot]) c = Point(c) if not isinstance(r, Basic): raise GeometryError("RegularPolygon.__new__ requires r to be a number or Basic instance") if n < 3: raise GeometryError("RegularPolygon.__new__ requires n >= 3") obj = GeometryEntity.__new__(self, c, r, n, **kwargs) obj._n = n obj._center = c obj._radius = r obj._rot = rot return obj
def __new__(cls, *args, **kwargs): if iterable(args[0]): coords = Tuple(*args[0]) elif isinstance(args[0], Point): coords = args[0].args else: coords = Tuple(*args) if len(coords) != 2: raise NotImplementedError("Only two dimensional points currently supported") if kwargs.get('evaluate', True): coords = [nsimplify(c) for c in coords] return GeometryEntity.__new__(cls, *coords)
def __new__(cls, *args, **kwargs): if iterable(args[0]): coords = Tuple(*args[0]) elif isinstance(args[0], Point): coords = args[0].args else: coords = Tuple(*args) if len(coords) != 2: raise NotImplementedError( "Only two dimensional points currently supported") if kwargs.get('evaluate', True): coords = [simplify(nsimplify(c, rational=True)) for c in coords] return GeometryEntity.__new__(cls, *coords)
def __new__(self, c, r, n, rot=0, **kwargs): r, n, rot = sympify([r, n, rot]) c = Point(c) if not isinstance(r, Basic): raise GeometryError( "RegularPolygon.__new__ requires r to be a number or Basic instance" ) if n < 3: raise GeometryError("RegularPolygon.__new__ requires n >= 3") obj = GeometryEntity.__new__(self, c, r, n, **kwargs) obj._n = n obj._center = c obj._radius = r obj._rot = rot return obj
def __new__(cls, *args, **kwargs): c, r = None, None if len(args) == 3 and isinstance(args[0], Point): from polygon import Triangle t = Triangle(args[0], args[1], args[2]) if t.area == 0: raise GeometryError("Cannot construct a circle from three collinear points") c = t.circumcenter r = t.circumradius elif len(args) == 2: # Assume (center, radius) pair c = args[0] r = sympify(args[1]) if not (c is None or r is None): return GeometryEntity.__new__(cls, c, r, **kwargs) raise GeometryError("Circle.__new__ received unknown arguments")
def __new__(cls, *args, **kwargs): c, r = None, None if len(args) == 3: args = [Point(a) for a in args] if Point.is_collinear(*args): raise GeometryError("Cannot construct a circle from three collinear points") from polygon import Triangle t = Triangle(*args) c = t.circumcenter r = t.circumradius elif len(args) == 2: # Assume (center, radius) pair c = Point(args[0]) r = sympify(args[1]) if not (c is None or r is None): return GeometryEntity.__new__(cls, c, r, **kwargs) raise GeometryError("Circle.__new__ received unknown arguments")
def __new__(cls, *args, **kwargs): if kwargs.get('n', 0): n = kwargs.pop('n') args = list(args) # return a virtual polygon with n sides if len(args) == 2: # center, radius args.append(n) elif len(args) == 3: # center, radius, rotation args.insert(2, n) return RegularPolygon(*args, **kwargs) vertices = [Point(a) for a in args] # remove consecutive duplicates nodup = [] for p in vertices: if nodup and p == nodup[-1]: continue nodup.append(p) if len(nodup) > 1 and nodup[-1] == nodup[0]: nodup.pop() # last point was same as first # remove collinear points unless they are shared points got = set() shared = set() for p in nodup: if p in got: shared.add(p) else: got.add(p) i = -3 while i < len(nodup) - 3 and len(nodup) > 2: a, b, c = sorted([nodup[i], nodup[i + 1], nodup[i + 2]]) if b not in shared and Point.is_collinear(a, b, c): nodup[i] = a nodup[i + 1] = None nodup.pop(i + 1) i += 1 vertices = filter(lambda x: x is not None, nodup) if len(vertices) > 3: rv = GeometryEntity.__new__(cls, *vertices, **kwargs) elif len(vertices) == 3: return Triangle(*vertices, **kwargs) elif len(vertices) == 2: return Segment(*vertices, **kwargs) else: return Point(*vertices, **kwargs) # reject polygons that have intersecting sides unless the # intersection is a shared point or a generalized intersection. # A self-intersecting polygon is easier to detect than a # random set of segments since only those sides that are not # part of the convex hull can possibly intersect with other # sides of the polygon...but for now we use the n**2 algorithm # and check all sides with intersection with any preceding sides hit = _symbol('hit') if not rv.is_convex: sides = rv.sides for i, si in enumerate(sides): pts = si[0], si[1] ai = si.arbitrary_point(hit) for j in xrange(i): sj = sides[j] if sj[0] not in pts and sj[1] not in pts: aj = si.arbitrary_point(hit) tx = (solve(ai[0] - aj[0]) or [S.Zero])[0] if tx.is_number and 0 <= tx <= 1: ty = (solve(ai[1] - aj[1]) or [S.Zero])[0] if (tx or ty) and ty.is_number and 0 <= ty <= 1: print ai, aj raise GeometryError("Polygon has intersecting sides.") return rv
def __new__(cls, *args, **kwargs): if kwargs.get('n', 0): n = kwargs.pop('n') args = list(args) # return a virtual polygon with n sides if len(args) == 2: # center, radius args.append(n) elif len(args) == 3: # center, radius, rotation args.insert(2, n) return RegularPolygon(*args, **kwargs) vertices = [Point(a) for a in args] # remove consecutive duplicates nodup = [] for p in vertices: if nodup and p == nodup[-1]: continue nodup.append(p) if len(nodup) > 1 and nodup[-1] == nodup[0]: nodup.pop() # last point was same as first # remove collinear points unless they are shared points got = set() shared = set() for p in nodup: if p in got: shared.add(p) else: got.add(p) i = -3 while i < len(nodup) - 3 and len(nodup) > 2: a, b, c = sorted([nodup[i], nodup[i + 1], nodup[i + 2]]) if b not in shared and Point.is_collinear(a, b, c): nodup[i] = a nodup[i + 1] = None nodup.pop(i + 1) i += 1 vertices = filter(lambda x: x is not None, nodup) if len(vertices) > 3: rv = GeometryEntity.__new__(cls, *vertices, **kwargs) elif len(vertices) == 3: return Triangle(*vertices, **kwargs) elif len(vertices) == 2: return Segment(*vertices, **kwargs) else: return Point(*vertices, **kwargs) # reject polygons that have intersecting sides unless the # intersection is a shared point or a generalized intersection. # A self-intersecting polygon is easier to detect than a # random set of segments since only those sides that are not # part of the convex hull can possibly intersect with other # sides of the polygon...but for now we use the n**2 algorithm # and check all sides with intersection with any preceding sides hit = _symbol('hit') if not rv.is_convex: sides = rv.sides for i, si in enumerate(sides): pts = si[0], si[1] ai = si.arbitrary_point(hit) for j in xrange(i): sj = sides[j] if sj[0] not in pts and sj[1] not in pts: aj = si.arbitrary_point(hit) tx = (solve(ai[0] - aj[0]) or [S.Zero])[0] if tx.is_number and 0 <= tx <= 1: ty = (solve(ai[1] - aj[1]) or [S.Zero])[0] if (tx or ty) and ty.is_number and 0 <= ty <= 1: print ai, aj raise GeometryError( "Polygon has intersecting sides.") return rv