944,082 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1069
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 3rd, 2009
0
Re: Max Height formula
From the example problem that My teacher gave me

I put it in the souce code as comment close to the top
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Max Height formula
I'm getting 373.402

Using:
C++ Syntax (Toggle Plain Text)
  1. Solve [0 == -9.82*(-(Sqrt[Sin[35]^2*200^2 - 2*9.82*(x)] -
  2. Sin[35]*200)/9.82) + Sin[35]*200, x]
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Skeen is offline Offline
77 posts
since Nov 2009
Nov 3rd, 2009
0
Re: Max Height formula
Why wouldn't you just build the project in OpenGL? - And like plot in the a lot of points? - Maybe even making the program calculate while drawing using parallel programming? - That would create a sweet effect

EDIT: You could find the top point using like derivative calculations as it's given, that in the toppoint of "y", there will be no slope.
Last edited by Skeen; Nov 3rd, 2009 at 4:27 pm.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Skeen is offline Offline
77 posts
since Nov 2009
Nov 3rd, 2009
0
Re: Max Height formula
Okay I'm getting 670 now, basically then the problem isn't the fomula, it's the number for "distance" you're using in it, you should be using; "1915.79" which is where the peak in height is, instead of "3831.57" which is where the impact is.

So I think that you should make a derivative calculation based upon the function with (d/d(distance))(function), and solve with 0=(d/d(distance))(function), then use the result in "d" in the formula to find the height, just tested it, and it works in my program

