home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / turtle / turtle.c next >
Encoding:
C/C++ Source or Header  |  1988-12-31  |  2.5 KB  |  97 lines

  1. /*
  2. **    TITLE: Turtle Graphics Commands
  3. **   AUTHOR: Jerry Hobart
  4. ** COMPUTER: IBM-PC or compatible
  5. **       OS: PC-DOS / MS-DOS
  6. ** LANGUAGE: TURBO-C 2.0
  7. **  PURPOSE: This packages provides functions necessary to program
  8. **           using TURTLE graphics with Turbo C ver 1.5 or 2.0.
  9. **           Many commands normally found in TURTLE graphics already
  10. **           have a counterpart in the Borland graphics library, and
  11. **           thus were not included here.
  12. */
  13.  
  14.  
  15. #include <math.h>
  16. #include <graphics.h>
  17.  
  18. #define   UP 0
  19. #define DOWN 1
  20.  
  21. const double DEG2RAD=.01745329;
  22. double heading=0.0;          /* the heading is expressed in degrees */
  23.                              /* with the following orientation:     */
  24.                              /* 0   - towards top of screen         */
  25.                              /* 90  - towards right side of screen  */
  26.                              /* 180 - towards bottom of screen      */
  27.                              /* 270 - towards left side of screen   */
  28.  
  29. int pen = UP;                /* writing will only take place when   */
  30.                              /* the pen is DOWN                     */
  31.  
  32. double round(double x);      /* function prototype                  */
  33.  
  34. /**********************************************************************
  35. *                      function definitions                           *
  36. **********************************************************************/
  37.  
  38. void setheading(double deg){
  39.      heading=deg;
  40.       while (heading>=360.) heading-=360.;
  41.       while (heading<0.) heading+=360.;
  42. }
  43.  
  44. void right (double angl){
  45.      heading+=angl;
  46.       while (heading>=360.) heading-=360.;
  47.       while (heading<0.) heading+=360.;
  48. }
  49.  
  50. void left (double angl){
  51.      heading-=angl;
  52.      while (heading>=360.) heading-=360.;
  53.       while (heading<0.) heading+=360.;
  54. }
  55.  
  56. void forward(int dist){
  57.      int dx,dy;
  58.  
  59.       dx = round(dist * sin(heading * DEG2RAD));
  60.       dy = round(- dist * cos(heading * DEG2RAD));
  61.      if (pen == DOWN)
  62.              linerel (dx,dy);
  63.       else
  64.              moverel (dx,dy);
  65. }
  66.  
  67. void back(int dist){
  68.      int dx,dy;
  69.  
  70.       dx = round(dist * sin((heading+180.)*DEG2RAD));
  71.       dy = round(dist * cos(heading*DEG2RAD));
  72.      if (pen == DOWN)
  73.              linerel (dx,dy);
  74.       else
  75.              moverel (dx,dy);
  76. }
  77.  
  78. void penup(void){
  79.      pen = UP;
  80. }
  81.  
  82. void pendown(void){
  83.      pen = DOWN;
  84. }
  85.  
  86. double getheading(){
  87.      return heading;
  88. }
  89.  
  90. int getpen(){
  91.      return pen;
  92. }
  93.  
  94. double round(double x){
  95.      return floor(x + .5);
  96. }
  97.