Computational Geometry

Chip Bell

Vectors

Vector
  • Can represents a group of real numbers (not useful for us)
  • Can represent a position in space (useful for us)
  • Can represent something with magnitude and direction (useful for us)

A Position in space (aka Cartesian coordinates)

\[ (x, y) \]

y x

Something with Magnitude and direction (aka Polar coordinates)

\[ (r, \theta) \]

r θ

Converting from Cartesian to Polar

\[ r = \sqrt{x^2 + y^2} \]

\[ \theta = \tan^{-1}{\frac{y}{x}} \]

Converting from Polar to Cartesian

\[ x = r\cos{\theta} \]

\[ y = r\sin{\theta} \]

Vector Operations

Addition

\[ (x_1, y_1) + (x_2, y_2) = (x_1 + x_2, y_1 + y_2) \]

  • Basically add up corresponding elements)
  • Essentially like saying "Move $x_1$ left, $y_1$ north. From there, move $x_2$ left, then $y_2$ north"
  • Subtraction works the same way

Dot Product

\[ (x_1, y_1) \cdot (x_2, y_2) = x_1 x_2 + y_1 y_2 \]

  • Measures how much "vectors have in common"
  • Can provide length of a vector: $\sqrt{v \cdot v}$ is the length of the vector $v$
  • Can provide angle between two vectors (later)

Cross Product

\[ (x_1, y_1) \times (x_2, y_2) = x_1y_2 - y_1x_2 \]

  • Can provided angle between vectors
  • Can be used to provide angle
  • Can be used for calculating "turn direction"
  • Pay attention to the subtraction!
  • Pay attention to the combination of $x$'s and $y$'s

Rotation

Given an angle $\theta$ \[ R(v, \theta) = (v_x\cos{\theta} + v_y\sin{\theta}, v_x\sin{\theta} + v_y\cos{\theta}) \]

  • Rotates a vector around the origin. To rotate around a vector $w$: \[ R_w(v, \theta) = w + R(v - w, \theta) \]
  • Pay attention to the comma!
  • Pay attention to the signs!

Implementations

Python


class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def add(self, other):
        return Vector(self.x + other.x, self.y + other.y)
    
    def negate(self):
        return Vector(-self.x, -self.y)

    def subtract(self, other):
        return self.add(other.negate())

    def dot(self, other):
        return self.x * other.x + self.y * other.y

    def magnitude(self):
        return (self.dot(self)) ** 0.5

    def cross(self, other):
        return self.x * other.y - self.y * other.x

    def __repr__(self):
        return "{0},{1}".format(self.x, self.y)

v = Vector(1, 0)
w = Vector(0, 1)

print(v.dot(w))
print(v.cross(w))
print(v.add(w))
print(v.negate())
print(v.add(w).magnitude())
                        

Java


public class Vector {
    protected double x;
    protected double y;

    public Vector(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Vector add(Vector other) {
        return new Vector(this.x + other.x, this.y + other.y);
    }

    public Vector negate() {
        return new Vector(-this.x, -this.y);
    }

    public Vector subtract(Vector other) {
        return this.add(other.negate());
    }

    public double dot(Vector other) {
        return this.x * other.x + this.y * other.y;
    }

    public double magnitude() {
        return Math.sqrt(this.dot(this));
    }

    public double cross(Vector other) {
        return this.x * other.y - this.y * other.x;
    }

    public String toString() {
        return "" + this.x + "," + this.y;
    }

    public static void main(String[] args) {
        Vector v = new Vector(1, 0);
        Vector w = new Vector(0, 1);

        System.out.println(v.dot(w));
        System.out.println(v.cross(w));
        System.out.println(v.add(w));
        System.out.println(v.negate());
        System.out.println(v.add(w).magnitude());
    }
}
                        

Basic Applications

Length of Vector

\[ || v || = \sqrt{v \cdot v } \]

Distance Between Vectors

It's the magnitude of the difference \[ || w - v || = \sqrt{ (w - v) \cdot (w - v) } \]

Angle Between Two Vectors

\[ \theta = \cos^{-1} \frac{a \cdot b }{ |a| |b| } \]

θ

Be mindful of the range of $cos^-1$

Area of Parallelogram

\[ |v \times w| \]

v w |v × w|

Note that you can divide in two to get the area of a triangle!

More Complex Applications

Area of a Polygon

We can extend the area notion with the cross product to get the area of a polygon

Given a polygon with $n$ points $(x_0, y_0), (x_1, y_1), ..., (x_{n-1}, y_{n-1})$, we can use the Shoelace formula to calculate area: \[ A = \frac{1}{2} \sum_{i=0}^{n-1} p_i \times p_{i+1} \]

  • Note that $p_i = (x_i, y_i)$ and $p_n = p_0$
  • This value can be negative, so take the absolute value

Convex Hull

Finds the smallest convex polygon that contains a set of points

Line Segment Intersection

Given two line segments: \[ L_1(t) = p_1 + d_1t, 0 \le t \le 1 \] and \[ L_2(t) = p_2 + d_2t, 0 \le t \le 1 \]

We can find where the intersect by clever usage of the dot product and cross product. Figure it out yourself!

Many, Many More

Example Problems

Welding

  • Premise is to count number of ways to place arc-shaped pieces of metal
  • Being able to move along an arc requires lots of rotation vectors!
  • Solution here

Robots

  • You've got a Martian robot with two 1-foot radius wheels
  • You're given a series of rotation commands for each wheel
  • Each rotation command has a speed and duration associated with it
  • Calculate the location of the robot after the commands are completed
  • Requires you to calculate the turn radius at each moment and do similar arc math as before
  • Solution here

Convex Hull

  • Requires you to calculate the points contained in the convex hull of a set
  • The convex hull is the smallest convex polygon that contains a set of points (rubber band around nails)
  • Basic solution is to calculate the convex hull, and then keep only points not in the convex hull
  • Pretty much self-explanatory: Implement Jarvis March...
  • Problem outlines an algorithm, but the Graham Scan is easier to write and understand in a competition
  • Really nice Convex Hull implementations here (using the Monotone Chain Algorithm). I suggest you copy, bring to the contest, and just modify for the problem!

Star Simulations

  • Given a series of points, how many are less than $k$ units away from each other?
  • Classic spatial indexing problem
  • Solution here, using simple but clever spatial indexing