Exemple #1
0
def extrusion(part, z0, z1):
    # max(part, max(z0-Z, Z-z1))
    s = MathTree('a%sa-f%gZ-Zf%g' % (part.math, z0, z1))
    s.bounds = part.bounds[0:4] + [z0, z1]
    s.shape = True
    s.color = part.color
    return s
Exemple #2
0
def triangle(x0, y0, x1, y1, x2, y2):
    # Find the angles of the points about the center
    xm = (x0 + x1 + x2) / 3.
    ym = (y0 + y1 + y2) / 3.
    angles = [
        math.atan2(y - ym, x - xm) for x, y in [(x0, y0), (x1, y1), (x2, y2)]
    ]

    # Sort the angles so that the smallest one is first
    if angles[1] < angles[0] and angles[1] < angles[2]:
        angles = [angles[1], angles[2], angles[0]]
    elif angles[2] < angles[0] and angles[2] < angles[1]:
        angles = [angles[2], angles[0], angles[1]]

    # Enforce that points must be in clockwise order by swapping if necessary
    if angles[2] > angles[1]:
        x0, y0, x1, y1 = x1, y1, x0, y0

    def edge(x, y, dx, dy):
        # dy*(X-x)-dx*(Y-y)
        return '-*f%(dy)g-Xf%(x)g*f%(dx)g-Yf%(y)g' % locals()

    e0 = edge(x0, y0, x1 - x0, y1 - y0)
    e1 = edge(x1, y1, x2 - x1, y2 - y1)
    e2 = edge(x2, y2, x0 - x2, y0 - y2)

    # -min(e0, min(e1, e2))
    s = MathTree('ni%(e0)si%(e1)s%(e2)s' % locals())

    s.xmin, s.xmax = min(x0, x1, x2), max(x0, x1, x2)
    s.ymin, s.ymax = min(y0, y1, y2), max(y0, y1, y2)

    s.shape = True
    return s
Exemple #3
0
def triangle(x0, y0, x1, y1, x2, y2):
    # Find the angles of the points about the center
    xm = (x0 + x1 + x2) / 3.
    ym = (y0 + y1 + y2) / 3.
    angles = [math.atan2(y - ym, x - xm) for x, y in [(x0,y0), (x1,y1), (x2,y2)]]

    # Sort the angles so that the smallest one is first
    if angles[1] < angles[0] and angles[1] < angles[2]:
        angles = [angles[1], angles[2], angles[0]]
    elif angles[2] < angles[0] and angles[2] < angles[1]:
        angles = [angles[2], angles[0], angles[1]]

    # Enforce that points must be in clockwise order by swapping if necessary
    if angles[2] > angles[1]:
        x0, y0, x1, y1 = x1, y1, x0, y0

    def edge(x, y, dx, dy):
        # dy*(X-x)-dx*(Y-y)
        return '-*f%(dy)g-Xf%(x)g*f%(dx)g-Yf%(y)g' % locals()

    e0 = edge(x0, y0, x1-x0, y1-y0)
    e1 = edge(x1, y1, x2-x1, y2-y1)
    e2 = edge(x2, y2, x0-x2, y0-y2)

    # -min(e0, min(e1, e2))
    s = MathTree('ni%(e0)si%(e1)s%(e2)s' % locals())

    s.xmin, s.xmax = min(x0, x1, x2), max(x0, x1, x2)
    s.ymin, s.ymax = min(y0, y1, y2), max(y0, y1, y2)

    s.shape = True
    return s
Exemple #4
0
def rectangle(x0, x1, y0, y1):
    # max(max(x0 - X, X - x1), max(y0 - Y, Y - y1)
    s = MathTree('aa-f%(x0)gX-Xf%(x1)ga-f%(y0)gY-Yf%(y1)g' % locals())

    s.xmin, s.xmax = x0, x1
    s.ymin, s.ymax = y0, y1

    s.shape = True
    return s
Exemple #5
0
def right_triangle(x0, y0, h):
    corner = MathTree.max(MathTree('-f%fX' % x0), MathTree('-f%fY' % y0))
    corner.shape = True
    shape = corner & MathTree('-X-f%f-Yf%f' % (x0 + h, y0))
    shape.xmin = x0
    shape.xmax = x0 + h
    shape.ymin = y0
    shape.ymax = y0 + h
    return shape
Exemple #6
0
def rectangle(x0, x1, y0, y1):
    # max(max(x0 - X, X - x1), max(y0 - Y, Y - y1)
    s = MathTree('aa-f%(x0)gX-Xf%(x1)ga-f%(y0)gY-Yf%(y1)g' % locals())

    s.xmin, s.xmax = x0, x1
    s.ymin, s.ymax = y0, y1

    s.shape = True
    return s
