Anonymous user
Find if a point is within a triangle: Difference between revisions
Find if a point is within a triangle (view source)
Revision as of 09:52, 2 December 2021
, 2 years ago→{{header|Haskell}}: added visual test
(→{{header|Haskell}}: added Haskell solution) |
(→{{header|Haskell}}: added visual test) |
||
Line 913:
(s, (x, y)) = toTriangle t p
in case () of
() | s == 0 && (x == 0
| s == 0 -> Outside
| x > 0 && y > 0 && y < s - x -> Inside
| (x
(x == 0 || y == 0 || y == s - x) -> Boundary
| otherwise -> Outside</lang>
Testing
Line 932 ⟶ 934:
, overlapping t1 <$> is
, overlapping t1 <$> os
, overlapping t2 <$> ps]
test2 = unlines
[ [case overlapping t (i,j) of
Inside -> '∗'
Boundary -> '+'
Outside -> '·'
| i <- [-10..10] :: [Int] ]
| j <- [-5..5] :: [Int] ]
where t = Triangle (-8,-3) (8,1) (-1,4)</lang>
<pre>λ> tests
Line 938 ⟶ 948:
[Inside,Inside,Inside,Inside,Inside]
[Outside,Outside,Outside,Outside]
[Boundary,Boundary,Outside,Outside]
λ> putStrLn test2
·····················
·····················
··+··················
···+∗∗+··············
····+∗∗∗∗∗+··········
·····+∗∗∗∗∗∗∗∗+······
······+∗∗∗∗∗∗∗∗∗∗∗+··
·······+∗∗∗∗∗∗∗+·····
········+∗∗∗+········
·········+···········
·····················</pre>
=={{header|Java}}==
|