Skip to content

Commit f36e1d4

Browse files
committed
Fix clamped when nan exists
1 parent 3f470d7 commit f36e1d4

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

SwiftDraw/LayerTree.Builder.Path.Arc.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ import Foundation
3333

3434
//converts DOM.Path.Arc -> LayerTree.Path.Cubic
3535

36-
private extension Comparable {
36+
extension Comparable {
3737
func clamped(to limits: ClosedRange<Self>) -> Self {
38-
return min(max(self, limits.lowerBound), limits.upperBound)
38+
return min(limits.upperBound, max(limits.lowerBound, self))
3939
}
4040
}
4141

4242
private func almostEqual<T: FloatingPoint>(_ a: T, _ b: T) -> Bool {
4343
return a >= b.nextDown && a <= b.nextUp
4444
}
4545

46-
private func vectorAngle(ux: LayerTree.Float, uy: LayerTree.Float, vx: LayerTree.Float, vy: LayerTree.Float) -> LayerTree.Float {
46+
func vectorAngle(ux: LayerTree.Float, uy: LayerTree.Float, vx: LayerTree.Float, vy: LayerTree.Float) -> LayerTree.Float {
4747
let sign: LayerTree.Float = (ux * vy - uy * vx) < 0.0 ? -1.0 : 1.0
4848
let dot = (ux * vx + uy * vy).clamped(to: -1.0...1.0)
4949
return sign * acos(dot)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// LayerTree.Builder.Path.ArcTests.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 14/2/25.
6+
// Copyright 2025 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
33+
import XCTest
34+
@testable import SwiftDraw
35+
36+
final class LayerTreeBuilderPathArcTests: XCTestCase {
37+
38+
func testClamped() {
39+
XCTAssertEqual(5.clamped(to: 0...10), 5)
40+
XCTAssertEqual((10.1).clamped(to: 0...10), 10)
41+
XCTAssertEqual((-1).clamped(to: 0...10), 0)
42+
XCTAssertEqual(Double.infinity.clamped(to: 0...10), 10)
43+
XCTAssertEqual((-Double.infinity).clamped(to: 0...10), 0)
44+
XCTAssertEqual(Double.nan.clamped(to: 0...10), 0)
45+
XCTAssertEqual((-Double.nan).clamped(to: 0...10), 0)
46+
}
47+
48+
func testVectorAngle() {
49+
XCTAssertEqual(vectorAngle(ux: 1, uy: 1, vx: 1, vy: 1), 0)
50+
XCTAssertEqual(vectorAngle(ux: 1, uy: 1, vx: -1, vy: 1), 1.5707964, accuracy: 0.001)
51+
XCTAssertEqual(vectorAngle(ux: 1, uy: 1, vx: .nan, vy: 1), 3.1415925, accuracy: 0.001)
52+
}
53+
}

0 commit comments

Comments
 (0)