-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorUtils.hs
More file actions
69 lines (55 loc) · 2.06 KB
/
VectorUtils.hs
File metadata and controls
69 lines (55 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{- This file is part of hs-panelmethod.
hs-panelmethod is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
hs-panelmethod is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with hs-panelmethod. If not, see <http://www.gnu.org/licenses/>. -}
module VectorUtils
( Line(..)
, FV2
, getNormal
, getMidpoint
, discretize
, furthestLines
, serializeVector2
, serializeVector4
, serializeMatrix4 )
where
import Linear
type FV2 = V2 Float
data Line = Line FV2 FV2
instance Show Line where
show (Line a b) = show (a,b)
getNormal :: Line -> FV2
getNormal (Line v1 v2) = Linear.normalize $ V2 (-dy) dx
where
V2 dx dy = v2 - v1
getMidpoint :: Line -> FV2
getMidpoint (Line v1 v2) = 0.5 * (v1 + v2)
discretize :: Line -> Float -> ([FV2],Float)
discretize (Line v1 v2) count = (map (\n -> v1 + dds * fromInteger n) [0..round count], nds)
where
ds = v2 - v1
dds = ds ^/ count
nds = norm dds
furthestLines :: FV2 -> [(Int,Line)] -> [Int]
furthestLines dir ls = hasPoint (furthestPoint . pointDistances dir $ ls) ls
where
hasPoint p = foldr (\(i,Line v1 v2) acc-> if v1 == p || v2 == p then i : acc else acc) []
furthestPoint = fst . foldr (\p@(_,d1) acc@(_,d2)-> if d1 > d2 then p else acc) (V2 0 0, 0)
pointDistances x = map (\(_,Line v1 v2)->
let d1 = dot v1 x
d2 = dot v2 x in
if d1 > d2 then (v1,d1) else (v2,d2)
)
serializeVector2 :: V2 a -> [a]
serializeVector2 (V2 x y) = [x,y]
serializeVector4 :: V4 a -> [a]
serializeVector4 (V4 x y z w) = [x,y,z,w]
serializeMatrix4 :: M44 a -> [a]
serializeMatrix4 = concatMap serializeVector4 . serializeVector4