From 17705e228878303bc4bc956f9e96acb49639fd32 Mon Sep 17 00:00:00 2001
From: Michael Sippel <micha@fragmental.art>
Date: Fri, 26 Apr 2024 11:16:39 +0200
Subject: [PATCH] move get_angle to util

---
 src/main.rs |  2 ++
 src/util.rs | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+)
 create mode 100644 src/util.rs

diff --git a/src/main.rs b/src/main.rs
index f3d0974..ba2626f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -13,6 +13,7 @@ use {
     angle::Turns
 };
 
+mod util;
 mod fixture;
 mod setup;
 mod view;
@@ -23,6 +24,7 @@ use crate::{
     fixture::Fixture,
     setup::LightingSetup,
     stripe_driver::StripeDriver,
+    util::get_angle
 };
 
 struct Breathing {
diff --git a/src/util.rs b/src/util.rs
new file mode 100644
index 0000000..c1698f4
--- /dev/null
+++ b/src/util.rs
@@ -0,0 +1,19 @@
+use cgmath::Vector2;
+
+pub fn get_angle(p: &Vector2<f32>) -> f32 {
+    let pi=3.1415926;
+    let pi2 = 2.0*pi;
+
+    if p.x < 0.0 {
+        (p.y / p.x).atan() / pi2 + 0.75
+    } else if p.x == 0.0 && p.y == 0.0 {
+        0.0
+    } else {
+        if p.y < 0.0 {
+            (-p.x / p.y).atan() / pi2
+        } else {
+            (p.y / p.x).atan() / pi2 + 0.25
+        }
+    }
+}
+