Merge: math: Intro `Comparable::clamp`
authorJean Privat <jean@pryen.org>
Mon, 27 Jun 2016 21:58:42 +0000 (17:58 -0400)
committerJean Privat <jean@pryen.org>
Mon, 27 Jun 2016 21:58:42 +0000 (17:58 -0400)
The `clamp` service restricts a value within a given range. It is a common service in GLSL and it is especially useful for games. Even though the alternative is short: `x = x.min(1.0).max(0.0)`, it is quite confusing and the simple `x = x.clamp(0.0, 1.0)` is easier to read.

Pull-Request: #2207
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>

contrib/action_nitro/src/action_nitro.nit
contrib/asteronits/src/game_logic.nit
contrib/tinks/src/client/client.nit
contrib/tinks/src/game/tanks.nit
lib/core/math.nit
lib/mpd.nit

index 17c7b05..dc0fac2 100644 (file)
@@ -216,7 +216,7 @@ redef class App
                var p = altitude / world.boss_altitude
                var ip = 1.0 - p
                glClearColor(0.3*ip, 0.3*ip, ip, 1.0)
-               stars.alpha = (1.4*p-0.4).min(1.0).max(0.0)
+               stars.alpha = (1.4*p-0.4).clamp(0.0, 1.0)
 
                # Randomly add smoke
                var poss = [
index b7ec3f4..785fe9d 100644 (file)
@@ -163,7 +163,7 @@ abstract class SpacialObject
                # Realistic rotation, kept for reference and reality minded individuals
                #var r = applied_rotation * 0.2
                #rotation_inertia += r
-               #rotation_inertia = rotation_inertia.min(2.0).max(-2.0)
+               #rotation_inertia = rotation_inertia.clamp(-2.0, 2.0)
 
                # Inertia to position
                rotation += rotation_inertia * dt
index ba508c9..bc434b7 100644 (file)
@@ -124,7 +124,7 @@ redef class App
                var local_tank = local_tank
                if local_tank != null then
                        var tank_speed = local_tank.direction_forwards*local_tank.rule.max_speed
-                       tank_speed = tank_speed.min(0.5).max(-0.5)
+                       tank_speed = tank_speed.clamp(-0.5, 0.5)
 
                        var prop_pos = local_tank.pos + local_tank.heading.to_vector(tank_speed * 16.0)
                        var old_pos = camera.center(display)
@@ -194,7 +194,7 @@ redef class App
                        var screen_pos = tank.pos.to_screen(camera)
 
                        var damage = tank.rule.max_health - tank.health
-                       damage = damage.max(0).min(tank.rule.base_images.length)
+                       damage = damage.clamp(0, tank.rule.base_images.length)
 
                        var base_image = tank.rule.base_images[damage]
                        display.blit_rotated(base_image, screen_pos.x, screen_pos.y, tank.heading)
index 1289415..9970941 100644 (file)
@@ -290,11 +290,11 @@ class TankDirectionOrder
        do
                # TODO use events
                var direction_heading = direction_heading
-               direction_heading = direction_heading.min(1.0).max(-1.0)
+               direction_heading = direction_heading.clamp(-1.0, 1.0)
                tank.direction_heading = direction_heading*tank.rule.max_direction
 
                var direction_forwards = direction_forwards
-               direction_forwards = direction_forwards.min(1.0).max(-1.0)
+               direction_forwards = direction_forwards.clamp(-1.0, 1.0)
                tank.direction_forwards = direction_forwards*tank.rule.max_speed
        end
 end
index 7b51e2a..c8842fd 100644 (file)
@@ -420,6 +420,17 @@ fun inf: Float do return 1.0 / 0.0
 # ~~~
 fun nan: Float do return 0.0 / 0.0
 
+redef class Comparable
+       # Constraint `self` within `[min..max]`
+       #
+       #     assert 1.clamp(5, 10) == 5
+       #     assert 7.clamp(5, 10) == 7
+       #     assert 15.clamp(5, 10) == 10
+       #     assert 1.5.clamp(1.0, 2.0) == 1.5
+       #     assert "a".clamp("b", "c") == "b"
+       fun clamp(min, max: OTHER): OTHER do return self.max(min).min(max)
+end
+
 redef class Collection[ E ]
        # Return a random element form the collection
        # There must be at least one element in the collection
index 727ac60..b0d25d8 100644 (file)
@@ -130,7 +130,7 @@ class MPDConnection
                        var vol = status.volume
                        if vol != null then
                                var new_vol = vol + diff
-                               new_vol = new_vol.max(0).min(100)
+                               new_vol = new_vol.clamp(0, 100)
                                volume = new_vol
                                return
                        end