Delete cruft, add Reader monad to some internal ai
This commit is contained in:
+27
-79
@@ -24,16 +24,13 @@ import Geometry.Vector
|
||||
import Data.Function
|
||||
import Data.List
|
||||
import Data.Maybe
|
||||
|
||||
import Control.Applicative
|
||||
|
||||
-- | Return a point a distance away from a first point towards a second point.
|
||||
-- Does not go past the second point.
|
||||
alongSegBy :: Float -> Point2 -> Point2 -> Point2
|
||||
alongSegBy !x !a !b = a +.+ y *.* normalizeV (b -.- a)
|
||||
where
|
||||
y = min x $ dist a b
|
||||
|
||||
-- | Given a line and a point return the point on the line closest to the
|
||||
-- point.
|
||||
closestPointOnLine
|
||||
@@ -44,7 +41,6 @@ closestPointOnLine
|
||||
{-# INLINE closestPointOnLine #-}
|
||||
closestPointOnLine !a !b !p = a +.+ u *.* (b -.- a)
|
||||
where u = closestPointOnLineParam a b p
|
||||
|
||||
-- | Given a line and a point return a value corresponding to how far along the
|
||||
-- line the point is.
|
||||
closestPointOnLineParam
|
||||
@@ -55,36 +51,28 @@ closestPointOnLineParam
|
||||
{-# INLINE closestPointOnLineParam #-}
|
||||
closestPointOnLineParam !a !b !p
|
||||
= (p -.- a) `dotV` (b -.- a) / (b -.- a) `dotV` (b -.- a)
|
||||
|
||||
-- | Draw a rectangle based on maximal N E S W values.
|
||||
rectNESW :: Float -> Float -> Float -> Float -> [Point2]
|
||||
rectNESW !a !b !c !d = [(b,a),(b,c),(d,c),(d,a) ]
|
||||
|
||||
-- | Draw a rectangle based on maximal N S E W values.
|
||||
rectNSEW :: Float -> Float -> Float -> Float -> [Point2]
|
||||
rectNSEW !n !s !e !w = rectNESW n e s w
|
||||
|
||||
-- | Draw a rectangle based on maximal N S W E values.
|
||||
rectNSWE :: Float -> Float -> Float -> Float -> [Point2]
|
||||
rectNSWE !n !s !w !e = [ (w,n), (w,s), (e,s), (e,n)]
|
||||
|
||||
-- | Draw a rectangle around the origin with given height and width
|
||||
rectWdthHght :: Float -> Float -> [Point2]
|
||||
rectWdthHght w h = rectNSWE h (-h) (-w) w
|
||||
|
||||
-- | Draw a rectangle around the origin with given height and width
|
||||
|
||||
-- | Test whether a point is in a polygon or on the polygon border.
|
||||
-- Supposes the points in the
|
||||
-- polygon are listed in anticlockwise order.
|
||||
pointInOrOnPolygon :: Point2 -> [Point2] -> Bool
|
||||
pointInOrOnPolygon !p (x:xs) = all (\l -> not (uncurry isRHS l p)) $ zip (x:xs) (xs ++ [x])
|
||||
|
||||
-- | Test whether a point is strictly inside a polygon.
|
||||
-- Supposes the points in the polygon are listed in anticlockwise order.
|
||||
pointInPolygon :: Point2 -> [Point2] -> Bool
|
||||
pointInPolygon !p [] = False
|
||||
pointInPolygon !p (x:xs) = all (\l -> uncurry (errorIsLHS 1) l p) $ zip (x:xs) (xs ++ [x])
|
||||
|
||||
-- | Debug version of 'pointInPolygon'.
|
||||
errorPointInPolygon :: Int -> Point2 -> [Point2] -> Bool
|
||||
errorPointInPolygon !i !p xs
|
||||
@@ -92,36 +80,30 @@ errorPointInPolygon !i !p xs
|
||||
| length xs == 2 = error "two point polygon"
|
||||
| nub xs == xs = pointInPolygon p xs
|
||||
| otherwise = error $ "errorPointInPolygon "++ show i
|
||||
|
||||
-- | Debug version of 'normalizeV'.
|
||||
errorNormalizeV :: Int -> Point2 -> Point2
|
||||
errorNormalizeV !i (0,0) = error $ "problem with function: errorNormalizeV "++show i
|
||||
errorNormalizeV !i !p = normalizeV p
|
||||
|
||||
-- | Debug version of 'angleVV'.
|
||||
errorAngleVV :: Int -> Point2 -> Point2 -> Float
|
||||
errorAngleVV !i (0,0) _ = error $ "problem with function: errorAngleVV "++show i
|
||||
errorAngleVV !i _ (0,0) = error $ "problem with function: errorAngleVV "++show i
|
||||
errorAngleVV !i !p !p' = angleVV p p'
|
||||
|
||||
-- | Debug version of 'isLHS'.
|
||||
errorIsLHS :: Int -> Point2 -> Point2 -> Point2 -> Bool
|
||||
errorIsLHS !i !x !y
|
||||
| x == y = error $ "problem with function: errorIsLHS " ++show i
|
||||
| otherwise = isLHS x y
|
||||
|
||||
-- | Debug version of 'closestPointOnLine'
|
||||
errorClosestPointOnLine :: Int -> Point2 -> Point2 -> Point2 -> Point2
|
||||
errorClosestPointOnLine !i !x !y
|
||||
| x == y = error $ "problem with function: errorClosestPointOnLine " ++show i
|
||||
| otherwise = closestPointOnLine x y
|
||||
|
||||
-- | Debug version of 'closestPointOnLineParam'
|
||||
errorClosestPointOnLineParam :: Int -> Point2 -> Point2 -> Point2 -> Float
|
||||
errorClosestPointOnLineParam !i !x! y! z
|
||||
| x == y = dist x z
|
||||
| otherwise = closestPointOnLineParam x y z
|
||||
|
||||
-- | Test whether a point is on the LHS of a line.
|
||||
-- Returns False if the line is of zero length.
|
||||
isLHS
|
||||
@@ -141,7 +123,6 @@ isLHS
|
||||
a2 = y' - y
|
||||
b1 = x'' - x
|
||||
b2 = y'' - y
|
||||
|
||||
-- | Test whether a point is on the LHS of a line.
|
||||
-- Returns False if the line is of zero length.
|
||||
isRHS
|
||||
@@ -161,14 +142,12 @@ isRHS
|
||||
a2 = y' - y
|
||||
b1 = x'' - x
|
||||
b2 = y'' - y
|
||||
|
||||
-- | Reorder points to be anticlockwise around their center.
|
||||
orderPolygon :: [Point2] -> [Point2]
|
||||
orderPolygon [] = []
|
||||
orderPolygon ps = sortBy (compare `on` \p -> argV (p -.- cen)) ps
|
||||
where
|
||||
cen = 1/ fromIntegral (length ps) *.* foldr1 (+.+) ps
|
||||
|
||||
-- | Adds a point to a convex polygon.
|
||||
-- If the point is inside, returns the original.
|
||||
-- Points ordered anticlockwise, input not checked.
|
||||
@@ -176,21 +155,17 @@ addPointPolygon :: Point2 -> [Point2] -> [Point2]
|
||||
addPointPolygon p ps
|
||||
| pointInOrOnPolygon p ps = ps
|
||||
| otherwise = orderPolygon $ p : ps
|
||||
|
||||
-- | Creates the convex hull of a set of points.
|
||||
convexHull :: [Point2] -> [Point2]
|
||||
convexHull (x:y:z:xs) = foldr addPointPolygon (orderPolygon (x:y:z:[])) xs
|
||||
convexHull (x:y:z:xs) = foldr addPointPolygon (orderPolygon [x,y,z]) xs
|
||||
convexHull _ = error "Tried to create the convex hull of two or less points"
|
||||
|
||||
-- | Return distance between two points.
|
||||
dist :: Point2 -> Point2 -> Float
|
||||
{-# INLINE dist #-}
|
||||
dist !p1 !p2 = magV (p2 -.- p1)
|
||||
|
||||
-- | Return midpoint between two points.
|
||||
pHalf :: Point2 -> Point2 -> Point2
|
||||
pHalf !a !b = 0.5 *.* (a +.+ b)
|
||||
|
||||
-- | Test whether a circle is on a segment by intersecting a new normal segment through the
|
||||
-- center of the circle with the segment itself.
|
||||
-- Returns False if the circle center is beyond the enpoints of the
|
||||
@@ -202,40 +177,34 @@ circOnSegNoEndpoints !p1 !p2 !c !rad = isJustTrue (fmap (\p -> magV (p -.- c) <
|
||||
y = intersectSegLine' p1 p2 c (c +.+ vNormal (p1 -.- p2))
|
||||
isJustTrue (Just True) = True
|
||||
isJustTrue _ = False
|
||||
|
||||
-- | Test whether a circle is on a segment by intersecting a normal and testing
|
||||
-- the distance to the endpoints of the segment.
|
||||
circOnSeg :: Point2 -> Point2 -> Point2 -> Float -> Bool
|
||||
{-# INLINE circOnSeg #-}
|
||||
circOnSeg !p1 !p2 !c !rad = magV (p1 -.- c) <= rad || magV (p2 -.- c) <= rad
|
||||
|| isJustTrue (fmap (\p -> magV (p -.- c) < rad) y)
|
||||
|| isJustTrue (fmap (\p -> magV (p -.- c) < rad) y)
|
||||
where
|
||||
y = intersectSegLine' p1 p2 c (c +.+ vNormal (p1 -.- p2))
|
||||
isJustTrue (Just True) = True
|
||||
isJustTrue _ = False
|
||||
|
||||
-- | Find the difference between two Nums.
|
||||
difference :: (Ord a, Num a) => a -> a -> a
|
||||
difference x y
|
||||
| x > y = x - y
|
||||
| otherwise = y - x
|
||||
|
||||
-- | Given vector line direction and a vector movement,
|
||||
-- reflects the movement accoring to the line.
|
||||
reflectIn :: Point2 -> Point2 -> Point2
|
||||
reflectIn line vec =
|
||||
let angle = 2 * angleBetween line vec
|
||||
in rotateV angle vec
|
||||
|
||||
reflectIn line vec = rotateV angle vec
|
||||
where
|
||||
angle = 2 * angleBetween line vec
|
||||
-- | Find angle between two points.
|
||||
-- Not normalised, ranges from -2*pi to 2*pi.
|
||||
angleBetween :: Point2 -> Point2 -> Float
|
||||
angleBetween v1 v2 = argV v1 - argV v2
|
||||
|
||||
-- | Return a list containing two copies of a pair.
|
||||
doublePair :: (a,a) -> [(a,a)]
|
||||
doublePair (x,y) = [(x,y),(y,x)]
|
||||
|
||||
-- | Test whether two polygons intersect by testing the intersection of each
|
||||
-- consecutive pair of points.
|
||||
polysIntersect :: [Point2] -> [Point2] -> Bool
|
||||
@@ -245,40 +214,32 @@ polysIntersect (p:ps) (q:qs)
|
||||
pairs1 = zip (p:ps) (ps++[p])
|
||||
pairs2 = zip (q:qs) (qs++[q])
|
||||
polysIntersect _ _ = False
|
||||
|
||||
-- | Test whether two polygons intersect or if one is contained in the other.
|
||||
polysOverlap :: [Point2] -> [Point2] -> Bool
|
||||
polysOverlap (p:ps) (q:qs) = polysIntersect (p:ps) (q:qs)
|
||||
|| pointInPolygon p (q:qs)
|
||||
|| pointInPolygon q (p:ps)
|
||||
polysOverlap _ _ = False
|
||||
|
||||
-- | Test whether any polygons from a first list intersect with any polygons from
|
||||
-- a second list.
|
||||
anyPolyssIntersect :: [[Point2]] -> [[Point2]] -> Bool
|
||||
anyPolyssIntersect x y = or $ polysIntersect <$> x <*> y
|
||||
|
||||
-- | Return n equidistant points on a circle with a radius of 600.
|
||||
nRays :: Int -> [Point2]
|
||||
nRays n = take n $ iterate (rotateV (2*pi/fromIntegral n)) (600,0)
|
||||
|
||||
-- | Return n equidistant points on a circle with a radius of x.
|
||||
nRaysRad :: Int -> Float -> [Point2]
|
||||
nRaysRad n x = take n $ iterate (rotateV (2*pi/fromIntegral n)) (x,0)
|
||||
|
||||
-- | Test whether an angle is to the left of another angle, according to the
|
||||
-- smallest change in rotation between them.
|
||||
-- This appears to sometimes fail if the angles are not normalized.
|
||||
isLeftOfA :: Float -> Float -> Bool
|
||||
isLeftOfA angle1 angle2 =
|
||||
(angle1 - angle2 < pi && angle1 > angle2)
|
||||
isLeftOfA angle1 angle2 = (angle1 - angle2 < pi && angle1 > angle2)
|
||||
|| (angle2 - angle1 > pi && angle2 > angle1)
|
||||
|
||||
-- | Test whether a vector is to the left of another, according to the smallest
|
||||
-- change of rotation between them.
|
||||
isLeftOf :: Point2 -> Point2 -> Bool
|
||||
isLeftOf x y = isLeftOfA (argV x) (argV y)
|
||||
|
||||
-- | Find the difference between two angles.
|
||||
-- Possibly not correct...
|
||||
diffAngles :: Float -> Float -> Float
|
||||
@@ -292,7 +253,6 @@ diffAngles x y
|
||||
|
||||
differenceAngles = diffAngles
|
||||
angleDifference = diffAngles
|
||||
|
||||
-- | Given a triangle where we know the length of a first side,
|
||||
-- the length of a second side, and the angle between the first side and the
|
||||
-- third side, finds the length of the third side.
|
||||
@@ -305,7 +265,6 @@ ssaTri ab bc a
|
||||
let c = asin ( (ab * sin a)/bc)
|
||||
b = pi - (a + c)
|
||||
in sin b * bc / sin a
|
||||
|
||||
-- | Given two points of a triangle and a third point, return
|
||||
-- the point which lies between pa and pc' on a line from pb of length bc.
|
||||
-- Note that there are likely two such points, this should return the point
|
||||
@@ -316,7 +275,6 @@ ssaTriPoint pa pb pc' bc
|
||||
a = errorAngleVV 6 (pb -.- pa) (pc' -.- pa)
|
||||
ac = ssaTri ab bc a
|
||||
in pa +.+ (ac *.* errorNormalizeV 47 (pc' -.- pa))
|
||||
|
||||
-- | Safe version of 'ssaTriPoint'.
|
||||
ssaTriPoint' :: Point2 -> Point2 -> Point2 -> Float -> Maybe Point2
|
||||
ssaTriPoint' pa pb pc' bc
|
||||
@@ -333,41 +291,34 @@ ssaTriPointCorrect pa pb pc' bc
|
||||
where
|
||||
p = ssaTriPoint pa pb pc' bc
|
||||
param = closestPointOnLineParam pa pc' p
|
||||
|
||||
-- | Given a segment and external point, find the closest point on the segment.
|
||||
closestPointOnSeg :: Point2 -> Point2 -> Point2 -> Point2
|
||||
closestPointOnSeg segP1 segP2 p
|
||||
| errorClosestPointOnLineParam 3 segP1 segP2 p <= 0 = segP1
|
||||
| errorClosestPointOnLineParam 4 segP1 segP2 p >= 1 = segP2
|
||||
| otherwise = errorClosestPointOnLine 2 segP1 segP2 p
|
||||
|
||||
-- | Return Just a point if it is inside a circle, Nothing otherwise.
|
||||
pointInCircle :: Point2 -> Float -> Point2 -> Maybe Point2
|
||||
pointInCircle p r c
|
||||
| p == c = Just p
|
||||
| magV (p -.- c) < r = Just p
|
||||
| otherwise = Nothing
|
||||
|
||||
-- | Determines if a moving point intersects with a circle,
|
||||
-- if so, returns a point on circle that intersects with the line passing
|
||||
-- throught the circle : HOPEFULLY THE CORRECT OF THE TWO!
|
||||
collidePointCirc :: Point2 -> Point2 -> Float -> Point2 -> Maybe Point2
|
||||
collidePointCirc p1 p2 rad c = ssaTriPoint' p2 c p1 rad
|
||||
|
||||
-- | As 'collidePointCirc', but changes the point to a measure of the distance.
|
||||
collidePointCirc' :: Point2 -> Point2 -> Float -> Point2 -> Maybe Float
|
||||
collidePointCirc' p1 p2 rad c = fmap (\x -> magV (x -.- p1))
|
||||
(collidePointCirc p1 p2 rad c)
|
||||
|
||||
(collidePointCirc p1 p2 rad c)
|
||||
-- | As 'collidePointCirc', but returns both the point and the measure of the distance.
|
||||
collidePointCirc'' :: Point2 -> Point2 -> Float -> Point2 -> Maybe (Point2,Float)
|
||||
collidePointCirc'' p1 p2 rad c = (,) <$> collidePointCirc p1 p2 rad c
|
||||
<*> collidePointCirc' p1 p2 rad c
|
||||
|
||||
<*> collidePointCirc' p1 p2 rad c
|
||||
-- | As 'collidePointCirc', but uses the supposedly correct version of ssaTriPoint.
|
||||
collidePointCircCorrect :: Point2 -> Point2 -> Float -> Point2 -> Maybe Point2
|
||||
collidePointCircCorrect p1 p2 rad c = ssaTriPointCorrect p2 c p1 rad
|
||||
|
||||
-- | Finds the height of a triangle using herons formula.
|
||||
-- The base is the line between the first two points.
|
||||
heron :: Point2 -> Point2 -> Point2 -> Float
|
||||
@@ -380,7 +331,6 @@ heron x y z
|
||||
s = (a+b+c)/2
|
||||
area = sqrt(s*(s-a)*(s-b)*(s-c))
|
||||
in 2*area/a
|
||||
|
||||
-- | Multiplies reflection in normal by factor.
|
||||
reflectInParam :: Float -> Point2 -> Point2 -> Point2
|
||||
reflectInParam x line vec =
|
||||
@@ -388,7 +338,6 @@ reflectInParam x line vec =
|
||||
rAng = rotateV angle vec
|
||||
p = x *.* errorClosestPointOnLine 3 (0,0) (vNormal line) rAng
|
||||
in rAng -.- p
|
||||
|
||||
--reflectIn' :: Point2 -> Point2 -> Point2 -> Point2 -> Point2
|
||||
--reflectIn' l1 l2 v1 v2 = v1 +.+ reflectIn (l1 -.- l2) (v2 -.- v1)
|
||||
|
||||
@@ -397,47 +346,47 @@ isOnSeg l1 l2 p =
|
||||
errorClosestPointOnLineParam 10 l1 (l1 +.+ vNormal (l2 -.- l1)) p == 0
|
||||
&& errorClosestPointOnLineParam 11 l1 l2 p <= 1
|
||||
&& errorClosestPointOnLineParam 12 l1 l2 p >= 0
|
||||
|
||||
-- | Divide a segment into a list of points with a maximal distance between
|
||||
-- them.
|
||||
-- the take 5000 here is a hack, otherwise divideLine seems to sometimes
|
||||
-- generate an infinite list, and I don't know why
|
||||
divideLine :: Float -> Point2 -> Point2 -> [Point2]
|
||||
--divideLine x a b = map (\i -> a +.+ (i / (fromIntegral numPoints)) *.* (b -.- a))
|
||||
divideLine x a b =
|
||||
take 5000
|
||||
divideLine x a b = take 5000
|
||||
$ map (\i -> a +.+ (fromIntegral i / fromIntegral numPoints *.* (b -.- a)) )
|
||||
ns
|
||||
ns
|
||||
where
|
||||
d = dist a b
|
||||
numPoints = max 1 $ ceiling $ d / x
|
||||
ns = [0 .. numPoints]
|
||||
|
||||
-- | As 'divideLine', but must return an odd number of points.
|
||||
divideLineOddNumPoints :: Float -> Point2 -> Point2 -> [Point2]
|
||||
--divideLine x a b = map (\i -> a +.+ (i / (fromIntegral numPoints)) *.* (b -.- a))
|
||||
divideLineOddNumPoints x a b = take 5000
|
||||
$ map (\i -> a +.+ (fromIntegral i / fromIntegral numPoints *.* (b -.- a)) )
|
||||
ns
|
||||
ns
|
||||
where
|
||||
d = dist a b
|
||||
numPoints' = max 1 $ ceiling $ d / x
|
||||
numPoints | even numPoints' = numPoints'
|
||||
| otherwise = numPoints' + 1
|
||||
numPoints
|
||||
| even numPoints' = numPoints'
|
||||
| otherwise = numPoints' + 1
|
||||
ns = [0 .. numPoints]
|
||||
|
||||
-- | Given two pairs of Ints, returns a list of pairs of Ints that form
|
||||
-- a digital line between them.
|
||||
digitalLine :: (Int,Int) -> (Int,Int) -> [(Int,Int)]
|
||||
{-# INLINE digitalLine #-}
|
||||
digitalLine (x1,y1) (x2,y2)
|
||||
| abs (x1-x2) > abs (y1-y2) = [ (x,( (y1-y2) * x + x1*y2 - x2*y1) `rdiv` (x1-x2) )
|
||||
| x <- intervalList x1 x2 ]
|
||||
| otherwise = [ ( ((x1-x2) * y + y1*x2 - y2*x1) `rdiv` (y1-y2) , y)
|
||||
| y <- intervalList y1 y2 ]
|
||||
| abs (x1-x2) > abs (y1-y2) =
|
||||
[ (x,( (y1-y2) * x + x1*y2 - x2*y1) `rdiv` (x1-x2) )
|
||||
| x <- intervalList x1 x2
|
||||
]
|
||||
| otherwise =
|
||||
[ ( ((x1-x2) * y + y1*x2 - y2*x1) `rdiv` (y1-y2) , y)
|
||||
| y <- intervalList y1 y2
|
||||
]
|
||||
where
|
||||
rdiv a b = round $ fromIntegral a / fromIntegral b
|
||||
|
||||
-- | Given two pairs of 'Int's, create a list of pairs of 'Int's that form a
|
||||
-- rectangle between them.
|
||||
digitalRect :: (Int,Int) -> (Int,Int) -> [(Int,Int)]
|
||||
@@ -448,13 +397,11 @@ digitalRect (a,b) (c,d) = [(s,t) | s <- [minx .. maxx] , t <- [miny .. maxy]]
|
||||
minx = min a c
|
||||
maxy = max b d
|
||||
miny = min b d
|
||||
|
||||
-- | Given two Ints, creates the list of Ints between these.
|
||||
intervalList :: Int -> Int -> [Int]
|
||||
intervalList x y
|
||||
| y >= x = [x .. y]
|
||||
| otherwise = reverse [y..x]
|
||||
|
||||
-- | Create points on the circumference of a circle with maximal distance
|
||||
-- between them.
|
||||
divideCircle :: Float -> Point2 -> Float -> [Point2]
|
||||
@@ -478,7 +425,7 @@ arcStepwisePositive
|
||||
-> Point2 -- ^ Center
|
||||
-> Point2 -- ^ Start vector from center
|
||||
-> [Point2]
|
||||
arcStepwisePositive ssize a cen v = ((cen +.+) . (\rot -> rotateV rot v) ) <$> rots
|
||||
arcStepwisePositive ssize a cen v = (cen +.+) . (`rotateV` v) <$> rots
|
||||
where
|
||||
rots :: [Float]
|
||||
rots = map ((a*) . (/ fromIntegral n ) . fromIntegral) [0 .. n]
|
||||
@@ -502,7 +449,6 @@ makeLoopPairs :: [Point2] -> [(Point2,Point2)]
|
||||
makeLoopPairs [] = error "tried to make loop with empty list of points"
|
||||
makeLoopPairs [x] = error "tried to make loop with singleton list of points"
|
||||
makeLoopPairs (x:xs) = zip (x:xs) (xs ++ [x])
|
||||
|
||||
-- | Test whether a point is in a cone.
|
||||
-- Note the pair is ordered.
|
||||
-- Doesn't work for obtuse angles.
|
||||
@@ -512,4 +458,6 @@ pointIsInCone
|
||||
-> Point2 -- ^ Point to test.
|
||||
-> Bool
|
||||
pointIsInCone c (rightp,leftp) p = isLHS c rightp p && isLHS leftp c p
|
||||
|
||||
-- | TODO: implement using Control.Foldl
|
||||
centroid :: Foldable t => t Point2 -> Point2
|
||||
centroid xs = 1 / fromIntegral (length xs) *.* foldl' (+.+) (0,0) xs
|
||||
|
||||
Reference in New Issue
Block a user