Created
December 3, 2015 13:19
-
-
Save krists/37ac61acca1bd04bc9eb to your computer and use it in GitHub Desktop.
Method that calculates all vectors to draw an arrow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "matrix" | |
require "cmath" | |
module Arrow | |
# Example arrow(6, 4, Vector[10, 10], Vector[100, 100]) | |
def arrow(arrowhead_length, half_width, foot_vector, tip_vector) | |
h = arrowhead_length.to_f * CMath.sqrt(3) | |
w = half_width.to_f | |
u = (tip_vector - foot_vector) / (tip_vector - foot_vector).norm | |
v = Vector[-u[1], u[0]] | |
{ foot: foot_vector, | |
tip: tip_vector - arrowhead_length * u, | |
arrowhead: { | |
tip: tip_vector, | |
left: tip_vector - h * u + w * v, | |
right: tip_vector - h * u - w * v }} | |
end | |
module_function :arrow | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment