Object Oriented ProgrammingVector: Mathematical PreliminariesVector: SpecificationVector: DefinitionCollection of Vectors
We will take up one final example of classes in action. This is to demonstrate few important points about OOP along the way.
To each point
Some operations on vectors:
Magnitude
The magnitude of a vector
Scale
A vector can be scaled by a value
Add
Two vectors
For example:
Reflect
A vector can be rotated about the origin. A particular instance of rotation is reflection about an axis. For example,
This corresponds to the transformation:
From the mathematical vector, we need to transition to the programmatic vector. The bridge between these two states is the specification. In this step, we come up with a written description of the attributes and methods that our Vector
class should possess. The source for this information comes from the mathematical vector that we just studied.
The following is the specification of the Vector
class:
Attributes
x
: the x-coordinate of the vectory
: the y-coordinate of the vectorThis choice is sufficient as any mathematical vector in 2D space can be completely defined with these two attributes.
Methods
__init__
: constructor of the class; populate the attributes based on the argumentsprint
: return the coordinates of the vector in the form (x,y)
magnitude
: return the magnitude of the vectorscale
: scale the vector by some number, rotate_xaxis
: reflect the vector about the X-axis; this transformation should be applied on the current vectorrotate_yaxis
: reflect the vector about the Y-axis; this transformation should be applied on the current vectoradd
: accept a vector as argument; return the sum of this argument with the current vector
The stage is now set to define the class:
1class Vector:
2 def __init__(self, x, y):
3 self.x, self.y = x, y
4 def print(self):
5 print(f'({self.x},{self.y})')
6 def magnitude(self):
7 return (self.x ** 2 + self.y ** 2) ** 0.5
8 def scale(self, s):
9 self.x, self.y = self.x * s, self.y * s
10 def rotate_xaxis(self):
11 self.y = -self.y
12 def rotate_yaxis(self):
13 self.x = -self.x
14 def add(self, P):
15 result = Vector(0, 0)
16 result.x, result.y = self.x + P.x, self.y + P.y
17 return result
Most methods are self-explanatory. Some require closer attention. Note that all methods except add
do not return any value. These are methods that transform the vector itself. The method add
however is interesting. It accepts a vector P
as an argument! Within the method, a new Vector
object is defined, it is the zero-vector. The current vector is added with P
and result is stored in the newly created vector result
. This is finally returned.
The whole point of having a class is to have objects. The class is just a template. Consider the following use case of a collection of objects:
xxxxxxxxxx
11triangle = [Vector(0, 1), Vector(3, 1), Vector(3, 0)]
The list triangle
is a collection of objects of type Vector
. In this instance, triangle
represents the following triangle:
We could now ask various questions here, one of which is this: how do we compute the lengths of the sides of this triangle?
x1def dist(P1, P2):
2 return ((P1.x - P2.x) ** 2 + (P1.y - P2.y) ** 2) ** 0.5
3
4def side_lengths(triangle):
5 la = dist(triangle[0], triangle[1])
6 lb = dist(triangle[1], triangle[2])
7 lc = dist(triangle[2], triangle[0])
8 return la, lb, lc
In this way, we could also define a square to be a list of four vectors. That brings to a close the discussion on object oriented programming in Python. We will cover these concepts in greater detail when we study Java.