GithubHelp home page GithubHelp logo

lionkor / vector2 Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 38 KB

A templated header-only 2D Vector class

License: MIT License

C++ 100.00%
maths vector geometry physics cpp cpp11 cpp17 math mathematics tools vector2 vector2d

vector2's Introduction

Vector2

No longer in development. Use glm instead.

Contact: [email protected]

Overview

This is a templated header-only class for a 2D Vector, written in C++17, with all common vector operations and some useful additional methods.

Features

Development Progress

  • Common operators
  • Templated operators
  • License (MIT)
  • Full in-source documentation
  • Common operations like normalizing, dot/cross/scalar product, distance, magnitude, etc.
  • Extra fancy operations like linear interpolation, abs, rotation, reflection

Notes

  • Floating point equality comparisons have been implemented roughly following this article. Feel free to override the functions used by #define-ing VECTOR2_FLOAT_COMPARE, VECTOR2_DOUBLE_COMPARE and VECTOR2_LONG_DOUBLE_COMPARE for float, double and long double comparisons, respectively. They're used like FUNCTION(first_float, second_float).
  • Vector-rotation is implemented to rotate CLOCKWISE.
  • rotated_rad is possibly faster than rotated_deg, since the latter converts from degrees to radians first.
  • The typedefs which specify size (for example Vector2i8) use int_fast8_t and similar (fast variant). Feel free to open an issue if this causes problems

How to use

It should be intuitive, but here's how intuitive:

Construct a vector

With typedefs...

Vector2f vec {};                // (0, 0)

Vector2u vec (6);               // (6, 6)

Vector2d vec { 2.0, 5.0 };      // (2.0, 5.0)

Vector2i vec (6, -15);          // (6, -15)

...or the typey way:

Vector2<long double> vec (2.5L, -18.5L);

Vector Operations

rotate (degrees)

auto result_vec = vec.rotated_deg (45.0);

rotate (radians)

auto result_vec = vec.rotated_rad (PI / 3.0);

linearly interpolate with other_vec

auto result_vec = vec.lerp (other_vec, 0.75);

linearly interpolate with other_vec unclamped

unclamped means that the value is not restricted between 0.0 and 1.0.

auto result_vec = vec.lerp_unclamped (other_vec, 1.5);

reflect off surface with normal_vec

auto result_vec = vec.reflected (normal_vec);

distance to other_vec

auto result_vec = vec.distance (other_vec);

squared distance to other_vec

auto result_vec = vec.sqr_distance (other_vec);

magnitude

double mag = vec.magnitude ();

squared magnitude

double sqr_mag = vec.sqr_magnitude ();

dot product with other_vec

auto result_vec = vec.dot (other_vec);

square root

auto result_vec = vec.square_root ();

normalize

result_vec = vec.normalized ();

vector2's People

Contributors

lionkor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vector2's Issues

rotate_deg does not work correctly

When rotating a vector, rotate_deg does not calculate correctly.

Example:
Vector2 (2, 5).rotated_deg (90) should rotate vector (2, 5) by 90 degrees, which should return a vector (-5, 2) (correct example result).

It instead returns a vector (2, -5).

Using the latest version, you can reproduce it with this code:

std::cout << Vector2f (2, 5).rotated_deg (90) << std::endl;

Which will output x: 2 y: -5, a wrong result.

This is reproducible with many different vectors and degrees.

Vector2/Vector2.hpp

Lines 364 to 371 in 17cdcf4

constexpr Vector2 rotated_deg (double angle_degrees) const
{
// convert to radians
long double radians = angle_degrees * (VECTOR2_MATH_PI<long double> / 180.0);
return Vector2 (
x * sin (radians) + x * cos (radians),
x * cos (radians) - y * sin (radians));
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.