import Data.Ratio
main
= mapM_ printIntersection ls
where
ls =
[
(line (0, 8) (4, 0), line (2, 4) (8, 12))
, (line (0, 0) (1, 2), line (1, 1) (2, 3))
, (line (-9, -9) (1, 1), line (5, 0) (5, 3))
, (line (3, 4) (6, 7), line (3, 8) (9, -1))
]
printIntersection
:: (Line
, Line
) -> IO () where
(x, y) = intersection l m
line :: Point -> Point -> Line
line (x1, y1) (x2, y2)
where
slope = dy / dx
dx = x2 - x1
dy = y2 - y1
intersection :: Line -> Line -> Point
intersection l@(a, b, p) m@(c, d, q)
where
x = invd * p + invb * q
y
| 0 `
elem`
[a
, b
] = c
* x
+ q
| 0 `
elem`
[c
, d
] = a
* x
+ p
[inva
, invb
, invc
, invd
] = map (/ det l m
) [a
, -b
, -c
, d
] nan = 0 / 0
parallel
:: Line
-> Line
-> Boolparallel l m = det l m == (0 % 1)
det
:: Line
-> Line
-> Ratio
Integerdet (a, b, _) (c, d, _) = a * d - b * c
aW1wb3J0IERhdGEuUmF0aW8KCm1haW4gOjogSU8gKCkKbWFpbiA9IG1hcE1fIHByaW50SW50ZXJzZWN0aW9uIGxzCiAgd2hlcmUKICAgIGxzID0KICAgICAgWwogICAgICAgIChsaW5lICgwLCA4KSAoNCwgMCksIGxpbmUgKDIsIDQpICg4LCAxMikpCiAgICAgICwgKGxpbmUgKDAsIDApICgxLCAyKSwgbGluZSAoMSwgMSkgKDIsIDMpKQogICAgICAsIChsaW5lICgtOSwgLTkpICgxLCAxKSwgbGluZSAoNSwgMCkgKDUsIDMpKQogICAgICAsIChsaW5lICgzLCA0KSAoNiwgNyksIGxpbmUgKDMsIDgpICg5LCAtMSkpCiAgICAgIF0KCnByaW50SW50ZXJzZWN0aW9uIDo6IChMaW5lLCBMaW5lKSAtPiBJTyAoKQpwcmludEludGVyc2VjdGlvbiAobCwgbSkgPSBwdXRTdHJMbiBzCiAgd2hlcmUKICAgIHMgfCBhbnkgaXNOYU4gW3gsIHldID0gIuS6pOeCueOBquOBlyIKICAgICAg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