EDIT: instead of programming a derivative function you could just make a simple test algoritm, like;
C++ Syntax (Toggle Plain Text)
  1. double Distance=0;
  2. double Y-values[1000];
  3. double Impact=(ImpactPointCalculatedEarlier);
  4. while (Distance<Impact)
  5. {
  6. Y-values[Distance]=function(Distance);
  7. if (Y-values[Distance]>Y-values[Distance-1]
  8. {
  9. distance++;
  10. }
  11. else
  12. {
  13. distance-=0.01
  14. }
  15. }
Something like that. (wont be as effecient as the derivative function at all, but will work)
Last edited by Skeen; Nov 3rd, 2009 at 5:01 pm.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Skeen is offline Offline
77 posts
since Nov 2009
Nov 3rd, 2009
0
Re: Max Height formula
Just plotted the Graphs and like, the highest point in height is right in the middle off all of them, so like, you could simply use this;
C++ Syntax (Toggle Plain Text)
  1. double mars_gravity = 3.77; // Fixed value, you used the wrong one.
  2. double earthx = distance_earth (angtorad(angle,pi),mps,earth_gravity)/2; // Divided by 2 (midpoint)
  3. double marsx = distance_mars (angtorad(angle,pi),mps,mars_gravity)/2; // Divided by 2 (midpoint)
  4. double moonx = distance_moon (angtorad(angle,pi),mps,moon_gravity)/2; // Divided by 2 (midpoint)
I'm not sure that the point right in the middle would apply with a different speed and angle, so you'll need to do some testing, or simply make the derivative function as described above.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Skeen is offline Offline
77 posts
since Nov 2009
Nov 3rd, 2009
0
Re: Max Height formula
You my friend are a genius, I get it now...

Or atleast I think so lol
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Nov 4th, 2009
0
Re: Max Height formula
Click to Expand / Collapse  Quote originally posted by UKmason ...
You my friend are a genius, I get it now...

Or atleast I think so lol
About the Graphical, you could use somethin like this:
C++ Syntax (Toggle Plain Text)
  1. #include <GL/glut.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <cmath>
  5. #include <windows.h>
  6.  
  7. #define MAXIMUM 25000
  8.  
  9. GLdouble yEarth[MAXIMUM];
  10. GLdouble yMars[MAXIMUM];
  11. GLdouble yMoon[MAXIMUM];
  12.  
  13. GLint InitGL(GLvoid) // All Setup For OpenGL Goes Here
  14. {
  15. glShadeModel(GL_SMOOTH); // Enable Smooth Shading
  16. glClearColor(1.0f, 1.0f, 1.0f, 0.5f); // Black Background
  17. glClearDepth(1.0f); // Depth Buffer Setup
  18. glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  19. glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
  20. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
  21. return TRUE; // Initialization Went OK
  22. }
  23.  
  24. GLvoid resize(GLint width, GLint height) // Resize And Initialize The GL Window
  25. {
  26. if (height==0) // Prevent A Divide By Zero By
  27. {
  28. height=1; // Making Height Equal One
  29. }
  30.  
  31. glViewport(0,0,width,height); // Reset The Current Viewport
  32.  
  33. glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
  34. glLoadIdentity(); // Reset The Projection Matrix
  35.  
  36. // Calculate The Aspect Ratio Of The Window
  37. gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,MAXIMUM);
  38. glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
  39. glLoadIdentity(); // Reset The Modelview Matrix
  40. }
  41.  
  42. double HeightOfY(double distance, double angle, double gravity, double speed)
  43. {
  44. double height=0;
  45. const double
  46. Pi = 3.141592653589793238462643383279502884197169399375105820974944592,
  47. Conversion = Pi/180;
  48. double Temp0 = distance * tan(angle*Conversion);
  49. double Temp1 = gravity * (distance*distance);
  50. double Temp2 = 2*(((speed*cos(angle*Conversion))*(speed*cos(angle*Conversion))));
  51. height = Temp0-(Temp1/Temp2);
  52. return height;
  53. }
  54.  
  55. GLvoid FPS(GLvoid)
  56. {
  57. static GLint FramesPerSecond = 0; // Create a var, to hold the frame per second
  58. static GLfloat lastTime = 0.0f; // Create a var, to hold the last time (ticks from last second)
  59. static char strFrameRate[256] = {0}; // Create a string, needed to post the new title
  60. GLfloat currentTime = GetTickCount() * 0.001f; // Get the current time, in ticks
  61. ++FramesPerSecond; // Raise framerate in the static var
  62. if ((currentTime - lastTime) > 1.0f) // When a second has elasped
  63. {
  64. lastTime = currentTime; // Base on new time
  65. sprintf(strFrameRate, "Space (running at: FPS: %d)", FramesPerSecond); // Update the string
  66. glutSetWindowTitle(strFrameRate); // Update window title
  67. FramesPerSecond = 0; // Start counting form 0, FramesPerSecond
  68. }
  69. }
  70.  
  71. GLvoid display(GLvoid)
  72. {
  73. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  74. glLoadIdentity();
  75. glTranslatef(-(MAXIMUM/2),-(MAXIMUM/5),-MAXIMUM);
  76. glBegin (GL_LINES);
  77. int xvalue=0;
  78. glColor3f(1,0,0);
  79. glVertex2f(0, 0);
  80. glVertex2f(MAXIMUM, 0);
  81. glVertex2f(0, MAXIMUM);
  82. glVertex2f(0, 0);
  83. glColor3f(0,0,1);
  84. for (xvalue=1; xvalue<=MAXIMUM && yEarth[xvalue]>0 ; xvalue++)
  85. {
  86. glVertex2f((xvalue-1), yEarth[(xvalue-1)]);
  87. glVertex2f(xvalue, yEarth[xvalue]);
  88. }
  89. glColor3f(0,1,1);
  90. for (xvalue=1; xvalue<=MAXIMUM && yMars[xvalue]>0 ; xvalue++)
  91. {
  92. glVertex2f((xvalue-1), yMars[(xvalue-1)]);
  93. glVertex2f(xvalue, yMars[xvalue]);
  94. }
  95. glColor3f(0,1,0);
  96. for (xvalue=1; xvalue<=MAXIMUM && yMoon[xvalue]>0 ; xvalue++)
  97. {
  98. glVertex2f((xvalue-1), yMoon[(xvalue-1)]);
  99. glVertex2f(xvalue, yMoon[xvalue]);
  100. }
  101. glEnd();
  102. glutSwapBuffers();
  103. }
  104.  
  105.  
  106. GLvoid key(unsigned char key, GLint x, GLint y)
  107. {
  108. switch (key)
  109. {
  110. case 27 :
  111. exit(0);
  112. break;
  113. }
  114. glutPostRedisplay();
  115. }
  116.  
  117. GLvoid idle(GLvoid)
  118. {
  119. FPS();
  120. glutPostRedisplay();
  121. }
  122.  
  123. GLvoid Initialization(int argc, char *argv[])
  124. {
  125. glutInit(&argc, argv); // Initialization of glut
  126. glutInitWindowSize(800,600); // Set WindowSize (based upon the var loaded)
  127. glutInitWindowPosition(0,0); // Set default window position
  128. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); // Initialate DisplayMode
  129. }
  130.  
  131. GLint main(GLint argc, char *argv[])
  132. {
  133. for (int xuse=0; xuse<MAXIMUM; xuse++)
  134. {
  135. yEarth[xuse]=HeightOfY(xuse, 35, 9.81, 200); // Earth
  136. }
  137.  
  138. for (int xuse=0; xuse<MAXIMUM; xuse++)
  139. {
  140. yMars[xuse]=HeightOfY(xuse, 35, 3.77, 200); // Mars
  141. }
  142.  
  143. for (int xuse=0; xuse<MAXIMUM; xuse++)
  144. {
  145. yMoon[xuse]=HeightOfY(xuse, 35, 1.62, 200); // The Moon
  146. }
  147.  
  148. Initialization(argc, argv);
  149. glutCreateWindow("Space");
  150.  
  151. glutReshapeFunc(resize); // Check if windows is being resized
  152. glutDisplayFunc(display);
  153. glutKeyboardFunc(key);
  154. glutIdleFunc(idle);
  155.  
  156. glClearColor(1,1,1,1);
  157. glEnable(GL_DEPTH_TEST); // Enable Depth testing
  158. glDepthFunc(GL_LESS); // Choose Depth function
  159.  
  160. glutMainLoop();
  161.  
  162. return EXIT_SUCCESS;
  163. }
