4. Examples

4.1. Point on Line

Given point P(1 | 2 | 3) and line l: \vec{x} = \begin{pmatrix}-1\\-2\\-3\end{pmatrix} + r\begin{pmatrix}1\\2\\3\end{pmatrix}; r \in \mathbb{R} , is P on l?

from sgl import *
p = Point(1, 2, 3)
l = Line(Point(-1, -2, -3), Vector(1, 2, 3))
print(p in l)

Output:

True

4.2. Line comparison

Are the lines l1: \vec{x} = \begin{pmatrix}0\\0\\0\end{pmatrix} + r\begin{pmatrix}3\\5\\7\end{pmatrix}; r \in \mathbb{R} and l2: \vec{x} = \begin{pmatrix}-3\\-5\\-7\end{pmatrix} + r\begin{pmatrix}6\\10\\14\end{pmatrix}; r \in \mathbb{R} the same?

from sgl import *
l1 = Line(Point(0, 0, 0), Vector(3, 5, 7))
l2 = Line(Point(-3, -5, -7), Vector(6, 10, 14))
print(l1 == l2)

Output:

True

4.3. Pyramid

Given Points A(0 | 0 | 0), B(3 | 0 | 0), C(0 | 5 | 0) and D(0 | 0 | 7). Compute the area of the Triangle \Delta ABC and the volume of the pyramid ABCD.

from sgl import *
A = Point(0, 0, 0)
B = Point(3, 0, 0)
C = Point(0, 5, 0)
D = Point(0, 0, 7)

# This could be simplified if we account for the fact that this is a
# right-angled triangle, but we're using the generic way here
base = distance(A, B)
baseline = Line(A, B)
baseheight = distance(baseline, C)
area = 0.5 * base * baseheight
print("Area: %f" % area)

# Again we could simplify here, but we're using the generic way
baseplane = Plane(A, B, C)
height = distance(baseplane, D)
volume = 1.0 / 3 * area * height
print("Volume: %f" % volume)

Output:

Area: 7.500000
Volume: 17.500000

4.4. Different representations

Give the general form and the parametric form of the plane given by P: \left[\vec{x} - \begin{pmatrix}2\\3\\5\end{pmatrix}\right]\begin{pmatrix}42\\0\\0\end{pmatrix} = 0

from sgl import *
plane = Plane(Point(2, 3, 5), Vector(42, 0, 0))

print("General: %ix1 + %ix2 + %ix3 = %i" % plane.general_form())
print("Parametric: x = %s + r * %s + s * %s ; r,s e R" % plane.parametric())

Output:

General: 42x1 + 0x2 + 0x3 = 84
Parametric: x = Vector(2, 3, 5) + r * Vector(0.0, 1.0, 1.0) + s * Vector(0.0, -1.0, 1.0) ; r,s e R

More mathy:

General: 42x_{1} + 0x_{2} + 0x_{3} = 84 \\

Parametric: \vec{x} = \begin{pmatrix}2\\3\\5\end{pmatrix} +
r\begin{pmatrix}0\\1\\1\end{pmatrix} +
s\begin{pmatrix}0\\-1\\1\end{pmatrix} ; (r, s) \in \mathbb{R}

4.5. Drawing

The draw() function is not hard to use:

from sgl import *
line = Line(Point(0, 0, 0), Vector(1, 1, 1))
plane = Plane(Point(0, 0, 0), Vector(1, 1, 1))
vector = Vector(3, 5, 7)
point = intersection(line, plane)

draw([line, plane, vector, point])

Output:

_images/Screenshot-example-draw.png

Table Of Contents

Previous topic

3. Class method reference

This Page