java - How do you calculate the angle between one object and another? -


how calculate angle between 1 object , assuming first object origin , vector is, up? struggled problem in android , java 6 hours , there wasn't questions or answers gave correct way calculate it.

if question not super clear: have object on screen , want know angle object being y axis (or 90 degrees) object on screen. if first object @ 1,1 , second object @ 2,2, angle should 315. because 0 degrees right, 90 degrees (y axis), 180 degrees left, , 270 degrees down.

there might more elegant solutions, found works well:

float angle = (float)math.todegrees(math.atan2((double)(y1 - y2),(double)(x1 - x2))); angle = (angle + 180.0f) % 360.0f; angle = 360.0f - angle; 

this gives angle starting 0 right, 90 up, 180 left, , 270 down. answered own question because took me on day find , wasn't on internet. hope helps someone. i'll leave question unanswered day or two, see if can find more elegant solution or better way of expressing answer. answer in java , has been tested on android.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -