Advertisement
j0h

Gear

j0h
Nov 3rd, 2023
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. /*
  2.  * Gear Curve
  3.  * x    =   r*cos*theta
  4.  * y    =   r*sin*theta
  5.  * r=a+(1/b)*tanh(b*sin(n*theta)),  
  6.  *where tanhx is the hyperbolic tangent. Plots above show gear curves for a=1, b=10, and n=1 to 12.
  7. */
  8.  
  9. //g++ -Wall -o "%e" "%f" -lboost_iostreams -lboost_system -lboost_filesystem -lSDL -lSDL_image -lSDL_ttf -lSDL_mixer -lGLU -lglut -lXi -lXmu `pkg-config --libs opencv`
  10. #include <iostream>
  11. #include <fstream>
  12. #include <math.h>
  13. #include "/usr/include/gnuplot-iostream.h"
  14.  
  15. using namespace std;
  16.  
  17. void init(){
  18.    
  19.     Gnuplot gp;
  20.    
  21.     }
  22.  
  23. void gear(double teeth){
  24.     Gnuplot gp;
  25.     ofstream plot;
  26.     plot.open("gear.dat");
  27.  // x   =   r*cos*theta
  28.  // y   =   r*sin*theta
  29.  // r=a+(1/b)*tanh(b*sin(n*t))
  30.    
  31.     double a =1.5;
  32.     double b =12;
  33.     const double pi = 3.14159265359;
  34.     double r =0;
  35.     double cR=0;
  36.     double n = 1;// teeth?
  37.     n=teeth;
  38.    
  39.     double xx=0;
  40.     double yy = 0;
  41.     double cx = 0;
  42.     double cy = 0;
  43.    
  44.         for(int t=0; t<=360; t++){
  45.             double theta =(t*pi)/180;
  46.              
  47.             r=a+(1/b)*tanh(b*sin(n*theta));
  48.             xx = r * cos(theta);
  49.             yy = r * sin(theta);
  50.             cR=a+(1-b)*tanh(b*sin(0*theta));
  51.             cx=cR*cos(theta);
  52.             cy=cR*sin(theta);
  53.       plot << r <<" " << xx << " " << yy << " " << cR << " " <<cx  << " "<< cy << endl;
  54.       //plot << r << " " << sin(theta) << endl;  
  55.        
  56.      }
  57.      plot.close();
  58.      gp << "unset polar\n";
  59.      gp << "plot \"gear.dat\" using 2:3 with lines\n";
  60.     // gp << "plot \"gear.dat\" using 5:6 with lines\n";
  61.      plot.close();
  62.     }
  63.  
  64. int main(int argc, char **argv){
  65.     //init();
  66.     cout << "Enter Number of teeth: ";
  67.     double k = 5;
  68.     cin >> k;
  69.    
  70.     gear(k);
  71.    
  72.    
  73.    
  74.     return 0;
  75.     }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement