{-|
Module      : TileLib
Description : Introducing Pieces and Patches and Drawable class
Copyright   : (c) Chris Reade, 2021
License     : BSD-style
Maintainer  : chrisreade@mac.com
Stability   : experimental

This module introduces Pieces and Patches for drawing finite tilings using Penrose's Dart and Kite tiles.
It includes several primitives for drawing half tiles (Pieces), a class Drawable with instance Patch
and commonly used operations for the Drawable class (draw, drawj, fillDK,..).
It also introduces class OKBackend to summarise constraints on a Backend for drawing.
There is also a decompose operation for Patches (decompPatch) and sun and star example Patches.
-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE FlexibleContexts          #-}
{-# LANGUAGE TypeFamilies              #-}
{-# LANGUAGE FlexibleInstances         #-} -- needed for Drawable Patch
{-# LANGUAGE TypeOperators             #-} -- needed for type equality constraints ~

module TileLib 
  ( OKBackend
  -- * Pieces
  , Piece
  , joinVector
  , ldart
  , rdart
  , lkite
  , rkite
    -- * Drawing Pieces
  , phi
  , ttangle
  , pieceEdges
  , wholeTileEdges
  -- $OKBackend
  , drawPiece
  , dashjPiece
  , joinDashing
  , dashjOnly
  , drawRoundPiece
  , drawJoin
  , fillOnlyPiece
  , fillPieceDK
  -- , fillMaybePieceDK
  , leftFillPieceDK
  , experiment
    -- * Patches and Drawable Class
  , Patch
  , Drawable(..)
  , draw
  , drawj
  , fillDK
  , fillKD
  -- , fillMaybeDK
  , colourDKG
    -- , colourMaybeDKG
    -- * Patch Decomposition and Compose choices
  , decompPatch
  , decompositionsP
  , compChoices
  , compNChoices
    -- * Example Patches
  , penta
  , sun
  , TileLib.star
  , suns
  , sun5
  , sun6
    -- * Example Diagrams of Patches
  , sun6Fig
  , leftFilledSun6
  , filledSun6
    -- * Rotation and Scaling operations
  , rotations
  , scales
  , phiScales
  , phiScaling
  ) where

import Diagrams.Prelude
--import Diagrams.TwoD.Text (Text) -- now in CheckBackend

import CheckBackend
import HalfTile

{-| Piece is a type for (scaled and oriented) tile halves: Left Dart, Right Dart, Left Kite, Right Kite
represented by a vector from their origin along the join edge where
origin for a dart is the tip, origin for a kite is the vertex opposite the vertex with
largest internal angle.

(This specialises polymorphic HalfTiles with 2D vectors).

Pieces are Transformable (but not translatable until they are located).
-}
type Piece = HalfTile (V2 Double)

-- | get the vector representing the join edge in the direction away from the origin of a piece
joinVector:: Piece -> V2 Double
joinVector :: Piece -> V2 Double
joinVector = Piece -> V2 Double
forall rep. HalfTile rep -> rep
tileRep

ldart,rdart,lkite,rkite:: Piece
-- |ldart is a left dart at the origin with join edge oriented along the x axis, unit length.
ldart :: Piece
ldart = V2 Double -> Piece
forall rep. rep -> HalfTile rep
LD V2 Double
forall (v :: * -> *) n. (R1 v, Additive v, Num n) => v n
unitX
-- |rdart is a right dartat the origin with join edge oriented along the x axis, unit length.
rdart :: Piece
rdart = V2 Double -> Piece
forall rep. rep -> HalfTile rep
RD V2 Double
forall (v :: * -> *) n. (R1 v, Additive v, Num n) => v n
unitX
-- |lkite is a left kite at the origin with join edge oriented along the x axis, length phi.
lkite :: Piece
lkite = V2 Double -> Piece
forall rep. rep -> HalfTile rep
LK (Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^V2 Double
forall (v :: * -> *) n. (R1 v, Additive v, Num n) => v n
unitX)
-- |rkite  is a right kite at the origin with join edge oriented along the x axis, length phi.
rkite :: Piece
rkite = V2 Double -> Piece
forall rep. rep -> HalfTile rep
RK (Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^V2 Double
forall (v :: * -> *) n. (R1 v, Additive v, Num n) => v n
unitX)

-- |All edge lengths are powers of the golden ratio (phi).
-- We have the following roperties of the golden ratio 
-- 
-- phi^2 == phi + 1 and  1/phi = phi-1
--
-- phi^3 = 2phi + 1 and  1/phi^2 = 2-phi
phi::Double
phi :: Double
phi = (Double
1.0 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double
forall a. Floating a => a -> a
sqrt Double
5.0) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2.0

-- |All angles used are multiples of tt where tt is a tenth of a turn
-- (so 36 degrees).
-- ttangle n is n multiples of tt.
ttangle:: Int -> Angle Double
ttangle :: Int -> Angle Double
ttangle Int
n = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
10) Double -> Angle Double -> Angle Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double
forall {b}. Floating b => Angle b
tt
             where tt :: Angle b
tt = b
1b -> b -> b
forall a. Fractional a => a -> a -> a
/b
10 b -> AReview (Angle b) b -> Angle b
forall b a. b -> AReview a b -> a
@@ AReview (Angle b) b
forall n. Floating n => Iso' (Angle n) n
Iso' (Angle b) b
turn

{-|This produces a list of the two adjacent non-join tile directed edges of a piece starting from the origin.

We consider left and right as viewed from the origin.
This means that darts are reversed with respect to a view from the tail, but kites are
in keeping with a common view (the kite tail being the origin).

So for right dart and left kite the edges are directed and ordered clockwise from the piece origin, and for left dart and right kite these are
directed and ordered anti-clockwise from the piece origin.
-}
pieceEdges:: Piece -> [V2 Double]
pieceEdges :: Piece -> [V2 Double]
pieceEdges (LD V2 Double
v) = [V2 Double
v',V2 Double
v V2 Double -> V2 Double -> V2 Double
forall a. Num a => V2 a -> V2 a -> V2 a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
^-^ V2 Double
v'] where v' :: V2 Double
v' = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
9) V2 Double
v
pieceEdges (RD V2 Double
v) = [V2 Double
v',V2 Double
v V2 Double -> V2 Double -> V2 Double
forall a. Num a => V2 a -> V2 a -> V2 a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
^-^ V2 Double
v'] where v' :: V2 Double
v' = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
1) V2 Double
v
pieceEdges (RK V2 Double
v) = [V2 Double
v',V2 Double
v V2 Double -> V2 Double -> V2 Double
forall a. Num a => V2 a -> V2 a -> V2 a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
^-^ V2 Double
v'] where v' :: V2 Double
v' = Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
9) V2 Double
v
pieceEdges (LK V2 Double
v) = [V2 Double
v',V2 Double
v V2 Double -> V2 Double -> V2 Double
forall a. Num a => V2 a -> V2 a -> V2 a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
^-^ V2 Double
v'] where v' :: V2 Double
v' = Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
1) V2 Double
v

-- |the 4 tile edges of a completed half-tile piece (used for colour fill).
-- These are directed and ordered clockwise from the origin of the tile.
wholeTileEdges:: Piece -> [V2 Double]
wholeTileEdges :: Piece -> [V2 Double]
wholeTileEdges (LD V2 Double
v) = Piece -> [V2 Double]
wholeTileEdges (V2 Double -> Piece
forall rep. rep -> HalfTile rep
RD V2 Double
v)
wholeTileEdges (RD V2 Double
v) = Piece -> [V2 Double]
pieceEdges (V2 Double -> Piece
forall rep. rep -> HalfTile rep
RD V2 Double
v) [V2 Double] -> [V2 Double] -> [V2 Double]
forall a. [a] -> [a] -> [a]
++ (V2 Double -> V2 Double) -> [V2 Double] -> [V2 Double]
forall a b. (a -> b) -> [a] -> [b]
map V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => f a -> f a
negated ([V2 Double] -> [V2 Double]
forall a. [a] -> [a]
reverse ([V2 Double] -> [V2 Double]) -> [V2 Double] -> [V2 Double]
forall a b. (a -> b) -> a -> b
$ Piece -> [V2 Double]
pieceEdges (V2 Double -> Piece
forall rep. rep -> HalfTile rep
LD V2 Double
v))
wholeTileEdges (LK V2 Double
v) = Piece -> [V2 Double]
pieceEdges (V2 Double -> Piece
forall rep. rep -> HalfTile rep
LK V2 Double
v) [V2 Double] -> [V2 Double] -> [V2 Double]
forall a. [a] -> [a] -> [a]
++ (V2 Double -> V2 Double) -> [V2 Double] -> [V2 Double]
forall a b. (a -> b) -> [a] -> [b]
map V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => f a -> f a
negated ([V2 Double] -> [V2 Double]
forall a. [a] -> [a]
reverse ([V2 Double] -> [V2 Double]) -> [V2 Double] -> [V2 Double]
forall a b. (a -> b) -> a -> b
$ Piece -> [V2 Double]
pieceEdges (V2 Double -> Piece
forall rep. rep -> HalfTile rep
RK V2 Double
v))
wholeTileEdges (RK V2 Double
v) = Piece -> [V2 Double]
wholeTileEdges (V2 Double -> Piece
forall rep. rep -> HalfTile rep
LK V2 Double
v)

{- $OKBackend 
Note: Most functions for drawing will have constraint OKBackend b and result type Diagram b
-}
   
-- |drawing lines for the 2 non-join edges of a piece.
drawPiece :: OKBackend b =>
             Piece -> Diagram b
drawPiece :: forall b. OKBackend b => Piece -> Diagram b
drawPiece = Trail' Line V2 Double -> QDiagram b V2 Double Any
forall n b.
(TypeableFloat n, Renderable (Path V2 n) b) =>
Trail' Line V2 n -> QDiagram b V2 n Any
strokeLine (Trail' Line V2 Double -> QDiagram b V2 Double Any)
-> (Piece -> Trail' Line V2 Double)
-> Piece
-> QDiagram b V2 Double Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
[V2 Double] -> Trail' Line V2 Double
forall t. TrailLike t => [Vn t] -> t
fromOffsets ([V2 Double] -> Trail' Line V2 Double)
-> (Piece -> [V2 Double]) -> Piece -> Trail' Line V2 Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Piece -> [V2 Double]
pieceEdges

-- |same as drawPiece but with join edge added as faint dashed line.
dashjPiece :: OKBackend b =>
              Piece -> Diagram b
dashjPiece :: forall b. OKBackend b => Piece -> Diagram b
dashjPiece Piece
piece = Piece -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => Piece -> Diagram b
drawPiece Piece
piece QDiagram b V2 Double Any
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall a. Semigroup a => a -> a -> a
<> Piece -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => Piece -> Diagram b
dashjOnly Piece
piece


-- |draw join edge only (as faint dashed line).
dashjOnly :: OKBackend b =>
             Piece -> Diagram b
-- dashjOnly piece = drawJoin piece # dashingN [0.003,0.003] 0 # lw ultraThin -- # lc grey 
dashjOnly :: forall b. OKBackend b => Piece -> Diagram b
dashjOnly Piece
piece = Piece -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => Piece -> Diagram b
drawJoin Piece
piece QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall c. (HasStyle c, N c ~ Double) => c -> c
joinDashing

-- |changes line style to ultraThin dashed lines (for drawing join edges)
joinDashing :: (HasStyle c, N c ~ Double) => c -> c
joinDashing :: forall c. (HasStyle c, N c ~ Double) => c -> c
joinDashing = [Measure Double] -> Measure Double -> c -> c
forall a n.
(N a ~ n, HasStyle a, Typeable n) =>
[Measure n] -> Measure n -> a -> a
dashing [Measure Double
forall {n}. (Ord n, Fractional n) => Measure n
dashmeasure,Measure Double
forall {n}. (Ord n, Fractional n) => Measure n
dashmeasure] Measure Double
0 (c -> c) -> (c -> c) -> c -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Measure Double -> c -> c
forall a n.
(N a ~ n, HasStyle a, Typeable n) =>
Measure n -> a -> a
lw Measure Double
forall n. OrderedField n => Measure n
ultraThin
                     where dashmeasure :: Measure n
dashmeasure = n -> Measure n
forall n. Num n => n -> Measure n
normalized n
0.003  Measure n -> Measure n -> Measure n
forall n. Ord n => Measure n -> Measure n -> Measure n
`atLeast` n -> Measure n
forall n. n -> Measure n
output n
0.5

-- |same as drawPiece but with added join edge (also fillable as a loop).
drawRoundPiece :: OKBackend b =>
                  Piece -> Diagram b
drawRoundPiece :: forall b. OKBackend b => Piece -> Diagram b
drawRoundPiece = Trail' Loop V2 Double -> QDiagram b V2 Double Any
forall n b.
(TypeableFloat n, Renderable (Path V2 n) b) =>
Trail' Loop V2 n -> QDiagram b V2 n Any
strokeLoop (Trail' Loop V2 Double -> QDiagram b V2 Double Any)
-> (Piece -> Trail' Loop V2 Double)
-> Piece
-> QDiagram b V2 Double Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Trail' Line V2 Double -> Trail' Loop V2 Double
forall (v :: * -> *) n. Trail' Line v n -> Trail' Loop v n
closeLine (Trail' Line V2 Double -> Trail' Loop V2 Double)
-> (Piece -> Trail' Line V2 Double)
-> Piece
-> Trail' Loop V2 Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
[V2 Double] -> Trail' Line V2 Double
forall t. TrailLike t => [Vn t] -> t
fromOffsets ([V2 Double] -> Trail' Line V2 Double)
-> (Piece -> [V2 Double]) -> Piece -> Trail' Line V2 Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Piece -> [V2 Double]
pieceEdges

-- |draw join edge only.
drawJoin :: OKBackend b =>
            Piece -> Diagram b
drawJoin :: forall b. OKBackend b => Piece -> Diagram b
drawJoin Piece
piece = Trail' Line V2 (N b) -> QDiagram b V2 (N b) Any
forall n b.
(TypeableFloat n, Renderable (Path V2 n) b) =>
Trail' Line V2 n -> QDiagram b V2 n Any
strokeLine (Trail' Line V2 (N b) -> QDiagram b V2 (N b) Any)
-> Trail' Line V2 (N b) -> QDiagram b V2 (N b) Any
forall a b. (a -> b) -> a -> b
$ [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
forall t. TrailLike t => [Vn t] -> t
fromOffsets [Piece -> V2 Double
joinVector Piece
piece]

-- |fillOnlyPiece col piece - fills piece with colour col without drawing any lines.
-- Can be used with both Colour and AlphaColour
fillOnlyPiece :: (OKBackend b, Color c) =>
                  c -> Piece -> Diagram b
fillOnlyPiece :: forall b c. (OKBackend b, Color c) => c -> Piece -> Diagram b
fillOnlyPiece c
col Piece
piece  = Piece -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => Piece -> Diagram b
drawRoundPiece Piece
piece QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# c -> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall n a c.
(InSpace V2 n a, Color c, Typeable n, Floating n, HasStyle a) =>
c -> a -> a
fillColor c
col QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# Measure Double
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall a n.
(N a ~ n, HasStyle a, Typeable n) =>
Measure n -> a -> a
lw Measure Double
forall n. OrderedField n => Measure n
none

-- |fillPieceDK dcol kcol piece - draws and fills the half-tile piece
-- with colour dcol for darts and kcol for kites.
-- Note the order D K.
-- Can be used with both Colour and AlphaColour
fillPieceDK :: (OKBackend b, Color c1, Color c2) =>
                c1 -> c2 -> HalfTile (V2 Double) -> Diagram b
fillPieceDK :: forall b c1 c2.
(OKBackend b, Color c1, Color c2) =>
c1 -> c2 -> Piece -> Diagram b
fillPieceDK c1
dcol c2
kcol Piece
piece = Piece -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => Piece -> Diagram b
drawPiece Piece
piece QDiagram b V2 Double Any
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall a. Semigroup a => a -> a -> a
<> QDiagram b (V b) (N b) Any
QDiagram b V2 Double Any
filledPiece where
  filledPiece :: QDiagram b (V b) (N b) Any
filledPiece = case Piece
piece of
     (LD V2 Double
_) -> c1 -> Piece -> QDiagram b (V b) (N b) Any
forall b c. (OKBackend b, Color c) => c -> Piece -> Diagram b
fillOnlyPiece c1
dcol Piece
piece
     (RD V2 Double
_) -> c1 -> Piece -> QDiagram b (V b) (N b) Any
forall b c. (OKBackend b, Color c) => c -> Piece -> Diagram b
fillOnlyPiece c1
dcol Piece
piece
     (LK V2 Double
_) -> c2 -> Piece -> QDiagram b (V b) (N b) Any
forall b c. (OKBackend b, Color c) => c -> Piece -> Diagram b
fillOnlyPiece c2
kcol Piece
piece
     (RK V2 Double
_) -> c2 -> Piece -> QDiagram b (V b) (N b) Any
forall b c. (OKBackend b, Color c) => c -> Piece -> Diagram b
fillOnlyPiece c2
kcol Piece
piece

-- |leftFillPieceDK dcol kcol pc fills the whole tile when pc is a left half-tile,
-- darts are filled with colour dcol and kites with colour kcol.
-- (Right half-tiles produce nothing, so whole tiles are not drawn twice).
-- Works with AlphaColours as well as Colours.
leftFillPieceDK :: (OKBackend b, Color c1, Color c2) =>
                   c1 -> c2 -> HalfTile (V2 Double) -> Diagram b
leftFillPieceDK :: forall b c1 c2.
(OKBackend b, Color c1, Color c2) =>
c1 -> c2 -> Piece -> Diagram b
leftFillPieceDK c1
dcol c2
kcol Piece
pc =
     case Piece
pc of (LD V2 Double
_) -> Trail' Loop V2 Double -> QDiagram b V2 Double Any
forall n b.
(TypeableFloat n, Renderable (Path V2 n) b) =>
Trail' Loop V2 n -> QDiagram b V2 n Any
strokeLoop (Trail' Line V2 Double -> Trail' Loop V2 Double
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Trail' Line v n -> Trail' Loop v n
glueLine (Trail' Line V2 Double -> Trail' Loop V2 Double)
-> Trail' Line V2 Double -> Trail' Loop V2 Double
forall a b. (a -> b) -> a -> b
$ [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
forall t. TrailLike t => [Vn t] -> t
fromOffsets ([Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double)
-> [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
forall a b. (a -> b) -> a -> b
$ Piece -> [V2 Double]
wholeTileEdges Piece
pc)  QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# c1 -> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall n a c.
(InSpace V2 n a, Color c, Typeable n, Floating n, HasStyle a) =>
c -> a -> a
fillColor c1
dcol
                (LK V2 Double
_) -> Trail' Loop V2 Double -> QDiagram b V2 Double Any
forall n b.
(TypeableFloat n, Renderable (Path V2 n) b) =>
Trail' Loop V2 n -> QDiagram b V2 n Any
strokeLoop (Trail' Line V2 Double -> Trail' Loop V2 Double
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Trail' Line v n -> Trail' Loop v n
glueLine (Trail' Line V2 Double -> Trail' Loop V2 Double)
-> Trail' Line V2 Double -> Trail' Loop V2 Double
forall a b. (a -> b) -> a -> b
$ [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
forall t. TrailLike t => [Vn t] -> t
fromOffsets ([Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double)
-> [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
forall a b. (a -> b) -> a -> b
$ Piece -> [V2 Double]
wholeTileEdges Piece
pc)  QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# c2 -> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall n a c.
(InSpace V2 n a, Color c, Typeable n, Floating n, HasStyle a) =>
c -> a -> a
fillColor c2
kcol
                Piece
_      -> Diagram b
QDiagram b V2 Double Any
forall a. Monoid a => a
mempty
        
-- |experiment uses a different rule for drawing half tiles.
-- This clearly displays the larger kites and darts.
-- Half tiles are first drawn with dashed lines, then certain edges are overlayed to emphasise them.
-- Half darts have the join edge emphasised in red, while
-- Half kites have the long edge emphasised in black.
experiment:: OKBackend b =>
             Piece ->  Diagram b
experiment :: forall b. OKBackend b => Piece -> Diagram b
experiment Piece
piece = Piece -> QDiagram b V2 Double Any
forall {b}.
Renderable (Path V2 Double) b =>
Piece -> QDiagram b V2 Double Any
emph Piece
piece QDiagram b V2 Double Any
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall a. Semigroup a => a -> a -> a
<> (Piece -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => Piece -> Diagram b
drawRoundPiece Piece
piece QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# [Double]
-> Double -> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall a n.
(N a ~ n, HasStyle a, Typeable n, Num n) =>
[n] -> n -> a -> a
dashingN [Double
0.003,Double
0.003] Double
0 QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# Measure Double
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall a n.
(N a ~ n, HasStyle a, Typeable n) =>
Measure n -> a -> a
lw Measure Double
forall n. OrderedField n => Measure n
ultraThin)
    --emph pc <> (drawRoundPiece pc # dashingO [1,2] 0 # lw ultraThin)
  where emph :: Piece -> QDiagram b V2 Double Any
emph Piece
pc = case Piece
pc of
          (LD V2 Double
v) -> (Trail' Line V2 Double -> QDiagram b V2 Double Any
forall n b.
(TypeableFloat n, Renderable (Path V2 n) b) =>
Trail' Line V2 n -> QDiagram b V2 n Any
strokeLine (Trail' Line V2 Double -> QDiagram b V2 Double Any)
-> ([V2 Double] -> Trail' Line V2 Double)
-> [V2 Double]
-> QDiagram b V2 Double Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
[V2 Double] -> Trail' Line V2 Double
forall t. TrailLike t => [Vn t] -> t
fromOffsets) [V2 Double
v] QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# Colour Double
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall n a.
(InSpace V2 n a, Typeable n, Floating n, HasStyle a) =>
Colour Double -> a -> a
lc Colour Double
forall a. (Ord a, Floating a) => Colour a
red   -- emphasise join edge of darts in red
          (RD V2 Double
v) -> (Trail' Line V2 Double -> QDiagram b V2 Double Any
forall n b.
(TypeableFloat n, Renderable (Path V2 n) b) =>
Trail' Line V2 n -> QDiagram b V2 n Any
strokeLine (Trail' Line V2 Double -> QDiagram b V2 Double Any)
-> ([V2 Double] -> Trail' Line V2 Double)
-> [V2 Double]
-> QDiagram b V2 Double Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
[V2 Double] -> Trail' Line V2 Double
forall t. TrailLike t => [Vn t] -> t
fromOffsets) [V2 Double
v] QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# Colour Double
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall n a.
(InSpace V2 n a, Typeable n, Floating n, HasStyle a) =>
Colour Double -> a -> a
lc Colour Double
forall a. (Ord a, Floating a) => Colour a
red 
          (LK V2 Double
v) -> (Trail' Line V2 Double -> QDiagram b V2 Double Any
forall n b.
(TypeableFloat n, Renderable (Path V2 n) b) =>
Trail' Line V2 n -> QDiagram b V2 n Any
strokeLine (Trail' Line V2 Double -> QDiagram b V2 Double Any)
-> ([V2 Double] -> Trail' Line V2 Double)
-> [V2 Double]
-> QDiagram b V2 Double Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
[V2 Double] -> Trail' Line V2 Double
forall t. TrailLike t => [Vn t] -> t
fromOffsets) [Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
1) V2 Double
v] -- emphasise long edge for kites
          (RK V2 Double
v) -> (Trail' Line V2 Double -> QDiagram b V2 Double Any
forall n b.
(TypeableFloat n, Renderable (Path V2 n) b) =>
Trail' Line V2 n -> QDiagram b V2 n Any
strokeLine (Trail' Line V2 Double -> QDiagram b V2 Double Any)
-> ([V2 Double] -> Trail' Line V2 Double)
-> [V2 Double]
-> QDiagram b V2 Double Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Vn (Trail' Line V2 Double)] -> Trail' Line V2 Double
[V2 Double] -> Trail' Line V2 Double
forall t. TrailLike t => [Vn t] -> t
fromOffsets) [Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
9) V2 Double
v]



-- |A patch is a list of Located pieces (the point associated with each piece locates its originV)
-- Patches are Transformable (including translatable)
type Patch = [Located Piece]

-- | A class for things that can be turned to diagrams when given a function to draw pieces.
class Drawable a where
  drawWith :: OKBackend b =>
              (Piece ->  Diagram b) -> a ->  Diagram b

-- | Patches are drawable
instance Drawable Patch where
  drawWith :: forall b. OKBackend b => (Piece -> Diagram b) -> Patch -> Diagram b
drawWith = (Piece -> QDiagram b (V b) (N b) Any)
-> Patch -> QDiagram b (V b) (N b) Any
(Piece -> QDiagram b V2 Double Any)
-> Patch -> QDiagram b V2 Double Any
forall {a} {c}.
(V a ~ V c, N a ~ N c, Additive (V c), Num (N c), HasOrigin c,
 Monoid' c) =>
(a -> c) -> [Located a] -> c
drawPatchWith where
    -- turn a patch into a diagram using the first argument for drawing pieces.
    -- drawPatchWith:: (Piece -> Diagram B) -> Patch -> Diagram B      
      drawPatchWith :: (a -> c) -> [Located a] -> c
drawPatchWith a -> c
pd = [(Point (V c) (N c), c)] -> c
forall (v :: * -> *) n a.
(InSpace v n a, HasOrigin a, Monoid' a) =>
[(Point v n, a)] -> a
position ([(Point (V c) (N c), c)] -> c)
-> ([Located a] -> [(Point (V c) (N c), c)]) -> [Located a] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Located a -> (Point (V c) (N c), c))
-> [Located a] -> [(Point (V c) (N c), c)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Located c -> (Point (V c) (N c), c)
forall a. Located a -> (Point (V a) (N a), a)
viewLoc (Located c -> (Point (V c) (N c), c))
-> (Located a -> Located c) -> Located a -> (Point (V c) (N c), c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> c) -> Located a -> Located c
forall a b. SameSpace a b => (a -> b) -> Located a -> Located b
mapLoc a -> c
pd)

-- | the main default case for drawing using drawPiece.
draw :: (Drawable a, OKBackend b) =>
        a -> Diagram b
draw :: forall a b. (Drawable a, OKBackend b) => a -> Diagram b
draw = (Piece -> QDiagram b (V b) (N b) Any)
-> a -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => (Piece -> Diagram b) -> a -> Diagram b
forall a b.
(Drawable a, OKBackend b) =>
(Piece -> Diagram b) -> a -> Diagram b
drawWith Piece -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => Piece -> Diagram b
drawPiece

-- | alternative default case for drawing, adding dashed lines for join edges.
drawj :: (Drawable a, OKBackend b) =>
         a -> Diagram b
drawj :: forall a b. (Drawable a, OKBackend b) => a -> Diagram b
drawj = (Piece -> QDiagram b (V b) (N b) Any)
-> a -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => (Piece -> Diagram b) -> a -> Diagram b
forall a b.
(Drawable a, OKBackend b) =>
(Piece -> Diagram b) -> a -> Diagram b
drawWith Piece -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => Piece -> Diagram b
dashjPiece

fillDK, fillKD :: (Drawable a, OKBackend b, Color c1, Color c2) =>
                  c1 -> c2 -> a -> Diagram b
-- |fillDK dcol kcol a - draws and fills a with colour dcol for darts and kcol for kites.
-- Note the order D K.
-- Works with AlphaColours as well as Colours.
fillDK :: forall a b c1 c2.
(Drawable a, OKBackend b, Color c1, Color c2) =>
c1 -> c2 -> a -> Diagram b
fillDK c1
c1 c2
c2 = (Piece -> QDiagram b (V b) (N b) Any)
-> a -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => (Piece -> Diagram b) -> a -> Diagram b
forall a b.
(Drawable a, OKBackend b) =>
(Piece -> Diagram b) -> a -> Diagram b
drawWith (c1 -> c2 -> Piece -> QDiagram b (V b) (N b) Any
forall b c1 c2.
(OKBackend b, Color c1, Color c2) =>
c1 -> c2 -> Piece -> Diagram b
fillPieceDK c1
c1 c2
c2)

-- |fillKD kcol dcol a - draws and fills a with colour kcol for kites and dcol for darts.
-- Note the order K D.
-- Works with AlphaColours as well as Colours.
fillKD :: forall a b c1 c2.
(Drawable a, OKBackend b, Color c1, Color c2) =>
c1 -> c2 -> a -> Diagram b
fillKD c1
c1 c2
c2 = c2 -> c1 -> a -> QDiagram b (V b) (N b) Any
forall a b c1 c2.
(Drawable a, OKBackend b, Color c1, Color c2) =>
c1 -> c2 -> a -> Diagram b
fillDK c2
c2 c1
c1

{- {-# DEPRECATED fillMaybeDK "Use fillDK which now works with AlphaColours such as transparent" #-}
-- |fillMaybeDK *Deprecated*
-- (Use fillDK which works with AlphaColours such as transparent as well as Colours).
fillMaybeDK :: (Drawable a, OKBackend b) =>
               Maybe (Colour Double) -> Maybe (Colour Double) -> a -> Diagram b
fillMaybeDK c1 c2 = drawWith (fillMaybePieceDK c1 c2)
 -}
-- |colourDKG (c1,c2,c3) p - fill in a drawable with colour c1 for darts, colour c2 for kites and
-- colour c3 for grout (that is, the non-join edges).
-- Note the order D K G.
-- Can be used with both Colour and AlphaColour
colourDKG :: (Drawable a, OKBackend b, Color c1, Color c2, Color c3) =>
             (c1,c2,c3) -> a -> Diagram b
colourDKG :: forall a b c1 c2 c3.
(Drawable a, OKBackend b, Color c1, Color c2, Color c3) =>
(c1, c2, c3) -> a -> Diagram b
colourDKG (c1
c1,c2
c2,c3
c3) a
a = c1 -> c2 -> a -> QDiagram b (V b) (N b) Any
forall a b c1 c2.
(Drawable a, OKBackend b, Color c1, Color c2) =>
c1 -> c2 -> a -> Diagram b
fillDK c1
c1 c2
c2 a
a QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# c3 -> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall n a c.
(InSpace V2 n a, Color c, Typeable n, Floating n, HasStyle a) =>
c -> a -> a
lineColor c3
c3

{- {-# DEPRECATED colourMaybeDKG "Use colourDKG which now works with AlphaColours such as transparent" #-}
-- |colourMaybeDKG *Deprecated*
-- (Use colourDKG which works with AlphaColours such as transparent as well as Colours)
colourMaybeDKG:: (Drawable a, OKBackend b) =>
                 (Maybe (Colour Double),  Maybe (Colour Double), Maybe (Colour Double)) -> a -> Diagram b
colourMaybeDKG (d,k,g) a = fillMaybeDK d k a # maybeGrout g where
    maybeGrout (Just c) = lc c
    maybeGrout Nothing = lw none
 -}

{-|
Decomposing splits each located piece in a patch into a list of smaller located pieces to create a refined patch.
(See also decompose in Tgraph.Decompose.hs for a more abstract version of this operation).
-}
decompPatch :: Patch -> Patch
decompPatch :: Patch -> Patch
decompPatch = (Located Piece -> Patch) -> Patch -> Patch
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Located Piece -> Patch
decompPiece

-- |Decomposing a located piece to a list of (2 or 3) located pieces at smaller scale.
decompPiece :: Located Piece -> [Located Piece]
decompPiece :: Located Piece -> Patch
decompPiece Located Piece
lp = case Located Piece -> (Point (V Piece) (N Piece), Piece)
forall a. Located a -> (Point (V a) (N a), a)
viewLoc Located Piece
lp of
  (Point (V Piece) (N Piece)
p, RD V2 Double
vd)-> [ V2 Double -> Piece
forall rep. rep -> HalfTile rep
LK V2 Double
vd  Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
p
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
RD V2 Double
vd' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
v')
               ] where v' :: V2 Double
v'  = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
1) V2 Double
vd
                       vd' :: V2 Double
vd' = (Double
2Double -> Double -> Double
forall a. Num a => a -> a -> a
-Double
phi) Double -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => f a -> f a
negated V2 Double
v' -- (2-phi) = 1/phi^2
  (Point (V Piece) (N Piece)
p, LD V2 Double
vd)-> [ V2 Double -> Piece
forall rep. rep -> HalfTile rep
RK V2 Double
vd Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
p
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
LD V2 Double
vd' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
v')
               ]  where v' :: V2 Double
v'  = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
9) V2 Double
vd
                        vd' :: V2 Double
vd' = (Double
2Double -> Double -> Double
forall a. Num a => a -> a -> a
-Double
phi) Double -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => f a -> f a
negated V2 Double
v'  -- (2-phi) = 1/phi^2
  (Point (V Piece) (N Piece)
p, RK V2 Double
vk)-> [ V2 Double -> Piece
forall rep. rep -> HalfTile rep
RD V2 Double
vd' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
p
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
LK V2 Double
vk' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
v')
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
RK V2 Double
vk' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
v')
               ] where v' :: V2 Double
v'  = Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
9) V2 Double
vk
                       vd' :: V2 Double
vd' = (Double
2Double -> Double -> Double
forall a. Num a => a -> a -> a
-Double
phi) Double -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V2 Double
v'  -- (2-phi) = 1/phi^2
                       vk' :: V2 Double
vk' = ((Double
phiDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
1) Double -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V2 Double
vk) V2 Double -> V2 Double -> V2 Double
forall a. Num a => V2 a -> V2 a -> V2 a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
^-^ V2 Double
v' -- (phi-1) = 1/phi
  (Point (V Piece) (N Piece)
p, LK V2 Double
vk)-> [ V2 Double -> Piece
forall rep. rep -> HalfTile rep
LD V2 Double
vd' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
p
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
RK V2 Double
vk' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
v')
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
LK V2 Double
vk' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
v')
               ] where v' :: V2 Double
v'  = Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
1) V2 Double
vk
                       vd' :: V2 Double
vd' = (Double
2Double -> Double -> Double
forall a. Num a => a -> a -> a
-Double
phi) Double -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V2 Double
v'  -- (2-phi) = 1/phi^2
                       vk' :: V2 Double
vk' = ((Double
phiDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
1) Double -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V2 Double
vk) V2 Double -> V2 Double -> V2 Double
forall a. Num a => V2 a -> V2 a -> V2 a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
^-^ V2 Double
v' -- (phi-1) = 1/phi

-- |Create an infinite list of increasing decompositions of a patch
decompositionsP:: Patch -> [Patch]
decompositionsP :: Patch -> [Patch]
decompositionsP = (Patch -> Patch) -> Patch -> [Patch]
forall a. (a -> a) -> a -> [a]
iterate Patch -> Patch
decompPatch

{-|
compChoices applied to  a single located piece produces a list of alternative located pieces NOT a Patch.
Each of these is a larger scale single piece with a location such that when decomposed
the original piece in its original position is part of the decomposition)
-}
compChoices :: Located Piece -> [Located Piece]
compChoices :: Located Piece -> Patch
compChoices Located Piece
lp = case Located Piece -> (Point (V Piece) (N Piece), Piece)
forall a. Located a -> (Point (V a) (N a), a)
viewLoc Located Piece
lp of
  (Point (V Piece) (N Piece)
p, RD V2 Double
vd)-> [ V2 Double -> Piece
forall rep. rep -> HalfTile rep
RD V2 Double
vd' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
v')
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
RK V2 Double
vk  Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
p
               ] where v' :: V2 Double
v'  = (Double
phiDouble -> Double -> Double
forall a. Num a => a -> a -> a
+Double
1) Double -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V2 Double
vd                  -- vd*phi^2
                       vd' :: V2 Double
vd' = Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
9) (V2 Double
vd V2 Double -> V2 Double -> V2 Double
forall a. Num a => V2 a -> V2 a -> V2 a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
^-^ V2 Double
v')
                       vk :: V2 Double
vk  = Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
1) V2 Double
v'
  (Point (V Piece) (N Piece)
p, LD V2 Double
vd)-> [ V2 Double -> Piece
forall rep. rep -> HalfTile rep
LD V2 Double
vd' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
v')
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
LK V2 Double
vk Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
p
               ] where v' :: V2 Double
v'  = (Double
phiDouble -> Double -> Double
forall a. Num a => a -> a -> a
+Double
1) Double -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ V2 Double
vd                  -- vd*phi^2
                       vd' :: V2 Double
vd' = Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
1) (V2 Double
vd V2 Double -> V2 Double -> V2 Double
forall a. Num a => V2 a -> V2 a -> V2 a
forall (f :: * -> *) a. (Additive f, Num a) => f a -> f a -> f a
^-^ V2 Double
v')
                       vk :: V2 Double
vk  = Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
9) V2 Double
v'
  (Point (V Piece) (N Piece)
p, RK V2 Double
vk)-> [ V2 Double -> Piece
forall rep. rep -> HalfTile rep
LD V2 Double
vk  Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
p
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
LK V2 Double
lvk' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
lv') 
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
RK V2 Double
rvk' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
rv')
               ] where lv' :: V2 Double
lv'  = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
9) V2 Double
vk
                       rv' :: V2 Double
rv'  = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
1) V2 Double
vk
                       rvk' :: V2 Double
rvk' = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
7) V2 Double
vk
                       lvk' :: V2 Double
lvk' = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
3) V2 Double
vk
  (Point (V Piece) (N Piece)
p, LK V2 Double
vk)-> [ V2 Double -> Piece
forall rep. rep -> HalfTile rep
RD V2 Double
vk  Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
p
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
RK V2 Double
rvk' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
rv')
               , V2 Double -> Piece
forall rep. rep -> HalfTile rep
LK V2 Double
lvk' Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` (Point (V Piece) (N Piece)
Point V2 Double
p Point V2 Double -> Diff (Point V2) Double -> Point V2 Double
forall a. Num a => Point V2 a -> Diff (Point V2) a -> Point V2 a
forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ V2 Double
Diff (Point V2) Double
lv')
               ] where lv' :: V2 Double
lv'  = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
9) V2 Double
vk
                       rv' :: V2 Double
rv'  = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
1) V2 Double
vk
                       rvk' :: V2 Double
rvk' = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
7) V2 Double
vk
                       lvk' :: V2 Double
lvk' = Double
phiDouble -> V2 Double -> V2 Double
forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^Angle Double -> V2 Double -> V2 Double
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
3) V2 Double
vk

-- |compNChoices n lp - gives a list of all the alternatives after n compChoices starting with lp
-- Note that the result is not a Patch as the list represents alternatives.
compNChoices :: Int -> Located Piece -> [Located Piece]
compNChoices :: Int -> Located Piece -> Patch
compNChoices Int
0 Located Piece
lp = [Located Piece
lp]
compNChoices Int
n Located Piece
lp = do
    Located Piece
lp' <- Located Piece -> Patch
compChoices Located Piece
lp
    Int -> Located Piece -> Patch
compNChoices (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Located Piece
lp'


                                
-- |combine 5 copies of a patch (each rotated by ttangle 2 successively)
-- (ttAngle 2 is 72 degrees) 
-- Must be used with care to avoid creating a nonsense patch
penta:: Patch -> Patch
penta :: Patch -> Patch
penta Patch
p = (Int -> Patch) -> [Int] -> Patch
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Int -> Patch
copy [Int
0..Int
4] 
            where copy :: Int -> Patch
copy Int
n = Angle Double -> Patch -> Patch
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n)) Patch
p
  
sun,star::Patch         
-- |sun is a patch with five kites sharing common origin (base of kite)
sun :: Patch
sun =  Patch -> Patch
penta [Piece
rkite Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
Point V2 Double
forall (f :: * -> *) a. (Additive f, Num a) => Point f a
origin, Piece
lkite Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
Point V2 Double
forall (f :: * -> *) a. (Additive f, Num a) => Point f a
origin]
-- |star is a patch with five darts sharing common origin (tip of dart)
star :: Patch
star = Patch -> Patch
penta [Piece
rdart Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
Point V2 Double
forall (f :: * -> *) a. (Additive f, Num a) => Point f a
origin, Piece
ldart Piece -> Point (V Piece) (N Piece) -> Located Piece
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V Piece) (N Piece)
Point V2 Double
forall (f :: * -> *) a. (Additive f, Num a) => Point f a
origin]


-- |An infinite list of patches of increasingly decomposed sun
suns::[Patch]
suns :: [Patch]
suns = Patch -> [Patch]
decompositionsP Patch
sun
sun5,sun6:: Patch
-- |a patch of a 6 times decomposed sun
sun6 :: Patch
sun6 = [Patch]
suns[Patch] -> Int -> Patch
forall a. HasCallStack => [a] -> Int -> a
!!Int
6
-- |a patch of a 5 times decomposed sun
sun5 :: Patch
sun5 = [Patch]
suns[Patch] -> Int -> Patch
forall a. HasCallStack => [a] -> Int -> a
!!Int
5 


   -- * Diagrams of Patches

-- |diagram for sun6.
sun6Fig :: OKBackend b => Diagram b
sun6Fig :: forall b. OKBackend b => Diagram b
sun6Fig = Patch -> QDiagram b (V b) (N b) Any
forall a b. (Drawable a, OKBackend b) => a -> Diagram b
draw Patch
sun6 QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# Measure Double
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall a n.
(N a ~ n, HasStyle a, Typeable n) =>
Measure n -> a -> a
lw Measure Double
forall n. OrderedField n => Measure n
thin


-- |Colour filled using leftFillPieceDK. 
leftFilledSun6 :: OKBackend b => Diagram b
leftFilledSun6 :: forall b. OKBackend b => Diagram b
leftFilledSun6 = (Piece -> QDiagram b (V b) (N b) Any)
-> Patch -> QDiagram b (V b) (N b) Any
forall b. OKBackend b => (Piece -> Diagram b) -> Patch -> Diagram b
forall a b.
(Drawable a, OKBackend b) =>
(Piece -> Diagram b) -> a -> Diagram b
drawWith (Colour Double
-> Colour Double -> Piece -> QDiagram b (V b) (N b) Any
forall b c1 c2.
(OKBackend b, Color c1, Color c2) =>
c1 -> c2 -> Piece -> Diagram b
leftFillPieceDK Colour Double
forall a. (Ord a, Floating a) => Colour a
red Colour Double
forall a. (Ord a, Floating a) => Colour a
blue) Patch
sun6 QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# Measure Double
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall a n.
(N a ~ n, HasStyle a, Typeable n) =>
Measure n -> a -> a
lw Measure Double
forall n. OrderedField n => Measure n
thin

-- |Colour filled using fillDK.
filledSun6 :: OKBackend b => Diagram b
filledSun6 :: forall b. OKBackend b => Diagram b
filledSun6 = Colour Double
-> Colour Double -> Patch -> QDiagram b (V b) (N b) Any
forall a b c1 c2.
(Drawable a, OKBackend b, Color c1, Color c2) =>
c1 -> c2 -> a -> Diagram b
fillDK Colour Double
forall a. (Ord a, Floating a) => Colour a
darkmagenta Colour Double
forall a. (Ord a, Floating a) => Colour a
indigo Patch
sun6 QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# Measure Double
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall a n.
(N a ~ n, HasStyle a, Typeable n) =>
Measure n -> a -> a
lw Measure Double
forall n. OrderedField n => Measure n
thin QDiagram b V2 Double Any
-> (QDiagram b V2 Double Any -> QDiagram b V2 Double Any)
-> QDiagram b V2 Double Any
forall a b. a -> (a -> b) -> b
# Colour Double
-> QDiagram b V2 Double Any -> QDiagram b V2 Double Any
forall n a.
(InSpace V2 n a, Typeable n, Floating n, HasStyle a) =>
Colour Double -> a -> a
lc Colour Double
forall a. (Ord a, Floating a) => Colour a
gold


-- |rotations takes a list of integers (representing ttangles) for respective rotations of items in the second list (things to be rotated).
-- This includes Diagrams, Patches, VPatches.
-- The integer list can be shorter than the list of items - the remaining items are left unrotated.
-- It will raise an error if the integer list is longer than the list of items to be rotated.
-- (Rotations by an angle are anti-clockwise)
rotations :: (Transformable a, V a ~ V2, N a ~ Double) => [Int] -> [a] -> [a]
rotations :: forall a.
(Transformable a, V a ~ V2, N a ~ Double) =>
[Int] -> [a] -> [a]
rotations (Int
n:[Int]
ns) (a
d:[a]
ds) = Angle Double -> a -> a
forall n t.
(InSpace V2 n t, Transformable t, Floating n) =>
Angle n -> t -> t
rotate (Int -> Angle Double
ttangle Int
n) a
da -> [a] -> [a]
forall a. a -> [a] -> [a]
: [Int] -> [a] -> [a]
forall a.
(Transformable a, V a ~ V2, N a ~ Double) =>
[Int] -> [a] -> [a]
rotations [Int]
ns [a]
ds
rotations [] [a]
ds = [a]
ds
rotations [Int]
_  [] = [Char] -> [a]
forall a. HasCallStack => [Char] -> a
error [Char]
"rotations: too many rotation integers"

-- |scales takes a list of doubles for respective scalings of items in the second list (things to be scaled).
-- This includes Diagrams, Pieces, Patches, VPatches.
-- The list of doubles can be shorter than the list of items - the remaining items are left unscaled.
-- It will raise an error if the integer list is longer than the list of items to be scaled.
scales :: (Transformable a, V a ~ V2, N a ~ Double) => [Double] -> [a] -> [a]
scales :: forall a.
(Transformable a, V a ~ V2, N a ~ Double) =>
[Double] -> [a] -> [a]
scales (Double
s:[Double]
ss) (a
d:[a]
ds) = Double -> a -> a
forall (v :: * -> *) n a.
(InSpace v n a, Eq n, Fractional n, Transformable a) =>
n -> a -> a
scale Double
s a
da -> [a] -> [a]
forall a. a -> [a] -> [a]
: [Double] -> [a] -> [a]
forall a.
(Transformable a, V a ~ V2, N a ~ Double) =>
[Double] -> [a] -> [a]
scales [Double]
ss [a]
ds
scales [] [a]
ds = [a]
ds
scales [Double]
_  [] = [Char] -> [a]
forall a. HasCallStack => [Char] -> a
error [Char]
"scales: too many scalars"

-- |increasing scales by a factor of phi along a list starting with 1.
phiScales:: (Transformable a, V a ~ V2, N a ~ Double) => [a] -> [a]
phiScales :: forall a. (Transformable a, V a ~ V2, N a ~ Double) => [a] -> [a]
phiScales = Double -> [a] -> [a]
forall a.
(Transformable a, V a ~ V2, N a ~ Double) =>
Double -> [a] -> [a]
phiScaling Double
1

-- |increasing scales by a factor of phi along a list starting with given first argument
phiScaling:: (Transformable a, V a ~ V2, N a ~ Double) => Double -> [a] -> [a]
phiScaling :: forall a.
(Transformable a, V a ~ V2, N a ~ Double) =>
Double -> [a] -> [a]
phiScaling Double
_ [] = []
phiScaling Double
s (a
d:[a]
more) = Double -> a -> a
forall (v :: * -> *) n a.
(InSpace v n a, Eq n, Fractional n, Transformable a) =>
n -> a -> a
scale Double
s a
da -> [a] -> [a]
forall a. a -> [a] -> [a]
: Double -> [a] -> [a]
forall a.
(Transformable a, V a ~ V2, N a ~ Double) =>
Double -> [a] -> [a]
phiScaling (Double
phiDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
s) [a]
more