Points vs vectors
A point is a position; a vector is a step with a direction and a length. Learn the four rules that connect them, and why keeping them apart prevents a whole class of bugs.
The four rules
| Expression | Result | Meaning |
|---|---|---|
Points in a BIM model, and vectors
| Points (positions) | Vectors (directions and steps) |
|---|---|
Let the compiler keep them apart
XYZ, System.Numerics.Vector3), so nothing stops you from adding two points. If you write your own types, you can make the four rules the only operations that compile:
var a = new Pt(2, 1);var b = new Pt(8, 4); Vec v = b - a; // point - point = vectorConsole.WriteLine($"v = {v}"); var east = new Vec(10, 0);Pt a2 = a + east, b2 = b + east; // point + vector = pointConsole.WriteLine($"Moved: {a2} to {b2}");Console.WriteLine($"v after the move = {b2 - a2}"); // Pt nonsense = a + b; // does not compile: no Pt + Pt operator readonly record struct Pt(double X, double Y){ public static Vec operator -(Pt a, Pt b) => new(a.X - b.X, a.Y - b.Y); public static Pt operator +(Pt p, Vec v) => new(p.X + v.X, p.Y + v.Y); public override string ToString() => $"({X}, {Y})";} readonly record struct Vec(double X, double Y){ public override string ToString() => $"<{X}, {Y}>";}v = <6, 3> Moved: (12, 1) to (18, 4) v after the move = <6, 3>
Pt and Vec with a new Vec * double operator, print the positions of three hangers at ¼, ½ and ¾ of the way along it.
Show a hint
operator *(Vec v, double k) to Vec.
Show the solution
var start = new Pt(1, 2);var end = new Pt(9, 6);Vec span = end - start; for (int i = 1; i <= 3; i++) Console.WriteLine($"Hanger {i}: {start + span * (i / 4.0)}"); readonly record struct Pt(double X, double Y){ public static Vec operator -(Pt a, Pt b) => new(a.X - b.X, a.Y - b.Y); public static Pt operator +(Pt p, Vec v) => new(p.X + v.X, p.Y + v.Y); public override string ToString() => $"({X}, {Y})";} readonly record struct Vec(double X, double Y){ public static Vec operator *(Vec v, double k) => new(v.X * k, v.Y * k);}Hanger 1: (3, 3) Hanger 2: (5, 4) Hanger 3: (7, 5)
Remember
A point is a position; a vector is a displacement with a direction and a length, but no position. point − point = vector, point + vector = point, vector ± vector = vector; point + point is meaningless. Weighted averages of points are fine when the weights add up to 1: A + t(B − A) is any point along AB. Moving a model changes its points but not its directions and normals; rotating changes both. Separate types, or at least clear names, keep the two from being mixed up.