# How to draw triangle in the middle of the line?

I need to draw a triangle like an arrow in the middle of the line. How can I calculate triangle's coordinates in order to draw it in the middle of the line PLEASE?

** UPDATE **

Here I have found out how to find the middle of the line, so now I have a coordinate for one of three vertices of the triangle. I need to calculate coordinates of other two vertices.

-
How big do you want the triangle to be? Do you want the tip of the Triangle to be exactly half way? Do you want the 'center' of the triangle to be half way? If so, which center (centroid, circumcenter, etc.)? –  Nicolas Villanueva Jun 7 '11 at 15:43
Generally it is not so important for me. I need to locate a triangle like an arrow in the middle of the line more or less. The size of the triangle is enough to be like in the figure above. –  Bakhtiyor Jun 7 '11 at 15:47
... The coordinates depend solely on the size of the triangle... so be more specific... –  Nicolas Villanueva Jun 7 '11 at 15:55
@Nicolas. Let's say I need a triangle of 2cm of each sides. Could you propose me any formula for that please? –  Bakhtiyor Jun 7 '11 at 15:59

First start with a triangle of the size you want, say directed to the right. The three corners are then $(0,0), (x_0,y_0), (x_0,-y_0)$ where $x_0 \lt 0$. You just multiply these by the rotation matrix $$\left(\begin {array}{c c}\cos \theta & \sin \theta \\ -\sin \theta & \cos \theta \end{array}\right)\left(\begin {array}{c}x_0 \\ y_0 \end{array}\right)$$ to get each point relative to the head of your arrow. To get $\theta$ you need the angle of your line. How is that represented?
@Bakhtiyor: Do you know how to find the slope $m$ of the line? The angle $\theta$ is such that $\tan \theta=m$. –  Américo Tavares Jun 7 '11 at 16:20
The slope $m=\frac{y_2-y_1}{x_2-x_1}$ –  Ross Millikan Jun 7 '11 at 16:22
$(x_0,y_0)$ sets the size of your triangle. It looks like something about $(x_2-x_1)/20$ is about right-I don't know the scale of your figure. These are offsets from your xx1, so xx2=xx1+Math.cos(alfa)*x0+Math.sin(alfa)*y0 and similar for xx3, yy2, yy3. –  Ross Millikan Jun 7 '11 at 18:17