4 Position and Direction Vectors🔗ℹ
A vector in 3D space is uniquely identified by three real numbers called coordinates or
components.
For example, the coordinates of (pos 1 2 3) are 1, 2 and 3.
Pict3D distinguishes between two kinds of vectors:
A position vector, which is a value of type Pos, represents an absolute
position in 3D space.
A direction vector, which is a value of type Dir, represents a direction,
with distance, in 3D space.
Generally, think of direction vectors as how to get from one position vector in space to another.
Suppose we define a direction vector dv1 as
To picture it, we need a starting position vector.
We’ll arbitrarily choose the
origin, which is
(pos 0 0 0).
We can use the direction vector dv1 to move the center of a sphere.
|
> move-once |
|
We might even define another direction vector dv2 (pointing upward) to move it again.
But we don’t have to move it twice: we can move it just once, by adding the direction vectors
dv1 and dv2.
Composing transformations—
not just movement, but all parallel-line-preserving transformations—
is
discussed in the section
Transformation.
Directions are used to move entire scenes, not just points:
When used as scaling factors, as in
(rectangle origin (dir 1/3 1/3 1)), directions represent
moving the center to a corner:
4.1 Direction Vectors🔗ℹ
Constructs a
direction vector from its components
dx,
dy and
dz, or
converts a vector
v in another representation to a direction vector.
The zero direction. Analogous to
origin.
The positive and negative coordinate axis directions.
Alternatively, these are the face normals for an axis-aligned rectangle, or the directions from the
center of a
(cube v 1) to the centers of each of its faces.
The directions from the center of a
(cube v 1) to the centers of each of its edges.
The directions from the center of a
(cube v 1) to each of its corners.
(dir+ dv1 dv2) composes movement in the directions
dv1 and
dv2 by adding
their components.
(dir-scale dv s) scales the direction dv by multiplying each component by
s.
(dir-negate dv) returns the direction opposite dv by negating each component, and is
equivalent to (dir-scale dv -1).
(dir- dv1 dv2) is equivalent to (dir+ dv1 (dir-negate dv2)).
Return the distance and squared distance represented by dv.
Return the distance represented by
dv under the L-
p
norm.
Returns the
projection of
dv1
onto
dv2, or
#f if
dv2 is
(dir 0 0 0).
Returns the
rejection of
dv1
from
dv2.
Equivalent to
Reflects
dv1 in the direction
dv2.
Equivalent to
(dir-reject dv1 dv2 2).
If dv is the velocity of an object that hits an immovable surface with normal n,
then (dir-reject dv n) is the object’s new velocity, assuming an ideal
elastic collision.
In other words, use this function to bounce things off walls in games.
Converts yaw (aka longitude, or rotation about the z axis) and pitch
(aka latitude, or rotation about the x axis) into a direction with distance 1.
Both angles are measured in degrees.
Yaw is applied first.
Converts a direction
dv into yaw (aka longitude) and pitch (aka latitude).
This is the inverse of
angles->dir.
Return the components of dv.
4.2 Position Vectors🔗ℹ
Constructs a
position vector from its coordinates
x,
y and
z, or
converts a vector
v in another representation to a position vector.
The origin. If 3D space has a center, this is it. Analogous to
zero-dir.
Moves
v in direction
dv optionally scaled by
s.
(pos+ v dv s) is equivalent to
(pos+ v (dir-scale dv s)), but more accurate
when one of the resulting components is near
0.0.
Returns the direction to move in to get from
start to
end; i.e. the
dv for
which
(pos+ start dv) is
end.
Interpolates between start and end by fraction t.
The fraction t should be between 0 and 1, but does not have to be.
Return the distance and squared distance between v1 and v2.
Return the coordinates of v.