How to plot an arrow with a point, length and direction?
Afficher commentaires plus anciens
Hi.
I want to plot an arrow with a starting point (x,y), length and direction(degree), but there shouldn't be cosinus and sinus commands in code. So, at the end of the process, the length should be the same. When I use cos and sin commands to define endpoint (xend,yend), the length is changing.
How can I do that? (I tried arrow and quiver commands but I'm failed)
Thanks for your answers.
2 commentaires
Walter Roberson
le 13 Fév 2014
Modifié(e) : Walter Roberson
le 13 Fév 2014
annotate() ?
If the length is changing, is it possible that you have not done
axis equal
?
Image Analyst
le 14 Fév 2014
I moved what poster said in an "Answer" to a comment here since it's not an "Answer" to his original question:
Walter Roberson, thanks for your answer. But I can't understand what did you mean.
In my opinion, the problem is not about the axis. Because I can't find a command that I can direction, length and point, all them together except for arrow command.
If you know anything about this that I can, could you give me a sample code?
Thanks again.
Réponses (3)
Walter Roberson
le 13 Fév 2014
MATLAB does not have an arrow() command. Arrows are created using
annotation('arrow', x, y)
You cannot set direction by angle; you need to give x and y coordinates. You can use pol2cart() to do the transformation:
[delta_x, delta_y] = pol2cart(length, direction_angle);
annotation('arrow', [base_x, base_x + delta_x], [base_y, base_y + delta_y] )
2 commentaires
The mistake in this answer is that the order of the arguments to pol2cart() are swapped.
[x y z] = peaks(100);
pcolor(x,y,z);
shading flat
base_x = 0;
base_y = 0;
len = 0.707;
th = pi/4;
[delta_x, delta_y] = pol2cart(th,len);
annotation('arrow', [base_x, base_x + delta_x], [base_y, base_y + delta_y] )
This might also demonstrate what may be another misconception. The annotation is being placed in normalized figure coordinates, not data coordinates. Since you haven't described the problem you're having, that might be part of it.
Tord Vincent Heggland
le 4 Mar 2020
0 votes
[delta_x, delta_y] = pol2cart(length, direction_angle);
annotation('arrow', [base_x, base_x + delta_x], [base_y, base_y + delta_y] )
1 commentaire
Majid
le 12 Nov 2021
I guess this does not work because the input of annotation() should be between 0 and 1. Am I right?
Robert Sacker
le 17 Oct 2020
0 votes
Don't you have "direction" and "length" interchanged?
Catégories
En savoir plus sur Image Acquisition Toolbox Supported Hardware dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
