module XMonad.Util.Rectangle
(
PointRectangle (..)
, pixelsToIndices, pixelsToCoordinates
, indicesToRectangle, coordinatesToRectangle
, empty
, intersects
, supersetOf
, difference
, withBorder
, center
, toRatio
) where
import XMonad
import XMonad.Prelude (fi)
import qualified XMonad.StackSet as W
import Data.Ratio
data PointRectangle a = PointRectangle
{ forall a. PointRectangle a -> a
point_x1::a
, forall a. PointRectangle a -> a
point_y1::a
, forall a. PointRectangle a -> a
point_x2::a
, forall a. PointRectangle a -> a
point_y2::a
} deriving (PointRectangle a -> PointRectangle a -> Bool
forall a. Eq a => PointRectangle a -> PointRectangle a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PointRectangle a -> PointRectangle a -> Bool
$c/= :: forall a. Eq a => PointRectangle a -> PointRectangle a -> Bool
== :: PointRectangle a -> PointRectangle a -> Bool
$c== :: forall a. Eq a => PointRectangle a -> PointRectangle a -> Bool
Eq,ReadPrec [PointRectangle a]
ReadPrec (PointRectangle a)
ReadS [PointRectangle a]
forall a. Read a => ReadPrec [PointRectangle a]
forall a. Read a => ReadPrec (PointRectangle a)
forall a. Read a => Int -> ReadS (PointRectangle a)
forall a. Read a => ReadS [PointRectangle a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PointRectangle a]
$creadListPrec :: forall a. Read a => ReadPrec [PointRectangle a]
readPrec :: ReadPrec (PointRectangle a)
$creadPrec :: forall a. Read a => ReadPrec (PointRectangle a)
readList :: ReadS [PointRectangle a]
$creadList :: forall a. Read a => ReadS [PointRectangle a]
readsPrec :: Int -> ReadS (PointRectangle a)
$creadsPrec :: forall a. Read a => Int -> ReadS (PointRectangle a)
Read,Int -> PointRectangle a -> ShowS
forall a. Show a => Int -> PointRectangle a -> ShowS
forall a. Show a => [PointRectangle a] -> ShowS
forall a. Show a => PointRectangle a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PointRectangle a] -> ShowS
$cshowList :: forall a. Show a => [PointRectangle a] -> ShowS
show :: PointRectangle a -> String
$cshow :: forall a. Show a => PointRectangle a -> String
showsPrec :: Int -> PointRectangle a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> PointRectangle a -> ShowS
Show)
pixelsToIndices :: Rectangle -> PointRectangle Integer
pixelsToIndices :: Rectangle -> PointRectangle Integer
pixelsToIndices (Rectangle Position
px Position
py Dimension
dx Dimension
dy) =
forall a. a -> a -> a -> a -> PointRectangle a
PointRectangle (forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
px)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
py)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
px forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
dx forall a. Num a => a -> a -> a
- Integer
1)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
py forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
dy forall a. Num a => a -> a -> a
- Integer
1)
pixelsToCoordinates :: Rectangle -> PointRectangle Integer
pixelsToCoordinates :: Rectangle -> PointRectangle Integer
pixelsToCoordinates (Rectangle Position
px Position
py Dimension
dx Dimension
dy) =
forall a. a -> a -> a -> a -> PointRectangle a
PointRectangle (forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
px)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
py)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
px forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
dx)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
py forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
dy)
indicesToRectangle :: PointRectangle Integer -> Rectangle
indicesToRectangle :: PointRectangle Integer -> Rectangle
indicesToRectangle (PointRectangle Integer
x1 Integer
y1 Integer
x2 Integer
y2) =
Position -> Position -> Dimension -> Dimension -> Rectangle
Rectangle (forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x1)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
y1)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Integer
x2 forall a. Num a => a -> a -> a
- Integer
x1 forall a. Num a => a -> a -> a
+ Integer
1)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Integer
y2 forall a. Num a => a -> a -> a
- Integer
y1 forall a. Num a => a -> a -> a
+ Integer
1)
coordinatesToRectangle :: PointRectangle Integer -> Rectangle
coordinatesToRectangle :: PointRectangle Integer -> Rectangle
coordinatesToRectangle (PointRectangle Integer
x1 Integer
y1 Integer
x2 Integer
y2) =
Position -> Position -> Dimension -> Dimension -> Rectangle
Rectangle (forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x1)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
y1)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Integer
x2 forall a. Num a => a -> a -> a
- Integer
x1)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Integer
y2 forall a. Num a => a -> a -> a
- Integer
y1)
empty :: Rectangle -> Bool
empty :: Rectangle -> Bool
empty (Rectangle Position
_ Position
_ Dimension
_ Dimension
0) = Bool
True
empty (Rectangle Position
_ Position
_ Dimension
0 Dimension
_) = Bool
True
empty Rectangle{} = Bool
False
intersects :: Rectangle -> Rectangle -> Bool
intersects :: Rectangle -> Rectangle -> Bool
intersects Rectangle
r1 Rectangle
r2 | Rectangle -> Bool
empty Rectangle
r1 Bool -> Bool -> Bool
|| Rectangle -> Bool
empty Rectangle
r2 = Bool
False
| Bool
otherwise = Integer
r1_x1 forall a. Ord a => a -> a -> Bool
< Integer
r2_x2
Bool -> Bool -> Bool
&& Integer
r1_x2 forall a. Ord a => a -> a -> Bool
> Integer
r2_x1
Bool -> Bool -> Bool
&& Integer
r1_y1 forall a. Ord a => a -> a -> Bool
< Integer
r2_y2
Bool -> Bool -> Bool
&& Integer
r1_y2 forall a. Ord a => a -> a -> Bool
> Integer
r2_y1
where PointRectangle Integer
r1_x1 Integer
r1_y1 Integer
r1_x2 Integer
r1_y2 = Rectangle -> PointRectangle Integer
pixelsToCoordinates Rectangle
r1
PointRectangle Integer
r2_x1 Integer
r2_y1 Integer
r2_x2 Integer
r2_y2 = Rectangle -> PointRectangle Integer
pixelsToCoordinates Rectangle
r2
supersetOf :: Rectangle -> Rectangle -> Bool
supersetOf :: Rectangle -> Rectangle -> Bool
supersetOf Rectangle
r1 Rectangle
r2 = Integer
r1_x1 forall a. Ord a => a -> a -> Bool
<= Integer
r2_x1
Bool -> Bool -> Bool
&& Integer
r1_y1 forall a. Ord a => a -> a -> Bool
<= Integer
r2_y1
Bool -> Bool -> Bool
&& Integer
r1_x2 forall a. Ord a => a -> a -> Bool
>= Integer
r2_x2
Bool -> Bool -> Bool
&& Integer
r1_y2 forall a. Ord a => a -> a -> Bool
>= Integer
r2_y2
where PointRectangle Integer
r1_x1 Integer
r1_y1 Integer
r1_x2 Integer
r1_y2 = Rectangle -> PointRectangle Integer
pixelsToCoordinates Rectangle
r1
PointRectangle Integer
r2_x1 Integer
r2_y1 Integer
r2_x2 Integer
r2_y2 = Rectangle -> PointRectangle Integer
pixelsToCoordinates Rectangle
r2
difference :: Rectangle -> Rectangle -> [Rectangle]
difference :: Rectangle -> Rectangle -> [Rectangle]
difference Rectangle
r1 Rectangle
r2 | Rectangle
r1 Rectangle -> Rectangle -> Bool
`intersects` Rectangle
r2 = forall a b. (a -> b) -> [a] -> [b]
map PointRectangle Integer -> Rectangle
coordinatesToRectangle forall a b. (a -> b) -> a -> b
$
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[PointRectangle Integer]
rt,[PointRectangle Integer]
rr,[PointRectangle Integer]
rb,[PointRectangle Integer]
rl]
| Bool
otherwise = [Rectangle
r1]
where PointRectangle Integer
r1_x1 Integer
r1_y1 Integer
r1_x2 Integer
r1_y2 = Rectangle -> PointRectangle Integer
pixelsToCoordinates Rectangle
r1
PointRectangle Integer
r2_x1 Integer
r2_y1 Integer
r2_x2 Integer
r2_y2 = Rectangle -> PointRectangle Integer
pixelsToCoordinates Rectangle
r2
rt :: [PointRectangle Integer]
rt = [forall a. a -> a -> a -> a -> PointRectangle a
PointRectangle (forall a. Ord a => a -> a -> a
max Integer
r2_x1 Integer
r1_x1) Integer
r1_y1 Integer
r1_x2 Integer
r2_y1 | Integer
r2_y1 forall a. Ord a => a -> a -> Bool
> Integer
r1_y1 Bool -> Bool -> Bool
&& Integer
r2_y1 forall a. Ord a => a -> a -> Bool
< Integer
r1_y2]
rr :: [PointRectangle Integer]
rr = [forall a. a -> a -> a -> a -> PointRectangle a
PointRectangle Integer
r2_x2 (forall a. Ord a => a -> a -> a
max Integer
r2_y1 Integer
r1_y1) Integer
r1_x2 Integer
r1_y2 | Integer
r2_x2 forall a. Ord a => a -> a -> Bool
> Integer
r1_x1 Bool -> Bool -> Bool
&& Integer
r2_x2 forall a. Ord a => a -> a -> Bool
< Integer
r1_x2]
rb :: [PointRectangle Integer]
rb = [forall a. a -> a -> a -> a -> PointRectangle a
PointRectangle Integer
r1_x1 Integer
r2_y2 (forall a. Ord a => a -> a -> a
min Integer
r2_x2 Integer
r1_x2) Integer
r1_y2 | Integer
r2_y2 forall a. Ord a => a -> a -> Bool
> Integer
r1_y1 Bool -> Bool -> Bool
&& Integer
r2_y2 forall a. Ord a => a -> a -> Bool
< Integer
r1_y2]
rl :: [PointRectangle Integer]
rl = [forall a. a -> a -> a -> a -> PointRectangle a
PointRectangle Integer
r1_x1 Integer
r1_y1 Integer
r2_x1 (forall a. Ord a => a -> a -> a
min Integer
r2_y2 Integer
r1_y2) | Integer
r2_x1 forall a. Ord a => a -> a -> Bool
> Integer
r1_x1 Bool -> Bool -> Bool
&& Integer
r2_x1 forall a. Ord a => a -> a -> Bool
< Integer
r1_x2]
withBorder :: Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> Rectangle -> Rectangle
withBorder :: Integer
-> Integer
-> Integer
-> Integer
-> Integer
-> Rectangle
-> Rectangle
withBorder Integer
t Integer
b Integer
r Integer
l Integer
i (Rectangle Position
x Position
y Dimension
w Dimension
h) =
let
w' :: Integer
w' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
w
h' :: Integer
h' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
h
i' :: Integer
i' = forall a. Ord a => a -> a -> a
max Integer
i Integer
0
iw :: Integer
iw = forall a. Ord a => a -> a -> a
min Integer
i' Integer
w'
ih :: Integer
ih = forall a. Ord a => a -> a -> a
min Integer
i' Integer
h'
bh :: Integer
bh = Integer
w' forall a. Num a => a -> a -> a
- Integer
iw
bv :: Integer
bv = Integer
h' forall a. Num a => a -> a -> a
- Integer
ih
rh :: Rational
rh = if Integer
l forall a. Num a => a -> a -> a
+ Integer
r forall a. Ord a => a -> a -> Bool
<= Integer
0
then Rational
1
else forall a. Ord a => a -> a -> a
min Rational
1 forall a b. (a -> b) -> a -> b
$ Integer
bh forall a. Integral a => a -> a -> Ratio a
% (Integer
l forall a. Num a => a -> a -> a
+ Integer
r)
rv :: Rational
rv = if Integer
t forall a. Num a => a -> a -> a
+ Integer
b forall a. Ord a => a -> a -> Bool
<= Integer
0
then Rational
1
else forall a. Ord a => a -> a -> a
min Rational
1 forall a b. (a -> b) -> a -> b
$ Integer
bv forall a. Integral a => a -> a -> Ratio a
% (Integer
t forall a. Num a => a -> a -> a
+ Integer
b)
t' :: Position
t' = forall a b. (RealFrac a, Integral b) => a -> b
truncate forall a b. (a -> b) -> a -> b
$ Rational
rv forall a. Num a => a -> a -> a
* forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
t
b' :: Dimension
b' = forall a b. (RealFrac a, Integral b) => a -> b
truncate forall a b. (a -> b) -> a -> b
$ Rational
rv forall a. Num a => a -> a -> a
* forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
b
r' :: Dimension
r' = forall a b. (RealFrac a, Integral b) => a -> b
truncate forall a b. (a -> b) -> a -> b
$ Rational
rh forall a. Num a => a -> a -> a
* forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
r
l' :: Position
l' = forall a b. (RealFrac a, Integral b) => a -> b
truncate forall a b. (a -> b) -> a -> b
$ Rational
rh forall a. Num a => a -> a -> a
* forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
l
in Position -> Position -> Dimension -> Dimension -> Rectangle
Rectangle (Position
x forall a. Num a => a -> a -> a
+ Position
l')
(Position
y forall a. Num a => a -> a -> a
+ Position
t')
(Dimension
w forall a. Num a => a -> a -> a
- Dimension
r' forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
l')
(Dimension
h forall a. Num a => a -> a -> a
- Dimension
b' forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
t')
center :: Rectangle -> (Ratio Integer,Ratio Integer)
center :: Rectangle -> (Rational, Rational)
center (Rectangle Position
x Position
y Dimension
w Dimension
h) = (Rational
cx,Rational
cy)
where cx :: Rational
cx = forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
x forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
w forall a. Integral a => a -> a -> Ratio a
% Integer
2
cy :: Rational
cy = forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
y forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
h forall a. Integral a => a -> a -> Ratio a
% Integer
2
toRatio :: Rectangle -> Rectangle -> W.RationalRect
toRatio :: Rectangle -> Rectangle -> RationalRect
toRatio (Rectangle Position
x1 Position
y1 Dimension
w1 Dimension
h1) (Rectangle Position
x2 Position
y2 Dimension
w2 Dimension
h2) =
Rational -> Rational -> Rational -> Rational -> RationalRect
W.RationalRect ((forall a b. (Integral a, Num b) => a -> b
fi Position
x1 forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fi Position
x2) forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fi Dimension
w2)
((forall a b. (Integral a, Num b) => a -> b
fi Position
y1 forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fi Position
y2) forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fi Dimension
h2)
(forall a b. (Integral a, Num b) => a -> b
fi Dimension
w1 forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fi Dimension
w2) (forall a b. (Integral a, Num b) => a -> b
fi Dimension
h1 forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fi Dimension
h2)