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

@@ -10,8 +10,8 @@
#include "range/range.hpp"
#include "container/container.hpp"
#include "logger/logger.hpp"
#include "matrix/matrix.hpp"
#include "vector/vector.hpp"
#include "math/matrix.hpp"
#include "math/vector.hpp"
#include <string_view>
@@ -110,7 +110,23 @@ int main(int argc, char *argv[])
// }
{// ex: vector
hack::vector<int, int, int> v3 { 1, 2, 3, };
hack::log()(v3.get_value());
hack::vector<int, int, int> v3_1 { 8, 4, 9, };
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));
}
}

View File

@@ -6,8 +6,7 @@ deps += string_dep
deps += range_dep
deps += container_dep
deps += logger_dep
deps += vector_dep
deps += matrix_dep
deps += math_dep
executable(
'hack', 'main.cpp',

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