changeset 288:cc0f02ae9005

core: add maths_scale function
author David Demelier <markand@malikania.fr>
date Thu, 07 Jan 2021 15:50:01 +0100
parents 75d2fdf96064
children 63d9fb56c609
files doc/docs/dev/api/core/maths.md libmlk-core/core/maths.c libmlk-core/core/maths.h
diffstat 3 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/doc/docs/dev/api/core/maths.md	Mon Jan 04 11:34:18 2021 +0100
+++ b/doc/docs/dev/api/core/maths.md	Thu Jan 07 15:50:01 2021 +0100
@@ -19,3 +19,12 @@
 bool
 maths_is_boxed(int x, int y, unsigned int w, unsigned int h, int px, int py)
 ```
+
+### maths\_scale
+
+Scale a the value `v` from the range `[omin..omax]` to `[nmin..nmax]`.
+
+```c
+float
+maths_scale(float v, float omin, float omax, float nmin, float nmax)
+```
--- a/libmlk-core/core/maths.c	Mon Jan 04 11:34:18 2021 +0100
+++ b/libmlk-core/core/maths.c	Thu Jan 07 15:50:01 2021 +0100
@@ -26,3 +26,9 @@
 	       px < x + (int)w &&
 	       py < y + (int)h;
 }
+
+float
+maths_scale(float in, float old_min, float old_max, float new_min, float new_max)
+{
+	return (in / ((old_max - old_min) / (new_max - new_min))) + new_min;
+}
--- a/libmlk-core/core/maths.h	Mon Jan 04 11:34:18 2021 +0100
+++ b/libmlk-core/core/maths.h	Thu Jan 07 15:50:01 2021 +0100
@@ -24,4 +24,7 @@
 bool
 maths_is_boxed(int x, int y, unsigned int w, unsigned int h, int px, int py);
 
+float
+maths_scale(float v, float omin, float omax, float nmin, float nmax);
+
 #endif /* !MOLKO_CORE_MATHS_H */