ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Calculate mouse move/path angle
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

egrobler

42 Posts

Posted - May 18 2014 :  07:20:00  Show Profile  Reply
Hi Everyone,

I need to allow the user to move an object with the mouse in the direction of the mouse path and rotate the object (a car for example) accordingly.

I assume it would involve code that is something like:
angle := ArcTan2(deltaX,deltaY)*180.0 / PI;

Is there an existing function/code in the ImageEn library that calculates the direction of the mouse movement?

Any tips or suggestions will be helpful.

Thanks and regards
Eric

egrobler

42 Posts

Posted - May 18 2014 :  17:06:17  Show Profile  Reply
This seems to work ok:

function CalculateMouseDirection(const aStartPos, aCurrentPos : TPoint) : Integer;
{
 North      ->   0
 North-East ->  45
 East       ->  90
 South      -> 180
 West       -> 270
 NorthWest  -> 315
 etc.
}
var xDelta,yDelta : Integer;
               ex : Extended;
begin
  xDelta := aCurrentPos.X-aStartPos.X;
  yDelta := aStartPos.Y-aCurrentPos.Y;
  ex :=  (ArcTan2(xDelta, yDelta)*180) / PI;
  if ex<0 then ex := ex+360;
  Result := Round(ex);
end;
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: