Module

Linear.V2

Package
purescript-linear
Repository
afcondon/purescript-linear

2-dimensional vectors.

This module provides the V2 type for 2D vector operations, commonly used for 2D graphics, physics, and game development.

#V2 Source

data V2 a

A 2-dimensional vector.

origin = V2 0.0 0.0
direction = V2 1.0 0.0

Constructors

Instances

#perp Source

perp :: forall a. Ring a => V2 a -> V2 a

The counter-clockwise perpendicular vector.

perp (V2 1.0 0.0) = V2 0.0 1.0
perp (V2 0.0 1.0) = V2 (-1.0) 0.0

#angle Source

angle :: Number -> V2 Number

Construct a unit vector at the given angle (in radians) from the positive x-axis.

angle 0.0 = V2 1.0 0.0
angle (pi / 2.0) ≈ V2 0.0 1.0

#unangle Source

unangle :: V2 Number -> Number

Extract the angle (in radians) from a vector, measured from the positive x-axis.

unangle (V2 1.0 0.0) = 0.0
unangle (V2 0.0 1.0) = pi / 2.0

#crossZ Source

crossZ :: forall a. Ring a => V2 a -> V2 a -> a

The z-component of the cross product of two 2D vectors.

This is useful for determining the orientation of two vectors:

  • Positive: v2 is counter-clockwise from v1
  • Negative: v2 is clockwise from v1
  • Zero: vectors are parallel
crossZ (V2 1.0 0.0) (V2 0.0 1.0) = 1.0  -- counter-clockwise
crossZ (V2 0.0 1.0) (V2 1.0 0.0) = -1.0 -- clockwise

#ex Source

ex :: forall a. Semiring a => V2 a

Unit vector along the x-axis.

#ey Source

ey :: forall a. Semiring a => V2 a

Unit vector along the y-axis.

#getX Source

getX :: forall a. V2 a -> a

Get the x component of a V2.

#getY Source

getY :: forall a. V2 a -> a

Get the y component of a V2.