3.1 Coordinates

Architecture relies on positioning elements in space. That position is expressed in terms of what we designate as coordinates: each coordinate is a number, and a sequence of coordinates identifies a point in space. This figure demonstrates a sequence of coordinates \((x,y,z)\) identifying a point \(P\) in a three-dimensional space. Different types of coordinate systems are possible and, in the case of this figure, we are using the \(Cartesian\) coordinate system (also known as \(rectangular\) coordinate system). The Cartesian coordinate system was named in honor of its inventor: René Descartes. Descartes was a 17th century French philosopher, author of the famous quote: "Cogito ergo sum", (I think, therefore I am) and of countless contributions in the fields of mathematics and physics.

Cartesian coordinates of a point in space.

image

There is a large number of useful operations that we can make using coordinates. For example, we can calculate the distance between two positions in space: \(P=(p_x,p_y,p_z)\) and \(Q=(q_x,q_y,q_z)\). That distance can be determined using the formula:

\[d = \sqrt{(q_x-p_x)^2+(q_y-p_y)^2+(q_z-p_z)^2}\]

A possible translation of this definition into Julia’s notation would be:

dist(px, py, pz, qx, qy, qz) = sqrt((qx-px)^2+(qy-py)^2+(qz-pz)^2)

The distance between \((2,1,3)\) and \((5,6,4)\) would then be:

> dist(2, 1, 3, 5, 6, 4)

5.916079783099616

Unfortunately, by treating coordinates as three independent numbers, its use becomes unclear. This can be observed in the function dist, which requires six different parameters, making it hard for the reader to know where the coordinates of each position start and end. This problem becomes even worse when a function must return a position in space, as it happens, for example, with the function that computes the mid position \(M\) between \(P=(p_x,p_y,p_z)\) and \(Q=(q_x,q_y,q_z)\). That position can be calculated using the formula:

\[M = (\frac{p_x+q_x}{2},\frac{p_y+q_y}{2},\frac{p_z+q_z}{2})\]

However, it is difficult to conceive a function that implements this behavior because, apparently, it would have to calculate three different results simultaneously, one for each of the coordinates \(x\), \(y\) and \(z\).

To deal with this kind of problems, mathematicians came up with the concept of \(tuple\). Informally, a tuple is just a group of values. A position in space is a tuple that groups three coordinates.

Most programming languages have mechanisms to create and manipulate tuples, and Julia is no exception. Julia provides a native tuple type and dedicated syntax to simplify the creation of tuples. For our purposes, however, it is preferable to use a specific kind of tuple to express the coordinate system being used. To use it, we must first require the module Khepri:

using Khepri

To specify the (Cartesian) coordinates \((x,y,z)\), Khepri provides the function xyz:

> xyz(1, 2, 3)

xyz(1,2,3)

> xyz(2*3, 4+1, 6-2)

xyz(6,5,4)

Note that the result of evaluating the expression xyz(1, 2, 3) is a value that represents a position in the three-dimensional Cartesian space. The value belongs to a type of information that is different from the ones previously seen, such as numbers and strings, and is written by Julia using the same syntax used to create it.