geometry

View Source
class Geometry:
    """Generalized multidimensional shape class"""
    def __init__(self, parts=None, dimensions=None):
        """Create a new geometry object"""

        if parts:
            self.parts = parts
        else:
            self.parts = []

        if dimensions is None:
            self.dimensions = self.parts[0].dimensions + 1
        else:
            self.dimensions = dimensions


class Point(Geometry):
    def __init__(self):
        # super(Geometry, self).__init__()
        super().__init__(dimensions=0)

# 1D geometry convenience subclass
class Line(Geometry):
    def __init__(self):
        super().__init__(dimensions=1)

# 2D geometry convenience subclass
class Shape(Geometry):
    def __init__(self):
        super().__init__(dimensions=2)

# 3D geometry convenience subclass
class Solid(Geometry):
    def __init__(self):
        super().__init__(dimensions=3)

# 4D geometry convenience subclass
class Hypersolid(Geometry):
    def __init__(self):
        super().__init__(dimensions=4)



# should this subclass Geometry instead?
class Polygon(Shape):
    """General polygon class that extends the Shape class"""
    def __init__(self):
        """Create a new polygon"""
        super().__init__()
        self.sides: [line] = []
    def regular(self, sides, radius):
        """Define polygon's geometry as a regular polygon; one with equal sides and angles"""
        for s in range(sides):
            self.sides.append(Line())

# TODO: numerical precision setting

class Ellipse(Shape):
    pass

class Circle(Shape):
    """A geometric 2D circle with a certain radius; subclass of Shape"""
    def __init__(self, radius):
        super().__init__()
        self.radius: Scalar = radius
    def get_tangents(self):
        """Quickly calculate incline angle of tangent line for each cell rendered on circle outline; these will be used to render the outline in ASCII characters"""
        r = self.radius()
        # possibly move this code
        minigrid = np.zeros([r*2+1, r*2+1])
        # crossed_cells = minigrid
        # TODO: mirroring for efficiency?
        for x, y in np.ndindex(minigrid.shape):
            # print(5)
            # print(np.round(np.linalg.norm(np.array([x, y]) - np.array([r, r]))))
            if np.round(np.linalg.norm(np.array([x, y]) - np.array([r, r]))) == r:
                minigrid[x, y] = 1
        num_crossed = np.sum(minigrid)
        d_theta = 360 / num_crossed
        c = 0
        for x, y in np.ndindex(minigrid.shape):
            # if
            c += minigrid[x, y]
            # minigrid[x, y] = Angle(d_theta * c)
            minigrid[x, y] = d_theta * c * minigrid[x, y]
        return np.round(minigrid)
#   class Geometry:
View Source
class Geometry:
    """Generalized multidimensional shape class"""
    def __init__(self, parts=None, dimensions=None):
        """Create a new geometry object"""

        if parts:
            self.parts = parts
        else:
            self.parts = []

        if dimensions is None:
            self.dimensions = self.parts[0].dimensions + 1
        else:
            self.dimensions = dimensions

Generalized multidimensional shape class

#   Geometry(parts=None, dimensions=None)
View Source
    def __init__(self, parts=None, dimensions=None):
        """Create a new geometry object"""

        if parts:
            self.parts = parts
        else:
            self.parts = []

        if dimensions is None:
            self.dimensions = self.parts[0].dimensions + 1
        else:
            self.dimensions = dimensions

Create a new geometry object

#   class Point(Geometry):
View Source
class Point(Geometry):
    def __init__(self):
        # super(Geometry, self).__init__()
        super().__init__(dimensions=0)

Generalized multidimensional shape class

#   Point()
View Source
    def __init__(self):
        # super(Geometry, self).__init__()
        super().__init__(dimensions=0)

Create a new geometry object

#   class Line(Geometry):
View Source
class Line(Geometry):
    def __init__(self):
        super().__init__(dimensions=1)

Generalized multidimensional shape class

#   Line()
View Source
    def __init__(self):
        super().__init__(dimensions=1)

Create a new geometry object

#   class Shape(Geometry):
View Source
class Shape(Geometry):
    def __init__(self):
        super().__init__(dimensions=2)

Generalized multidimensional shape class

#   Shape()
View Source
    def __init__(self):
        super().__init__(dimensions=2)

