Skip to content

Commit

Permalink
add few additions to Vec2
Browse files Browse the repository at this point in the history
  • Loading branch information
s1ddok committed Nov 13, 2020
1 parent d81238a commit 4fc945a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
54 changes: 53 additions & 1 deletion Sources/Vector2+simd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import simd

@frozen
public struct Vector2f {
public struct Vector2f: Codable {
internal var d: SIMD2<Float>

public var x: Float { get { return d.x } set { d.x = newValue } }
Expand Down Expand Up @@ -46,6 +46,10 @@ public struct Vector2f {
public init(_ x: Float, _ y: Float) {
self.d = SIMD2<Float>(x, y)
}

public init(_ simdV: SIMD2<Float>) {
self.d = simdV
}
}

public extension Vector2f {
Expand Down Expand Up @@ -78,6 +82,14 @@ public extension Vector2f {

//MARK: - functions

func distance(to other: Vector2f) -> Float {
return (self - other).length
}

func angle(with other: Vector2f) -> Float {
return atan2f(other.y - self.y, other.x - self.x)
}

func dot(_ x: Vector2f) -> Float {
return simd.dot(d, x.d)
}
Expand All @@ -90,8 +102,24 @@ public extension Vector2f {
return unsafeBitCast(simd.mix(d, to.d, t: factor), to: Vector2f.self)
}

func min() -> Float {
return self.d.min()
}

func max() -> Float {
return self.d.max()
}

mutating func round(_ rule: FloatingPointRoundingRule) {
self.d.round(rule)
}

//MARK: - operators

static func +(lhs: Vector2f, rhs: Float) -> Vector2f {
return unsafeBitCast(lhs.d + rhs, to: Vector2f.self)
}

static func +(lhs: Vector2f, rhs: Vector2f) -> Vector2f {
return unsafeBitCast(lhs.d + rhs.d, to: Vector2f.self)
}
Expand All @@ -112,6 +140,18 @@ public extension Vector2f {
return unsafeBitCast(lhs.d * rhs.d, to: Vector2f.self)
}

static func /(lhs: Vector2f, rhs: Float) -> Vector2f {
return unsafeBitCast(lhs.d / rhs, to: Vector2f.self)
}

static func /(lhs: Float, rhs: Vector2f) -> Vector2f {
return unsafeBitCast(lhs / rhs.d, to: Vector2f.self)
}

static func /(lhs: Vector2f, rhs: Vector2f) -> Vector2f {
return unsafeBitCast(lhs.d / rhs.d, to: Vector2f.self)
}

static func *(lhs: Matrix4x4f, rhs: Vector2f) -> Vector2f {
let res = lhs.d * Vector4f(rhs).d
return Vector2f(res.x, res.y)
Expand All @@ -125,6 +165,18 @@ public extension Vector2f {
static func *=(lhs: inout Vector2f, rhs: Float) {
lhs.d *= rhs
}

static func /=(lhs: inout Vector2f, rhs: Float) {
lhs.d /= rhs
}

static func *=(lhs: inout Vector2f, rhs: Vector2f) {
lhs.d *= rhs.d
}

static func /=(lhs: inout Vector2f, rhs: Vector2f) {
lhs.d /= rhs.d
}
}

extension Vector2f: Equatable {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Vector4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ public typealias vec4 = Vector4f
public extension Vector4f {
//MARK: - initializers

init(_ v1: Vector2f, _ v2: Vector2f) {
self.init(v1.x, v1.y, v2.x, v2.y)
}

init(_ v: Vector2f, _ z: Float = 0.0, _ w: Float = 1.0) {
self.init(v.x, v.y, z, w)
}
Expand Down

0 comments on commit 4fc945a

Please sign in to comment.