3
$\begingroup$

What's the specification to draw arrows of a certain length (from a reference point), at a given angle?

Say, I want to draw an arrow of length 1 at 0 degrees, and one of length 0.5 at 90 degrees, each arrow starting at a giving originPt. Ideally, I'd like a function where I give as argument a list of arrowSpecs, like this:

(*Arrow list {arrowColor, originPt, lengthFromOrigin, angle}*)
originPt = {0,0};
arrowSpecs = {{Blue,originPt,1,0},{Red,originPt,0.5,90}}

(*My desired function*)
arrowDrawing[arrowSpecs]

Getting this:

enter image description here

Note that the originPt could be different for every arrow, here just for simplicity I used the same.

Thanks!

$\endgroup$

3 Answers 3

4
$\begingroup$
originPt = {0, 0};
arrowSpecs = {{Blue, originPt, 1, 0}, {Red, originPt, 0.5, 90 Degree}};

arrowDrawing[specs_] := 
 Graphics[{Thickness[.01], Arrowheads[.1], #[[1]], 
     Arrow[{#[[2]], #[[2]] + {#[[3]] Cos[#[[4]]], #[[3]] Sin[#[[4]]]}}]} & /@ 
   specs]

arrowDrawing[arrowSpecs]

Mathematica graphics

Note the usage of Degree when defining the angle.

$\endgroup$
2
  • 2
    $\begingroup$ If we test with originPt = RandomReal[{-10, 10}, 2], what happens if originPt is not (0, 0)? Does Arrow[{#[[2]], {#[[3]] Cos[#[[4]]], #[[3]] Sin[#[[4]]]} + #[[2]]}]} & /@ specs solve the problem? $\endgroup$
    – creidhne
    Aug 29, 2021 at 18:57
  • 3
    $\begingroup$ @creidhne, yes, sorry, I have fixed the problem. $\endgroup$
    – Domen
    Aug 29, 2021 at 20:35
9
$\begingroup$

You can use the function AnglePath for this purpose:

toArrow[{color_, orig_, len_, angle_}] := {
    color, 
    Arrow @ AnglePath[orig, {{len,angle}}]
}

For example:

arrowSpecs = {{Blue, {1, 1}, 1, 0}, {Red, {-1, -1}, 0.5, 90 Degree}};
Graphics[toArrow /@ arrowSpecs, Axes->True]

enter image description here

$\endgroup$
2
$\begingroup$
ClearAll[transformedArrow]
transformedArrow[{style_, origin_, length_, angle_}] :=
   {style, 
   Arrow @ AffineTransform[{length RotationMatrix @ angle, origin}] @ {{0, 0}, {1, 0}}}

Examples:

arrowSpecs = {{Directive[AbsoluteThickness[5], Arrowheads[Large], Blue], {1,1}, 1, π/4}, 
 {Red, {-1, 0}, 0.5, 90 Degree}};

Graphics[transformedArrow /@ arrowSpecs]

enter image description here

SeedRandom[1]
aSpecs = Table[{RandomColor[], RandomReal[{-2, 2}, 2], 
   RandomReal[{.5, 2}], RandomReal[{0, 2 Pi}]}, 20]; 

Graphics[{Thick, transformedArrow /@ aSpecs}, Axes -> True]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.