Approaches to programming: Procedural vs. Object-Oriented

Problem: Perimeter of Triangle

Write a program to calculate the perimeter of a triangle given the coordinates of the three verticies.

The distance between two points is: $$ d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$$

Write this program yourself before looking at the approaches below.
Notes and Code Outcome

Procedural: Basic

Show Notes ▼

                  # INPUT
                  p1_x = 4
                  p1_y = 3
                  p2_x = -4
                  p2_y = 7
                  p3_x = -5
                  p3_y = -4

                  # CALCULATIONS
                  # distance between (1) and (2)
                  side1 = ((p2_x - p1_x)**2 + (p2_y - p1_y)**2)**0.5

                  # distance between (2) and (3)
                  side2 = ((p3_x - p2_x)**2 + (p3_y - p2_y)**2)**0.5

                  # distance between (3) and (1)
                  side3 = ((p1_x - p3_x)**2 + (p1_y - p3_y)**2)**0.5

                  # Perimeter = total distance
                  perimeter = side1 + side2 + side3


                  # OUTPUT
                  print("Perimeter: ", perimeter)

                
perimeter-p.py
Output:
Show Notes ▼

Procedural: Using a distance function

Show Notes ▼

                  # FUNCTIONS
                  def distance(x1, y1, x2, y2):
                      d = ((x2-x1)**2 + (y2-y1)**2)**0.5
                      return d

                  # INPUT
                  p1_x = 4
                  p1_y = 3
                  p2_x = -4
                  p2_y = 7
                  p3_x = -5
                  p3_y = -4

                  # CALCULATIONS
                  # distance between (1) and (2)
                  side1 = distance(p1_x, p1_y, p2_x, p2_y)

                  # distance between (2) and (3)
                  side2 = distance(p2_x, p2_y, p3_x, p3_y)

                  # distance between (3) and (1)
                  side3 = distance(p3_x, p3_y, p1_x, p1_y)

                  # Perimeter = total distance
                  perimeter = side1 + side2 + side3


                  # OUTPUT
                  print("Perimeter: ", perimeter)

                
perimeter-p.py
Output:
Show Notes ▼

Object Oriented: Using a point class

Uses a point class with a distanceTo method that calculates the distance to another point.
Show Notes ▼

                  # CLASSES
                  class point:
                      def __init__(self, x, y):
                          self.x = x
                          self.y = y
                      # Method to find the distance to another point (p)
                      def distanceTo(self, p):
                          d = ((self.x-p.x)**2 + (self.y-p.y)**2)**0.5
                          return d

                  # INPUT
                  p1 = point(4,3)
                  p2 = point(-4,7)
                  p3 = point(-5,-4)

                  # CALCULATIONS
                  # distance between (1) and (2)
                  side1 = p1.distanceTo(p2)

                  # distance between (2) and (3)
                  side2 = p2.distanceTo(p3)

                  # distance between (3) and (1)
                  side3 = p3.distanceTo(p1)

                  # Perimeter = total distance
                  perimeter = side1 + side2 + side3


                  # OUTPUT
                  print("Perimeter: ", perimeter)

                
perimeter-oo.py
Output:
Show Notes ▼

Object Oriented: Using a point class and a triangle class

Uses a point class with a distanceTo method that calculates the distance to another point and a triangle class that calculates the perimeter.
Show Notes ▼

                  # CLASSES
                  class point:
                      def __init__(self, x, y):
                          self.x = x
                          self.y = y
                      # Method to find the distance to another point (p)
                      def distanceTo(self, p):
                          d = ((self.x-p.x)**2 + (self.y-p.y)**2)**0.5
                          return d

                  # Triangle class that takes three vertices as point class instances.
                  class triangle:
                      def __init__(self, p1, p2, p3):
                          self.p1 = p1
                          self.p2 = p2
                          self.p3 = p3

                      def perimeter(self):
                          p = self.p1.distanceTo(self.p2)
                          p += self.p2.distanceTo(self.p3)
                          p += self.p3.distanceTo(self.p1)
                          return p

                  # INPUT
                  p1 = point(4,3)
                  p2 = point(-4,7)
                  p3 = point(-5,-4)

                  tri = triangle(p1, p2, p3)

                  # CALCULATIONS
                  perimeter = tri.perimeter()

                  # OUTPUT
                  print("Perimeter: ", perimeter)

                
perimeter-oo.py
Output:
Show Notes ▼

Assignment: Area of a triangle

Write a program to calculate the area of the triangle.

You can use Heron's formula to determine the area of a triangle based on the lengths of its sides, or there is a formula you can use based on the vertices of triangle.

Output:
Show Notes ▼
If you did not use an object-oriented approach, try this problem again doing so.

Assignment: Find the perimeter of a polygon.

Find the perimeter of the polygon. Explain which method you used and why.

The distance between two points is: $$ d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$$

Output:
Show Notes ▼
If you did not use an object-oriented approach, try this problem again doing so.

Assignment: Find the Area of a polygon.

Find the area of the polygon. A convex polygon can be broken down into triangles.
Output:
Show Notes ▼
If you did not use an object-oriented approach, try this problem again doing so.