small logic error

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 158
Reputation: cam875 is an unknown quantity at this point 
Solved Threads: 3
cam875 cam875 is offline Offline
Junior Poster

small logic error

 
0
  #1
Nov 22nd, 2008
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.


  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: small logic error

 
0
  #2
Nov 22nd, 2008
Originally Posted by cam875 View Post
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)

Originally Posted by cam875 View Post
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 158
Reputation: cam875 is an unknown quantity at this point 
Solved Threads: 3
cam875 cam875 is offline Offline
Junior Poster

Re: small logic error

 
0
  #3
Nov 22nd, 2008
yeah i realize the math is wrong and im changing that but the program still doesnt work
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 158
Reputation: cam875 is an unknown quantity at this point 
Solved Threads: 3
cam875 cam875 is offline Offline
Junior Poster

Re: small logic error

 
0
  #4
Nov 22nd, 2008
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: small logic error

 
0
  #5
Nov 23rd, 2008
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.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 158
Reputation: cam875 is an unknown quantity at this point 
Solved Threads: 3
cam875 cam875 is offline Offline
Junior Poster

Re: small logic error

 
0
  #6
Nov 23rd, 2008
oh ok thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC