summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-05-17 19:36:15 +0200
committerAki <please@ignore.pl>2022-05-17 19:50:04 +0200
commit21341f788654cfc806778fa34d09885431083d76 (patch)
tree313cf15a37ed1f34b5807874a2a03ca8f421aeeb
parente9115c36c3580b209f7b53692deb52905125256e (diff)
downloadderelict-21341f788654cfc806778fa34d09885431083d76.zip
derelict-21341f788654cfc806778fa34d09885431083d76.tar.gz
derelict-21341f788654cfc806778fa34d09885431083d76.tar.bz2
Created a function for repeated distance calculations
-rw-r--r--Reader.cpp15
-rw-r--r--Utils-inl.h24
-rw-r--r--Utils.h8
-rw-r--r--View.cpp18
4 files changed, 40 insertions, 25 deletions
diff --git a/Reader.cpp b/Reader.cpp
index f5c8de3..f7c7ed6 100644
--- a/Reader.cpp
+++ b/Reader.cpp
@@ -1,11 +1,11 @@
#include "Reader.h"
-#include <cmath>
#include <vector>
#include "Grid.h"
#include "LongVector3.h"
#include "Source.h"
+#include "Utils.h"
#include "Wreck.h"
@@ -51,17 +51,10 @@ Reader::read(Source& source)
Grid&
find_grid_for(std::vector<Grid>& grids, const LongVector3& position)
{
- for (auto& grid : grids) {
- for (auto& wreck : grid.wrecks) {
- const long double dist =
- std::sqrt(
- std::pow(position.x - wreck.killmail.position.x, 2) +
- std::pow(position.y - wreck.killmail.position.y, 2) +
- std::pow(position.z - wreck.killmail.position.z, 2));
- if (dist < EXTENT)
+ for (auto& grid : grids)
+ for (auto& wreck : grid.wrecks)
+ if (dist3(position, wreck.killmail.position) < EXTENT)
return grid;
- }
- }
grids.push_back(Grid{});
return grids.back();
}
diff --git a/Utils-inl.h b/Utils-inl.h
new file mode 100644
index 0000000..01e0ca6
--- /dev/null
+++ b/Utils-inl.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include <cmath>
+
+
+template <typename V, typename T>
+T
+dist2(const V& lhs, const V& rhs)
+{
+ return std::sqrt(
+ std::pow(lhs.x - rhs.x, 2) +
+ std::pow(lhs.y - rhs.y, 2));
+}
+
+
+template <typename V, typename T>
+T
+dist3(const V& lhs, const V& rhs)
+{
+ return std::sqrt(
+ std::pow(lhs.x - rhs.x, 2) +
+ std::pow(lhs.y - rhs.y, 2) +
+ std::pow(lhs.z - rhs.z, 2));
+}
diff --git a/Utils.h b/Utils.h
new file mode 100644
index 0000000..b582540
--- /dev/null
+++ b/Utils.h
@@ -0,0 +1,8 @@
+#pragma once
+
+
+template <typename V, typename T=decltype(V::x)> T dist2(const V& lhs, const V& rhs);
+template <typename V, typename T=decltype(V::x)> T dist3(const V& lhs, const V& rhs);
+
+
+#include "Utils-inl.h"
diff --git a/View.cpp b/View.cpp
index ff4ff58..8e8296d 100644
--- a/View.cpp
+++ b/View.cpp
@@ -1,12 +1,12 @@
#include "View.h"
#include <algorithm>
-#include <cmath>
#include <raylib.h>
#include "Grid.h"
#include "Label.h"
+#include "Utils.h"
View::View(std::vector<Grid> grids) :
@@ -53,19 +53,9 @@ View::update(const float dt)
if (0 > pos.x || width < pos.x || 0 > pos.y || height < pos.y)
if (0 > base.x || width < base.x || 0 > base.y || height < base.y)
continue;
- const float depth =
- std::sqrt(
- std::pow(m_camera.position.x - wreck.position.x, 2) +
- std::pow(m_camera.position.y - wreck.position.y, 2) +
- std::pow(m_camera.position.z - wreck.position.z, 2));
- const float length =
- std::sqrt(
- std::pow(pos.x - base.x, 2) +
- std::pow(pos.y - base.y, 2));
- const bool hover =
- 8.0f > std::sqrt(
- std::pow(mouse.x - pos.x, 2) +
- std::pow(mouse.y - pos.y, 2));
+ const auto depth = dist3(m_camera.position, wreck.position);
+ const auto length = dist2(pos, base);
+ const bool hover = 8.0f > dist2(mouse, pos);
m_labels.push_back(Label{pos, base, depth, length, hover});
}
std::sort(m_labels.begin(), m_labels.end(), [](auto& a, auto& b){ return a.depth > b.depth; });