| | |
small logic error
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 158
Reputation:
Solved Threads: 3
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.
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)
#include <iostream> using namespace std; int main() { double x1; double y1; int heading; double distance; double xHeadingRatio; double yHeadingRatio; double x2 = 0; double y2 = 0; cout << "Enter object's x co-ordinate: "; cin >> x1; cout << "\n"; cout << "Enter object's y co-ordinate: "; cin >> y1; cout << "\n"; cout << "Enter object's heading (degrees): "; cin >> heading; cout << "\n"; cout << "Enter object's distance travelled: "; cin >> distance; if (heading == 0) { y2 = y1 + distance; } if (heading == 90) { x2 = x1 + distance; } if (heading == 180) { y2 = y1 - distance; } if (heading == 270) { x2 = x1 - distance; } if (heading > 0 && heading < 90) { xHeadingRatio = heading / 90; yHeadingRatio = 1 - xHeadingRatio; x2 = distance * xHeadingRatio; y2 = distance * yHeadingRatio; } if (heading > 90 && heading < 180) { // math not finished } if (heading > 180 && heading < 270) { // math not finished } if (heading > 270 && heading < 360) { // math not finished } cout << "\n"; cout << "The new x and y co-ordinates are ("; cout << x2; cout << ","; cout << y2; cout << ")"; cout << "\n"; system("PAUSE"); }
Last edited by cam875; Nov 22nd, 2008 at 8:17 pm.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
•
•
•
•
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.
•
•
•
•
cpp Syntax (Toggle Plain Text)
if (heading > 0 && heading < 90) { xHeadingRatio = heading / 90; yHeadingRatio = 1 - xHeadingRatio; x2 = distance * xHeadingRatio; y2 = distance * yHeadingRatio; }
C++ Syntax (Toggle Plain Text)
dx = distance * cos(heading / 180. * M_PI); dy = distance * sin(heading / 180. * M_PI);
Last edited by dougy83; Nov 22nd, 2008 at 8:34 pm.
•
•
Join Date: Jun 2008
Posts: 158
Reputation:
Solved Threads: 3
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)
#include <iostream> #include <math.h> using namespace std; int main() { double x1; double y1; int Heading; double Distance; double xDisplacement; double yDisplacement; double x2; double y2; cout << "Enter object's x co-ordinate: "; cin >> x1; cout << "\n"; cout << "Enter object's y co-ordinate: "; cin >> y1; cout << "\n"; cout << "Enter object's heading (degrees): "; cin >> Heading; cout << "\n"; cout << "Enter object's distance travelled: "; cin >> Distance; xDisplacement = cos(Heading) * Distance; yDisplacement = sin(Heading) * Distance; x2 = x1 + xDisplacement; y2 = y1 + yDisplacement; cout << "\n"; cout << "The new x and y co-ordinates are ("; cout << x2; cout << ","; cout << y2; cout << ")"; cout << "\n"; system("PAUSE"); }
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Tutorial: An introduction to Test Driven Development (Computer Science)
- Class Template Problem - Constructor Issues - Please help (C++)
- Hardware Interrupts & 100% CPU usage (Windows NT / 2000 / XP)
- Programming FAQ - Updated 1/March/2005 (Computer Science)
- Syntax error in INSERT INTO statement (Java)
- Error coming while running an App (JSP)
- Queue; correct code but error msg :Help (C++)
- Python GUI build: Logic Complications and Mistakes (Python)
- Looking for Reseller Hosting (Web Hosting Deals)
Other Threads in the C++ Forum
- Previous Thread: generating an 'infinite' number
- Next Thread: help with double link list
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






