aboutsummaryrefslogtreecommitdiff
path: root/src/math/mod.rs
diff options
context:
space:
mode:
authorArne Dußin2020-11-01 17:44:51 +0100
committerArne Dußin2020-11-01 17:44:51 +0100
commit2db23f3213ff1bb2fe0cf005e922536da7ff5cd7 (patch)
tree912b6d829ea8c8c28a2ed257e70e0fd6814bef50 /src/math/mod.rs
parenta6be14ada463cb5a3fc0a810b8b341093abbb68d (diff)
downloadgraf_karto-2db23f3213ff1bb2fe0cf005e922536da7ff5cd7.tar.gz
graf_karto-2db23f3213ff1bb2fe0cf005e922536da7ff5cd7.zip
Refactor a major part of the project
In order to be able to save and load the map, a major rework of the code seemed necessary, since Vector2 and Rectangle of raylib do not implement serialize, and it seems cleanest to use the serialize/deserialize traits of serde, to save for instance to RON. ToolShed was renamed to Editor, since it should better show, that it does quite a bit more than harbour tools. The map data is now centrally saved in the editor, instead of decentralised in the tool structs.
Diffstat (limited to 'src/math/mod.rs')
-rw-r--r--src/math/mod.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/math/mod.rs b/src/math/mod.rs
new file mode 100644
index 0000000..07d518e
--- /dev/null
+++ b/src/math/mod.rs
@@ -0,0 +1,23 @@
+pub mod rect;
+pub use self::rect::*;
+
+pub mod vec2;
+pub use self::vec2::*;
+
+/// Round a floating point number to the nearest step given by the step argument. For instance, if
+/// the step is 0.5, then all numbers from 0.0 to 0.24999... will be 0., while all numbers from
+/// 0.25 to 0.74999... will be 0.5 and so on.
+pub fn round(num: f32, step: f32) -> f32 {
+ // Only positive steps will be accepted.
+ assert!(step > 0.);
+
+ let lower_bound = ((num / step) as i32) as f32 * step;
+ let upper_bound = lower_bound + step;
+
+ // Compare the distances and prefer the smaller. If they are the same, prefer the upper bound.
+ if (num - lower_bound) < (upper_bound - num) {
+ lower_bound
+ } else {
+ upper_bound
+ }
+}