Author
Travis Kirton


atan2()

This example shows the effect of the atan2() function.

let result = atan2(value)

To run this example you need to include the MathComparePaths.swift file in your project.

Example

var shapes = [Shape]()

override func setup() {
    let columns = 5
    let rows = 5
    let dx = canvas.width / Double(columns)
    let dy = canvas.height / Double(rows)

    let frame = Rect(0, 0, 50, 16)
    for x in 0..<columns {
        for y in 0..<rows {
            let shape = Rectangle(frame: frame)
            shape.anchorPoint = Point(0.0, 0.5)
            shape.interactionEnabled = false

            let newX = dx * (Double(x) + 0.5)
            let newY = dy * (Double(y) + 0.5)
            shape.center = Point(newX, newY)

            shapes.append(shape)
            canvas.add(shape)
        }
    }

    canvas.addPanGestureRecognizer { locations, center, translation, velocity, state in
        for shape in self.shapes {
            let angle = -1 * atan2(center.y-shape.center.y, center.x-shape.center.x)
            let transform = Transform.makeRotation(angle)
            shape.transform = transform
        }
    }
}