diagrams / diagrams-lib

Diagrams standard library
https://diagrams.github.io/
Other
138 stars 62 forks source link

sample :: _ -> _ -> Crossings producing incorrect result #337

Open bacchanalia opened 5 years ago

bacchanalia commented 5 years ago

l1/r1 are equivalent to l0/r0 except the straight segments get converted to cubic segments by B.union. They draw and fill correctly, so I think it's a problem with (sample :: -> -> Crossings) rather than a problem with B.union.

import Diagrams.Prelude
import qualified Diagrams.TwoD.Path.Boolean as B

l0, r0 :: Located (Trail V2 Double)
l0 = square 1
r0 = square 1 # translateX 2
[l1, r1] = pathTrails $ B.union Winding $ toPath [l0,r0]

main = do
  putStrLn $ "r0/l0: " ++ show (sample r0 (atStart l0)) -- Expected: 0, Actual:  0
  putStrLn $ "r1/l0: " ++ show (sample r1 (atStart l1)) -- Expected: 0, Actual: -1
bacchanalia commented 5 years ago

One problem is that filter (liftA2 (&&) (>=0) (<=1)) in trailCrossings from Diagrams.TwoD.Path should be filter (liftA2 (&&) (>=(-eps)) (<=(1+eps)), but there is still another problem because given

[p] = pathTrails $ B.union Winding (square 2)
crossingYIsMinus1 = trailCrossings (p2 (-2, -1)) p
crossingYIsPlus1  = trailCrossings (p2 (-2,  1)) p

crossingYIsMinus1 now produces the expected result, but crossingYIsPlus1 still does not.

edit: The param is also compared to 0 or 1 in signFromDerivAt. Loosening those comparisons fixes the example, but might possibly break other things. It requires further consideration.