Dot products and saving face

A few weeks back, someone asked me a question that, as someone who has worked with computer graphics quite a bit, I should have instantly known the answer to: how to compute a dot product of two vectors. Instead, I floundered for an answer and tried just about every way but the correct way I could conceive of because the correct way ‘didn’t look right’ to me. If you’ve ever spelled a word and then thought, ‘that doesn’t look right’ even though you have spelled it correctly, you’ll know what I mean.

In my defense, I had been running low on sleep and energy for a few weeks at that point due to working on a proposal.  So not only was I tired, I was cold on writing code. Worse, I hadn’t written a line of graphics-related code in months.

These are really just excuses though.  Eventually I slogged through to the correct answer, but I felt like I had completely misrepresented myself to this person.

So, without further ado, here is an example of how to compute a dot product between two vectors in C++:


#ifndef ____vec4__
#define ____vec4__
class vec4 {
    float x, y, z, w;
    vec4(float x, float y, float z, float w);
    float dot(vec4 v);
#endif /* defined(____vec4__) */


#include "vec4.h"
#include <stdio.h>
int main() {
    vec4 v1 = vec4(5.0f, 4.0f, 3.0f, 1.0f);
    vec4 v2 = vec4(1.0f, 2.0f, 3.0f, 1.0f);
    float dotProduct =;
    fprintf(stdout, "dot product: %.2f\n", dotProduct);
vec4::vec4(float x, float y, float z, float w) {
    this->x = x;
    this->y = y;
    this->z = z;
    this->w = w;
float vec4::dot(vec4 v) {
    float sum = 0.0f;
    sum += this->x*v.x;
    sum += this->y*v.y;
    sum += this->z*v.z;
    sum += this->w*v.w;
    return sum;

There are a dozen other methods that should be added to this class as well, like a cross-product, conversion to a unit vector, or a method for determining the angle between this vector and another, but it occurred to me that there are plenty of C++ libraries out there that already do this. However, is there one for Swift? I’m not sure – there are the beginnings of a Vector2D in the Swift Programming Language Guide, but it is clearly not finished.

I decided I’d mirror the functionality of the above C++ class in a Swift class in a playground as a start in the direction of creating a reusable vector class:

class Vector4D {
    var x, y, z, w : Float;
    init(var x : Float, var y : Float, var z : Float, var w : Float) {
        self.x = x;
        self.y = y;
        self.z = z;
        self.w = w;
    convenience init() {
        self.init(x: 0.0, y: 0.0, z: 0.0, w: 0.0);
    func dot(vec : Vector4D) -> Float {
        var sum : Float = 0.0;
        sum += self.x*vec.x;
        sum += self.y*vec.y;
        sum += self.z*vec.z;
        sum += self.w*vec.w;
        return sum;
var v1 : Vector4D = Vector4D(x: 5.0, y: 4.0, z: 3.0, w: 1.0);
var v2 : Vector4D = Vector4D(x: 1.0, y: 2.0, z: 3.0, w: 1.0);;

The code is remarkably similar to the C++ code, but there’s definitely some new flavors in there too.  I’ll come back to this later if it turns out that I need something like this.