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'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 ();

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.