Last active
April 12, 2021 18:02
-
-
Save fewlinesofcode/ef88362186cc955cfb7119c2602607a3 to your computer and use it in GitHub Desktop.
2d Vector Arithmetics Swift 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// | |
// 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) | |
} | |
} |
Alt + 8 in US layout. You can also replace the operator with anything you want.
—
… On 10. Mar 2020, at 13:38, Phung Tien Minh ***@***.***> wrote:
How can I type this symbol • ? I can't find any keys containing it on my Mac keyboard.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I type this symbol • ? I can't find any keys containing it on my Mac keyboard.