943,898 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 568
  • C++ RSS
Nov 22nd, 2008
0

small logic error

Expand Post »
I am having a problem with the results that I am getting when I run my program, according to the math i wrote in the program when I enter these results

object's x co-ord = 0
object's y cor-ord = 0
object's heading = 45 degrees
object's distance travelled = 1

then

object's new x,y position should display as (0.5,0.5) but everytime i run it with those values i get (0,1) and I am not sure what is wrong. All help is appreciated.


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. double x1;
  7. double y1;
  8. int heading;
  9. double distance;
  10. double xHeadingRatio;
  11. double yHeadingRatio;
  12.  
  13. double x2 = 0;
  14. double y2 = 0;
  15.  
  16. cout << "Enter object's x co-ordinate: ";
  17. cin >> x1;
  18. cout << "\n";
  19. cout << "Enter object's y co-ordinate: ";
  20. cin >> y1;
  21. cout << "\n";
  22. cout << "Enter object's heading (degrees): ";
  23. cin >> heading;
  24. cout << "\n";
  25. cout << "Enter object's distance travelled: ";
  26. cin >> distance;
  27.  
  28. if (heading == 0)
  29. {
  30. y2 = y1 + distance;
  31. }
  32.  
  33. if (heading == 90)
  34. {
  35. x2 = x1 + distance;
  36. }
  37.  
  38. if (heading == 180)
  39. {
  40. y2 = y1 - distance;
  41. }
  42.  
  43. if (heading == 270)
  44. {
  45. x2 = x1 - distance;
  46. }
  47.  
  48. if (heading > 0 && heading < 90)
  49. {
  50. xHeadingRatio = heading / 90;
  51. yHeadingRatio = 1 - xHeadingRatio;
  52. x2 = distance * xHeadingRatio;
  53. y2 = distance * yHeadingRatio;
  54. }
  55.  
  56. if (heading > 90 && heading < 180)
  57. {
  58. // math not finished
  59. }
  60.  
  61. if (heading > 180 && heading < 270)
  62. {
  63. // math not finished
  64. }
  65.  
  66. if (heading > 270 && heading < 360)
  67. {
  68. // math not finished
  69. }
  70.  
  71. cout << "\n";
  72. cout << "The new x and y co-ordinates are (";
  73. cout << x2;
  74. cout << ",";
  75. cout << y2;
  76. cout << ")";
  77. cout << "\n";
  78. system("PAUSE");
  79. }
Last edited by cam875; Nov 22nd, 2008 at 8:17 pm.
Similar Threads
Reputation Points: 12
Solved Threads: 4
Junior Poster
cam875 is offline Offline
170 posts
since Jun 2008
Nov 22nd, 2008
0

Re: small logic error

Click to Expand / Collapse  Quote originally posted by cam875 ...
object's new x,y position should display as (0.5,0.5) but everytime i run it with those values i get (0,1) and I am not sure what is wrong.
Not quite. Distance of 1 from (0,0) heading 45^ will be (0.707, 0.707)

Click to Expand / Collapse  Quote originally posted by cam875 ...
cpp Syntax (Toggle Plain Text)
  1. if (heading > 0 && heading < 90)
  2. {
  3. xHeadingRatio = heading / 90;
  4. yHeadingRatio = 1 - xHeadingRatio;
  5. x2 = distance * xHeadingRatio;
  6. y2 = distance * yHeadingRatio;
  7. }
Where did you get this formulae? Travelled distance is not proportional to heading. try
C++ Syntax (Toggle Plain Text)
  1. dx = distance * cos(heading / 180. * M_PI);
  2. dy = distance * sin(heading / 180. * M_PI);
Last edited by dougy83; Nov 22nd, 2008 at 8:34 pm.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Nov 22nd, 2008
0

Re: small logic error

yeah i realize the math is wrong and im changing that but the program still doesnt work
Reputation Points: 12
Solved Threads: 4
Junior Poster
cam875 is offline Offline
170 posts
since Jun 2008
Nov 22nd, 2008
0

Re: small logic error

Ok here is the new code, and this math works when i follow it exactly with a calculator i get (0.707, 0.707) with the previously entered values in my first post. But in the program i get like .502 and .803 something or w.e. its really weird.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. double x1;
  8. double y1;
  9. int Heading;
  10. double Distance;
  11.  
  12. double xDisplacement;
  13. double yDisplacement;
  14.  
  15. double x2;
  16. double y2;
  17.  
  18. cout << "Enter object's x co-ordinate: ";
  19. cin >> x1;
  20. cout << "\n";
  21. cout << "Enter object's y co-ordinate: ";
  22. cin >> y1;
  23. cout << "\n";
  24. cout << "Enter object's heading (degrees): ";
  25. cin >> Heading;
  26. cout << "\n";
  27. cout << "Enter object's distance travelled: ";
  28. cin >> Distance;
  29.  
  30. xDisplacement = cos(Heading) * Distance;
  31. yDisplacement = sin(Heading) * Distance;
  32.  
  33. x2 = x1 + xDisplacement;
  34. y2 = y1 + yDisplacement;
  35.  
  36. cout << "\n";
  37. cout << "The new x and y co-ordinates are (";
  38. cout << x2;
  39. cout << ",";
  40. cout << y2;
  41. cout << ")";
  42. cout << "\n";
  43. system("PAUSE");
  44. }
Reputation Points: 12
Solved Threads: 4
Junior Poster
cam875 is offline Offline
170 posts
since Jun 2008
Nov 23rd, 2008
0

Re: small logic error

sin( ) and cos( ) take angles in radians, not degrees. Take your degree angle and multiply by (PI/180).

Note that you will find a very good value of PI in the cmath library - check what you need to access it with your compiler.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 23rd, 2008
0

Re: small logic error

oh ok thanks
Reputation Points: 12
Solved Threads: 4
Junior Poster
cam875 is offline Offline
170 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: generating an 'infinite' number
Next Thread in C++ Forum Timeline: help with double link list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC