Beispiel #1
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
Beispiel #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
Beispiel #3
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
Beispiel #4
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
Beispiel #5
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
Beispiel #6
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
Beispiel #7
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))