html5 canvas - Android drawing lines with arrows -
I'm doing Java + Android leverage, and this topic helped me a lot in allowing users to draw lines Of But, in my app I want to add a head of the arrow to each line. Attempted to use Path.lineTo to add this triangle, but the problem is that I can not do it to rotate in the right direction.
Thinks for help
You have to do some math. Say that the last point of your line is A, and the last but one is B. Then we have to calculate the direction vector D as A-B. That vector should be normalized, that is to be divided by its length, and then to be extended to that length from which you want the key lines of your arrow, the use of Android PointF
class for A, B and D It should look like this:
PointF a = & lt; Start with the last point of your line & gt; PointFB = & lt; Start with the last but one point of your line & gt; // difference vector point d = new point (ax-bx, ai-bi); // normalized d.set (d.x / d.length (), d.y / d.length ()); // segment, say, 10 units d.set (10.0f * d.x, 10.0f * dyy);
Now the rotation part. We will rotate by 3/4 * pie, once clockwise and once towards the counterclockwise. We can do this by using one. For a 2-dimensional case, it is quite simple:
float angle = 0.75 f * math.pi; Points DLFE = new point (+ DX * Math Kos (-gangle) -dy * Math. Sin (-Tongle), + D.X * Math. Sin (-) + D.I. * Math Kos (- Jungle)); Point Dreit = New Point (+ DX * Math Cos (+ Angle) -YY * Mathematics. (+ Angle), + D.X * Mathematics (+ angle) + DE * Math Kaos (+ angle) );
Note that DLEFT and DIRE are directional vectors. To calculate the two finishing points of your arrow lines, you have to add it. So do the proxy (non-Java) -code:
moveTo (A); LineTo (A + dLeft); MoveTo (A); LineTo (A + dRight);
Hope this will work; Good luck!
Comments
Post a Comment