Git Product home page Git Product logo

ppb-vector's Introduction

ppb-vector

The 2D Vector Class for the PursuedPyBear project.

Install

You can install Vector2 pip package using

pip install 'ppb-vector'

Usage

Vector2 is an immutable 2D Vector. Instantiated as expected:

>>>> from ppb_vector import Vector2
>>> Vector2(3, 4)
Vector2(3, 4)

Implements many convenience features:

Unpacking

>>> x, y = *Vector(1, 3)
>>> print(x)
1
>>> print(y)
3

Addition

>>> Vector2(1, 0) + Vector2(0, 1)
Vector2(1, 1)

In addition to Vector2 addition also accepts vector-like objects such as tuple, list, and dict.

>>> Vector2(1, 1) + [1, 3]
Vector2(2, 4)

>>> Vector2(1, 1) + (2, 4)
Vector2(3, 5)

>>> Vector2(1, 1) + {"x": 3, "y": 5}
Vector2(4, 6)

Subtraction

>>> Vector2(3, 3) - Vector2(1, 1)
Vector2(2, 2)

As with addition, subtraction also takes vector-like objects.

>>> Vector2(3, 3) - [2, 1]
Vector2(1, 2)

>>> Vector2(3, 3) - (2, 1)
Vector2(1, 2)

>>> Vector2(3, 3) - {"x": 2, "y": 1}
Vector2(1, 2)

Equality

Vectors are equal if their members are equal.

>>> Vector2(1, 0) == Vector2(0, 1)
False

Scalar Multiplication

Multiply a Vector2 by a scalar to get a scaled Vector2

>>> Vector2(1, 1) * 3
Vector2(3, 3)

Dot Product

Multiply a Vector2 by another Vector2 to get the dot product.

>>> Vector2(1, 1) * Vector2(-1, -1)
-2

Vector Length

>>> Vector2(45, 60).length
75.0

Access Values

Convenient access to Vector2 members via dot notation, indexes, or keys.

>>> my_vector = Vector2(2, 3)
>>> my_vector.x
2
>>> my_vector[1]
3
>>> my_vector["x"]
2

Also iterable for translation between Vector2 and other sequence types.

>>> tuple(Vector(2, 3))
(2, 3)

Methods

Useful functions for game development.

rotate(deg)

Rotate a vector in relation to its own origin and return a new Vector2.

>>> Vector2(1, 0).rotate(90)
Vector2(0.0, 1.0)

Positive rotation is counter/anti-clockwise.

normalize()

Return the normalized Vector2 for the given Vector2.

>>> Vector2(5, 5).normalize()
Vector2(0.7071067811865475, 0.7071067811865475)

truncate(scalar)

Scale a given Vector2 to length of scalar.

>>> Vector2(700, 500).truncate(5)
Vector2(4.068667356033675, 2.906190968595482)

Note that Vector2.normalize() is equivalent to Vector2.truncate(1).

>>> Vector2(200, 300).normalize()
Vector2(0.5547001962252291, 0.8320502943378436)
>>> Vector2(200, 300).scale(1)
Vector2(0.5547001962252291, 0.8320502943378436)

scale(scalar)

Scale given Vector2 to length of scalar.

>>> Vector2(7, 7).scale(5)
Vector2(3.5355339059327373, 3.5355339059327373)

Note that Vector2.scale is equivalent to Vector2.truncate when scalar is less than length.

>>> Vector2(3, 4).scale(4)
Vector2(2.4000000000000004, 3.2)
>>> Vector2(3, 4).truncate(4)
Vector2(2.4000000000000004, 3.2)
>>> Vector2(3, 4).scale(6)
Vector2(3.5999999999999996, 4.8)
>>> Vector2(3, 4).truncate(6)
Vector2(3, 4)

ppb-vector's People

Contributors

pathunstrom avatar ak9999 avatar astraluma avatar royopa avatar

Watchers

James Cloos avatar

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.