Lecture 12 - Exercises

12B - Tuples

  1. What does the following code print?

  2. What does the following code print?

  3. Suppose the following code is executed. The numbers at left are line numbers and are not part of the code itself. If any line causes an error, assume that line is not run and continue execution with the next line.

    1. Which line(s), if any, cause errors? List all that apply.
    2. What does line 5 print?
  4. Consider the following function:

    For each of the following calls to the above function, say what the function returns, or “error” if an error will occur.

    1. f(1, 2)
    2. f(1, 2, 3, 4)
    3. f((1, 2), (3, 4))
    4. f(((1, 1), 2), (3, 4))
    5. f((1, 1, 2), (3, 4))

Problems

  1. Recall that the distance between two points can be calculated using the pythagorean theorem. The distnace between the two points is the hypotenuse of a triangle formed by the difference in x coordinates and the difference in y coordinates:

    With this in mind, implement the following function:

  2. A common task in computer graphics is to triangulate a polygon - or in other words, generate a collection of triangles that cover a polygon. A simple case of this is triangulating a quadrilateral - or in other words, splitting a 4-sided shape into two triangles. We’ll assume that the quadrilateral in question is convex (i.e., the line between any pair of vertices does not leave the quadrilateral). We can triangulate this shape by connecting one pair of opposite points with a line. For example, the following quadrilateral can be triangulated by either connecting p_1 and p_3 or connecting p_2 and p_4. For various reasons it turns out to be desirable to have triangles whose smallest angle isn’t too small; this means that in the shape below, a better triangulation would be to connect p_2 and p_4.

    Implement the following function, which returns the pair of opposite points that are closest together. You can make use of the distance function you implemented above.

  3. Implement the following function to draw a triangulation of a quadrilateral using a turtle. You can make use of any of the functions you’ve written so far. You also may want to check out the turtle’s goto method to help make things a little simpler.

    Test your program using a main program like the following:

    When I run this, I get a drawing in the turtle window that looks like this:

  4. Polygons with more vertices are trickier to optimally triangulate, because there are more choices of pairs of edges to connect. A simple, foolproof method for getting a triangulation (but not necessarily an optimal one) is called a fan trianguation. Starting at one vertex, a line is drawn to connect that vertex to each other one that’s not adjacent to it, as in the example image below:

    Implement the following function, which draws a fan-triangulated picture of a polygon specified by a list of vertices.

    Hint: You may find it helpful to be able to access a specific element of the list: you can do this with poly[i], where i is the index of the vertex you want, starting with 0 as the first element.

    Write a main program to test your function.

  5. See if you can divise a method for triangulating a convex polygon that will minimize long-and-narrow triangles. This could involve a fan triangulation with a carefully chosen starting vertex, or a different approach entirely. Focus on the algorithm first, writing it in pseudocode; once you’ve got something you think will work, feel free to go ahead and write Python code to implement it.