Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data Direction
- data Coords a = Coords {}
- newtype Coord a = Coord Int
- data Col
- data Row
- data Size = Size {}
- newtype Length a = Length Int
- data Width
- data Height
- toCoords :: Length Height -> Length Width -> Coords Pos
- maxLength :: Size -> Int
- onOuterBorder :: Coords Pos -> Size -> Maybe Direction
- containsWithOuterBorder :: Coords Pos -> Size -> Bool
- data Segment
- mkSegment :: Coords Pos -> Coords Pos -> Segment
- bresenhamLength :: Coords Pos -> Coords Pos -> Int
- bresenham :: Segment -> [Coords Pos]
- data Pos
- data Vel
- mkSegmentByExtendingWhile :: Coords Pos -> Direction -> (Coords Pos -> Bool) -> Segment
- changeSegmentLength :: Int -> Segment -> Segment
- extremities :: Segment -> (Coords Pos, Coords Pos)
- segmentContains :: Coords Pos -> Segment -> Maybe Int
- zeroCoords :: Coords a
- coordsForDirection :: Direction -> Coords a
- diffCoords :: Coords a -> Coords a -> Coords a
- diffPosToSpeed :: Coords Pos -> Coords Pos -> Coords Vel
- sumCoords :: Coords a -> Coords a -> Coords a
- sumPosSpeed :: Coords Pos -> Coords Vel -> Coords Pos
- move :: Int -> Direction -> Coords a -> Coords a
- translate :: Coords a -> Coords a -> Coords a
- translate' :: Length Height -> Length Width -> Coords Pos -> Coords Pos
- translateInDir :: Direction -> Coords a -> Coords a
- bla :: (Int, Int) -> (Int, Int) -> [(Int, Int)]
- bresenham3Length :: (Int, Int, Int) -> (Int, Int, Int) -> Int
- bresenham3 :: (Int, Int, Int) -> (Int, Int, Int) -> [(Int, Int, Int)]
- resampleWithExtremities :: [a] -> Int -> Int -> [a]
Discrete geometry types
Direction
Discrete directions.
Coordinates
Discrete coordinate.
Size
Represents a discrete size (width and height)
Discrete length
:: Coords Pos | The coordinates to test |
-> Size | The size |
-> Maybe Direction | If the coordinates are on the border, returns a |
Tests if a Coords
lies on the outer border of a region of a given size,
containing (0,0) and positive coordinates.
containsWithOuterBorder :: Coords Pos -> Size -> Bool Source #
Tests if a Coords
is contained or on the outer border of a region
of a given size, containing (0,0) and positive coordinates.
Segment
A segment is a line betwen two discrete coordinates.
Bresenham line algorithm
bresenhamLength :: Coords Pos -> Coords Pos -> Int Source #
Returns the bresenham 2d distance between two coordinates.
bresenham :: Segment -> [Coords Pos] Source #
Bresenham 2d algorithm, slightly optimized for horizontal and vertical lines.
Reexports
Phantom type : position
DiscreteDistance (Coords Pos) Source # | Using bresenham 2d line algorithm. |
DiscreteInterpolation (Coords Pos) Source # | Using bresenham 2d line algorithm. |
Construct Segment
changeSegmentLength :: Int -> Segment -> Segment Source #
Modify the end of the segment to reach the given length
Use Segment
:: Coords Pos | The coordinates to test |
-> Segment | |
-> Maybe Int |
|
Returns the distance from segment start
Construct Coords
zeroCoords :: Coords a Source #
zeroCoords
= Coords
0 0
coordsForDirection :: Direction -> Coords a Source #
Returns the coordinates that correspond to one step in the given direction.
Use Coords
sumPosSpeed :: Coords Pos -> Coords Vel -> Coords Pos Source #
Assumes that we integrate over one game step.
Returns a + b
Translate by a given height and width.
translateInDir :: Direction -> Coords a -> Coords a Source #
Translate of 1 step in a given direction.
Discrete algorithms
Bresenham
The 2d version, bresenham
, allows to
draw a line on a 2d grid.
The 3d version, bresenham3
, allows to interpolate discrete colors in RGB space.
bla :: (Int, Int) -> (Int, Int) -> [(Int, Int)] Source #
Bresenham's line algorithm. Includes the first point and goes through the second to infinity.
bresenham3Length :: (Int, Int, Int) -> (Int, Int, Int) -> Int Source #
Returns the 3D bresenham length between two 3D coordinates.
bresenham3 :: (Int, Int, Int) -> (Int, Int, Int) -> [(Int, Int, Int)] Source #
3D version of the bresenham algorithm.
List resampling
Typically, resampleWithExtremities
will be used on the result of bresenham
to over-sample the produced line.
resampleWithExtremities Source #
:: [a] | Input |
-> Int | \( n \) : input length. It is expected that \( 0 <= n <= \) |
-> Int | \( m \) : output length. It is expected that \( 0 <= m \). |
-> [a] | Output :
|
Resamples a list, using the analogy where a list is seen as a uniform sampling of a geometrical segment.
With a uniform sampling strategy, for an input of length \( n \), and a desired output of length \( m \):
- Regular samples are repeated \( r = \lfloor {m \over n} \rfloor \) times.
- Over-represented samples are repeated \( r + 1 \) times.
If \( m' \) is the number of over-represented samples,
\[ \begin{alignedat}{2} m &= r*n + m' \\ \implies \quad m' &= m - r*n \end{alignedat} \]
We can chose over-represented samples in at least two different ways:
Even spread :
- Given a partition of the input continuous interval \( [\,0, length]\, \) in \( m' \) equal-length intervals, the over-represented samples are located at the (floored) centers of these intervals.
More precisely, over-represented samples indexes are:
\[ \biggl\{ a + \Bigl\lfloor {1 \over 2} + { n-1-a \over m-1 } * s \Bigl\rfloor \mid s \in [\,0\,..\,m'-1] \;,\; a = {1 \over 2} * {n \over m'} \biggl\} \]
- Example : for a length 5 input, and 2 over-represented samples:
input samples: ----- over-represented samples: - -
"Even with extremities" spread:
- The first and last over-represented samples match with an input extremity. The rest of the over-represented samples are positionned "regularly" in-between the first and last. An exception is made when there is only one over-represented sample : in that case it is placed in the middle.
More precisely, over-represented samples indexes are:
\[ if \; m' == 1 : \biggl\{ \Bigl\lfloor {n-1 \over 2} \Bigl\rfloor \biggl\} \]
\[ otherwise : \biggl\{ \Bigl\lfloor {1 \over 2} + {n-1 \over m'-1}*s \Bigl\rfloor \mid s \in [\,0,m'-1]\, \biggl\} \]
- Example : for a length 5 input, and 2 over-represented samples:
input samples: ----- over-represented samples: - -
As its name suggests, this function uses the "even with extremities" spread.
For clarity, the variable names used in the code match the ones in the documentation.