Writing Functions

Functions are bits of code that do specific things so you don't have to recreate the wheel every time you have to do something complicated. Given two points, can you write functions to find:
  1. the slope of the line that connects them
  2. the intercept of the line that passes through them,
  3. the location of the mid-point between the two points
  4. the distance between them
We'll use these functions to find things like the perimeter of a shape, and the length of a curved line. Our test points:
          p1 = (-3, 1)
          p2 = (5, 7)
        
The two points and the line between them.
Notes and Code Outcome

Plotting points

First, let's plot the points. We'll do it a little differently from some of the other lessons because we want to make all these calculations about the points.

Lines 6&7: The two points are created as vectors and named p1 and p2.


                  # Create points as vectors
                  p1 = vec(-3,1,0)
                  p2 = vec(5,7,0)
                

Lines 10: Since the points are vectors, when we create sphere for the first point, we can set the position by using p1: pos=p1


                  # Draw spheres to show the points
                  s1 = sphere(pos=p1, radius=0.5, color=color.red)
                

Line 14: We can also use p1 and p2 when we create the line between the points.


                  # Draw line between two points
                  line = curve(pos=[p1, p2])
                

Line 17: One of the reasons we're making the points vectors is that vectors have a few useful properties. One of these is the fact that you can refer to the x-coordinate of the vector using the dot notation. So, p1's x-coordinate is p1.x.


                  print("Point 1:", p1.x, ",", p1.y)
                

Full program for plotting two points

              # Draw axes
              xaxis = curve(pos=[vec(-10,0,0), vec(10,0,0)])
              yaxis = curve(pos=[vec(0,-10,0), vec(0,10,0)])

              # Create points as vectors
              p1 = vec(-3,1,0)
              p2 = vec(5,7,0)

              # Draw spheres to show the points
              s1 = sphere(pos=p1, radius=0.5, color=color.red)
              s2 = sphere(pos=p2, radius=0.5, color=color.yellow)

              # Draw line between two points
              line = curve(pos=[p1, p2])

              # Output the x and y values of point 1 (p1)
              print("Point 1:", p1.x, ",", p1.y)

            
Output

Slope Equation

The slope between two points is given by: $$ m = \dfrac{y_2 - y_1}{x_2 - x_1} \tag{1}$$
We can calculate this within the code by doing:

              m = (p2.y - p1.y) / (p2.x - p1.x)

              print("The slope between the two points is:", m)
            

Slope Function

Now we'll write a function to get the slope. $$ m = \dfrac{y_2 - y_1}{x_2 - x_1} \tag{1}$$

Inputs: As you can see from the equation, you need four values (x1, y1, x2, and y2) to calculate the slope.

Output/s: The equation calculates one value, the slope, so there will be only one output from our function.

Writing and Calling the Function

The slope Function
The function is defined on Lines 1-3

                      def slope(x1, y1, x2, y2):
                          s = (y2 - y1) / (x2 - x1)
                          return s
                    

Line 2:

  • The def keyword indicates that we're defining a function.
  • the name of the function is slope
  • the inputs of the function are listed within parentheses.

Line 3: Here we calculate the slope.

Line 4: Here we return the value of the slope (s) to the program.


Calling the slope function

Functions stand alone. When you run the program, the function definition is stored in memory and saved until the function is called.


                      m = slope(p1.x, p1.y, p2.x, p2.y)
                    

Line 19: The function is called by using its name, and passing the input values. The function returns the result to the place in the code where the function is called.

WE could have also called the function and passed the x and y values directly like:


                      m = slope(-3, 1, 5, 7)
                    

This function call is a little clunky with the four inputs, so next we'll create another function that takes two vectors as inputs.

Code with slope function:

                    # function to find the slope of a line given the coordinates of two points.
                    def slope(x1, y1, x2, y2):
                        s = (y2 - y1) / (x2 - x1)
                        return s

                    xaxis = curve(pos=[vec(-10,0,0), vec(10,0,0)])
                    yaxis = curve(pos=[vec(0,-10,0), vec(0,10,0)])

                    p1 = vec(-3,1,0)
                    p2 = vec(5,7,0)

                    s1 = sphere(pos=p1, radius=0.5, color=color.red)
                    s2 = sphere(pos=p2, radius=0.5, color=color.yellow)

                    line = curve(pos=[p1, p2])

                    print("Point 1:", p1.x, ",", p1.y)
                    print("Point 2:", p2.x, ",", p2.y)

                    m = slope(p1.x, p1.y, p2.x, p2.y)

                    print("The slope between the two points is:", m)
                  

Function using vectors as inputs.

Line 26: The call to the new function is much simpler and, hopefully, more intuitive, now you sent only two values, the vectors for the two points.


                  m = slopeVectors(p1, p2)
                

Line 7-9: The new function, called slopeVectors that calculates the slope using vector inputs.


                # function for slope given two points as vectors
                def slopeVectors(v1, v2):
                    s = (v2.y - v1.y) / (v2.x - v1.x)
                    return s
              

Local Variables: Notice that both functions (slope and slopeVectors) use the variable name s when they calculate the slope. This causes no conflice with each other, or with other places in the code where we might want to use a variable named s because variables created in a function only exist in that function . These are refered to as local variables.

Show Notes ▼

              # function to find slope given the coordinates of two points.
              def slope(x1, y1, x2, y2):
                  s = (y2 - y1) / (x2 - x1)
                  return s

              # function for slope given two points as vectors
              def slopeVectors(v1, v2):
                  s = (v2.y - v1.y) / (v2.x - v1.x)
                  return s

              xaxis = curve(pos=[vec(-10,0,0), vec(10,0,0)])
              yaxis = curve(pos=[vec(0,-10,0), vec(0,10,0)])

              p1 = vec(-3,1,0)
              p2 = vec(5,7,0)

              s1 = sphere(pos=p1, radius=0.5, color=color.red)
              s2 = sphere(pos=p2, radius=0.5, color=color.yellow)

              line = curve(pos=[p1, p2])

              print("Point 1:", p1.x, ",", p1.y)
              print("Point 2:", p2.x, ",", p2.y)

              # calling the slopeVectors function
              m = slopeVectors(p1, p2)

              print("The slope between the two points is:", m)
            

Distance Function

Write a function that calculates the distance between the two points. The equation is: $$ d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$$
Show Notes ▼

Intercept Function

Write a function that calculates the intercept of the line that passes through the points:
Show Notes ▼

Distance along a curve

Generate a number of points, and use your distance function to calculate the total distance along the curve.

Number of points:



            

Check your answer (total distance):
Show Notes ▼