Exemple #7
0
def sphere(x0, y0, z0, r):
    s = MathTree('-r++q%sq%sq%sf%g' % (('-Xf%g' % x0) if x0 else 'X',
                                       ('-Yf%g' % y0) if y0 else 'Y',
                                       ('-Zf%g' % z0) if z0 else 'Z',
                                       r))
    s.xmin, s.xmax = x0-r, x0+r
    s.ymin, s.ymax = y0-r, y0+r
    s.zmin, s.zmax = z0-r, z0+r
    s.shape = True
    return s
Exemple #8
0
def circle(x0, y0, r):

    # sqrt((X-x0)**2 + (Y-y0)**2) - r
    r = abs(r)
    s = MathTree('-r+q%sq%sf%g' % (('-Xf%g' % x0) if x0 else 'X',
                                   ('-Yf%g' % y0) if y0 else 'Y', r))

    s.xmin, s.xmax = x0-r, x0+r
    s.ymin, s.ymax = y0-r, y0+r

    s.shape = True
    return s
Exemple #9
0
def circle(x0, y0, r):

    # sqrt((X-x0)**2 + (Y-y0)**2) - r
    r = abs(r)
    s = MathTree('-r+q%sq%sf%g' % (('-Xf%g' % x0) if x0 else 'X',
                                   ('-Yf%g' % y0) if y0 else 'Y', r))

    s.xmin, s.xmax = x0 - r, x0 + r
    s.ymin, s.ymax = y0 - r, y0 + r

    s.shape = True
    return s
Exemple #10
0
def triangle(x0, y0, x1, y1, x2, y2):
    def edge(x, y, dx, dy):
        # dy*(X-x)-dx*(Y-y)
        return '-*f%(dy)g-Xf%(x)g*f%(dx)g-Yf%(y)g' % locals()

    e0 = edge(x0, y0, x1-x0, y1-y0)
    e1 = edge(x1, y1, x2-x1, y2-y1)
    e2 = edge(x2, y2, x0-x2, y0-y2)

    # -min(e0, min(e1, e2))
    s = MathTree('ni%(e0)si%(e1)s%(e2)s' % locals())

    s.xmin, s.xmax = min(x0, x1, x2), max(x0, x1, x2)
    s.ymin, s.ymax = min(y0, y1, y2), max(y0, y1, y2)

    s.shape = True
    return s
Exemple #11
0
def triangle(x0, y0, x1, y1, x2, y2):
    def edge(x, y, dx, dy):
        # dy*(X-x)-dx*(Y-y)
        return '-*f%(dy)g-Xf%(x)g*f%(dx)g-Yf%(y)g' % locals()

    e0 = edge(x0, y0, x1 - x0, y1 - y0)
    e1 = edge(x1, y1, x2 - x1, y2 - y1)
    e2 = edge(x2, y2, x0 - x2, y0 - y2)

    # -min(e0, min(e1, e2))
    s = MathTree('ni%(e0)si%(e1)s%(e2)s' % locals())

    s.xmin, s.xmax = min(x0, x1, x2), max(x0, x1, x2)
    s.ymin, s.ymax = min(y0, y1, y2), max(y0, y1, y2)

    s.shape = True
    return s
Exemple #12
0
def right_triangle(x0, y0, h):
    corner = MathTree.max(MathTree('-f%fX' % x0),MathTree('-f%fY' % y0))
    corner.shape = True
    shape = corner & MathTree('-X-f%f-Yf%f' % (x0+h, y0))
    shape.xmin = x0
    shape.xmax = x0 + h
    shape.ymin = y0
    shape.ymax = y0 + h
    return shape
Exemple #13
0
def blend(p0, p1, amount):
    if not p0.shape or not p1.shape:
        raise TypeError('Arguments must be math objects with shape=True')
    joint = p0 + p1

    # sqrt(abs(p0)) + sqrt(abs(p1)) - amount
    fillet = MathTree('-+rb%srb%sf%g' % (p0.math, p1.math, amount), shape=True)
    out = joint + fillet
    out.bounds = [b for b in joint.bounds]

    return out
Exemple #14
0
 def shapes(self, value):
     if value is None:
         self._shapes = value
     elif type(value) in (list, tuple):
         for v in value:
             if type(v) is not MathTree:
                 raise TypeError(
                     'cad.shapes must be of type MathTree, not %s.'
                     % type(v))
         self._shapes = list(value)
     else:
         self._shapes = [MathTree.wrap(value)]
Exemple #15
0
def right_triangle(x0, y0, h):
    corner = MathTree.max(MathTree('-f%fX' % x0),MathTree('-f%fY' % y0))
    corner.shape = True
    return corner & MathTree('-X-f%f-Yf%f' % (x0+h, y0))
Exemple #16
0
 def shape(self, value):
     self.shapes = [MathTree.wrap(value)]
Exemple #17
0
 def shape(self, value): self.shapes = [MathTree.wrap(value)]
 @property
Exemple #18
0
def right_triangle(x0, y0, h):
    corner = MathTree.max(MathTree('-f%fX' % x0), MathTree('-f%fY' % y0))
    corner.shape = True
    return corner & MathTree('-X-f%f-Yf%f' % (x0 + h, y0))