add vector
This commit is contained in:
24
bin/main.cpp
24
bin/main.cpp
@@ -10,8 +10,8 @@
|
|||||||
#include "range/range.hpp"
|
#include "range/range.hpp"
|
||||||
#include "container/container.hpp"
|
#include "container/container.hpp"
|
||||||
#include "logger/logger.hpp"
|
#include "logger/logger.hpp"
|
||||||
#include "matrix/matrix.hpp"
|
#include "math/matrix.hpp"
|
||||||
#include "vector/vector.hpp"
|
#include "math/vector.hpp"
|
||||||
|
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
@@ -110,7 +110,23 @@ int main(int argc, char *argv[])
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
{// ex: vector
|
{// ex: vector
|
||||||
hack::vector<int, int, int> v3 { 1, 2, 3, };
|
hack::vector<int, int, int> v3_1 { 8, 4, 9, };
|
||||||
hack::log()(v3.get_value());
|
hack::vector<int, int, int> v3_2 { 1, 2, 3, };
|
||||||
|
hack::log()(v3_1.get_value());
|
||||||
|
hack::log()(v3_2.get_value());
|
||||||
|
|
||||||
|
v3_1 = v3_2;
|
||||||
|
|
||||||
|
hack::log()(v3_1.get_value());
|
||||||
|
hack::log()(v3_2.get_value());
|
||||||
|
|
||||||
|
hack::log()("length 3", v3_2.length());
|
||||||
|
|
||||||
|
hack::vector<int, int> v2_1 { 11, 22 };
|
||||||
|
hack::log()("length 2", v2_1.length());
|
||||||
|
|
||||||
|
hack::vector<int, int, int> lerp_1 { 1, 2, 3 };
|
||||||
|
hack::vector<int, int, int> lerp_2 { 5, 6, 7 };
|
||||||
|
hack::log()("lerp", lerp_1.lerp(lerp_2, 0.75f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ deps += string_dep
|
|||||||
deps += range_dep
|
deps += range_dep
|
||||||
deps += container_dep
|
deps += container_dep
|
||||||
deps += logger_dep
|
deps += logger_dep
|
||||||
deps += vector_dep
|
deps += math_dep
|
||||||
deps += matrix_dep
|
|
||||||
|
|
||||||
executable(
|
executable(
|
||||||
'hack', 'main.cpp',
|
'hack', 'main.cpp',
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
#include "concepts/concepts.hpp"
|
#include "concepts/concepts.hpp"
|
||||||
#include "iterators/sequence_ostream_iterator.hpp"
|
#include "iterators/sequence_ostream_iterator.hpp"
|
||||||
#include "iterators/associative_ostream_iterator.hpp"
|
#include "iterators/associative_ostream_iterator.hpp"
|
||||||
#include "matrix/matrix.hpp"
|
#include "math/matrix.hpp"
|
||||||
|
|
||||||
namespace hack
|
namespace hack
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
headers = ['matrix.hpp']
|
headers = ['vector.hpp', 'matrix.hpp']
|
||||||
sources = []
|
sources = []
|
||||||
|
|
||||||
lib = library(
|
lib = library(
|
||||||
'matrix',
|
'math',
|
||||||
include_directories : inc,
|
include_directories : inc,
|
||||||
install : true,
|
install : true,
|
||||||
sources: [headers, sources]
|
sources: [headers, sources]
|
||||||
)
|
)
|
||||||
|
|
||||||
matrix_dep = declare_dependency(
|
math_dep = declare_dependency(
|
||||||
include_directories: inc,
|
include_directories: inc,
|
||||||
link_with: lib
|
link_with: lib
|
||||||
)
|
)
|
||||||
70
src/math/vector.hpp
Normal file
70
src/math/vector.hpp
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
namespace hack
|
||||||
|
{
|
||||||
|
template<typename T, typename... Args>
|
||||||
|
struct generate_tuple
|
||||||
|
{
|
||||||
|
using type = decltype(std::tuple_cat(std::make_tuple(T{}), typename generate_tuple<Args...>::type{}));
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct generate_tuple<T>
|
||||||
|
{
|
||||||
|
using type = std::tuple<T>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace hack
|
||||||
|
{
|
||||||
|
template<typename... Args>
|
||||||
|
class vector
|
||||||
|
{
|
||||||
|
using value_t = decltype(typename generate_tuple<Args...>::type{});
|
||||||
|
|
||||||
|
public:
|
||||||
|
vector(Args... args) : value_ { args... } {}
|
||||||
|
vector& operator=(const vector& v)
|
||||||
|
{
|
||||||
|
if (this == &v) return *this;
|
||||||
|
value_ = v.value_;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
value_t get_value() { return value_; };
|
||||||
|
|
||||||
|
auto length()
|
||||||
|
{
|
||||||
|
return std::sqrt(length_idx(std::make_index_sequence<std::tuple_size<value_t>::value>{}));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
dest.x = a.x + ((b.x - a.x) * t);
|
||||||
|
dest.y = a.y + ((b.y - a.y) * t);
|
||||||
|
*/
|
||||||
|
value_t lerp(vector<Args...>& v, float t)
|
||||||
|
{
|
||||||
|
return lerp_idx(v.get_value(), t, std::make_index_sequence<std::tuple_size<value_t>::value>{});
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<typename std::size_t... idx>
|
||||||
|
auto length_idx(std::index_sequence<idx...>)
|
||||||
|
{
|
||||||
|
return ((std::get<idx>(value_) * std::get<idx>(value_)) + ...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename std::size_t... idx>
|
||||||
|
auto lerp_idx(const value_t& v, float t, std::index_sequence<idx...>)
|
||||||
|
{
|
||||||
|
return std::make_tuple((std::get<idx>(value_) + (std::get<idx>(v) - std::get<idx>(value_)) * t)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
value_t value_;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -8,5 +8,4 @@ subdir('string')
|
|||||||
subdir('range')
|
subdir('range')
|
||||||
subdir('container')
|
subdir('container')
|
||||||
subdir('logger')
|
subdir('logger')
|
||||||
subdir('vector')
|
subdir('math')
|
||||||
subdir('matrix')
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
namespace hack::utils
|
namespace hack::utils
|
||||||
{
|
{
|
||||||
template<typename T, size_t N>
|
template<typename T, std::size_t N>
|
||||||
struct generate_tuple
|
struct generate_tuple
|
||||||
{
|
{
|
||||||
using type = decltype(std::tuple_cat(typename generate_tuple<T, N - 1>::type{}, std::make_tuple(T{})));
|
using type = decltype(std::tuple_cat(typename generate_tuple<T, N - 1>::type{}, std::make_tuple(T{})));
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
headers = ['vector.hpp']
|
|
||||||
sources = []
|
|
||||||
|
|
||||||
lib = library(
|
|
||||||
'vector',
|
|
||||||
include_directories : inc,
|
|
||||||
install : true,
|
|
||||||
sources: [headers, sources]
|
|
||||||
)
|
|
||||||
|
|
||||||
vector_dep = declare_dependency(
|
|
||||||
include_directories: inc,
|
|
||||||
link_with: lib
|
|
||||||
)
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <tuple>
|
|
||||||
|
|
||||||
namespace hack
|
|
||||||
{
|
|
||||||
template<typename T, typename... Args>
|
|
||||||
struct generate_tuple
|
|
||||||
{
|
|
||||||
using type = decltype(std::tuple_cat(std::make_tuple(T{}), typename generate_tuple<Args...>::type{}));
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct generate_tuple<T>
|
|
||||||
{
|
|
||||||
using type = std::tuple<T>;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace hack
|
|
||||||
{
|
|
||||||
template<typename... Args>
|
|
||||||
class vector
|
|
||||||
{
|
|
||||||
using value_t = decltype(typename generate_tuple<Args...>::type{});
|
|
||||||
|
|
||||||
public:
|
|
||||||
vector(Args... args) : value_ { args... } {}
|
|
||||||
|
|
||||||
public:
|
|
||||||
value_t get_value() { return value_; };
|
|
||||||
|
|
||||||
private:
|
|
||||||
value_t value_;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user