Skip to content

Instantly share code, notes, and snippets.

@anandology
Last active June 9, 2021 10:30
Show Gist options
  • Save anandology/a02041798553736c7a326f49eddc22ec to your computer and use it in GitHub Desktop.
Save anandology/a02041798553736c7a326f49eddc22ec to your computer and use it in GitHub Desktop.
API optioons for Joy

API - V1

Basic Types

Create a circle:

circle()
circle(r=50)
circle(cx=100, cy=100, r=50)

create a rectangle:

rect()
rect(width=200, height=100)
rect(x=50, y=50, width=100, height=50)

create a line:

line()
line(x1=100, y1=0, x2=150, y2=100)

Combining Shapes

c = circle()
r = rect()
shape = combine(c, r)

Transformations

translate:

shape = translate(circle(), x=10, y=10)

rotate:

shape = rotate(rect(), angle=45)

shape = rotate(rect(), angle=45, x=100, y=100) # rotate by 45 degrees around (100, 100)

scale:

shape = scale(circle(), sx=1, sy=0.5)

Higher Order Transformations

shape = cycle(rect())
shape = cycle(rect(), n=3)
shape = cycle(rect(), n=3, s=0.8) # s is for scale

API - V2

Basic Types

Create a circle:

c = Circle()
c = Circle(radius=50)
c = Circle(center=Point(x=100, y=100), radius=50)

create a rectangle:

r = Rectangle()
r = Rectangle(width=200, height=100)
r = Rectangle(center=Point(x=0, y=50),  width=100, height=50)

create a line:

z = Line()
z = Line(start=Point(x=100, y=0), end=Point(x=150, y=100))

Combining Shapes

c = Circle()
r = Rectangle()
shape = c + r

When you have an list of shapes, you can combine all of them using Group.

def concentric_circles(center, radius, n):
    circles = [Circle(center, radius*i/n) for i in range(1, n+1)]
    return Group(circles)

Transformations

translate:

shape = Circle() | Translate(x=10, y=10)

rotate:

shape = Rectangle() | Rotate(angle=45)

shape = Rectangle() | Rotate(angle=45, anchor=Point(x=100, y=100))

shape = Line() | Rotate(angle=15, anchor="end") # rotate around the end point of the line

# using anchors on combines shapes
r = Rectangle() 
d = Line(start=rect.points[1], end=rect.points[2])
shape = r + d

shape2 = shape | Rotate(angle=15, anchor="1.end")

# or using labels

r = Rectangle() 
d = Line(start=rect.points[1], end=rect.points[2])

shape = r.label("r") + d.label("diag")

shape2 = shape | Rotate(angle=15, anchor="diag.end")
shape2 = shape | Rotate(angle=15, anchor="r.p2")

scale:

shape = Circle() | Scale(sx=1, sy=0.5)

Higher Order Transformations

shape = Rectangle() | Repeat(n=4, Rotate(angle=15))
shape = Rectangle() | Repeat(n=4, Rotate(angle=15) | Scale(sx=0.9, sy=0.9))
shape = Line() | Repeat(n=4, Rotate(angle=15, anchor="end"))

grid3 = Repeat(3, Translate(x=50, y=0)) | Repeat(3, Translate(x=50, y=0)) 
shape = Circle(center=Point(x=-100, y=-100), radius=50) | grid3

API - V2 (Plan B)

Basic Types

Create a circle:

c = circle()
c = circle(radius=50)
c = circle(center=Point(x=100, y=100), radius=50)

create a rectangle:

r = rectangle()
r = rectangle(width=200, height=100)
r = rectangle(center=Point(x=0, y=50),  width=100, height=50)

create a line:

z = line()
z = line(start=point(x=100, y=0), end=Point(x=150, y=100))

Combining Shapes

c = circle()
r = rectangle()
shape = c + r

When you have an list of shapes, you can combine all of them using Group.

def concentric_circles(center, radius, n):
    circles = [circle(center, radius*i/n) for i in range(1, n+1)]
    return group(circles)

Transformations

translate:

shape = circle() | translate(x=10, y=10)

rotate:

shape = rectangle() | rotate(angle=45)

shape = rectangle() | rotate(angle=45, anchor=point(x=100, y=100))

shape = line() | rotate(angle=15, anchor="end") # rotate around the end point of the line

# using anchors on combines shapes
r = rectangle() 
d = line(start=rect.points[1], end=rect.points[2])
shape = r + d

shape2 = shape | rotate(angle=15, anchor="1.end")

# or using labels

r = rectangle() 
d = line(start=rect.points[1], end=rect.points[2])

shape = r.label("r") + d.label("diag")

shape2 = shape | rotate(angle=15, anchor="diag.end")
shape2 = shape | rotate(angle=15, anchor="r.p2")

scale:

shape = circle() | scale(sx=1, sy=0.5)

Higher Order Transformations

shape = rectangle() | repeat(n=4, rotate(angle=15))
shape = rectangle() | repeat(n=4, rotate(angle=15) | scale(sx=0.9, sy=0.9))
shape = line() | repeat(n=4, rotate(angle=15, anchor="end"))

grid3 = repeat(3, translate(x=50, y=0)) | repeat(3, translate(x=50, y=0)) 
shape = circle(center=point(x=-100, y=-100), radius=50) | grid3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment