Complex Shapes

Jed Rembold & Fred Agbo

February 26, 2024

Announcements

  • Midterm Exam now on Friday 1st of March. This is due to the network issues.
  • Section Leaders should be working on Project 1 feedback
  • Problem set 4 should be the next assignment!
  • The planned section focus and problem set for this week will continue as normal
    • Since the midterm exams is also this week, we will be limiting PS4 to 2 problems instead of 3
    • We are also extending the due date to Tuesday March 5 at 10:00 PM
  • Polling continues https://www.polleverywhere.com/agbofred203

Review Question!

When the function to the right is run, what does the screen look like just after 1 second has passed?

(A)
(C)
(B)
(D)
def rev_q():
    def step():
        rect.move(1, 1)

    def once():
        rect.set_filled(True)

    gw = GWindow(200, 200)
    rect = GRect(0, 0, 25, 25)
    gw.add(rect)
    gw.set_interval(step, 20)
    gw.set_timeout(once, 1000)

Something to smile about

  • The GArc class represents an arc formed by taking a section of the perimeter of an oval.
  • 3 things necessary:
    • The bounding rectangle geometry (upper left corner and width and height)
    • The starting angle (in degrees)
    • The sweep angle (in degrees) which is how far the arc extends
  • Negative angles move in the clockwise direction

Fillable Arcs

  • The GArc class is a GFillableObject, and so you can call .set_filled() on a GArc object
  • Filled like a pie-shaped wedge formed between the center of the bounding box and the starting and end points of the arc
def filled_arc():
    gw = GWindow(400, 400)
    arc = GArc(50, 50, 
               350, 350, 
               90, 135)
    arc.set_color("orange")
    arc.set_filled(True)
    gw.add(arc)

The GPolygon class

  • Used to represent graphical objects bounded by line segments
    • Polygons consist of several vertices bounded by edges
image/svg+xml
 
  • Location not fixed in upper left, but at some convenient reference point
  • Often a convenient reference point is near the center of the object, but it doesn’t need to be
  • GPolygons are GFillableObjects, so they can be filled

Polygonal Construction

  • The GPolygon function creates an empty polygon, to which you then can add vertexes
  • Can create a vertex by calling .add_vertex(x,y) on the GPolygon object
    • x and y measured relative to the reference point
  • Vertexes past the first can be defined in a few ways:
    • .add_vertex(x,y) adds another new vertex relative to the reference point
    • .add_edge(dx,dy) adds a new vertex relative to the preceding vertex
    • .add_polar_edge(r, theta) adds a new vertex relative to the previous using polar coordinates

Triangle By Vertex

def triangle_by_vertex():
    def create_triangle(b, h):
        tri = GPolygon()
        tri.add_vertex(-b / 2, h / 2)
        tri.add_vertex(b / 2, h / 2)
        tri.add_vertex(0, -h / 2)
        return tri

    gw = GWindow(500, 500)
    triangle = create_triangle(200, 200)
    triangle.set_filled(True)
    triangle.set_color("red")
    gw.add(triangle, 250, 250)

Triangle by Polar Edge

def triangle_by_polar_edge():
    def create_eq_triangle(side):
        tri = GPolygon()
        tri.add_vertex(0, 0)
        for i in range(0, 360, 120):
            tri.add_polar_edge(side, i)
        return tri

    gw = GWindow(500, 500)
    triangle = create_eq_triangle(100)
    triangle.set_filled(True)
    triangle.set_color("green")
    gw.add(triangle, 250, 250)

Compound Objects

  • The GCompound class makes it possible to combine several graphical objects so that the entire structure behaves as a single object
  • Can be thought of as a combination of GWindow and GObject
    • You can add objects to it, but then you can also add it (and everything in it) to a window as a single unit
  • Uses its own coordinate system relative to a reference point
    • When adding objects to the GCompound, you place them relative to the reference point
    • When adding the GCompound to a canvas, you set the location of the reference point

And my Axe!

def my_axe():
    def create_axe():
        axe = GCompound()
        shaft = GRect(-15, 0, 30, 300)
        shaft.set_filled(True)
        shaft.set_color("brown")
        axe.add(shaft)

        blade = GPolygon()
        blade.add_vertex(0, 0)
        blade.add_vertex(200, -50)
        blade.add_vertex(200, 50)
        blade.set_filled(True)
        blade.set_color("gray")
        axe.add(blade, -80, 50)
        return axe

    gw = GWindow(500, 500)
    axe = create_axe()
    gw.add(axe, 250, 100)
// reveal.js plugins