Welcome, Guest. Please login or register.

Author Topic: C++ Trigonometry  (Read 5598 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: C++ Trigonometry
« on: September 11, 2007, 08:40:41 PM »
I think atan() has sign ambiguity problems with respect to your needs - it will always return the smallest angle subtended.

Try using atan2(y, x) instead.

-edit-

Also, don't forget: the sort of trig you are doing assumes cartesian coordinate axes. Pixel coordinates, of course, are the opposite way around with respect to the y axis.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: C++ Trigonometry
« Reply #1 on: September 11, 2007, 08:48:01 PM »
Yes.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: C++ Trigonometry
« Reply #2 on: September 12, 2007, 01:29:59 PM »
If you have no FPU, the sqrt() call is still a killer though. If you are using integer only, it might be worth looking to see if there are any approximation methods for "nearest integer root" that don't rely on sqrt() calls at all.

Also, as I've suggested before, if you want a balance of precision and performance, consider using fixed point maths. You can easily write a concrete FixedPoint class that wraps a single 32-bit integer and have it support, say 16.16 fixed point format. You could provide all the standard arithmetic operator methods for it.
int p; // A