home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / EARTHQUA.ZIP / TUTOR.CPP < prev    next >
C/C++ Source or Header  |  1995-02-04  |  45KB  |  2,079 lines

  1. // TUTOR.CPP - The tutorial for the Earthquake
  2. // Damage Prevention program. Displays the tutorial.
  3. // Created by Misha Koshelev.
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8. #include <graphics.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include <dos.h>
  12. #include <math.h>
  13. #include <time.h>
  14. #include "tutor.h"
  15.  
  16. // How many pixels we skip in between lines.
  17. #define LINESKIP 5
  18.  
  19. // Copies one string to another starting at charachter n.
  20. //
  21. void strcpyn(char *dest, char *src, int n)
  22. {
  23.    int i;
  24.  
  25.    for (i=n; i < strlen(src); i++)
  26.    {
  27.       dest[i-n] = src[i];
  28.    }
  29.    dest[i+1-n] = NULL;
  30. }
  31.  
  32. // Sets n charachters in a string to ch.
  33. //
  34. void strsetchr(char *dest, char ch, int n)
  35. {
  36.    int i;
  37.  
  38.    for (i=0; i<n; i++)
  39.    {
  40.      dest[i] = ch;
  41.    }
  42. }
  43.  
  44. /* Not used in this program.  *
  45.  *                            *
  46. // Writes something to the screen. Wraps it when it gets to RIGHT_WRAP pixels
  47. // or less. Writes it starting from LEFT_WRAP.
  48. //
  49. void gprintf(char *str)
  50. {
  51.    int done = 0, ci = 0;
  52.    char cs[80], os[80];
  53.    int estimatedsize = (getmaxx()/textwidth("a"));
  54.  
  55.    while (!done)
  56.    {
  57.      strsetchr(cs, ' ', 81);
  58.      strsetchr(os, ' ', 81);
  59.      strcpyn(cs, str, ci);
  60.      if (strlen(cs) < estimatedsize - (getx() / textwidth("a")))
  61.      {
  62.         done = 1;
  63.         outtext(cs);
  64.      }
  65.      else
  66.      {
  67.         strncpy(os, cs, estimatedsize);
  68.         os[estimatedsize] = NULL;
  69.         outtext(os);
  70.         moverel(-textwidth(os), textheight(os) + LINESKIP);
  71.         ci += estimatedsize;
  72.       }
  73.    }
  74. }
  75. */
  76.  
  77. // Moves the current position to the next line.
  78. //
  79. void gnewline(void)
  80. {
  81.    moveto(0, gety() + textheight("some text") + LINESKIP);
  82. }
  83.  
  84. // Prints the message "Press any key to continue..." and waits for a key.
  85. // Then deletes the message.
  86. //
  87. int waitforkey(void)
  88. {
  89.    int savec, ch, savex, savey, ret = 1;
  90.  
  91.    savec = getcolor();
  92.    savex = getx(); savey = gety();
  93.    setviewport(1,1,639,479,1);
  94.    setcolor(BLUE);
  95.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  96.    outtextxy(216,450,"Press any key to continue,");
  97.    outtextxy(196,460,"or press the <ESC> key to quit.");
  98.    while (!kbhit());
  99.    ch = getch();
  100.    if (ch == 27)
  101.      ret = 0;
  102.    if (ch == 0)
  103.      getch();
  104.    setfillstyle(SOLID_FILL, 24);
  105.    bar(0,448,640,480);
  106.    // Sets the viewport to inside the square
  107.    setviewport(25,36,615,440,1);
  108.    moveto(savex, savey);
  109.    setcolor(savec);
  110.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  111.    return ret;
  112. }
  113.  
  114. // Prints a message and waits for a key to be struck. Returns 0 if that
  115. // key is not struck, or 1 if it is.
  116. //
  117. int waitforkeymsg(char *msg1, char *msg2, char c)
  118. {
  119.    int savec, ch, savex, savey, ret = 1;
  120.  
  121.    savec = getcolor();
  122.    savex = getx(); savey = gety();
  123.    setviewport(1,1,639,479,1);
  124.    setcolor(BLUE);
  125.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  126.    outtextxy(320 - (textwidth(msg1)/2),450,msg1);
  127.    outtextxy(320 - (textwidth(msg2)/2),460,msg2);
  128.    while (!kbhit());
  129.    ch = getch();
  130.    if (toupper(ch) == c)
  131.      ret = 0;
  132.    if (ch == 0)
  133.      getch();
  134.    setfillstyle(SOLID_FILL, 24);
  135.    bar(0,448,640,480);
  136.    // Sets the viewport to inside the square
  137.    setviewport(25,36,615,440,1);
  138.    moveto(savex, savey);
  139.    setcolor(savec);
  140.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  141.    return ret;
  142. }
  143.  
  144.  
  145. // Displays the tutorial window with the defined topic.
  146. //
  147. void tutorwindow(char *topic)
  148. {
  149.    // Fills the borders of the windows with a grayish color
  150.    setfillstyle(SOLID_FILL, 24);
  151.    bar(0,0,640,32);
  152.    bar(0,448,640,480);
  153.    bar(0,0,20,480);
  154.    bar(620,0,640,480);
  155.    // Draws a black border between the interior and exterior windows
  156.    setcolor(BLACK);
  157.    rectangle(21,33,619,447);
  158.    // Fills the interior of the window with a lighter grayish color
  159.    setcolor(20);
  160.    setfillstyle(SOLID_FILL, 20);
  161.    bar(22,34,618,446);
  162.    // Writes the title and the topic
  163.    setcolor(BLUE);
  164.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  165.    outtextxy(320-64, 5, "Tutorial");
  166.    setcolor(GREEN);
  167.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  168.    outtextxy(320 - (textwidth(topic)/2), 23, topic);
  169.    // Sets the viewport to inside the square
  170.    setviewport(25,36,615,440,1);
  171. }
  172.  
  173. // Show the tutorial introduction.
  174. //
  175. void tutorintro(void)
  176. {
  177.    cleardevice();
  178.    tutorwindow("Introduction");
  179.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  180.    setcolor(GREEN);
  181.    outtext("The goal: ");
  182.    setcolor(BLACK);
  183.    outtext("To compare several methods");
  184.    gnewline();
  185.    outtext("of earthquake damage prevention.");
  186.    gnewline();
  187.    gnewline();
  188.    if (!waitforkey())
  189.      goto Done;
  190.    setcolor(GREEN);
  191.    outtext("Method: ");
  192.    setcolor(BLACK);
  193.    outtext("I simulated the effect of an");
  194.    gnewline();
  195.    outtext("earthquake on a building with");
  196.    gnewline();
  197.    outtext("different earthquake damage");
  198.    gnewline();
  199.    outtext("prevention methods.");
  200.    gnewline();
  201.    gnewline();
  202.    if (!waitforkey())
  203.      goto Done;
  204.    setcolor(GREEN);
  205.    outtext("Programs: ");
  206.    setcolor(BLACK);
  207.    outtext("This project consists of");
  208.    gnewline();
  209.    outtext("two programs:");
  210.    gnewline();
  211.    moverel(40,0);
  212.    setcolor(RED);
  213.    outtext("- ");
  214.    setcolor(BLACK);
  215.    outtext("A program that simulates an");
  216.    gnewline();
  217.    moverel(40,0);
  218.    outtext("  earthquake.");
  219.    gnewline();
  220.    gnewline();
  221.    moverel(40,0);
  222.    setcolor(RED);
  223.    outtext("- ");
  224.    setcolor(BLACK);
  225.    outtext("A program that simulates how a");
  226.    gnewline();
  227.    moverel(40,0);
  228.    outtext("  building moves during an");
  229.    gnewline();
  230.    moverel(40,0);
  231.    outtext("  earthquake.");
  232.    gnewline();
  233.    if (!waitforkey())
  234.      goto Done;
  235. Done:
  236.    setviewport(1,1,639,479,1);
  237.    cleardevice();
  238. }
  239.  
  240. int tutoreqsimgenidea()
  241. {
  242.    int ret;
  243.  
  244.    cleardevice();
  245.    tutorwindow("EARTHQUAKE SIMULATION: General Idea");
  246.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  247.    setcolor(GREEN);
  248.    outtext("The main problem: ");
  249.    setcolor(BLACK);
  250.    outtext("earthquakes are");
  251.    gnewline();
  252.    setcolor(RED);
  253.    outtext("unpredictable");
  254.    setcolor(BLACK);
  255.    outtext(". There is no known");
  256.    gnewline();
  257.    outtext("formula to describe an earthquake.");
  258.    gnewline();
  259.    gnewline();
  260.    if (!waitforkey())
  261.      return 0;
  262.    setcolor(GREEN);
  263.    outtext("Solution: ");
  264.    setcolor(BLACK);
  265.    outtext("To describe unpredictable");
  266.    gnewline();
  267.    outtext("phenomena, Professor Mandlebrot");
  268.    gnewline();
  269.    outtext("of Yale University invented");
  270.    gnewline();
  271.    setcolor(RED);
  272.    outtext("fractals");
  273.    setcolor(BLACK);
  274.    outtext(".");
  275.    gnewline();
  276.    gnewline();
  277.    ret = waitforkeymsg("Press any key to continue,",
  278.                "or press the <ESC> key to skip to waves", 27);
  279.    if (ret == 0)
  280.      return 3;
  281.    if (ret == 1)
  282.      return 2;
  283.  
  284.    setviewport(1,1,639,479,1);
  285.    cleardevice();
  286.    return 1;
  287. }
  288.  
  289. int tutoreqsimfractals()
  290. {
  291.    cleardevice();
  292.    tutorwindow("EARTHQUAKE SIMULATION: Fractals");
  293.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  294.    setcolor(GREEN);
  295.    outtext("Definiton: ");
  296.    setcolor(BLACK);
  297.    outtext("A fractal is a set of");
  298.    gnewline();
  299.    outtext("points whose dimension is a fraction");
  300.    gnewline();
  301.    outtext("(not an integer).");
  302.    gnewline();
  303.    gnewline();
  304.    if (!waitforkey())
  305.      return 0;
  306.    setcolor(GREEN);
  307.    outtext("What is dimension? ");
  308.    setcolor(BLACK);
  309.    outtext("Dimension sort of");
  310.    gnewline();
  311.    outtext("describes how \`\`big\'\' a set is.");
  312.    gnewline();
  313.    gnewline();
  314.    if (!waitforkey())
  315.       return 0;
  316.  
  317.    setviewport(1,1,639,479,1);
  318.    cleardevice();
  319.    return 1;
  320. }
  321.  
  322. int tutoreqsimpasta()
  323. {
  324.    int fettucini[8];
  325.  
  326.    cleardevice();
  327.    tutorwindow("EARTHQUAKE SIMULATION: Dimension example");
  328.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  329.    setcolor(GREEN);
  330.    outtext("Example: ");
  331.    setcolor(BLACK);
  332.    outtext("Pasta");
  333.    gnewline();
  334.  
  335.    setcolor(YELLOW);
  336.    line(20,300,100,50);
  337.    moveto(20,320);
  338.    setcolor(BLACK);
  339.    outtext("Spaghetti");
  340.    moveto(20,340);
  341.    outtext("   1-D   ");
  342.    if (!waitforkey())
  343.      return 0;
  344.  
  345.    setfillstyle(SOLID_FILL, YELLOW);
  346.    fettucini[0] = 220;
  347.    fettucini[1] = 300;
  348.  
  349.    fettucini[2] = 230;
  350.    fettucini[3] = 310;
  351.  
  352.    fettucini[4] = 310;
  353.    fettucini[5] = 70;
  354.  
  355.    fettucini[6] = 300;
  356.    fettucini[7] = 50;
  357.  
  358.    fillpoly(4, fettucini);
  359.    moveto(220,320);
  360.    setcolor(BLACK);
  361.    outtext("Fettucini");
  362.    moveto(220,340);
  363.    outtext("   2-D   ");
  364.    if (!waitforkey())
  365.      return 0;
  366.  
  367.    setcolor(BLACK);
  368.    ellipse(460, 150, 0, 360, 51, 71);
  369.    setfillstyle(SOLID_FILL, YELLOW);
  370.    fillellipse(460, 150, 50, 70);
  371.    setcolor(BLACK);
  372.    line(460,80,480,220);
  373.    moveto(405,320);
  374.    outtext("Pasta shell");
  375.    moveto(405,340);
  376.    outtext("   3-D   ");
  377.    if (!waitforkey())
  378.      return 0;
  379.  
  380.    moveto(320 - (textwidth("Where is more dough?")/2),370);
  381.    setcolor(BLACK);
  382.    outtext("Where is more dough?");
  383.    gnewline();
  384.    if (!waitforkey())
  385.      return 0;
  386.  
  387.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 4);
  388.    setcolor(GREEN);
  389.    moveto(170, 330);
  390.    outtext("<");
  391.    sound(440);
  392.    delay(100);
  393.    nosound();
  394.    delay(700);
  395.    moveto(370, 330);
  396.    outtext("<");
  397.    sound(440);
  398.    delay(100);
  399.    nosound();
  400.    delay(700);
  401.    setcolor(BLACK);
  402.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  403.  
  404.    if (!waitforkey())
  405.      return 0;
  406.  
  407.    setviewport(1,1,639,479,1);
  408.    cleardevice();
  409.    return 1;
  410. }
  411.  
  412. int tutoreqsimdimension()
  413. {
  414.    cleardevice();
  415.    tutorwindow("EARTHQUAKE SIMULATION: General idea of dimension");
  416.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  417.    setcolor(GREEN);
  418.    outtext("General idea: ");
  419.    setcolor(BLACK);
  420.    outtext("A set is bigger if it");
  421.    gnewline();
  422.    outtext("contains more elements.");
  423.    gnewline();
  424.    if (!waitforkey())
  425.      return 0;
  426.  
  427.    setcolor(GREEN);
  428.    outtext("Question: ");
  429.    setcolor(BLACK);
  430.    outtext("How many elements are in");
  431.    gnewline();
  432.    outtext("a set?");
  433.    gnewline();
  434.    if (!waitforkey())
  435.      return 0;
  436.  
  437.    setcolor(GREEN);
  438.    outtext("Solution: ");
  439.    setcolor(BLACK);
  440.    outtext("Ask a fast food Co. (e.g. ");
  441.    gnewline();
  442.    outtext("Subway) to build its restaurants so");
  443.    gnewline();
  444.    outtext("that wherever you are in this set,");
  445.    gnewline();
  446.    outtext("the nearest Subway is less than or");
  447.    gnewline();
  448.    outtext("equal to D yards from you.");
  449.    gnewline();
  450.  
  451.    // Draw a stick figure
  452.    // The head
  453.    circle(20,250,10);
  454.    // The body
  455.    line(20,261,20,311);
  456.    // The legs
  457.    line(20,311,5,331);
  458.    line(20,311,35,331);
  459.    // The arms
  460.    line(20,266,60,256);
  461.    line(20,266,60,276);
  462.  
  463.    // Draw the Subway building
  464.    rectangle(200,331,320,200);
  465.    setcolor(YELLOW);
  466.    outtextxy(210,210,"SUBWAY");
  467.    setcolor(BLACK);
  468.  
  469.    // Draw the road between the subway building and the man
  470.    line(35,331,200,331);
  471.    outtextxy(120,336, "< D");
  472.    line(120,350,136,350);
  473.  
  474.    if (!waitforkey())
  475.      return 0;
  476.  
  477.    moveto(0,355);
  478.    outtext("The more restaurants needed, the");
  479.    gnewline();
  480.    outtext("bigger the set.");
  481.  
  482.    if (!waitforkey())
  483.      return 0;
  484.  
  485.    setviewport(1,1,639,479,1);
  486.    cleardevice();
  487.    return 1;
  488. }
  489.  
  490. int tutoreqsimdim1d()
  491. {
  492.    int i;
  493.  
  494.    cleardevice();
  495.    tutorwindow("EARTHQUAKE SIMULATION: 1-D Case");
  496.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  497.    setcolor(BLACK);
  498.    outtext("Road of length L.");
  499.    line(20,100,540,100);
  500.    setcolor(RED);
  501.    outtextxy(262,45,"L");
  502.    if (!waitforkey())
  503.      return 0;
  504.  
  505.    // Draw the first subway
  506.    setcolor(BLUE);
  507.    line(20,100,60,100);
  508.    setfillstyle(SOLID_FILL, RED);
  509.    fillellipse(40,100,3,3);
  510.    setcolor(GREEN);
  511.    line(20,75,20,125);
  512.    line(60,75,60,125);
  513.    outtextxy(22,75,"D");
  514.    outtextxy(42,75,"D");
  515.    sound(440);
  516.    delay(100);
  517.    nosound();
  518.    delay(200);
  519.  
  520.    setcolor(BLACK);
  521.    outtextxy(25,105,"/\\");
  522.    outtextxy(32,120,"|");
  523.    outtextxy(32,135,"|");
  524.    outtextxy(0,155, "1st Subway");
  525.    if (!waitforkey())
  526.      return 0;
  527.  
  528.    // Point to the zone served by the first subway.
  529.    setfillstyle(SOLID_FILL, 20);
  530.    bar(0,105,640,200);
  531.    setcolor(GREEN);
  532.    line(20,75,20,125);
  533.    line(60,75,60,125);
  534.  
  535.    setcolor(BLACK);
  536.    outtextxy(10,105,"/\\");
  537.    outtextxy(17,120,"|");
  538.    outtextxy(17,135,"|");
  539.    outtextxy(0,155, "Zone served by");
  540.    outtextxy(0,175, "1st Subway");
  541.    setcolor(GREEN);
  542.    if (!waitforkey())
  543.      return 0;
  544.  
  545.    // Draw the second subway
  546.    setfillstyle(SOLID_FILL, 20);
  547.    bar(0,105,640,200);
  548.    line(20,75,20,125);
  549.    line(60,75,60,125);
  550.  
  551.    setcolor(BLUE);
  552.    line(60,100,100,100);
  553.    setfillstyle(SOLID_FILL, RED);
  554.    fillellipse(80,100,3,3);
  555.    setcolor(GREEN);
  556.    line(100,75,100,125);
  557.    outtextxy(62,75,"D");
  558.    outtextxy(82,75,"D");
  559.    sound(440);
  560.    delay(100);
  561.    nosound();
  562.    delay(200);
  563.  
  564.    setcolor(BLACK);
  565.    outtextxy(65,105,"/\\");
  566.    outtextxy(72,120,"|");
  567.    outtextxy(72,135,"|");
  568.    outtextxy(0,155, "2nd Subway");
  569.    if (!waitforkey())
  570.      return 0;
  571.  
  572.    // Point to the zone served by the second subway.
  573.    setfillstyle(SOLID_FILL, 20);
  574.    bar(0,105,640,200);
  575.    setcolor(GREEN);
  576.    line(20,75,20,125);
  577.    line(60,75,60,125);
  578.    line(100,75,100,125);
  579.  
  580.    setcolor(BLACK);
  581.    outtextxy(50,105,"/\\");
  582.    outtextxy(57,120,"|");
  583.    outtextxy(57,135,"|");
  584.    outtextxy(0,155, "Zone served by");
  585.    outtextxy(0,175, "2nd Subway");
  586.    setcolor(GREEN);
  587.    if (!waitforkey())
  588.      return 0;
  589.  
  590.    setfillstyle(SOLID_FILL, 20);
  591.    bar(0,105,640,200);
  592.    setcolor(GREEN);
  593.    line(20,75,20,125);
  594.    line(60,75,60,125);
  595.    line(100,75,100,125);
  596.  
  597.    // Draw the rest of the subways
  598.    for (i=120;i<540;i+=40)
  599.    {
  600.      setcolor(BLUE);
  601.      line(i-20, 100, i+20,100);
  602.      setfillstyle(SOLID_FILL, RED);
  603.      fillellipse(i,100,3,3);
  604.      setcolor(GREEN);
  605.      line(i+20,75,i+20,125);
  606.      outtextxy(i-18,75,"D");
  607.      outtextxy(i+2,75,"D");
  608.      sound(440);
  609.      delay(100);
  610.      nosound();
  611.      delay(200);
  612.    }
  613.  
  614.    if (!waitforkey())
  615.      return 0;
  616.  
  617.    moveto(0,150);
  618.    setcolor(BLACK);
  619.    outtext("Each Subway serves the area of 2D");
  620.    gnewline();
  621.    outtext("yards. The total length is L. So");
  622.    gnewline();
  623.    outtext("the total number N of Subways is:");
  624.    gnewline();
  625.  
  626.    moveto(0,230);
  627.    outtext("N =");
  628.    moveto(64, 212);
  629.    outtext("L");
  630.    line(64,236,96,236);
  631.    moveto(64, 254);
  632.    outtext("2D");
  633.    if (!waitforkey())
  634.      return 0;
  635.  
  636.    setviewport(1,1,639,479,1);
  637.    cleardevice();
  638.    return 1;
  639. }
  640.  
  641. int tutoreqsimdim2d()
  642. {
  643.    int i, j;
  644.  
  645.    cleardevice();
  646.    tutorwindow("EARTHQUAKE SIMULATION: 2-D Case");
  647.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  648.    setcolor(BLACK);
  649.    outtext("City with a total area of A.");
  650.    rectangle(20,50,540,250);
  651.    if (!waitforkey())
  652.      return 0;
  653.  
  654.    // Draw the Subways
  655.  
  656.    for (i=70;i<250;i+=40)
  657.    {
  658.      for (j=40;j<540;j+=40)
  659.      {
  660.         setfillstyle(SOLID_FILL, RED);
  661.         fillellipse(j, i, 3, 3);
  662.         setcolor(GREEN);
  663.         circle(j,i,27);
  664.         outtextxy(j-18,i,"D");
  665.         sound(440);
  666.         delay(100);
  667.         nosound();
  668.      }
  669.    }
  670.  
  671.    if (!waitforkey())
  672.      return 0;
  673.  
  674.    moveto(0,260);
  675.    setcolor(BLACK);
  676.    outtext("Each Subway serves the area of");
  677.    gnewline();
  678.    outtext("PI * D");
  679.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  680.    outtext("2");
  681.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  682.    outtext(" yards. The total area is A.");
  683.    gnewline();
  684.    outtext("So the total number N of Subways is:");
  685.    gnewline();
  686.  
  687.    moveto(0,340);
  688.    outtext("N =");
  689.    moveto(64, 322);
  690.    outtext("A");
  691.    line(64,346,200,346);
  692.    moveto(64, 364);
  693.    outtext("PI * D");
  694.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  695.    outtext("2");
  696.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  697.    if (!waitforkey())
  698.      return 0;
  699.  
  700.    setviewport(1,1,639,479,1);
  701.    cleardevice();
  702.    return 1;
  703. }
  704.  
  705. int tutoreqsimdim3d()
  706. {
  707.    int i, j, l;
  708.    int toppoly[10] =
  709.         {100,100,
  710.         300,100,
  711.         250,150,
  712.         50,150,
  713.         100,100};
  714.    int frontpoly[10] =
  715.         {50,150,
  716.          250,150,
  717.          250,300,
  718.          50,300,
  719.          50,150};
  720.    int sidepoly[10] =
  721.         {250,150,
  722.          300,100,
  723.          300,250,
  724.          250,300,
  725.          250,150};
  726.    int backline1[4] =
  727.         {100,100,
  728.          100,250};
  729.    int backline2[4] =
  730.         {300,250,
  731.          100,250};
  732.    int sideline[4] =
  733.         {50,300,
  734.          100,250};
  735.  
  736.    cleardevice();
  737.    tutorwindow("EARTHQUAKE SIMULATION: 3-D Case");
  738.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  739.    setcolor(BLACK);
  740.    outtext("Year 2015: Subways in space.");
  741.    gnewline();
  742.    outtext("Total volume of V.");
  743.    drawpoly(5, toppoly);
  744.    drawpoly(5, frontpoly);
  745.    drawpoly(5, sidepoly);
  746.    setlinestyle(DASHED_LINE, 0, 1);
  747.    line(backline1[0], backline1[1], backline1[2], backline1[3]);
  748.    line(backline2[0], backline2[1], backline2[2], backline2[3]);
  749.    line(sideline[0], sideline[1], sideline[2], sideline[3]);
  750.    setlinestyle(SOLID_LINE, 0, 1);
  751.    if (!waitforkey())
  752.      return 0;
  753.  
  754.    // Draw the Subways
  755.  
  756.    for (l=0; l<4; l++)
  757.    {
  758.      for (i=170;i<300;i+=40)
  759.      {
  760.         for (j=70;j<250;j+=40)
  761.         {
  762.           setfillstyle(SOLID_FILL, RED);
  763.           fillellipse(j + (l * 20), i - (l * 20), 3, 3);
  764.           sound(440);
  765.           delay(100);
  766.           nosound();
  767.         }
  768.      }
  769.    }
  770.    if (!waitforkey())
  771.      return 0;
  772.  
  773.    moveto(320,100);
  774.    setcolor(BLACK);
  775.    outtext("Each Subway");
  776.    gnewline();
  777.    moverel(320,0);
  778.    outtext("serves the area of");
  779.    gnewline();
  780.    moverel(320,0);
  781.    outtext("4/3 * PI * D");
  782.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  783.    outtext("3");
  784.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  785.    gnewline();
  786.    moverel(320,0);
  787.    outtext("yards. The total");
  788.    gnewline();
  789.    moverel(320,0);
  790.    outtext("volume is V. So");
  791.    gnewline();
  792.    moverel(320,0);
  793.    outtext("the total number");
  794.    gnewline();
  795.    moverel(320,0);
  796.    outtext("N of Subways is:");
  797.    gnewline();
  798.  
  799.    moveto(0,340);
  800.    outtext("N =");
  801.    moveto(64, 322);
  802.    outtext("V");
  803.    line(64,346,300,346);
  804.    moveto(64, 364);
  805.    outtext("4/3 * PI * D");
  806.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  807.    outtext("3");
  808.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  809.    if (!waitforkey())
  810.      return 0;
  811.  
  812.    setviewport(1,1,639,479,1);
  813.    cleardevice();
  814.    return 1;
  815. }
  816.  
  817. int tutoreqsimpattern()
  818. {
  819.    cleardevice();
  820.    tutorwindow("EARTHQUAKE SIMULATION: A pattern");
  821.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  822.  
  823.    setcolor(BLACK);
  824.    moverel(0,20);
  825.    outtext("1-D Case:");
  826.    moverel(-getx(), 0);
  827.    moverel(300,0);
  828.    outtext("N = ");
  829.    moverel(0, -18);
  830.    outtext("c");
  831.    moverel(-16, 20);
  832.    outtext("D");
  833.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  834.    outtext("1");
  835.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  836.    moverel(-24, -2);
  837.    line(getx(),gety(),getx()+20,gety());
  838.  
  839.    setcolor(BLACK);
  840.    moverel(-getx(),50);
  841.    outtext("2-D Case:");
  842.    moverel(-getx(), 0);
  843.    moverel(300,0);
  844.    outtext("N = ");
  845.    moverel(0, -18);
  846.    outtext("c");
  847.    moverel(-16, 20);
  848.    outtext("D");
  849.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  850.    outtext("2");
  851.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  852.    moverel(-24, -2);
  853.    line(getx(),gety(),getx()+20,gety());
  854.  
  855.    setcolor(BLACK);
  856.    moverel(-getx(),50);
  857.    outtext("3-D Case:");
  858.    moverel(-getx(), 0);
  859.    moverel(300,0);
  860.    outtext("N = ");
  861.    moverel(0, -18);
  862.    outtext("c");
  863.    moverel(-16, 20);
  864.    outtext("D");
  865.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  866.    outtext("3");
  867.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  868.    moverel(-24, -2);
  869.    line(getx(),gety(),getx()+20,gety());
  870.    if (!waitforkey())
  871.      return 0;
  872.  
  873.    // Highlight the matching numbers
  874.    setcolor(GREEN);
  875.    moveto(0,20);
  876.    outtext("1");
  877.    moverel(-getx(), 0);
  878.    moverel(364, 0);
  879.    moverel(16,2);
  880.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  881.    outtext("1");
  882.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  883.    moveto(0,70);
  884.    outtext("2");
  885.    moverel(-getx(), 0);
  886.    moverel(364, 0);
  887.    moverel(16,2);
  888.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  889.    outtext("2");
  890.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  891.    moveto(0,120);
  892.    outtext("3");
  893.    moverel(-getx(), 0);
  894.    moverel(364, 0);
  895.    moverel(16,2);
  896.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  897.    outtext("3");
  898.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  899.    if (!waitforkey())
  900.      return 0;
  901.  
  902.    moverel(-getx(), 50);
  903.    setcolor(BLACK);
  904.    moverel(320 - (textwidth("GENERAL DEFINITION")/2), 0);
  905.    outtext("GENERAL DEFINITON");
  906.    gnewline();
  907.    gnewline();
  908.    outtext("If N =    , a is called dimension.");
  909.    moveto(128,gety());
  910.    moverel(0,-18);
  911.    outtext("c");
  912.    moverel(-16,+20);
  913.    line(getx(), gety()-2, getx() + 20, gety()-2);
  914.    outtext("D");
  915.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  916.    outtext("a");
  917.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  918.    moverel(-32,-2);
  919.    gnewline();
  920.    gnewline();
  921.    setcolor(BLACK);
  922.    outtext("A ");
  923.    setcolor(RED);
  924.    outtext("fractal");
  925.    setcolor(BLACK);
  926.    outtext(" is a set of");
  927.    gnewline();
  928.    outtext("points whose dimension is a fraction");
  929.    gnewline();
  930.    outtext("(not an integer).");
  931.    gnewline();
  932.    gnewline();
  933.    if (!waitforkey())
  934.      return 0;
  935.  
  936.    setviewport(1,1,639,479,1);
  937.    cleardevice();
  938.    return 1;
  939. }
  940.  
  941. int tutoreqsimwgenidea()
  942. {
  943.    cleardevice();
  944.    tutorwindow("EARTHQUAKE SIMULATION: General Idea (Continued)");
  945.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  946.    setcolor(GREEN);
  947.    outtext("Summary: ");
  948.    setcolor(BLACK);
  949.    outtext("An earthquake can be");
  950.    gnewline();
  951.    outtext("described by a fractal.");
  952.    gnewline();
  953.    gnewline();
  954.    if (!waitforkey())
  955.      return 0;
  956.  
  957.    setcolor(GREEN);
  958.    outtext("Problem: ");
  959.    setcolor(BLACK);
  960.    outtext("How to simulate a fractal?");
  961.    gnewline();
  962.    gnewline();
  963.    if (!waitforkey())
  964.      return 0;
  965.  
  966.    setcolor(GREEN);
  967.    outtext("Method: ");
  968.    setcolor(BLACK);
  969.    outtext("We represent a fractal as a");
  970.    gnewline();
  971.    outtext("sum of monochromatic waves.");
  972.    gnewline();
  973.    gnewline();
  974.    if (!waitforkey())
  975.      return 0;
  976.  
  977.    setcolor(GREEN);
  978.    outtext("Question: ");
  979.    setcolor(BLACK);
  980.    outtext("What is a wave?");
  981.    gnewline();
  982.    gnewline();
  983.    if (!waitforkey())
  984.      return 0;
  985.  
  986.    setviewport(1,1,639,479,1);
  987.    cleardevice();
  988.    return 1;
  989. }
  990.  
  991. int tutoreqsimw1(void)
  992. {
  993.    float step;
  994.  
  995.    cleardevice();
  996.    tutorwindow("EARTHQUAKE SIMULATION: Waves");
  997.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  998.    moveto(0,30);
  999.    setcolor(BLACK);
  1000.    outtext("A ");
  1001.    setcolor(RED);
  1002.    outtext("wave");
  1003.    setcolor(BLACK);
  1004.    outtext(" is a real-life pattern that");
  1005.    gnewline();
  1006.    outtext("repeats itself again and again.");
  1007.    gnewline();
  1008.    gnewline();
  1009.    if (!waitforkey())
  1010.      return 0;
  1011.  
  1012.    step = M_PI / 90;
  1013.    line(20,200,20+(4 * M_PI) / step,200);
  1014.    for (float a=0.0; a<4 * M_PI; a+=step)
  1015.    {
  1016.       putpixel(20+a/step,200+(sin(a)*100),BLUE);
  1017.    }
  1018.  
  1019.    if (!waitforkey())
  1020.      return 0;
  1021.  
  1022.    setfillstyle(SOLID_FILL, 20);
  1023.    bar(0,0,640,100);
  1024.    setcolor(BLACK);
  1025.    outtextxy(50,10, "How to characterize a wave?");
  1026.    moveto(0, 330);
  1027.    setcolor(BLACK);
  1028.    outtext("1) ");
  1029.    setcolor(GREEN);
  1030.    outtext("Amplitude A: ");
  1031.    setcolor(BLACK);
  1032.    outtext("distance from the");
  1033.    gnewline();
  1034.    outtext("middle to the crest.");
  1035.    setcolor(RED);
  1036.    outtextxy(0,142,"A");
  1037.    line(18,100,18,200);
  1038.    line(18,100,22,100);
  1039.    line(18,200,22,200);
  1040.    if (!waitforkey())
  1041.      return 0;
  1042.  
  1043.    setfillstyle(SOLID_FILL, 20);
  1044.    bar(0,310,640,480);
  1045.    moveto(0, 330);
  1046.    setcolor(BLACK);
  1047.    outtext("2) ");
  1048.    setcolor(GREEN);
  1049.    outtext("Frequency F: ");
  1050.    setcolor(BLACK);
  1051.    outtext("number of");
  1052.    gnewline();
  1053.    outtext("repetative segments in a second.");
  1054.    setcolor(RED);
  1055.    line(20,300,20,100);
  1056.    line(20+(2 * M_PI)/step,300,20+(2 * M_PI)/step,100);
  1057.    line(20,300,20+(2 * M_PI)/step,300);
  1058.    outtextxy(20,310,"1 sec., F = 1");
  1059.    line(18,100,18,200);
  1060.    line(18,100,22,100);
  1061.    line(18,200,22,200);
  1062.    if (!waitforkey())
  1063.      return 0;
  1064.  
  1065.    setfillstyle(SOLID_FILL, 20);
  1066.    bar(0,310,640,480);
  1067.    moveto(0, 330);
  1068.    setcolor(BLACK);
  1069.    outtext("3) ");
  1070.    setcolor(GREEN);
  1071.    outtext("Phase P: ");
  1072.    setcolor(BLACK);
  1073.    outtext("how far the initial");
  1074.    gnewline();
  1075.    outtext("position of the wave is from the");
  1076.    gnewline();
  1077.    outtext("middle");
  1078.    setcolor(RED);
  1079.    line(20,300,20,100);
  1080.    outtextxy(0,192,"P");
  1081.    outtextxy(0,210,"=");
  1082.    outtextxy(0,228,"0");
  1083.    if (!waitforkey())
  1084.      return 0;
  1085.  
  1086.    setfillstyle(SOLID_FILL, 20);
  1087.    bar(0,0,640,100);
  1088.    setcolor(BLACK);
  1089.    outtextxy(50,10, "Monochromatic wave:");
  1090.    moveto(0, 330);
  1091.    setfillstyle(SOLID_FILL, 20);
  1092.    bar(0,310,640,480);
  1093.    moveto(0, 330);
  1094.    setcolor(BLACK);
  1095.    moveto(320-(textwidth("x(t) = A sin(wt + p)")/2), gety());
  1096.    outtext("x(t) = A sin(wt + p)");
  1097.    gnewline();
  1098.    moveto(320-(textwidth("w = f * 2PI")/2), gety());
  1099.    outtext("w = f * 2PI");
  1100.    gnewline();
  1101.    if (!waitforkey())
  1102.      return 0;
  1103.  
  1104.    setviewport(1,1,639,479,1);
  1105.    cleardevice();
  1106.    return 1;
  1107. }
  1108.  
  1109. int tutoreqsimwlight(void)
  1110. {
  1111.    int prism[8] =
  1112.         {320,100,
  1113.         400,300,
  1114.         240,300,
  1115.         320,100};
  1116.    int i;
  1117.  
  1118.    cleardevice();
  1119.    tutorwindow("EARTHQUAKE SIMULATION: Light Waves");
  1120.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1121.    setcolor(BLACK);
  1122.    outtext("Light from any source can be");
  1123.    gnewline();
  1124.    outtext("separated into several basic colors,");
  1125.    gnewline();
  1126.    outtext("as in a prism.");
  1127.    gnewline();
  1128.  
  1129.    drawpoly(4, prism);
  1130.    for (i=50;i<281;i++)
  1131.    {
  1132.      putpixel(i,200,YELLOW);
  1133.      delay(20);
  1134.    }
  1135.    for (i=280;i<500;i++)
  1136.    {
  1137.      putpixel(i,200,RED);
  1138.      putpixel(i,200+((i-280)/5),57 /*ORANGE*/);
  1139.      putpixel(i,200+((i-280)/4),GREEN);
  1140.      putpixel(i,200+((i-280)/3),BLUE);
  1141.      putpixel(i,200+((i-280)/2),173 /*VIOLET*/);
  1142.      delay(40);
  1143.    }
  1144.  
  1145.    if (!waitforkey())
  1146.      return 0;
  1147.  
  1148.    setviewport(1,1,639,479,1);
  1149.    cleardevice();
  1150.    return 1;
  1151. }
  1152.  
  1153. int tutoreqsimwsound(void)
  1154. {
  1155.    int piano[30] =
  1156.         {100,100,
  1157.         120,100,
  1158.         120,300,
  1159.         140,300,
  1160.         140,100,
  1161.         160,100,
  1162.         160,300,
  1163.         180,300,
  1164.         180,100,
  1165.         200,100,
  1166.         200,300,
  1167.         220,300,
  1168.         220,100,
  1169.         240,100,
  1170.         240,300};
  1171.    int bk1[8] =
  1172.         {115,100,
  1173.         125,100,
  1174.         125,200,
  1175.         115,200};
  1176.    int bk2[8] =
  1177.         {135,100,
  1178.         145,100,
  1179.         145,200,
  1180.         135,200};
  1181.    int bk3[8] =
  1182.         {155,100,
  1183.         165,100,
  1184.         165,200,
  1185.         155,200};
  1186.    int bk4[8] =
  1187.         {195,100,
  1188.         205,100,
  1189.         205,200,
  1190.         195,200};
  1191.    int bk5[8] =
  1192.         {215,100,
  1193.         225,100,
  1194.         225,200,
  1195.         215,200};
  1196.    int i;
  1197.  
  1198.    cleardevice();
  1199.    tutorwindow("EARTHQUAKE SIMULATION: Sound Waves");
  1200.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1201.    setcolor(BLACK);
  1202.    outtext("Any sound can be simulated by a");
  1203.    gnewline();
  1204.    outtext("piano.");
  1205.    gnewline();
  1206.    gnewline();
  1207.  
  1208.    drawpoly(15, piano);
  1209.    rectangle(100,100,240,300);
  1210.    setfillstyle(SOLID_FILL, BLACK);
  1211.    fillpoly(4, bk1);
  1212.    fillpoly(4, bk2);
  1213.    fillpoly(4, bk3);
  1214.    fillpoly(4, bk4);
  1215.    fillpoly(4, bk5);
  1216.    setcolor(RED);
  1217.    outtextxy(120,320,"YAMAHA");
  1218.    if (!waitforkey())
  1219.      return 0;
  1220.  
  1221.    setcolor(BLACK);
  1222.    moveto(0,340);
  1223.    outtext("This means that a sound can be");
  1224.    gnewline();
  1225.    outtext("played as a chord.");
  1226.    if (!waitforkey())
  1227.      return 0;
  1228.  
  1229.    for (i=0;i<1000;i++)
  1230.    {
  1231.       sound(440);
  1232.       delay(1);
  1233.       nosound();
  1234.       sound(880);
  1235.       delay(1);
  1236.       nosound();
  1237.    }
  1238.    if (!waitforkey())
  1239.      return 0;
  1240.  
  1241.    setviewport(1,1,639,479,1);
  1242.    cleardevice();
  1243.    return 1;
  1244. }
  1245.  
  1246. int tutoreqsimwwave(void)
  1247. {
  1248.    int x,y;
  1249.  
  1250.    cleardevice();
  1251.    tutorwindow("EARTHQUAKE SIMULATION: Waves");
  1252.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1253.    setcolor(BLACK);
  1254.    outtext("Every wave can be represented as");
  1255.    gnewline();
  1256.    outtext("a sum of monochromatic waves:");
  1257.    gnewline();
  1258.    gnewline();
  1259.    gnewline();
  1260.    gnewline();
  1261.    outtext("x(t) = ");
  1262.    // Draw sigma
  1263.    x=getx(); y = gety();
  1264.    line(x-20,y-20,x,y);
  1265.    line(x-20,y+20,x,y);
  1266.    line(x,y-20,x-20,y-20);
  1267.    line(x,y+20,x-20,y+20);
  1268.    // Write text on it
  1269.    outtextxy(x-18,y-38,"N");
  1270.    outtextxy(x-36,y+22,"n=0");
  1271.    // Continue with the formula
  1272.    outtext("A");
  1273.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  1274.    moverel(0,10);
  1275.    outtext("n");
  1276.    moverel(0,-10);
  1277.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1278.    outtext("sin(w");
  1279.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  1280.    moverel(0,10);
  1281.    outtext("n");
  1282.    moverel(0,-10);
  1283.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1284.    outtext("t");
  1285.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  1286.    moverel(0,10);
  1287.    outtext("n");
  1288.    moverel(0,-10);
  1289.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1290.    outtext("+p");
  1291.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  1292.    moverel(0,10);
  1293.    outtext("n");
  1294.    moverel(0,-10);
  1295.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1296.    outtext(")");
  1297.    if (!waitforkey())
  1298.      return 0;
  1299.  
  1300.    gnewline();
  1301.    gnewline();
  1302.    gnewline();
  1303.    outtext("To get a ");
  1304.    setcolor(RED);
  1305.    outtext("fractal");
  1306.    setcolor(BLACK);
  1307.    outtext(" you must take:");
  1308.    gnewline();
  1309.    gnewline();
  1310.    outtext("A");
  1311.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  1312.    moverel(0,10);
  1313.    outtext("n");
  1314.    moverel(0,-10);
  1315.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1316.    outtext(" = I / (w");
  1317.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  1318.    moverel(0,10);
  1319.    outtext("n");
  1320.    moverel(0,-10);
  1321.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1322.    outtext(")");
  1323.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
  1324.    outtext("a");
  1325.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1326.    if (!waitforkey())
  1327.      return 0;
  1328.  
  1329.    setviewport(1,1,639,479,1);
  1330.    cleardevice();
  1331.    return 1;
  1332. }
  1333.  
  1334. // Global variables for the wave example.
  1335. float *eqsimoffset = NULL;            // Array of random phases (or eqsimoffsets)
  1336. float *eqsimsteps = NULL;             // Steps
  1337. float *eqsimspectrand = NULL;            // Spectrum random numbers
  1338.  
  1339. // Destruction function for the wave example.
  1340. void tutoreqsimwdoneglobals(void)
  1341. {
  1342.    if (eqsimoffset)
  1343.      free(eqsimoffset);
  1344.    if (eqsimsteps)
  1345.      free(eqsimsteps);
  1346.    if (eqsimspectrand)
  1347.      free(eqsimspectrand);
  1348. }
  1349.  
  1350. // Double random function.
  1351. float tutoreqsimwdblrand(float limit, unsigned char digitn)
  1352. {
  1353.     int n;
  1354.     time_t t;
  1355.  
  1356.     srand((unsigned) time(&t));                // Start random number generator
  1357.     n = random((int)(limit * pow10(digitn)));  // Get a random integer
  1358.     if (n < 0)                           // If that number is negative
  1359.        n *= -1;                          // make it positive
  1360.     return (n / pow10(digitn));   // Make integer a decimal number
  1361. };
  1362.  
  1363. // Initialization function for the wave example.
  1364. void tutoreqsimwinitglobals(int waveletn)
  1365. {
  1366.    if (eqsimoffset && eqsimsteps && eqsimspectrand)        // If arrays not empty destroy
  1367.      tutoreqsimwdoneglobals();
  1368.    eqsimoffset = (float *)malloc(sizeof(float) * waveletn);
  1369.    eqsimsteps = (float *)malloc(sizeof(float) * waveletn);
  1370.    eqsimspectrand = (float *)malloc(sizeof(float) * waveletn);
  1371.    if (!eqsimoffset || !eqsimsteps || !eqsimspectrand)
  1372.    {
  1373.       closegraph();
  1374.       printf("Not enough memory.\n");
  1375.       exit(1);
  1376.    }
  1377.    for (int n=0; n<waveletn; n++)
  1378.    {
  1379.      eqsimoffset[n] = tutoreqsimwdblrand(2 * M_PI, 4);
  1380.      eqsimspectrand[n] = tutoreqsimwdblrand(1, 4);
  1381.      eqsimsteps[n] = 0.2 * (n + 1);
  1382.    }
  1383. };
  1384.  
  1385. // Spectrum function. Modified slightly.
  1386. float tutoreqsimwspectr(float omega, float intensity)
  1387. {
  1388.     int idx = (int)(omega / eqsimsteps[0]);
  1389.  
  1390.     return ((intensity / pow(omega, 0.35)) * eqsimspectrand[idx]);
  1391. }
  1392.  
  1393. // Earthquake displacement function. Modified slightly.
  1394. // Generates the displacement for a certain time.
  1395. float tutoreqsimwdisp(float time, int waveletn)
  1396. {
  1397.     float cur_disp = 0.0;
  1398.  
  1399.     if (time > 30.0)
  1400.     {
  1401.        closegraph();
  1402.        printf("The parameter time as passed to function disp(time) exceeds\n");
  1403.        printf("the maximum specified.\n");
  1404.        exit(1);
  1405.     }
  1406.     for (int n=0; n<waveletn; n++)       // Loop wavelets
  1407.      cur_disp += tutoreqsimwspectr(eqsimsteps[n], 25) * sin((eqsimsteps[n] * time) +
  1408.             eqsimoffset[n]);
  1409.     return (float)cur_disp;
  1410. }
  1411.  
  1412. int tutoreqsimwexample(void)
  1413. {
  1414.    int y = 310, i;
  1415.    float j, ret;
  1416.    char *tmp;
  1417.  
  1418.    cleardevice();
  1419.    tutorwindow("EARTHQUAKE SIMULATION: Fractal wave example");
  1420.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1421.    setcolor(BLACK);
  1422.    tutoreqsimwinitglobals(5);
  1423.    setfillstyle(SOLID_FILL, BLACK);
  1424.    bar(0,0,300,300);
  1425.  
  1426.    moveto(320,10);
  1427.    setcolor(RED);
  1428.    outtext("Example: ");
  1429.    setcolor(BLACK);
  1430.    outtext("how to");
  1431.    gnewline();
  1432.    moverel(320,0);
  1433.    outtext("generate a");
  1434.    gnewline();
  1435.    moverel(320,0);
  1436.    outtext("fractal.");
  1437.  
  1438.    for (i=1;i<6;i++)
  1439.    {
  1440.       setcolor(BLACK);
  1441.       sprintf(tmp, "%d monochromatic wave(s): ", i);
  1442.       moveto(0,y);
  1443.       outtext(tmp);
  1444.       setcolor(i);
  1445.       moverel(10,0);
  1446.       linerel(10,0);
  1447.       y+=18;
  1448.       for (j=0.0; j<30.0; j+=0.1)
  1449.       {
  1450.         ret = tutoreqsimwdisp(j, i);
  1451.         if (ret * 10 <= 150 && ret * 10 >= -150)
  1452.            putpixel(j*10, 150 + (ret * 10), i);
  1453.       }
  1454.       if (!waitforkey())
  1455.         return 0;
  1456.    }
  1457.  
  1458.    tutoreqsimwdoneglobals();
  1459.  
  1460.    setviewport(1,1,639,479,1);
  1461.    cleardevice();
  1462.    return 1;
  1463. }
  1464.  
  1465. void tutoreqsim(void)
  1466. {
  1467.    int ret;
  1468.  
  1469.    ret = tutoreqsimgenidea();
  1470.    setviewport(1,1,639,479,1);
  1471.    cleardevice();
  1472.    if (ret == 0)
  1473.      goto Done;
  1474.    if (ret == 3)
  1475.      goto Waves;
  1476.  
  1477.    if (!tutoreqsimfractals())
  1478.      goto Done;
  1479.    if (!tutoreqsimpasta())
  1480.      goto Done;
  1481.    if (!tutoreqsimdimension())
  1482.      goto Done;
  1483.    if (!tutoreqsimdim1d())
  1484.      goto Done;
  1485.    if (!tutoreqsimdim2d())
  1486.      goto Done;
  1487.    if (!tutoreqsimdim3d())
  1488.      goto Done;
  1489.    if (!tutoreqsimpattern())
  1490.      goto Done;
  1491. Waves:
  1492.    if (!tutoreqsimwgenidea())
  1493.      goto Done;
  1494.    if (!tutoreqsimw1())
  1495.      goto Done;
  1496.    if (!tutoreqsimwlight())
  1497.      goto Done;
  1498.    if (!tutoreqsimwsound())
  1499.      goto Done;
  1500.    if (!tutoreqsimwwave())
  1501.      goto Done;
  1502.    if (!tutoreqsimwexample())
  1503.      goto Done;
  1504. Done:
  1505.    setviewport(1,1,639,479,1);
  1506.    cleardevice();
  1507. }
  1508.  
  1509. int tutorbmsimgenidea(void)
  1510. {
  1511.    cleardevice();
  1512.    tutorwindow("BUILDING MOVEMENT SIMULATION: General idea");
  1513.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1514.    setcolor(GREEN);
  1515.    outtext("General Idea: ");
  1516.    setcolor(BLACK);
  1517.    outtext("Netwon's law f(t) = Ma(T)");
  1518.    gnewline();
  1519.    moverel(40,0);
  1520.    setcolor(RED);
  1521.    outtext("- ");
  1522.    setcolor(BLACK);
  1523.    outtext("f(t) is a force.");
  1524.    gnewline();
  1525.    moverel(40,0);
  1526.    setcolor(RED);
  1527.    outtext("- ");
  1528.    setcolor(BLACK);
  1529.    outtext("M is a mass.");
  1530.    gnewline();
  1531.    moverel(40,0);
  1532.    setcolor(RED);
  1533.    outtext("- ");
  1534.    setcolor(BLACK);
  1535.    outtext("a(t) is acceleration.");
  1536.    gnewline();
  1537.    gnewline();
  1538.    if (!waitforkey())
  1539.      return 0;
  1540.  
  1541.    setcolor(GREEN);
  1542.    outtext("Problems: ");
  1543.    setcolor(BLACK);
  1544.    gnewline();
  1545.    moverel(40,0);
  1546.    setcolor(RED);
  1547.    outtext("- ");
  1548.    setcolor(BLACK);
  1549.    outtext("How force depends on time?");
  1550.    gnewline();
  1551.    moverel(40,0);
  1552.    setcolor(RED);
  1553.    outtext("- ");
  1554.    setcolor(BLACK);
  1555.    outtext("How to simulate the motion");
  1556.    gnewline();
  1557.    moverel(40,0);
  1558.    outtext("  if we know the acceleration?");
  1559.    gnewline();
  1560.    gnewline();
  1561.    if (!waitforkey())
  1562.      return 0;
  1563.  
  1564. Done:
  1565.    setviewport(1,1,639,479,1);
  1566.    cleardevice();
  1567.    return 1;
  1568. }
  1569.  
  1570. int tutorbmsimforce(void)
  1571. {
  1572.    int i;
  1573.  
  1574.    cleardevice();
  1575.    tutorwindow("BUILDING MOVEMENT SIMULATION: Components of the force");
  1576.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1577.    setcolor(GREEN);
  1578.    outtext("Elastic force: ");
  1579.    setcolor(BLACK);
  1580.    outtext("f_elastic(t) = -kx(t)");
  1581.    gnewline();
  1582.    if (!waitforkey())
  1583.      return 0;
  1584.  
  1585.    setcolor(YELLOW);
  1586.    for (i=100;i<400;i++)
  1587.    {
  1588.      line(10,40,i,40);
  1589.      delay(10);
  1590.    }
  1591.  
  1592.    delay(100);
  1593.  
  1594.    for (i=400;i>100;i--)
  1595.    {
  1596.      setcolor(20);
  1597.      line(10,40,i+1,40);
  1598.      setcolor(YELLOW);
  1599.      line(10,40,i,40);
  1600.      delay(10);
  1601.    }
  1602.    if (!waitforkey())
  1603.      return 0;
  1604.  
  1605.    moveto(0,50);
  1606.    setcolor(GREEN);
  1607.    outtext("Friction force: ");
  1608.    setcolor(BLACK);
  1609.    gnewline();
  1610.    outtext("f_friction(t) = -nv(t)");
  1611.    gnewline();
  1612.    if (!waitforkey())
  1613.      return 0;
  1614.  
  1615.    setcolor(GREEN);
  1616.    line(22,141,320,141);
  1617.  
  1618.    setcolor(BLACK);
  1619.    rectangle(22,100,122,140);
  1620.  
  1621.    for (i=23;i<=101;i++)
  1622.    {
  1623.       setcolor(20);
  1624.       rectangle(i-1,100,i+99,140);
  1625.       setcolor(BLACK);
  1626.       rectangle(i,100,i+100,140);
  1627.       delay(20);
  1628.    }
  1629.    for (i=102;i<=150;i++)
  1630.    {
  1631.       setcolor(20);
  1632.       rectangle(i-1,100,i+99,140);
  1633.       setcolor(BLACK);
  1634.       rectangle(i,100,i+100,140);
  1635.       delay(50);
  1636.    }
  1637.    for (i=151;i<=200;i++)
  1638.    {
  1639.       setcolor(20);
  1640.       rectangle(i-1,100,i+99,140);
  1641.       setcolor(BLACK);
  1642.       rectangle(i,100,i+100,140);
  1643.       delay(100);
  1644.    }
  1645.    for (i=201;i<=220;i++)
  1646.    {
  1647.       setcolor(20);
  1648.       rectangle(i-1,100,i+99,140);
  1649.       setcolor(BLACK);
  1650.       rectangle(i,100,i+100,140);
  1651.       delay(200);
  1652.    }
  1653.  
  1654.    moveto(0,155);
  1655.    setcolor(GREEN);
  1656.    outtext("Earthquake force.");
  1657.    if (!waitforkey())
  1658.      return 0;
  1659.  
  1660. Done:
  1661.    setviewport(1,1,639,479,1);
  1662.    cleardevice();
  1663.    return 1;
  1664. }
  1665.  
  1666. int tutorbmsimcntrlforce(void)
  1667. {
  1668.    int i; int bpoly[10] =
  1669.              {70,220,
  1670.               120,220,
  1671.               120,180,
  1672.               70,180,
  1673.               70,220};
  1674.    int beampoly[10] =
  1675.              {70,220,
  1676.               80,220,
  1677.               115,185,
  1678.               110,185,
  1679.               70,220};
  1680.  
  1681.    cleardevice();
  1682.    tutorwindow("BUILDING MOVEMENT SIMULATION: Components of the force");
  1683.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1684.    moverel(150,0);
  1685.    setcolor(BLUE);
  1686.    outtext("CONTROL FORCE");
  1687.    gnewline();
  1688.    gnewline();
  1689.    gnewline();
  1690.    setcolor(GREEN);
  1691.    outtext("No control: ");
  1692.    setcolor(BLACK);
  1693.    outtext("f_control(t) = 0");
  1694.    gnewline();
  1695.    gnewline();
  1696.    if (!waitforkey())
  1697.      return 0;
  1698.  
  1699.    setcolor(GREEN);
  1700.    outtext("Active control: ");
  1701.    setcolor(BLACK);
  1702.    outtext("pulls the building");
  1703.    gnewline();
  1704.    outtext("in the direction opposite to");
  1705.    gnewline();
  1706.    outtext("the earthquake.");
  1707.    gnewline();
  1708.    gnewline();
  1709.    if (!waitforkey())
  1710.      return 0;
  1711.  
  1712.    setcolor(GREEN);
  1713.    drawpoly(5, bpoly);
  1714.  
  1715.    setcolor(RED);
  1716.    moveto(2,180);
  1717.    outtext("-");
  1718.    delay(200);
  1719.    outtext("-");
  1720.    delay(200);
  1721.    outtext("-");
  1722.    moverel(0,-14);
  1723.    outtext("\\");
  1724.    moverel(-16,24);
  1725.    outtext("/");
  1726.    moverel(0, -2);
  1727.  
  1728.    for (i=0;i<20;i++)
  1729.    {
  1730.       setcolor(20);
  1731.       drawpoly(5,bpoly);
  1732.       setcolor(GREEN);
  1733.       bpoly[4]++;
  1734.       bpoly[6]++;
  1735.       drawpoly(5,bpoly);
  1736.       delay(50);
  1737.    }
  1738.  
  1739.    setcolor(GREEN);
  1740.    moveto(130,196);
  1741.    moverel(0,-24);
  1742.    outtext("/");
  1743.    moverel(-16,14);
  1744.    outtext("\\");
  1745.    moverel(0, -2);
  1746.    outtext("-");
  1747.    delay(200);
  1748.    outtext("-");
  1749.    delay(200);
  1750.    outtext("-");
  1751.  
  1752.    for (i=0;i<20;i++)
  1753.    {
  1754.       setcolor(20);
  1755.       drawpoly(5,bpoly);
  1756.       setcolor(GREEN);
  1757.       bpoly[4]--;
  1758.       bpoly[6]--;
  1759.       drawpoly(5,bpoly);
  1760.       delay(50);
  1761.    }
  1762.    if (!waitforkey())
  1763.      return 0;
  1764.  
  1765.    setcolor(GREEN);
  1766.    moveto(0,230);
  1767.    gnewline();
  1768.    gnewline();
  1769.    outtext("Problem: ");
  1770.    setcolor(BLACK);
  1771.    outtext("We cannot react");
  1772.    gnewline();
  1773.    outtext("immediately so there is a delay T.");
  1774.    gnewline();
  1775.    outtext("f_control(t) = -hx(t-T)");
  1776.    if (!waitforkey())
  1777.      return 0;
  1778.  
  1779.    setfillstyle(SOLID_FILL, 20);
  1780.    bar(0,18,640,479);
  1781.  
  1782.    moveto(0,30);
  1783.    setcolor(GREEN);
  1784.    outtext("Hybrid control: ");
  1785.    setcolor(BLACK);
  1786.    outtext("Use an extra beam");
  1787.    gnewline();
  1788.    outtext("that is normally unattached. If the");
  1789.    gnewline();
  1790.    outtext("building moves too far, attach the");
  1791.    gnewline();
  1792.    outtext("beam and thus, get an extra elastic");
  1793.    gnewline();
  1794.    outtext("force.");
  1795.    if (!waitforkey())
  1796.      return 0;
  1797.  
  1798.    setcolor(GREEN);
  1799.    drawpoly(5, bpoly);
  1800.    setcolor(BLACK);
  1801.    drawpoly(5, beampoly);
  1802.  
  1803.    setcolor(RED);
  1804.    moveto(2,180);
  1805.    outtext("-");
  1806.    delay(200);
  1807.    outtext("-");
  1808.    delay(200);
  1809.    outtext("-");
  1810.    moverel(0,-14);
  1811.    outtext("\\");
  1812.    moverel(-16,24);
  1813.    outtext("/");
  1814.    moverel(0, -2);
  1815.  
  1816.    for (i=0;i<20;i++)
  1817.    {
  1818.       setcolor(20);
  1819.       drawpoly(5,bpoly);
  1820.       drawpoly(5,beampoly);
  1821.       setcolor(GREEN);
  1822.       bpoly[4]++;
  1823.       bpoly[6]++;
  1824.       beampoly[4]++;
  1825.       beampoly[6]++;
  1826.       drawpoly(5,bpoly);
  1827.       setcolor(BLACK);
  1828.       drawpoly(5,beampoly);
  1829.       delay(70);
  1830.    }
  1831.  
  1832.    setcolor(20);
  1833.    drawpoly(5,bpoly);
  1834.    drawpoly(5,beampoly);
  1835.    beampoly[4] += 5;
  1836.    beampoly[6] += 5;
  1837.    beampoly[5] -= 5;
  1838.    beampoly[7] -= 5;
  1839.  
  1840.    for (i=0;i<20;i++)
  1841.    {
  1842.       setcolor(20);
  1843.       drawpoly(5,bpoly);
  1844.       drawpoly(5,beampoly);
  1845.       setcolor(GREEN);
  1846.       bpoly[4]--;
  1847.       bpoly[6]--;
  1848.       beampoly[4]--;
  1849.       beampoly[6]--;
  1850.       drawpoly(5,bpoly);
  1851.       setcolor(BLACK);
  1852.       drawpoly(5,beampoly);
  1853.       delay(70);
  1854.    }
  1855.    if (!waitforkey())
  1856.      return 0;
  1857.  
  1858. Done:
  1859.    setviewport(1,1,639,479,1);
  1860.    cleardevice();
  1861.    return 1;
  1862. }
  1863.  
  1864. int tutorbmsimmotionsim(void)
  1865. {
  1866.    int i;
  1867.  
  1868.    cleardevice();
  1869.    tutorwindow("BUILDING MOVEMENT SIMULATION: How to simulate motion?");
  1870.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1871.    setcolor(RED);
  1872.    outtext("Velocity ");
  1873.    setcolor(BLACK);
  1874.    delay(1000);
  1875.    line(1,300,400,300);
  1876.    line(150,295,150,305);
  1877.    line(350,295,350,305);
  1878.    outtextxy(138,310,"x(t)");
  1879.    sound(440);
  1880.    delay(200);
  1881.    nosound();
  1882.    outtextxy(320,310,"x(t+dt)");
  1883.    sound(440);
  1884.    delay(200);
  1885.    nosound();
  1886.  
  1887.    for (i=150; i<350; i++)
  1888.    {
  1889.       if (i<155)
  1890.       {
  1891.         setcolor(BLACK);
  1892.         line(150,295,150,305);
  1893.       }
  1894.       setcolor(20);
  1895.       setfillstyle(SOLID_FILL, 20);
  1896.       fillellipse(i-1,300,5,5);
  1897.       setcolor(BLACK);
  1898.       line(i-6,300,i+4,300);
  1899.       setcolor(RED);
  1900.       setfillstyle(SOLID_FILL, RED);
  1901.       fillellipse(i,300,5,5);
  1902.       delay(5);
  1903.    }
  1904.  
  1905.    delay(100);
  1906.  
  1907.    moveto(146,0);
  1908.    setcolor(BLACK);
  1909.    outtext("is distance over time:");
  1910.    if (!waitforkey())
  1911.      return 0;
  1912.  
  1913.    moveto(194, 20);
  1914.    outtext("x(t+dt)-x(t)");
  1915.    sound(440);
  1916.    delay(200);
  1917.    nosound();
  1918.    moveto((17 * 16) + 146,20);
  1919.    outtext("dt");
  1920.    sound(440);
  1921.    delay(200);
  1922.    nosound();
  1923.  
  1924.    delay(1000);
  1925.  
  1926.    gnewline();
  1927.    gnewline();
  1928.    gnewline();
  1929.    outtext("v(t) = (x(t+dt) - x(t))/dt");
  1930.    if (!waitforkey())
  1931.      return 0;
  1932.  
  1933.    setfillstyle(SOLID_FILL, 20);
  1934.    bar(0,0,600,400);
  1935.  
  1936.    moveto(1,1);
  1937.    setcolor(RED);
  1938.    outtext("Acceleration ");
  1939.    setcolor(BLACK);
  1940.    outtext("is velocity over time.");
  1941.    gnewline();
  1942.    gnewline();
  1943.    gnewline();
  1944.    outtext("a(t) = (v(t+dt) - v(t))/dt");
  1945.    if (!waitforkey())
  1946.      return 0;
  1947.  
  1948.    gnewline();
  1949.    gnewline();
  1950.    outtext("So: a(t+dt) = a(t) * dt + v(t)");
  1951.    if (!waitforkey())
  1952.      return 0;
  1953.  
  1954. Done:
  1955.    setviewport(1,1,639,479,1);
  1956.    cleardevice();
  1957.    return 1;
  1958. }
  1959.  
  1960. int tutorbmsimgetcoordvel(void)
  1961. {
  1962.    int i;
  1963.  
  1964.    cleardevice();
  1965.    tutorwindow("BUILDING MOVEMENT SIMULATION");
  1966.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  1967.    setcolor(BLUE);
  1968.    outtext("How to get the coordinates and the");
  1969.    gnewline();
  1970.    outtext("velocity at the next moment of time?");
  1971.    gnewline();
  1972.    gnewline();
  1973.    gnewline();
  1974.    setcolor(GREEN);
  1975.    outtext("Known: ");
  1976.    setcolor(BLACK);
  1977.    outtext("x(t), v(t).");
  1978.    gnewline();
  1979.    gnewline();
  1980.    outtext("x(t+dt) = ");
  1981.    setcolor(GREEN);
  1982.    outtext("?");
  1983.    gnewline();
  1984.    setcolor(BLACK);
  1985.    outtext("v(t+dt) = ");
  1986.    setcolor(GREEN);
  1987.    outtext("?");
  1988.    setcolor(BLACK);
  1989.    if (!waitforkey())
  1990.      return 0;
  1991.  
  1992.    moveto(0,gety()-16-LINESKIP);
  1993.    setfillstyle(SOLID_FILL, 20);
  1994.    bar(getx(),gety(),640,400);
  1995.    outtext("x(t+dt) = v(t) * dt + x(t)");
  1996.    gnewline();
  1997.    outtext("v(t+dt) = a(t) * dt + v(t)");
  1998.    gnewline();
  1999.    gnewline();
  2000.    outtext("where: a(t) = f(t) / M");
  2001.    if (!waitforkey())
  2002.      return 0;
  2003.  
  2004. Done:
  2005.    setviewport(1,1,639,479,1);
  2006.    cleardevice();
  2007.    return 1;
  2008. }
  2009.  
  2010. int tutorbmsimenergy(void)
  2011. {
  2012.    int i;
  2013.  
  2014.    cleardevice();
  2015.    tutorwindow("BUILDING MOVEMENT SIMULATION");
  2016.    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
  2017.    setcolor(BLUE);
  2018.    outtext("How to computer the energy used by");
  2019.    gnewline();
  2020.    outtext("active control?");
  2021.    gnewline();
  2022.    gnewline();
  2023.    gnewline();
  2024.    setcolor(BLACK);
  2025.    outtext("The bigger the force we apply, the");
  2026.    gnewline();
  2027.    outtext("more energy we use:");
  2028.    gnewline();
  2029.    outtext("e(t) is proportional to f(t)");
  2030.    gnewline();
  2031.    gnewline();
  2032.    if (!waitforkey())
  2033.      return 0;
  2034.  
  2035.    outtext("The more we move the building, the");
  2036.    gnewline();
  2037.    outtext("more energy we use:");
  2038.    gnewline();
  2039.    outtext("e(t) is proportional to x(t)");
  2040.    gnewline();
  2041.    gnewline();
  2042.    if (!waitforkey())
  2043.      return 0;
  2044.  
  2045.    setcolor(GREEN);
  2046.    outtext("So: ");
  2047.    setcolor(BLACK);
  2048.    outtext("e(t) = f(t) * x(t),");
  2049.    gnewline();
  2050.    outtext("   E = e(0) + e(1) + ... + e(T)");
  2051.    if (!waitforkey())
  2052.      return 0;
  2053.  
  2054. Done:
  2055.    setviewport(1,1,639,479,1);
  2056.    cleardevice();
  2057.    return 1;
  2058. }
  2059.  
  2060.  
  2061. void tutorbmsim(void)
  2062. {
  2063.    if (!tutorbmsimgenidea())
  2064.      goto Done;
  2065.    if (!tutorbmsimforce())
  2066.      goto Done;
  2067.    if (!tutorbmsimcntrlforce())
  2068.      goto Done;
  2069.    if (!tutorbmsimmotionsim())
  2070.      goto Done;
  2071.    if (!tutorbmsimgetcoordvel())
  2072.      goto Done;
  2073.    if (!tutorbmsimenergy())
  2074.      goto Done;
  2075. Done:
  2076.    setviewport(1,1,639,479,1);
  2077.    cleardevice();
  2078. }
  2079.