## A little Vector Type Math

Problems with Canvas, OpenGL, etc...

### A little Vector Type Math

Heres the lowdown:

I want an arrow to point from where the user touches down on the screen to where they lift off and I want it to update as they move there finger across the screen

Using java Syntax Highlighting
1.                 m = new Matrix();
2.
3.                 float postScaleY = (float)(targetArrowSize/arrowHeight);
4.                 //arrowHeight is the height of the image targetArrow size is the wanted size
5.
6.                 m.postScale(1,postScaleY);
7.
8.                 // fromLocation is the touch down and targetArrowLocation is the up or move point
9.                 double x = (targetArrowLocation.x - fromLocation.x);
10.                 double y = (targetArrowLocation.y - fromLocation.y);
11.
12.                 double theta = Math.acos( y /Math.sqrt( Math.pow(x, 2) +Math.pow(y, 2) ) );
13.
14.                 m.postRotate( (float) Math.toDegrees(theta) );
15.
16.                 m.postTranslate(fromLocation.x - offsetX, fromLocation.y - offsetY);
17.
18.                 invalidate();
19.
Parsed in 0.014 seconds, using GeSHi 1.0.8.4

This works if the fromLocation.x is to the right of the targetArrowLocation but if this isnt the case it draws the arrow pointing to the corresponding point on the opposite side of the screen... so the arrow always points left..
I think that if I could do a reflection across the y-axis it would work, so if anyone can tell me how to do that I can try it... but there must be a way to get the math right... Any Ideas?

without any matrix being applied the arrow points down into the positive Y
multiplydivide
Developer

Posts: 34
Joined: Wed Apr 28, 2010 2:09 am