Skip to content

Instantly share code, notes, and snippets.

@fewlinesofcode
Last active April 12, 2021 18:02
Show Gist options
  • Select an option

  • Save fewlinesofcode/ef88362186cc955cfb7119c2602607a3 to your computer and use it in GitHub Desktop.

Select an option

Save fewlinesofcode/ef88362186cc955cfb7119c2602607a3 to your computer and use it in GitHub Desktop.
2d Vector Arithmetics Swift 5
//
//
// CGVectorArithmetics.swift
//
// Created by fewlinesofcode.com on 2/6/19.
// Copyright © 2019 fewlinesofcode.com All rights reserved.
//
import Foundation
import CoreGraphics
extension CGVector: AdditiveArithmetic {
// Vector addition
public static func + (left: CGVector, right: CGVector) -> CGVector {
return CGVector(dx: left.dx + right.dx, dy: left.dy + right.dy)
}
// Vector subtraction
public static func - (left: CGVector, right: CGVector) -> CGVector {
return left + (-right)
}
// Vector addition assignment
public static func += (left: inout CGVector, right: CGVector) {
left = left + right
}
// Vector subtraction assignment
public static func -= (left: inout CGVector, right: CGVector) {
left = left - right
}
// Vector negation
public static prefix func - (vector: CGVector) -> CGVector {
return CGVector(dx: -vector.dx, dy: -vector.dy)
}
}
infix operator * : MultiplicationPrecedence
infix operator / : MultiplicationPrecedence
infix operator • : MultiplicationPrecedence
extension CGVector {
// Scalar-vector multiplication
public static func * (left: CGFloat, right: CGVector) -> CGVector {
return CGVector(dx: right.dx * left, dy: right.dy * left)
}
public static func * (left: CGVector, right: CGFloat) -> CGVector {
return CGVector(dx: left.dx * right, dy: left.dy * right)
}
// Vector-scalar division
public static func / (left: CGVector, right: CGFloat) -> CGVector {
guard right != 0 else { fatalError("Division by zero") }
return CGVector(dx: left.dx / right, dy: left.dy / right)
}
// Vector-scalar division assignment
public static func /= (left: inout CGVector, right: CGFloat) -> CGVector {
guard right != 0 else { fatalError("Division by zero") }
return CGVector(dx: left.dx / right, dy: left.dy / right)
}
// Scalar-vector multiplication assignment
public static func *= (left: inout CGVector, right: CGFloat) {
left = left * right
}
}
extension CGVector {
// Vector magnitude (length)
public var magnitude: CGFloat {
return sqrt(dx*dx + dy*dy)
}
// Distance between two vectors
public func distance(to vector: CGVector) -> CGFloat {
return (self - vector).magnitude
}
// Vector normalization
public var normalized: CGVector {
return CGVector(dx: dx / magnitude, dy: dy / magnitude)
}
// Dot product of two vectors
public static func • (left: CGVector, right: CGVector) -> CGFloat {
return left.dx * right.dx + left.dy * right.dy
}
// Angle between two vectors
// θ = acos(AB)
public func angle(to vector: CGVector) -> CGFloat {
return acos(self.normalized • vector.normalized)
}
}
@littleduckcoding
Copy link
Copy Markdown

How can I type this symbol • ? I can't find any keys containing it on my Mac keyboard.

@fewlinesofcode
Copy link
Copy Markdown
Author

fewlinesofcode commented Mar 10, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment