Classes: Straight Lines

Classes are ways of grouping functions and values together to make them easier to use.
The specific equation for a straight line might be: $$ y = 2 x + 1 $$ where: $$ m = 2 $$ and, $$ b = 1 $$ We want a program that can handle any straight line equation we throw at it, so we need to work with the general equation.
Notes and Code Outcome

General Equation for a Straight Line

We want a program that can handle any straight line equation we throw at it, so we need to work with the general equation: $$ y = mx + b \tag{1}$$ We can use it to find and do things like:
  • Slope: $$ \text{slope} = m $$
  • y-intercept: $$ \text{y-intercept} = b $$
  • x-intercept: $$ \text{x-intercept} = \dfrac{-b}{m} $$

Example: Graph of the linear function: $$ y = 2x + 1 $$ where, $$ m = 2 , \,\,\, b = 1 $$

Creating a Class

Notice that:
  • Naming the Class (Line 1): This class' name is straightLine:
    
                      class straightLine:
                    
  • Initializing the Class (Line 3): We want to give the class the basic information it needs, m and b in this case.
    
                          def __init__(self, m, b):
                    
    self is passed as the first parameter. self lets the class refer to things internally, which we'll see in a moment.
  • Setting properties (Lines 4-9) :
    
                          self.slope = m
                    
    These are variables that are unique to each instance of the class. They are known within the class, and naming them things like self.slope allows us to refer to them internally, and externally.
    
                           self.xintercept = -b/m
                     
    Note that we can set properties by doing calculations.

              class straightLine:

                def __init__(self, m, b):
                    self.m = m
                    self.b = b

                    self.slope = m
                    self.yintercept = b
                    self.xintercept = -b/m

            
If you run this code, nothing will happen. Just like with a function, classes are stored in memory until they are used.

Using the straightLine Class

Let's use the straightLine class to create two lines and find which has the steeper slope. We'll use the two lines: $$ y = 2x + 1 \tag{2}$$ $$ y = -3x + 4 \tag{3}$$
  • Creating instances of the class (lines 11 and 12):
    
                      line1 = straightLine(2, 1)  # y =  2x + 1
                      line2 = straightLine(-3, 4) # y = -3x + 4
                    
    We pass the m and b values for Equations (2) and (3) to the __init__ method in the class by calling the class name.
  • Using properties of the instances (line 14):
    
                      if line1.slope > line2.slope:
                    
    Here we compare the slope properties of one line to the other.

              class straightLine:

                  def __init__(self, m, b):
                      self.m = m
                      self.b = b

                      self.slope = m
                      self.yintercept = b
                      self.xintercept = -b/m

              line1 = straightLine(2, 1)  # y =  2x + 1
              line2 = straightLine(-3, 4) # y = -3x + 4

              if line1.slope > line2.slope:
                  print("Line 1 has the steeper slope")
              else:
                  print("Line 2 has the steeper slope")
            
The result should look like:
Show Notes ▼

Exercise

Find which line has an x-intercept closer to zero of the two lines: $$ y = 4x - 5 \tag{4}$$ $$ y = x + 2 \tag{5}$$
Code:
Show Notes ▼
Result:
Show Notes ▼

Methods: Finding the y values given x

Something we'll very likely want to do if we have an equation is to find the value of y for a given x. To do so we'll create a method within the class. A method is a function that exists inside a class.
  • Creating the get_y method: (Line 11)
    
                      def get_y(self, x):
                    
    As with the __init__ initialization method, the first parameter in the method is self, however, when we call the method, we do not need to send the self value because the class already knows its self.
  • Using properties internally: (Line 12)
    
                        y = self.m * x + self.b
                    
    We calculate the y value from the equation for the given x. We did not have to tell the instance of the class what the m and b values were, because the instance stores this as properties (self.m and self.b) when it was initialized. However, we do have to refer to m and b by their property names (self.m and self.b).
  • Using the method: (Line 17):
    
                        print("For x = 5, y =", line1.get_y(5))
                    
    With line1.get_y(5) we call the method with the instance name line1 and its method name get_y and pass it the x value. The method returns the y value (Line 13).
Code.

              class straightLine:

                  def __init__(self, m, b):
                      self.m = m
                      self.b = b

                      self.slope = m
                      self.yintercept = b
                      self.xintercept = -b/m

                  def get_y(self, x):
                      y = self.m * x + self.b
                      return y

              line1 = straightLine(2, 1)

              print("For x = 5, y =", line1.get_y(5))
            
Results.

              For x = 5, y = 11
            

Exercise: find x given y

Add a method to the class that calculates the x value for a given y. Use this method to find x value when: $$ y = 11 $$ for the line $$ y = 2x + 1 $$
Code.
Show Notes ▼
Results.
Show Notes ▼

Exercise: perpendicular slope

Add a method to: calculate the slope of a line perpendicular to the given line.
Code.
Show Notes ▼
Results.
Show Notes ▼

Exercise: Check if a point is on the line

Add a method to: determine if a given point (x,y) is on the line.

Your method should return 'True' if the point is on the line and 'False' if not.

Code.
Show Notes ▼
Results.
Show Notes ▼