add vector

This commit is contained in:
chatlanin
2022-03-24 16:16:45 +03:00
parent 94b734647e
commit 3cfd5bec03
10 changed files with 97 additions and 63 deletions

View File

@@ -6,7 +6,7 @@
#include "concepts/concepts.hpp"
#include "iterators/sequence_ostream_iterator.hpp"
#include "iterators/associative_ostream_iterator.hpp"
#include "matrix/matrix.hpp"
#include "math/matrix.hpp"
namespace hack
{

View File

@@ -1,14 +1,14 @@
headers = ['matrix.hpp']
headers = ['vector.hpp', 'matrix.hpp']
sources = []
lib = library(
'matrix',
'math',
include_directories : inc,
install : true,
sources: [headers, sources]
)
matrix_dep = declare_dependency(
math_dep = declare_dependency(
include_directories: inc,
link_with: lib
)

70
src/math/vector.hpp Normal file
View 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_;
};
}

View File

@@ -8,5 +8,4 @@ subdir('string')
subdir('range')
subdir('container')
subdir('logger')
subdir('vector')
subdir('matrix')
subdir('math')

View File

@@ -5,7 +5,7 @@
namespace hack::utils
{
template<typename T, size_t N>
template<typename T, std::size_t N>
struct generate_tuple
{
using type = decltype(std::tuple_cat(typename generate_tuple<T, N - 1>::type{}, std::make_tuple(T{})));

View File

@@ -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
)

View File

@@ -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_;
};
}