Vectors and matrices
Points, directions, distances and rotations are everywhere in graphics, games and simulations. System.Numerics has small, fast types for them. This lesson uses Vector3 and Vector2 for lengths, sums, dot and cross products, and Matrix3x2 for moving and rotating a point.
What you will learn#
Vector2,Vector3: a point or a direction,Length,Distance,Normalize,Add,Subtract,Multiply,NegateandLerp,- the dot product and the cross product and what they tell you,
Matrix3x2: a move, a rotation, and both together.
The tutorial Drawing and animation moved shapes with Ring numbers. With vectors you describe a position as one value, and a transformation as one matrix, so the same code works for one point or for a thousand.
The program#
# vectors and matrices - Vector3 (length, dot and cross product, distance), Vector2 and a rotation with Matrix3x2
load "dotnet.ring"
using {
"System"
"System.Numerics"
}
func main
# 1. A Vector3 holds three numbers (X, Y, Z): a point or a direction in space (Vector2 has two numbers, Vector4 four).
# The numbers are single-precision floats, so results are shown rounded.
oA = new Vector3().@New([ :x = 3, :y = 4, :z = 0 ])
oB = new Vector3().@New([ :x = 1, :y = 2, :z = 3 ])
? "1. a = " + show3(oA) + ", length of a = " + oA.Length() + ", squared length = " + oA.LengthSquared()
# 2. The operations are static methods: Add, Subtract, Multiply (by a number), Negate
oVector = new Vector3()
? nl + "2. a + b = " + show3(oVector.Add(oA,oB))
? " a - b = " + show3(oVector.Subtract(oA,oB))
? " a * 2 = " + show3(oVector.Multiply(oA,2))
? " -a = " + show3(oVector.Negate(oA))
# 3. Dot product: a number that is 0 when two directions are at a right angle. Cross product: a vector at a right angle to both.
oX = new Vector3().@New([ :x = 1, :y = 0, :z = 0 ])
oY = new Vector3().@New([ :x = 0, :y = 1, :z = 0 ])
? nl + "3. x . y = " + oVector.Dot(oX,oY) + " (a right angle), a . b = " + oVector.Dot(oA,oB)
? " x cross y = " + show3(oVector.Cross(oX,oY)) + " (the z axis)"
# 4. Distance between two points, and the unit vector (length 1) of a direction
? nl + "4. distance from a to b = " + oVector.Distance(oA,oB)
? " a with length 1 = " + show3(oVector.Normalize(oA))
# 5. Lerp: the point between two points. 0 is the first one, 1 the second one, 0.5 the middle
? nl + "5. the middle of a and b = " + show3(oVector.Lerp(oA,oB,0.5))
# 6. A rotation of a 2D point with a matrix. Matrix3x2 does move, scale and rotate in the plane; the angle is in radians.
oPoint = new Vector2().@New([ :x = 1, :y = 0 ])
oMatrix = new Matrix3x2().CreateRotation(3.14159265358979 / 2) # a quarter of a turn
oRotated = new Vector2().Transform(oPoint,oMatrix)
? nl + "6. the point (1, 0) turned by 90 degrees is (" + roundNumber(oRotated.X) + ", " + roundNumber(oRotated.Y) + ")"
oMove = new Matrix3x2().CreateTranslation([ :xPosition = 10, :yPosition = 5 ])
oMoved = new Vector2().Transform(oPoint,oMove)
? " moved by (10, 5): (" + oMoved.X + ", " + oMoved.Y + ")"
oBoth = new Matrix3x2().Multiply(oMatrix,oMove) # first turn, then move
oResult = new Vector2().Transform(oPoint,oBoth)
? " turned and then moved: (" + roundNumber(oResult.X) + ", " + roundNumber(oResult.Y) + ")"
func show3 oVector
return "(" + roundNumber(oVector.X) + ", " + roundNumber(oVector.Y) + ", " + roundNumber(oVector.Z) + ")"
# a number rounded to 3 decimals (a float has about 7 digits, so 1E-8 is "zero")
func roundNumber nValue
nRounded = floor(nValue * 1000 + 0.5) / 1000
if nRounded = 0
nRounded = 0
ok
return nRounded1. a = (3, 4, 0), length of a = 5, squared length = 25
2. a + b = (4, 6, 3)
a - b = (2, 2, -3)
a * 2 = (6, 8, 0)
-a = (-3, -4, 0)
3. x . y = 0 (a right angle), a . b = 11
x cross y = (0, 0, 1) (the z axis)
4. distance from a to b = 4.12
a with length 1 = (0.60, 0.80, 0)
5. the middle of a and b = (2, 3, 1.50)
6. the point (1, 0) turned by 90 degrees is (0, 1)
moved by (10, 5): (11, 5)
turned and then moved: (10, 6)How it works#
Vectors#
A Vector3 holds three numbers X, Y, Z. It can be a position (a point in space) or a direction with a length (a speed, a force). Vector2 has two numbers and Vector4 four. They are value types (structs): new Vector3().@New([ :x = 3, :y = 4, :z = 0 ]) makes one by names, and oV.X reads a component. The numbers are single-precision floats (about 7 digits), so a result such as 0.6 can really be 0.6000000238; the program rounds before printing (roundNumber), and so should yours when you compare.
Operations#
The operators of Ring work on these structs: oA + oB adds two vectors, oA * 2 and 2 * oA scale one, oA = oB compares them. The operations are also static methods: Add(a, b), Subtract(a, b), Multiply(a, number), Negate(a). In Ring you call them on any object of the class (oVector = new Vector3(), then oVector.Add(oA, oB)). Length() is the distance from zero (3, 4, 0 has length 5), Distance(a, b) the distance between two points, and Normalize(a) the direction of a with length 1 — the unit vector, useful when you want a direction without a size. Lerp(a, b, t) is the point at the fraction t between a and b (0.5 is the middle): the base of every smooth movement.
Dot product#
Dot(a, b) is one number. It is 0 when the two directions are at a right angle, positive when they point more or less the same way, negative when they point in opposite ways. For two vectors of length 1 it is the cosine of the angle between them, so it answers questions such as "is the enemy in front of me?" without any trigonometry.
Cross product#
Cross(a, b) is a vector at a right angle to both a and b. The cross product of the x axis and the y axis is the z axis (step 3). It is used for the normal of a surface, for the axis of a turn, and for the sign of a turn in 2D.
Matrices#
A matrix describes a transformation of space as a block of numbers. Matrix3x2 works in the plane: CreateTranslation(x, y) moves, CreateScale scales and CreateRotation(angle) rotates around the origin, with the angle in radians (half a turn is 3.14159..., a quarter is half of that). Vector2.Transform(point, matrix) applies it. Turning (1, 0) by 90 degrees gives (0, 1); moving it by (10, 5) gives (11, 5).
Matrix3x2.Multiply(first, second) combines two transformations into one: first turn, then move gives (10, 6), because the point is turned to (0, 1) and then moved. The order matters — moving first and turning second gives another point — which is the most common mistake with matrices. These methods have several forms, so the bridge chooses the form from the arguments (new Matrix3x2().CreateRotation(...)). Matrix4x4 does the same in three dimensions (with CreatePerspective, CreateLookAt...) and Quaternion describes 3D rotations without the problems of angles.
These types are made for speed in .NET code. Each call from Ring crosses the bridge, so use them for the logic of your program, and let .NET do the heavy loops (for example a LINQ query, the tutorial Collections and LINQ) when you have thousands of points.
What you learned#
- vectors as points and directions,
- length, distance, unit vectors and
Lerp, - the dot product (a number) and the cross product (a vector),
- matrices that move and rotate, and combining them,
- the order of transformations matters.
Exercises#
- Write
angleBetween(a, b)withDot,LengthandAcos(the tutorial Numbers, dates and times). - Move a point along a straight line from A to B in 10 steps with
Lerpand print the points. - Rotate the four corners of a square by 45 degrees and print them.
- Try move first, then turn and compare the result with step 6.