Add random floor tiles to floors

This commit is contained in:
jgk
2021-06-16 00:14:18 +02:00
parent da631e3b37
commit 29902b6f22
10 changed files with 76 additions and 16 deletions
+29 -7
View File
@@ -21,7 +21,7 @@ import Geometry.Intersect
import Geometry.Bezier
import Geometry.Vector
import Data.Function
--import Data.Function
import Data.List
import Data.Maybe
--import Control.Applicative
@@ -143,12 +143,20 @@ isRHS
a2 = y' - y
b1 = x'' - x
b2 = y'' - y
orderPolygonAround
:: Point2 -- ^ point to order around
-> [Point2]
-> [Point2]
orderPolygonAround _ [] = []
orderPolygonAround cen ps = sortOn (\p -> argV (p -.- cen)) ps
orderAroundFirst :: [Point2] -> [Point2]
orderAroundFirst [] = []
orderAroundFirst (a:as) = a : orderPolygonAround a as
-- | 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
orderPolygon ps = orderPolygonAround (1/ fromIntegral (length ps) *.* foldr1 (+.+) ps) ps
-- | Adds a point to a convex polygon.
-- If the point is inside, returns the original.
-- Points ordered anticlockwise, input not checked.
@@ -158,8 +166,22 @@ addPointPolygon p 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 _ = error "Tried to create the convex hull of two or less points"
convexHull (x:y:z:xs) = grahamScan $ orderAroundFirst $ sortOn (\(a,b) -> (b,a)) (x:y:z:xs)
convexHull _ = error "Tried to create the convex hull of two or fewer points"
grahamScan :: [Point2] -> [Point2]
grahamScan = foldr push []
where
push point stack = grahamEliminate (point:stack)
-- | Remove second element if top three elements are not counterclockwise.
-- Repeat if necessary. See
-- https://codereview.stackexchange.com/questions/206019/graham-scan-algorithm-in-haskell
grahamEliminate :: [Point2] -> [Point2]
grahamEliminate (x:y:z:xs)
| isRHS x y z = grahamEliminate (x:z:xs)
grahamEliminate xs = xs
-- | Return distance between two points.
dist :: Point2 -> Point2 -> Float
{-# INLINE dist #-}
@@ -229,7 +251,7 @@ anyPolyssIntersect x y = or $ polysIntersect <$> x <*> y
-- split a list into triples, forms triangles from a polygon
polyToTris :: [s] -> [s]
{-# INLINE polyToTris #-}
polyToTris (a:b:c:as) = a : intercalate [a] (zipWith (\x y->[x,y]) (init (b:c:as)) (c:as))
polyToTris (a:b:c:as) = a : intercalate [a] (zipWith (\x y->[x,y]) (b:c:as) (c:as))
polyToTris _ = []
-- | Return n equidistant points on a circle with a radius of 600.