Skip to content

Commit

Permalink
Fix catmull-rom for 3 points
Browse files Browse the repository at this point in the history
And a bunch of other edge-cases with 0,1,2 or 3 points for the various
curve points. A note in tests/CurveTest details why we produce empty
string where D3 produces `Mx,y Z`.
  • Loading branch information
folkertdev committed Nov 9, 2018
1 parent 773e70b commit 527425c
Show file tree
Hide file tree
Showing 2 changed files with 289 additions and 31 deletions.
37 changes: 29 additions & 8 deletions src/Curve.elm
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,23 @@ basisClosed points =
SubPath.with (moveTo start)
[ cubicCurveTo (commonCase [] closing (p3 :: p4 :: rest)) ]

[ p0, p1 ] ->
SubPath.with (moveTo p0) [ lineTo [ p1 ] ]
[ p0_, p1_ ] ->
let
p0 =
Vector2d.fromComponents p0_

p1 =
Vector2d.fromComponents p1_

start =
Vector2d.scaleBy (1 / 3) (Vector2d.sum p0 (Vector2d.scaleBy 2 p1))
|> Vector2d.components

end =
Vector2d.scaleBy (1 / 3) (Vector2d.sum p1 (Vector2d.scaleBy 2 p0))
|> Vector2d.components
in
SubPath.with (moveTo start) [ lineTo [ end ], closePath ]

_ ->
empty
Expand All @@ -378,7 +393,7 @@ basisOpen points =
List.reverse acc
in
case points of
p0 :: p1 :: p :: rest ->
p0 :: p1 :: p :: pp :: rest ->
let
start =
Vector2d.fromComponents p0
Expand All @@ -387,7 +402,7 @@ basisOpen points =
|> Vector2d.scaleBy (1 / 6)
|> Vector2d.components
in
SubPath.with (moveTo start) [ cubicCurveTo (helper [] (p1 :: p :: rest)) ]
SubPath.with (moveTo start) [ cubicCurveTo (helper [] (p1 :: p :: pp :: rest)) ]

_ ->
empty
Expand All @@ -401,6 +416,12 @@ bundle beta points =
[] ->
empty

[ _ ] ->
empty

[ _, _ ] ->
empty

p0 :: rest ->
let
j =
Expand Down Expand Up @@ -523,7 +544,7 @@ cardinalClosed tension points =
empty

[ p ] ->
SubPath.with (moveTo p) [ closePath ]
empty

[ p0, p1 ] ->
SubPath.with (moveTo p1) [ lineTo [ p0 ], closePath ]
Expand Down Expand Up @@ -560,7 +581,7 @@ catmullRom alpha points =
[ p1, p2 ] ->
SubPath.with (moveTo p1) [ lineTo [ p2 ] ]

p0 :: p1 :: p2 :: p :: rest ->
p0 :: p1 :: p2 :: rest ->
let
ending q0 q1 q2 =
[ catmullRomPoint alpha q0 q1 q2 q2 ]
Expand Down Expand Up @@ -599,7 +620,7 @@ catmullRomOpen alpha points =
else
case points of
[ p0, p1, p2 ] ->
SubPath.with (moveTo p2) []
SubPath.with (moveTo p1) [ closePath ]

p0 :: p1 :: p2 :: p :: rest ->
let
Expand All @@ -625,7 +646,7 @@ catmullRomClosed alpha points =
empty

[ p ] ->
SubPath.with (moveTo p) []
empty

[ p1, p2 ] ->
SubPath.with (moveTo p2) [ lineTo [ p1 ], closePath ]
Expand Down
Loading

0 comments on commit 527425c

Please sign in to comment.