Skip to content

Commit

Permalink
calculate arrow shaftScale using projection of ends onto shaft offset
Browse files Browse the repository at this point in the history
  • Loading branch information
bergey committed Oct 10, 2013
1 parent 6b34bc3 commit ee1057f
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Diagrams/TwoD/Arrow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ spine tr tw hw size = tS <> shaft <> hS
-- | Calculate the amount required to scale a shaft trail so that an arrow with
-- head width hw and tail width tw has offset t.
shaftScale :: Trail R2 -> Double -> Double -> Double -> Double
shaftScale tr tw hw t = magnitude (end .-. start) / magnitude (trailOffset tr)
shaftScale tr tw hw t = (t - startOffset - endOffset) / magnitude (trailOffset tr)
where
tAngle = direction . startTangent $ (head $ trailSegments tr) :: Turn
hAngle = direction . endTangent $ (last $ trailSegments tr) :: Turn
start = origin .+^ (hw *^ fromDirection hAngle)
end = origin .+^ (t *^ unitX) .-^ (tw *^ fromDirection tAngle)
tSpine = normalized . startTangent $ (head $ trailSegments tr)
hSpine = normalized . endTangent $ (last $ trailSegments tr)
startOffset = hw *^ hSpine <.> normalized (trailOffset tr)
endOffset = tw *^ tSpine <.> normalized (trailOffset tr)

-- | @arrow len@ creates an arrow of length @len@ with default parameters.
arrow :: Renderable (Path R2) b => Double -> Diagram b R2
Expand Down

1 comment on commit ee1057f

@jeffreyrosenbluth
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to push this alternative.

scaleFactor :: Trail R2 -> Double -> Double -> Double -> Double
scaleFactor tr tw hw t = magnitude (end .-. start) / magnitude (trailOffset tr)
  where
    tAngle = direction . startTangent $ (head $ trailSegments tr) :: Turn
    hAngle = direction . endTangent $ (last $ trailSegments tr) :: Turn
    start  = origin .+^ (hw *^ fromDirection hAngle)
    end    = origin .+^ v .-^ (tw *^ fromDirection tAngle)
    v       = (normalized .trailOffset) tr # scale t

Either way is fine with me. I think @bergey 's is cleaner.

Please sign in to comment.