Skip to content

Latest commit

 

History

History
174 lines (116 loc) · 4.75 KB

File metadata and controls

174 lines (116 loc) · 4.75 KB

Lecture 26

Object-Oriented Programming Review and List Comprehension

16 Frimaire, CCXXXI

Song of the day: AXIOM by Ai Furihata (2021).


Part 0: (Vector) Space Oddity

In physics and mathematics, a vector is a geometric object that has a magnitude (or length) and direction.

Figure 1: A two-dimensional vector pointing from point A to B. Each point has an x-coordinate and a y-coordinate.

These vectors are often used to represent forces acting on an object. For example, if one person is pushing a box the north direction, and another person is pushing the same box in the east direction, the resulting net force will cause the box to move somewhere in the northeast direction, with the combined forces of both pushes. In other words, you can perform arithmetic on vectors.

We will be simulating three-dimensional vectors using classes.

The Point Class

Since each vector is delineated by two points in 3-dimensional space, we will create a class to represent such a point.

In the file point.py, create a class called Point that will accept 3 argument when being instantiated: an x-coordinate, a y-coordinate, and a z-coordinate. These will also be the Point objects' arguments:

point_a = Point(-2, -3, 0)

print(point_a.x_coord)
print(point_a.y_coord)
print(point_a.z_coord)

print(point_a)

Output:

-2
-3
0
(-2, -3, 0)

Note the behaviour when Point objects are printed. Make sure your class behaves the same way.

Point objects must be able to subtract from each other. For example:

point_a = Point(3.0, -0.67, -6)
point_b = Point(34.0, -5.67, -6.06)
point_c = point_b - point_a

print(point_c)

Output:

(31.0, -5.0, -0.06)

Each of these parameters must have a default value of 0.0.

The Vector Class

In the file vector.py, define a class called Vector. Vector objects will be created by passing in two Point objects as arguments:

from point import Point
from random import randrange

# Creating points with random coordinates
point_a = Point(randrange(-10, 10), randrange(-10, 10), randrange(-10, 10))
point_b = Point(randrange(-10, 10), randrange(-10, 10), randrange(-10, 10))

vector_a = Vector(point_b, point_a)

Each Vector object will only have one attribute, vector, whose value will be the difference between the second Point parameter and the first Point parameter. For example, if the first parameter was a point at the origin (0, 0, 0) and the second parameter was the point (10.3, 0.0, -3.4), the following code:

vector_a = Vector(Point(), Point(10.3, 0.0, -3.4))

print(vector_a.vector)

Would print:

`(10.3, 0.0, -3.4)`

When printing Vector objects, make sure they look as follows:

vector_a = Vector(Point(), Point(10.3, 0.0, -3.4))

print(vector_a)

Output:

10.3x + 0.0y - 3.4z

Note here that the signs change with the value of the x-, y-, and z-coordinates.


Once you have gotten your initializer and printing behaviour to work, define a method for the Vector class called get_magnitude() which will simply return the value of this vector's magnitude. The magnitude of a vector v, denoted by the |v| notation, is calculated using the following formula:

|v| = (x2 + y2 + z2)0.5

vector_a = Vector(Point(2, 45, 0.0), Point(10.3, 0.0, -3.4))

print(vector_a.get_magnitude())

Output:

45.88518279357727

Finally, make sure your Vector objects can multiply. For this, we will use the dot product:

a · b = (xa * xb) x + (ya * yb) y + (za * zb) z

# Creating vector A with two points of random coordinates
point_a = Point(randrange(-10, 10), randrange(-10, 10), randrange(-10, 10))
point_b = Point(randrange(-10, 10), randrange(-10, 10), randrange(-10, 10))
vector_a = Vector(point_b, point_a)

# Creating vector B with two points of random coordinates
point_c = Point(randrange(-10, 10), randrange(-10, 10), randrange(-10, 10))
point_d = Point(randrange(-10, 10), randrange(-10, 10), randrange(-10, 10))
vector_b = Vector(point_d, point_c)

print("Vector A: {}".format(vector_a))
print("Vector B: {}".format(vector_b))

dot_product = vector_a * vector_b

print("A · B = {}".format(dot_product))

Possible output:

Vector A: -15x - 2y - 7z
Vector B: 2x + 5y + 7z
A · B = -30.0x - 10.0y - 49.0z