 
     return Point2d(Int(x), Int(y))
 }
+
+/**
+ Projects a face consisting of three 3d coordinates from world space into 2d
+ image space orthographically.
+
+ Coordinates will be scaled both horiztonally and vertically to fit the image
+ dimensions.
+
+ - parameter face: The world-space 3d face to project (must be triangular).
+ - parameter mode: The model the face belongs to. This is used to look up vertices.
+ - parameter image: The image to project into.
+
+ - returns: The projected triangle.
+ */
+func projectModelFaceIntoScreenSpaceTriangle(face: Vector3<Int>, model: Mesh, image: Image) -> Triangle<Int> {
+    let vertex1 = model.vertices[face.x]
+    let vertex2 = model.vertices[face.y]
+    let vertex3 = model.vertices[face.z]
+
+    let point1 = screenCoordinateForWorldCoordinate(vertex1, image: image)
+    let point2 = screenCoordinateForWorldCoordinate(vertex2, image: image)
+    let point3 = screenCoordinateForWorldCoordinate(vertex3, image: image)
+    return Triangle(point1, point2, point3)
+}

    }
}

func triangleContainsPoint(triangle: Triangle<Int>, point: Point2d<Int>) -> Bool {
    let v1 = Vector3(Float(triangle.p3.x - triangle.p1.x), Float(triangle.p2.x - triangle.p1.x), Float(triangle.p1.x - point.x))
    let v2 = Vector3(Float(triangle.p3.y - triangle.p1.y), Float(triangle.p2.y - triangle.p1.y), Float(triangle.p1.y - point.y))

 */
public func renderWireframe(model: Mesh, image: Image) {
    (0..<model.faces.count).forEach { (index) in
        let triangle = projectModelFaceIntoScreenSpaceTriangle(model.faces[index], model: model, image: image)
        drawLine(triangle.p1, end: triangle.p2, colour: Colour.white(), image: image)
        drawLine(triangle.p2, end: triangle.p3, colour: Colour.white(), image: image)
        drawLine(triangle.p3, end: triangle.p1, colour: Colour.white(), image: image)
    }
}

/**
 Renders a mesh into an image using a flat shaded style.

 - parameter model: The mesh model to render.
 - parameter image: The image to render into.
 */
public func renderFlatShaded(model: Mesh, image: Image) {
    (0..<model.faces.count).forEach { (index) in
        let triangle = projectModelFaceIntoScreenSpaceTriangle(model.faces[index], model: model, image: image)
        drawTriangle(triangle, colour: Colour.white(), image: image)
    }
}