Create a new geometry object

#   class Solid(Geometry):
View Source
class Solid(Geometry):
    def __init__(self):
        super().__init__(dimensions=3)

Generalized multidimensional shape class

#   Solid()
View Source
    def __init__(self):
        super().__init__(dimensions=3)

Create a new geometry object

#   class Hypersolid(Geometry):
View Source
class Hypersolid(Geometry):
    def __init__(self):
        super().__init__(dimensions=4)

Generalized multidimensional shape class

#   Hypersolid()
View Source
    def __init__(self):
        super().__init__(dimensions=4)

Create a new geometry object

#   class Polygon(Shape):
View Source
class Polygon(Shape):
    """General polygon class that extends the Shape class"""
    def __init__(self):
        """Create a new polygon"""
        super().__init__()
        self.sides: [line] = []
    def regular(self, sides, radius):
        """Define polygon's geometry as a regular polygon; one with equal sides and angles"""
        for s in range(sides):
            self.sides.append(Line())

General polygon class that extends the Shape class

#   Polygon()
View Source
    def __init__(self):
        """Create a new polygon"""
        super().__init__()
        self.sides: [line] = []

Create a new polygon

#   sides: '[line]'
#   def regular(self, sides, radius):
View Source
    def regular(self, sides, radius):
        """Define polygon's geometry as a regular polygon; one with equal sides and angles"""
        for s in range(sides):
            self.sides.append(Line())

Define polygon's geometry as a regular polygon; one with equal sides and angles

#   class Ellipse(Shape):
View Source
class Ellipse(Shape):
    pass

Generalized multidimensional shape class

Inherited Members
Shape
Shape
#   class Circle(Shape):
View Source
class Circle(Shape):
    """A geometric 2D circle with a certain radius; subclass of Shape"""
    def __init__(self, radius):
        super().__init__()
        self.radius: Scalar = radius
    def get_tangents(self):
        """Quickly calculate incline angle of tangent line for each cell rendered on circle outline; these will be used to render the outline in ASCII characters"""
        r = self.radius()
        # possibly move this code
        minigrid = np.zeros([r*2+1, r*2+1])
        # crossed_cells = minigrid
        # TODO: mirroring for efficiency?
        for x, y in np.ndindex(minigrid.shape):
            # print(5)
            # print(np.round(np.linalg.norm(np.array([x, y]) - np.array([r, r]))))
            if np.round(np.linalg.norm(np.array([x, y]) - np.array([r, r]))) == r:
                minigrid[x, y] = 1
        num_crossed = np.sum(minigrid)
        d_theta = 360 / num_crossed
        c = 0
        for x, y in np.ndindex(minigrid.shape):
            # if
            c += minigrid[x, y]
            # minigrid[x, y] = Angle(d_theta * c)
            minigrid[x, y] = d_theta * c * minigrid[x, y]
        return np.round(minigrid)

A geometric 2D circle with a certain radius; subclass of Shape

#   Circle(radius)
View Source
    def __init__(self, radius):
        super().__init__()
        self.radius: Scalar = radius

Create a new geometry object

#   radius: 'Scalar'
#   def get_tangents(self):
View Source
    def get_tangents(self):
        """Quickly calculate incline angle of tangent line for each cell rendered on circle outline; these will be used to render the outline in ASCII characters"""
        r = self.radius()
        # possibly move this code
        minigrid = np.zeros([r*2+1, r*2+1])
        # crossed_cells = minigrid
        # TODO: mirroring for efficiency?
        for x, y in np.ndindex(minigrid.shape):
            # print(5)
            # print(np.round(np.linalg.norm(np.array([x, y]) - np.array([r, r]))))
            if np.round(np.linalg.norm(np.array([x, y]) - np.array([r, r]))) == r:
                minigrid[x, y] = 1
        num_crossed = np.sum(minigrid)
        d_theta = 360 / num_crossed
        c = 0
        for x, y in np.ndindex(minigrid.shape):
            # if
            c += minigrid[x, y]
            # minigrid[x, y] = Angle(d_theta * c)
            minigrid[x, y] = d_theta * c * minigrid[x, y]
        return np.round(minigrid)

Quickly calculate incline angle of tangent line for each cell rendered on circle outline; these will be used to render the outline in ASCII characters