This is based upon GLUT, just a little thing I made up in a couple of minuets, basically it calculates 25000points for "y" based upon x'es from 0 to 25000, and then plots those using lines in OpenGL.
The program is VERY inefficient, and you should easily be able to make it more efficient

As the program is right now, it simply lots the parable for the Earth (blue), Mars (teal), Moon (green).

With 35degrees angle, and like 200mps.

Hope it's helping you out, let me know if theres something else
EDIT: You could simply make a console program using the:
C++ Syntax (Toggle Plain Text)
  1. for (int xuse=0; xuse<MAXIMUM; xuse++)
  2. {
  3. yEarth[xuse]=HeightOfY(xuse, 35, 9.81, 200); // Earth
  4. }
  5.  
  6. for (int xuse=0; xuse<MAXIMUM; xuse++)
  7. {
  8. yMars[xuse]=HeightOfY(xuse, 35, 3.77, 200); // Mars
  9. }
  10.  
  11. for (int xuse=0; xuse<MAXIMUM; xuse++)
  12. {
  13. yMoon[xuse]=HeightOfY(xuse, 35, 1.62, 200); // The Moon
  14. }
and then export these values to a text file and let somethin' like GNUPlot handle the plotting .
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Skeen is offline Offline
77 posts
since Nov 2009
Nov 4th, 2009
0
Re: Max Height formula
There an update GLUT code:

C++ Syntax (Toggle Plain Text)
  1. ...
  2. for (xvalue=1; xvalue<=counter*MAXIMUM/1000 && yEarth[xvalue]>0 ; xvalue++)
  3. {
  4. glVertex2f((xvalue-1), yEarth[(xvalue-1)]);
  5. glVertex2f(xvalue, yEarth[xvalue]);
  6. }
  7. glColor3f(0,1,1);
  8. for (xvalue=1; xvalue<=counter*MAXIMUM/1000 && yMars[xvalue]>0 ; xvalue++)
  9. {
  10. glVertex2f((xvalue-1), yMars[(xvalue-1)]);
  11. glVertex2f(xvalue, yMars[xvalue]);
  12. }
  13. glColor3f(0,1,0);
  14. for (xvalue=1; xvalue<=counter*MAXIMUM/1000 && yMoon[xvalue]>0 ; xvalue++)
  15. {
  16. glVertex2f((xvalue-1), yMoon[(xvalue-1)]);
  17. glVertex2f(xvalue, yMoon[xvalue]);
  18. }
  19. counter++;
  20. Sleep(1);
  21. ...
Last edited by Skeen; Nov 4th, 2009 at 6:42 am.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Skeen is offline Offline
77 posts
since Nov 2009

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: (Beginner) How can make it slower?
Next Thread in C++ Forum Timeline: Disabling fullscreen using GLUT





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


Follow us on Twitter


© 2011 DaniWeb® LLC