home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / vort / vort / movies / mech / mech.c next >
C/C++ Source or Header  |  1990-06-19  |  8KB  |  273 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. /*
  5.  * Makes 36 frames of a simple four-bar mechanism.
  6.  *
  7.  */
  8.  
  9. float    W = 0.02, R1 = 0.06, R2 = 0.02;
  10.  
  11. float    r, l, d, c, l2;
  12. float    x1, y11, x0, y00;
  13. float    epsi();
  14.  
  15. #define    RAD2DEG    57.29577951308
  16. #define    DEG2RAD    0.017453292519
  17. #define    MY_PI    3.141592653589
  18.  
  19. FILE    *fp;
  20.  
  21.  
  22. main()
  23. {
  24.     float    theta, gamma, bonk;
  25.         char    buf[40];
  26.         int     i;
  27.  
  28.     r = 0.3;
  29.     l = 0.7555;
  30.     d = 0.8;
  31.     c = 0.4;
  32.     l2 = 1.0;
  33.  
  34.         for (i = 1; i <= 36; i++) {
  35.                 sprintf(buf, "mech%02d.scn", i);
  36.                 if ((fp = fopen(buf, "w")) == NULL) {
  37.                         fprintf(stderr, "Couldn't open '%s' for writing\n", buf);
  38.                         exit(1);
  39.                 }
  40.  
  41.         x1 = y11 = x0 = y00 = 0.0;
  42.  
  43.         fprintf(fp, "lookat (0.5, 0.5, 0.8, 0.4, 0.0, 0.0, 0.0)\n");
  44.         fprintf(fp, "raysperpixel 4\n");
  45.         fprintf(fp, "background 0.3, 0.0, 0.3\n");
  46.         fprintf(fp, "light {\n");
  47.         fprintf(fp, "\tlocation (-1.5, 0.3, 0.0)\n");
  48.         fprintf(fp, "\tcolour 0.8, 0.8, 0.8\n");
  49.         fprintf(fp, "}\n");
  50.         fprintf(fp, "light {\n");
  51.         fprintf(fp, "\tlocation (-0.1, 0.3, -0.7)\n");
  52.         fprintf(fp, "\tcolour 0.5, 0.5, 0.5\n");
  53.         fprintf(fp, "}\n");
  54.         fprintf(fp, "light {\n");
  55.         fprintf(fp, "\tlocation (0.5, 0.5, 0.8)\n");
  56.         fprintf(fp, "\tcolour 0.9, 0.9, 0.9\n");
  57.         fprintf(fp, "}\n");
  58.         fprintf(fp, "light {\n");
  59.         fprintf(fp, "\tlocation (2.2, 1.0, 0.0)\n");
  60.         fprintf(fp, "\tcolour 0.8, 0.8, 0.8\n");
  61.         fprintf(fp, "}\n");
  62.     
  63.         fprintf(fp, "polygon {\n");
  64.         fprintf(fp, "\tcolour 0.3, 0.5, 0.7\n");
  65.         fprintf(fp, "\tmaterial 0.0, 0.4, 0.6, 6\n");
  66.         fprintf(fp, "\treflectance 0.7\n");
  67.         fprintf(fp, "\tvertex (-0.5, -0.5, 0.5)\n");
  68.         fprintf(fp, "\tvertex (-0.5, -0.5, -0.5)\n");
  69.         fprintf(fp, "\tvertex (1.0, -0.5, -0.5)\n");
  70.         fprintf(fp, "\tvertex (1.0, -0.5, 0.5)\n");
  71.         fprintf(fp, "}\n");
  72.         fprintf(fp, "csg {\n");
  73.         theta = 10.0 * (i - 1) * DEG2RAD;
  74.         make_r_bar(theta);
  75.         bonk = epsi(theta);
  76.         make_c_bar(bonk);
  77.         make_l_bar();
  78.     
  79.         fprintf(fp, "\tb1 + b2 + b3 + c1 + c2 + c3 + c4 + c5 + c6 - c7 + pin1 + pin2 + pin3 + pin4 + pin5\n");
  80.         fprintf(fp, "}\n");
  81.         fclose(fp);
  82.     }
  83. }
  84.  
  85. float
  86. epsi(theta)
  87.     float    theta;
  88. {
  89.     float    s = 1.0;
  90.     float    a, b, e, w, p, q;
  91.     float    cosine;
  92.     
  93.     a = d * d + c * c  + r * r - l * l - 2.0 * r * d * cos(theta);
  94.     b = 2.0 * c * d - 2.0 * c * r * cos(theta);
  95.     e = -2.0 * c * r * sin(theta);
  96.     w = a * a - e * e;
  97.     p = b * b + e * e;
  98.     q = 2.0 * a * b;
  99.     if (theta > MY_PI)
  100.         s = -1.0;
  101.  
  102.     cosine = (-q + s * sqrt(fabs(q * q - 4.0 * w * p))) / (2.0 * p);
  103.     if (cosine > 1.0)
  104.         cosine = 1.0;
  105.     if (cosine < -1.0)
  106.         cosine = -1.0;
  107.     return (acos(cosine));
  108. }
  109.  
  110. make_r_bar(theta)
  111.     float    theta;
  112. {
  113.     float    a, b;
  114.  
  115.     fprintf(fp, "\tcylinder c1 {\n");
  116.     fprintf(fp, "\t\tcolour 0.0, 1.0, 0.0\n");
  117.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  118.     fprintf(fp, "\t\tcenter (0.0, 0.0, %f)\n", -W);
  119.     fprintf(fp, "\t\tcenter (0.0, 0.0, %f)\n", W);
  120.     fprintf(fp, "\t\tradius %f\n", R1);
  121.     fprintf(fp, "\t}\n");
  122.     fprintf(fp, "\tcylinder pin1 {\n");
  123.     fprintf(fp, "\t\tcolour 1.0, 1.0, 0.0\n");
  124.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  125.     fprintf(fp, "\t\tcenter (0.0, 0.0, %f)\n", W);
  126.     fprintf(fp, "\t\tcenter (0.0, 0.0, %f)\n", 3 * W);
  127.     fprintf(fp, "\t\tradius %f\n", R2);
  128.     fprintf(fp, "\t}\n");
  129.  
  130.     fprintf(fp, "\tbox b1 {\n");
  131.     fprintf(fp, "\t\tcolour 0.5, 0.4, 1.0\n");
  132.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  133.     fprintf(fp, "\t\treflectance 0.65\n");
  134.     fprintf(fp, "\t\tvertex (%f, %f, %f)\n", 0.0, R1, -W);
  135.     fprintf(fp, "\t\tvertex (%f, %f, %f)\n", r, -R1, W);
  136.     fprintf(fp, "\t\trotate(%f, z)\n", theta * RAD2DEG);
  137.     fprintf(fp, "\t}\n");
  138.  
  139.  
  140.     fprintf(fp, "\tcylinder c2 {\n");
  141.     fprintf(fp, "\t\tcolour 0.0, 1.0, 0.0\n");
  142.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  143.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x1, y11, -W);
  144.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x1, y11, W);
  145.     fprintf(fp, "\t\tradius %f\n", R1);
  146.     fprintf(fp, "\t}\n");
  147.     fprintf(fp, "\tcylinder pin2 {\n");
  148.     fprintf(fp, "\t\tcolour 1.0, 1.0, 0.0\n");
  149.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  150.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x1, y11, -6 * W);
  151.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x1, y11,  6 * W);
  152.     fprintf(fp, "\t\tradius %f\n", R2);
  153.     fprintf(fp, "\t}\n");
  154.  
  155.     x0 = r * cos(theta);
  156.     y00 = r * sin(theta);
  157. }
  158.  
  159. make_l_bar()
  160. {
  161.     float    gamma;
  162.     float    a, b;
  163.     float    cosg;
  164.  
  165.     a = x1 - x0;
  166.  
  167.     cosg = (a == 0.0 ? 0.0 : a / l);
  168.  
  169.     if (cosg > 1.0)
  170.         cosg = 1.0;
  171.     if (cosg < -1.0)
  172.         cosg = -1.0;
  173.  
  174.     gamma = acos(cosg);
  175.  
  176.     fprintf(fp, "\tcylinder c3 {\n");
  177.     fprintf(fp, "\t\tcolour 0.0, 1.0, 0.0\n");
  178.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  179.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x0, y00, W);
  180.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x0, y00, 3 * W);
  181.     fprintf(fp, "\t\tradius %f\n", R1);
  182.     fprintf(fp, "\t}\n");
  183.  
  184.     fprintf(fp, "\tbox b2 {\n");
  185.     fprintf(fp, "\t\tcolour 1.0, 0.4, 0.4\n");
  186.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  187.     fprintf(fp, "\t\treflectance 0.65\n");
  188.     fprintf(fp, "\t\tvertex (%f, %f, %f)\n", 0.0, R1, W);
  189.     fprintf(fp, "\t\tvertex (%f, %f, %f)\n", l, -R1, 3 * W);
  190.     fprintf(fp, "\t\trotate(%f, z)\n", gamma * RAD2DEG);
  191.     fprintf(fp, "\t\ttranslate(%f, %f, 0.0)\n", x0, y00);
  192.     fprintf(fp, "\t}\n");
  193.  
  194.     fprintf(fp, "\tcylinder c4 {\n");
  195.     fprintf(fp, "\t\tcolour 0.0, 1.0, 0.0\n");
  196.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  197.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x1, y11, W);
  198.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x1, y11, 3 * W);
  199.     fprintf(fp, "\t\tradius %f\n", R1);
  200.     fprintf(fp, "\t}\n");
  201.     fprintf(fp, "\tcylinder pin3 {\n");
  202.     fprintf(fp, "\t\tcolour 1.0, 1.0, 0.0\n");
  203.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  204.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x1, y11, -3 * W);
  205.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x1, y11, 4 * W);
  206.     fprintf(fp, "\t\tradius %f\n", R2);
  207.     fprintf(fp, "\t}\n");
  208.     x0 = x0 + l * cos(gamma);
  209.     y00 = y00 + l * sin(gamma);
  210. }
  211.  
  212. make_c_bar(epsi)
  213.     float    epsi;
  214. {
  215.     float    a, b;
  216.  
  217.     fprintf(fp, "\tcylinder c5 {\n");
  218.     fprintf(fp, "\t\tcolour 0.0, 1.0, 0.0\n");
  219.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  220.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", d, 0.0, -W);
  221.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", d, 0.0, W);
  222.     fprintf(fp, "\t\tradius %f\n", R1);
  223.     fprintf(fp, "\t}\n");
  224.     fprintf(fp, "\tcylinder pin4 {\n");
  225.     fprintf(fp, "\t\tcolour 1.0, 1.0, 0.0\n");
  226.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  227.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", d, 0.0, -3 * W);
  228.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", d, 0.0, 3 * W);
  229.     fprintf(fp, "\t\tradius %f\n", R2);
  230.     fprintf(fp, "\t}\n");
  231.  
  232.  
  233.     x1 = l2 * cos(epsi);
  234.     y11 = l2 * sin(epsi);
  235.  
  236.     fprintf(fp, "\tbox b3 {\n");
  237.     fprintf(fp, "\t\tcolour 0.5, 0.4, 1.0\n");
  238.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  239.     fprintf(fp, "\t\treflectance 0.65\n");
  240.     fprintf(fp, "\t\tvertex (%f, %f, %f)\n", 0.0, R1, -W);
  241.     fprintf(fp, "\t\tvertex (%f, %f, %f)\n", l2, -R1, W);
  242.     fprintf(fp, "\t\trotate(%f, z)\n", epsi * RAD2DEG);
  243.     fprintf(fp, "\t\ttranslate(%f, %f, 0.0)\n", d, 0.0);
  244.     fprintf(fp, "\t}\n");
  245.  
  246.     fprintf(fp, "\tcylinder c6 {\n");
  247.     fprintf(fp, "\t\tcolour 1.0, 0.0, 0.0\n");
  248.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  249.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", d + x1, y11, -4 * W);
  250.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", d + x1, y11, 4 * W);
  251.     fprintf(fp, "\t\tradius %f\n", 2.5 * R1);
  252.     fprintf(fp, "\t}\n");
  253.  
  254.     fprintf(fp, "\tcylinder c7 {\n");
  255.     fprintf(fp, "\t\tcolour 1.0, 0.0, 0.0\n");
  256.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  257.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", d + x1, y11, -5.1 * W);
  258.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", d + x1, y11, 5.1 * W);
  259.     fprintf(fp, "\t\tradius %f\n", R1);
  260.     fprintf(fp, "\t}\n");
  261.  
  262.  
  263.     x1 = d + c * cos(epsi);
  264.     y11 = c * sin(epsi);
  265.     fprintf(fp, "\tcylinder pin5 {\n");
  266.     fprintf(fp, "\t\tcolour 1.0, 1.0, 0.0\n");
  267.     fprintf(fp, "\t\tmaterial 0.0, 0.4, 0.6, 6\n");
  268.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x1, y11, -2 * W);
  269.     fprintf(fp, "\t\tcenter (%f, %f, %f)\n", x1, y11, 3 * W);
  270.     fprintf(fp, "\t\tradius %f\n", R2);
  271.     fprintf(fp, "\t}\n");
  272. }
  273.