From 588755f2bdcb65e223c418cfa77e88d79aebf65c Mon Sep 17 00:00:00 2001 From: Yilin Wei Date: Tue, 6 Aug 2024 00:57:25 +0100 Subject: [PATCH] Add helper functions to Radians. Adds 'min', 'max' and 'unary_-' to Radians. --- .../src/main/scala/indigo/shared/datatypes/Radians.scala | 8 ++++++++ .../test/scala/indigo/shared/datatypes/RadiansTests.scala | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/indigo/indigo/src/main/scala/indigo/shared/datatypes/Radians.scala b/indigo/indigo/src/main/scala/indigo/shared/datatypes/Radians.scala index 1c4221bfc..e7a363c1c 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/datatypes/Radians.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/datatypes/Radians.scala @@ -3,6 +3,8 @@ package indigo.shared.datatypes import indigo.shared.dice.Dice import indigo.shared.time.Seconds +import scala.math + import annotation.targetName opaque type Radians = Double @@ -74,12 +76,18 @@ object Radians: def invert: Radians = negative + def `unary_-`: Radians = negative + def ~==(other: Radians): Boolean = Math.abs(r.toDouble - other.toDouble) < 0.001 def toDouble: Double = r + def max(other: Radians): Radians = math.max(r, other) + + def min(other: Radians): Radians = math.min(r, other) + def toFloat: Float = r.toFloat diff --git a/indigo/indigo/src/test/scala/indigo/shared/datatypes/RadiansTests.scala b/indigo/indigo/src/test/scala/indigo/shared/datatypes/RadiansTests.scala index 62ca73bf8..36c5a012b 100644 --- a/indigo/indigo/src/test/scala/indigo/shared/datatypes/RadiansTests.scala +++ b/indigo/indigo/src/test/scala/indigo/shared/datatypes/RadiansTests.scala @@ -52,6 +52,14 @@ class RadiansTests extends munit.FunSuite { assert(clue(Radians.mod(Radians(-11), Radians(-10))) ~== clue(Radians(-1))) } + test("max") { + assert(clue(Radians.PI.max(Radians.zero)) ~== Radians.PI) + } + + test("min") { + assert(clue(Radians.PI.min(Radians.zero)) ~== -Radians.zero) + } + def doubleCloseEnough(r1: Double, r2: Double): Boolean = r1 - 0.01 < r2 && r1 + 0.01 > r2