Safe Haskell | None |
---|
Basic operations on 2D vectors represented in an efficient, but not unique, way.
- data Vector
- toVector :: X -> VectorXY -> Vector
- toDir :: X -> VectorXY -> Vector
- shift :: Point -> Vector -> Point
- shiftBounded :: X -> (X, Y, X, Y) -> Point -> Vector -> Point
- moves :: X -> [Vector]
- isUnit :: X -> Vector -> Bool
- euclidDistSq :: X -> Vector -> Vector -> Int
- diagonal :: X -> Vector -> Bool
- neg :: Vector -> Vector
- towards :: X -> Point -> Point -> Vector
- displacement :: Point -> Point -> Vector
- displacePath :: [Point] -> [Vector]
- shiftPath :: Point -> [Vector] -> [Point]
Documentation
2D vectors represented as offsets in the linear framebuffer
indexed by Point
.
A newtype is used to prevent mixing up the type with Point
itself.
Note that the offset representations of a vector is usually not unique.
E.g., for vectors of length 1 in the chessboard metric, used to denote
geographical directions, the representations are pairwise distinct
if and only if the level width and height are at least 3.
toDir :: X -> VectorXY -> VectorSource
Converts a unit vector in cartesian representation into Vector
.
shift :: Point -> Vector -> PointSource
Translate a point by a vector.
Particularly simple and fast implementation in the linear representation.
shiftBounded :: X -> (X, Y, X, Y) -> Point -> Vector -> PointSource
Translate a point by a vector, but only if the result fits in an area.
euclidDistSq :: X -> Vector -> Vector -> IntSource
Squared euclidean distance between two unit vectors.
diagonal :: X -> Vector -> BoolSource
Checks whether a unit vector is a diagonal direction, as opposed to cardinal. If the vector is not unit, it reject horizontal and vertical vectors.
towards :: X -> Point -> Point -> VectorSource
Given two distinct positions, determine the direction (a unit vector) in which one should move from the first in order to get closer to the second. Ignores obstacles. Of several equally good directions (in the chessboard metric) it picks one of those that visually (in the euclidean metric) maximally align with the vector between the two points.
displacement :: Point -> Point -> VectorSource
A vector from a point to another. We have
shift pos1 (displacement pos1 pos2) == pos2
Particularly simple and fast implementation in the linear representation.
displacePath :: [Point] -> [Vector]Source
A list of vectors between a list of points.