Swiftpack.co -
koher/CGPointVector as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

koher/CGPointVector

Makes it easy to use CGPoint as vectors in Swift

.package(url: "https://github.com/koher/CGPointVector.git", from: "0.4.1")
*CGPointVector* provides the extension for arithmetic operations with *CGPoint* in **Swift**, which are convenient when *CGPoint* values are used as vectors.

```
let a = CGPoint(x: 1, y: 2), b = CGPoint(x: -3, y: 5)
let sum = a + b // (-2.0, 7.0)
let distance = (b - a).length // 5.0
let direction = (b - a).unit // (-0.8, 0.6)
let rotated = a * CGAffineTransform(rotationAngle: CGFloat.pi / 2) // (-2.0, 1.0)
```

```
import CGPointVector
let a = CGPoint(x: 3.0, y: -4.0)
let b = CGPoint(x: 2.0, y: -5.0)
let sum = a + b // (5.0, -9.0)
let length = a.length // 5.0
let distance = a.distance(from: b) // 1.4142135623731
let unitVector = a.unit // (0.6, -0.8)
let dotProduct = a.dot(b) // 26.0
let angle = a.angle(from: b) // 0.26299473168091936
```

link |

Stars: 33 |

Last commit: 3 weeks ago |

Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Avoid overflow and underflow of `length` and `distance`

1 year ago

- Replace
`x * x + y * y`

with`hypot(x, y)`

(9feb2ead44f320705c89fbb8e3a40baa7e1a1fb7)- This prevents overflow and underflow during calculating
`length`

and`distance`

. It makes it possible to pass the following tests.

- This prevents overflow and underflow during calculating

```
XCTAssertEqual(CGPoint(x: .greatestFiniteMagnitude, y: 0.0).length, .greatestFiniteMagnitude)
XCTAssertEqual(CGPoint(x: 0.0, y: .leastNonzeroMagnitude).length, .leastNonzeroMagnitude)
```

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API