Revert "Remove almost all 'type' usage to make types more transparent"
This reverts commit 5120a44d0f.
Conflicts:
Parser/Meshparser.hs
This commit is contained in:
@@ -4,15 +4,14 @@ module Algebra.Polygon where
|
||||
|
||||
import Algebra.Vector
|
||||
import Data.Maybe
|
||||
import Diagrams.TwoD.Types
|
||||
import MyPrelude
|
||||
|
||||
|
||||
-- |Split a polygon by a given segment which must be vertices of the
|
||||
-- polygon (returns empty array otherwise).
|
||||
splitPoly :: [P2]
|
||||
-> (P2, P2)
|
||||
-> [[P2]]
|
||||
splitPoly :: [PT]
|
||||
-> Segment
|
||||
-> [[PT]]
|
||||
splitPoly pts (a, b)
|
||||
| elem a pts && elem b pts =
|
||||
[b : takeWhile (/= b) shiftedPoly, a : dropWhile (/= b) shiftedPoly]
|
||||
@@ -22,7 +21,7 @@ splitPoly pts (a, b)
|
||||
|
||||
|
||||
-- |Get all edges of a polygon.
|
||||
polySegments :: [P2] -> [(P2, P2)]
|
||||
polySegments :: [PT] -> [Segment]
|
||||
polySegments p@(x':_:_:_) = go p ++ [(last p, x')]
|
||||
where
|
||||
go (x:y:xs) = (x, y) : go (y:xs)
|
||||
@@ -33,7 +32,7 @@ polySegments _ = []
|
||||
-- |Check whether the given segment is inside the polygon.
|
||||
-- This doesn't check for segments that are completely outside
|
||||
-- of the polygon yet.
|
||||
isInsidePoly :: [P2] -> (P2, P2) -> Bool
|
||||
isInsidePoly :: [PT] -> Segment -> Bool
|
||||
isInsidePoly pts seg =
|
||||
null
|
||||
. catMaybes
|
||||
@@ -42,21 +41,21 @@ isInsidePoly pts seg =
|
||||
|
||||
|
||||
-- |Check whether two points are adjacent vertices of a polygon.
|
||||
adjacent :: P2 -> P2 -> [P2] -> Bool
|
||||
adjacent :: PT -> PT -> [PT] -> Bool
|
||||
adjacent u v = any (\x -> x == (u, v) || x == (v, u)) . polySegments
|
||||
|
||||
|
||||
-- |Check whether the polygon is a triangle polygon.
|
||||
isTrianglePoly :: [P2] -> Bool
|
||||
isTrianglePoly :: [PT] -> Bool
|
||||
isTrianglePoly [_, _, _] = True
|
||||
isTrianglePoly _ = False
|
||||
|
||||
|
||||
-- |Get all triangle polygons.
|
||||
triangleOnly :: [[P2]] -> [[P2]]
|
||||
triangleOnly :: [[PT]] -> [[PT]]
|
||||
triangleOnly = filter isTrianglePoly
|
||||
|
||||
|
||||
-- |Get all non-triangle polygons.
|
||||
nonTriangleOnly :: [[P2]] -> [[P2]]
|
||||
nonTriangleOnly :: [[PT]] -> [[PT]]
|
||||
nonTriangleOnly = filter (not . isTrianglePoly)
|
||||
|
||||
@@ -13,6 +13,13 @@ import GHC.Float
|
||||
import MyPrelude
|
||||
|
||||
|
||||
type Vec = R2
|
||||
type PT = P2
|
||||
type Coord = (Double, Double)
|
||||
type Segment = (PT, PT)
|
||||
type Square = (Coord, Coord)
|
||||
|
||||
|
||||
data Alignment = CW
|
||||
| CCW
|
||||
| CL
|
||||
@@ -24,13 +31,13 @@ data Alignment = CW
|
||||
-- ((xmin, ymin), (xmax, ymax))
|
||||
dimToSquare :: (Double, Double) -- ^ x dimension
|
||||
-> (Double, Double) -- ^ y dimension
|
||||
-> ((Double, Double), (Double, Double)) -- ^ square describing those dimensions
|
||||
-> Square -- ^ square describing those dimensions
|
||||
dimToSquare (x1, x2) (y1, y2) = ((x1, y1), (x2, y2))
|
||||
|
||||
|
||||
-- |Checks whether the Point is in a given Square.
|
||||
inRange :: ((Double, Double), (Double, Double)) -- ^ the square: ((xmin, ymin), (xmax, ymax))
|
||||
-> P2 -- ^ Coordinate
|
||||
inRange :: Square -- ^ the square: ((xmin, ymin), (xmax, ymax))
|
||||
-> PT -- ^ Coordinate
|
||||
-> Bool -- ^ result
|
||||
inRange ((xmin, ymin), (xmax, ymax)) (coords -> x :& y)
|
||||
= x >= min xmin xmax
|
||||
@@ -40,7 +47,7 @@ inRange ((xmin, ymin), (xmax, ymax)) (coords -> x :& y)
|
||||
|
||||
|
||||
-- |Get the angle between two vectors.
|
||||
getAngle :: R2 -> R2 -> Double
|
||||
getAngle :: Vec -> Vec -> Double
|
||||
getAngle a b =
|
||||
acos
|
||||
. flip (/) (vecLength a * vecLength b)
|
||||
@@ -49,50 +56,48 @@ getAngle a b =
|
||||
|
||||
|
||||
-- |Get the length of a vector.
|
||||
vecLength :: R2 -> Double
|
||||
vecLength :: Vec -> Double
|
||||
vecLength v = sqrt (x^(2 :: Int) + y^(2 :: Int))
|
||||
where
|
||||
(x, y) = unr2 v
|
||||
|
||||
|
||||
-- |Compute the scalar product of two vectors.
|
||||
scalarProd :: R2 -> R2 -> Double
|
||||
scalarProd :: Vec -> Vec -> Double
|
||||
scalarProd (R2 a1 a2) (R2 b1 b2) = a1 * b1 + a2 * b2
|
||||
|
||||
|
||||
-- |Multiply a scalar with a vector.
|
||||
scalarMul :: Double -> R2 -> R2
|
||||
scalarMul :: Double -> Vec -> Vec
|
||||
scalarMul d (R2 a b) = R2 (a * d) (b * d)
|
||||
|
||||
|
||||
-- |Construct a vector that points to a point from the origin.
|
||||
pt2Vec :: P2 -> R2
|
||||
pt2Vec :: PT -> Vec
|
||||
pt2Vec = r2 . unp2
|
||||
|
||||
|
||||
-- |Give the point which is at the coordinates the vector
|
||||
-- points to from the origin.
|
||||
vec2Pt :: R2 -> P2
|
||||
vec2Pt :: Vec -> PT
|
||||
vec2Pt = p2 . unr2
|
||||
|
||||
|
||||
-- |Construct a vector between two points.
|
||||
vp2 :: P2 -- ^ vector origin
|
||||
-> P2 -- ^ vector points here
|
||||
-> R2
|
||||
vp2 :: PT -- ^ vector origin
|
||||
-> PT -- ^ vector points here
|
||||
-> Vec
|
||||
vp2 a b = pt2Vec b - pt2Vec a
|
||||
|
||||
|
||||
-- |Computes the determinant of 3 points.
|
||||
det :: P2 -> P2 -> P2 -> Double
|
||||
det :: PT -> PT -> PT -> Double
|
||||
det (coords -> ax :& ay) (coords -> bx :& by) (coords -> cx :& cy) =
|
||||
(bx - ax) * (cy - ay) - (by - ay) * (cx - ax)
|
||||
|
||||
|
||||
-- |Get the point where two lines intesect, if any.
|
||||
intersectSeg' :: (P2, P2) -- ^ first segment
|
||||
-> (P2, P2) -- ^ second segment
|
||||
-> Maybe P2
|
||||
intersectSeg' :: Segment -> Segment -> Maybe PT
|
||||
intersectSeg' (a, b) (c, d) =
|
||||
glossToPt <$> intersectSegSeg (ptToGloss a)
|
||||
(ptToGloss b)
|
||||
@@ -105,7 +110,7 @@ intersectSeg' (a, b) (c, d) =
|
||||
|
||||
-- |Get the point where two lines intesect, if any. Excludes the
|
||||
-- case of end-points intersecting.
|
||||
intersectSeg'' :: (P2, P2) -> (P2, P2) -> Maybe P2
|
||||
intersectSeg'' :: Segment -> Segment -> Maybe PT
|
||||
intersectSeg'' (a, b) (c, d) = case intersectSeg' (a, b) (c, d) of
|
||||
Just x -> if x `notElem` [a,b,c,d] then Just a else Nothing
|
||||
Nothing -> Nothing
|
||||
@@ -115,7 +120,7 @@ intersectSeg'' (a, b) (c, d) = case intersectSeg' (a, b) (c, d) of
|
||||
-- * clock-wise
|
||||
-- * counter-clock-wise
|
||||
-- * collinear
|
||||
getOrient :: P2 -> P2 -> P2 -> Alignment
|
||||
getOrient :: PT -> PT -> PT -> Alignment
|
||||
getOrient a b c = case compare (det a b c) 0 of
|
||||
LT -> CW
|
||||
GT -> CCW
|
||||
@@ -125,7 +130,7 @@ getOrient a b c = case compare (det a b c) 0 of
|
||||
--- |Checks if 3 points a,b,c do not build a clockwise triangle by
|
||||
--- connecting a-b-c. This is done by computing the determinant and
|
||||
--- checking the algebraic sign.
|
||||
notcw :: P2 -> P2 -> P2 -> Bool
|
||||
notcw :: PT -> PT -> PT -> Bool
|
||||
notcw a b c = case getOrient a b c of
|
||||
CW -> False
|
||||
_ -> True
|
||||
@@ -134,22 +139,22 @@ notcw a b c = case getOrient a b c of
|
||||
--- |Checks if 3 points a,b,c do build a clockwise triangle by
|
||||
--- connecting a-b-c. This is done by computing the determinant and
|
||||
--- checking the algebraic sign.
|
||||
cw :: P2 -> P2 -> P2 -> Bool
|
||||
cw :: PT -> PT -> PT -> Bool
|
||||
cw a b c = not . notcw a b $ c
|
||||
|
||||
|
||||
-- |Sort X and Y coordinates lexicographically.
|
||||
sortedXY :: [P2] -> [P2]
|
||||
sortedXY :: [PT] -> [PT]
|
||||
sortedXY = fmap p2 . sortLex . fmap unp2
|
||||
|
||||
|
||||
-- |Sort Y and X coordinates lexicographically.
|
||||
sortedYX :: [P2] -> [P2]
|
||||
sortedYX :: [PT] -> [PT]
|
||||
sortedYX = fmap p2 . sortLexSwapped . fmap unp2
|
||||
|
||||
|
||||
-- |Sort all points according to their X-coordinates only.
|
||||
sortedX :: [P2] -> [P2]
|
||||
sortedX :: [PT] -> [PT]
|
||||
sortedX xs =
|
||||
fmap p2
|
||||
. sortBy (\(a1, _) (a2, _) -> compare a1 a2)
|
||||
@@ -157,7 +162,7 @@ sortedX xs =
|
||||
|
||||
|
||||
-- |Sort all points according to their Y-coordinates only.
|
||||
sortedY :: [P2] -> [P2]
|
||||
sortedY :: [PT] -> [PT]
|
||||
sortedY xs =
|
||||
fmap p2
|
||||
. sortBy (\(_, b1) (_, b2) -> compare b1 b2)
|
||||
@@ -165,25 +170,25 @@ sortedY xs =
|
||||
|
||||
|
||||
-- |Apply a function on the coordinates of a point.
|
||||
onPT :: ((Double, Double) -> (Double, Double)) -> P2 -> P2
|
||||
onPT :: (Coord -> Coord) -> PT -> PT
|
||||
onPT f = p2 . f . unp2
|
||||
|
||||
|
||||
-- |Compare the y-coordinate of two points.
|
||||
ptCmpY :: P2 -> P2 -> Ordering
|
||||
ptCmpY :: PT -> PT -> Ordering
|
||||
ptCmpY (coords -> _ :& y1) (coords -> _ :& y2) =
|
||||
compare y1 y2
|
||||
|
||||
|
||||
-- |Compare the x-coordinate of two points.
|
||||
ptCmpX :: P2 -> P2 -> Ordering
|
||||
ptCmpX :: PT -> PT -> Ordering
|
||||
ptCmpX (coords -> x1 :& _) (coords -> x2 :& _) =
|
||||
compare x1 x2
|
||||
|
||||
|
||||
posInfPT :: P2
|
||||
posInfPT :: PT
|
||||
posInfPT = p2 (read "Infinity", read "Infinity")
|
||||
|
||||
|
||||
negInfPT :: P2
|
||||
negInfPT :: PT
|
||||
negInfPT = p2 (negate . read $ "Infinity", negate . read $ "Infinity")
|
||||
|
||||
Reference in New Issue
Block a user