{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A simple 3D vector class\n", "\n", "A 3D vector has 3 coordinates along the X, Y and Z-axis and is commonly used to represent geometry in computer graphics. In it simple form here it supports only one operation `add` that computes the component-wise sum of two vectors. \n", "\n", "$$\n", "\\vec{a} + \\vec{b} \n", "= \n", "\\begin{pmatrix} \n", "a_x \\\\ a_y \\\\ a_z\n", "\\end{pmatrix}\n", "+\n", "\\begin{pmatrix} \n", "b_x \\\\ b_y \\\\ b_z\n", "\\end{pmatrix}\n", "=\n", "\\begin{pmatrix} \n", "a_x + b_x \\\\ a_y + b_y \\\\ a_z + b_z\n", "\\end{pmatrix}\n", "$$\n", "\n", "\n", "Implementations like below can often be found in the wild:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "public class Vec3 {\n", " private double x, y, z;\n", "\n", " public Vec3(double x, double y, double z) {\n", " this.x = x;\n", " this.y = y;\n", " this.z = z;\n", " }\n", "\n", " public Vec3 add(Vec3 v) {\n", " x += v.x;\n", " y += v.y;\n", " z += v.z;\n", " return this;\n", " }\n", "\n", " @Override\n", " public String toString() {\n", " return \"(\" + x + \", \" + y + \", \" + z + \")\";\n", " }\n", "}\n", "\n", "System.out.println(new Vec3(1.0, 2.0 ,3.0));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This implementation has a [*fluent interface*](https://en.wikipedia.org/wiki/Fluent_interface) for it's `add` method that allows easy chaining of operations on a vector. For example the addition of 3 vectors can be written as \n", "\n", "```java\n", "Vec3 sumABC = a.add(b).add(c);\n", "```\n", "\n", "Now, consider the follwing declarations of vector variables `a` and `b`:" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "Vec3 a = new Vec3(0, 1, 0);\n", "Vec3 b = new Vec3(1, 0, 0);\n", "\n", "System.out.println(a);\n", "System.out.println(b);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assume, the following calculations need to be performed:\n", "\n", "$$\n", "\\begin{eqnarray}\n", "\\vec{c} & = & \\vec{a} + \\vec{b} \\\\\n", "\\vec{d} & = & \\vec{c} + \\vec{a}\n", "\\end{eqnarray}\n", "$$\n", "\n", "Try to predict, which values variables `c` and `d` will have after the following implementation of this calculation has been executed:" ] }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "Vec3 a = new Vec3(0, 1, 0);\n", "Vec3 b = new Vec3(1, 0, 0);\n", "Vec3 c = a.add(b);\n", "Vec3 d = c.add(a);\n", "\n", "System.out.println(c);\n", "System.out.println(d);" ] } ], "metadata": { "file_extension": ".py", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "mimetype": "text/x-python", "name": "python", "npconvert_exporter": "python", "pygments_lexer": "ipython3", "version": 3 }, "nbformat": 4, "nbformat_minor": 2 }