Advertisement
colonel007-fps

Polar-Cartesien TU156

Nov 5th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.15 KB | None | 0 0
  1. //--============================================================================================================//
  2. //--====||||||||||||====||||||||||====|===========||||||||||=====|============|======|||||||||||=====|==========//
  3. //--====|===============|========|====|===========|========|=====|=\==========|======|===============|==========//
  4. //--====|===============|========|====|===========|========|=====|===\========|======|===============|==========//
  5. //--====|===============|========|====|===========|========|=====|====\=======|======|===============|==========//
  6. //--====|===============|========|====|===========|========|=====|=====\======|======|===============|==========//
  7. //--====|===============|========|====|===========|========|=====|=====\======|======|||||||||||=====|==========//
  8. //--====|===============|========|====|===========|========|=====|======\=====|======|===============|==========//
  9. //--====|===============|========|====|===========|========|=====|=======\====|======|===============|==========//
  10. //--====|===============|========|====|===========|========|=====|=======\====|======|===============|==========//
  11. //--====|===============|========|====|===========|========|=====|========\===|======|===============|==========//
  12. //--====||||||||||||====||||||||||====|||||||||===||||||||||=====|==========\=|======|||||||||||=====||||||||===//
  13. //--============================================================================================================//
  14. //--============================================================================================================//
  15. /*
  16. Code by : Promsurin Phutthammawong
  17. ID: 5910613313
  18. Task : Polar-Cart.c
  19. Date Submit : 7/11/2016
  20. */
  21.  
  22. //////////////--- Call header files --- /////////////
  23. #include <stdio.h> // Include standard input output function header
  24. #include <math.h> // Include math function header
  25. #define pi 3.1415926535897932384626433832795  ///// Define PI VALUE
  26.  
  27. ///////////////  Declare Functions //////////////////
  28. void pol2cart (double r , double theta);
  29. //   /\       |  Variable in function |
  30. //   |
  31. // Function name //
  32. void cart2pol  (double x, double y);
  33. //   /\      |  Variable in function |
  34. //   |
  35. // Function name //
  36.  
  37.  
  38. ////////////// Declare Global Variable for using entire code //////////////
  39. double r1=0,theta1=0;   // Because its must be use all section in code
  40. double x1 = 0 , y1 = 0 ;       ///   Give default Value when declare = 0 prevent bug;
  41.  
  42.  
  43. // Main Code //
  44. int main()
  45. {  
  46.     printf("Enter x: ");        /// Printf ...
  47.     scanf("%lf",&x1);           /// Scanf ...
  48.     printf("Enter y: ");        /// print ...
  49.     scanf("%lf",&y1);           /// scanf ...
  50.    
  51.     cart2pol(x1,y1);            /// Call function cart2pol and now its working on (*) symbols find it in code
  52.     ////////This x1 and y1 will be placed in ***cart2pol ( double x , double y)***
  53.    
  54.     printf("Change to Polar coordinates: (%.2f,%.2f)\n",r1,theta1);
  55.    
  56.     pol2cart(r1,theta1);        /// Call function pol2cart and now its working on (**) symbols find it in code
  57.     ////////This r1 and thta1 will be placed in ***cart2pol ( double r , double theta)***
  58.    
  59.     printf("Change to Cartesian coordinates: (%.2f,%.2f)\n",x1,y1);
  60.     return 0;
  61. }
  62. ////////// End main //////////////
  63.  
  64. /////// Function pol2cart ////////
  65. void pol2cart ( double r , double theta) // (**)
  66. {
  67.     theta = theta*pi/180;       /// Theta is degree so we change it in to radian
  68.     x1 = r*cos(theta);          /// Calculate
  69.     y1 = r*sin(theta);          /// Calculate
  70.     // End function continue on main
  71. }
  72.  
  73. /////// Function cart2pol ////////
  74. void cart2pol ( double x , double y)  // (*)
  75. {
  76.     r1 = sqrt(pow(x,2)+pow(y,2));           /// Calculate
  77.     theta1 = atan(y/x);                     /// Calculate
  78.     theta1 = theta1*180/pi;
  79.     /// start quadrant check
  80.     if(x == 0 && y == 0) /// if Theta x y = 0;
  81.         theta1= 0;
  82.     if(x > 0 && y > 0)
  83.         theta1 = theta1;    /// Check Quadrant
  84.     else if(x > 0 && y < 0)    
  85.         theta1 -= 180;
  86.     else if(x < 0 && y < 0)
  87.         theta1 -= 180;
  88.     else if( x < 0 && y > 0)
  89.         theta1 += 180;
  90.     /// End if
  91.     if ( theta1 > 180)      /// if more than 180 substact it
  92.         theta1 -= 180;
  93.     else if ( theta1 < -180) /// if less than -180 plus it
  94.         theta1 += 180;
  95.     /// End if
  96.     /// End function continue on main
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement