home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 3 / CD_Magazyn_EXEC_nr_3.iso / Recent / docs / mags / saku33d2.lha / Ohjelmat / Cantor1.c < prev    next >
C/C++ Source or Header  |  2000-07-16  |  10KB  |  345 lines

  1.  
  2. /*
  3. Kaikki kokonaisluvut n=0,1,2,...,miljoona,...,n+1,...,miljardi^miljardi,...
  4. ovat kuvattavissa yksikäsitteisesti mille tahansa välille a:sta b:hen esim.
  5. kuvauksella f(n)=b-(b-a)/(n+1), vaikka a ja b olisivat kuinka lähellä
  6. toisiaan esim. a=1/(miljardi+1) ja b=1/miljardi.
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <intuition/intuition.h>
  13. #include <intuition/screens.h>
  14. #include <clib/exec_protos.h>
  15. #include <clib/graphics_protos.h>
  16. #include <clib/intuition_protos.h>
  17. #include <graphics/rastport.h>
  18. #include <graphics/view.h>
  19.  
  20. typedef struct Screen Screen;
  21. typedef struct Window Window;
  22. typedef struct IntuiMessage IntuiMessage;
  23. Screen *Scr;
  24. Window *Win;
  25. #define depth 5
  26. int cols;
  27. struct TmpRas TmpRas;
  28. PLANEPTR Pointer;
  29. double koord[10];
  30. UBYTE AreaBuffer[(5+1)*5];
  31. struct AreaInfo AreaInfo;
  32. #define MAXX 640
  33. #define RASX 640
  34. #define MAXY 256
  35. #define scrid  0x8000 //0x0000 //0x89024
  36. char buf1[256];
  37. char title[256]={"Kokonaislukukuvaus N->[a...b]: f(n)=b-(b-a)/(n+1)"};
  38. struct NewScreen Ns = {
  39.   0,            /* LeftEdge */
  40.   0,            /* TopEdge */
  41.   MAXX,         /* Width   */
  42.   MAXY,         /* Height  */
  43.   depth,        /* Depth   */
  44.   0,            /* DetailPen */
  45.   1,            /* BlockPen  */
  46.   scrid,      /* ViewModes */
  47.   CUSTOMSCREEN, /* Type */
  48.   NULL,         /* Font */
  49.   title,
  50.   NULL,         /* Gadget */
  51.   NULL          /* BitMap */
  52. };
  53. struct NewWindow Nw =  {
  54.   0,            /* LeftEdge */
  55.   0,            /* TopEdge  */
  56.   MAXX,         /* Width  */
  57.   MAXY,         /* Height */
  58.   0,            /* DetailPen */
  59.   1,            /* BlockPen   */
  60.   IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS, /* IDCMPFlags */
  61.   WFLG_CLOSEGADGET|  /* Flags*/
  62.   WFLG_ACTIVATE|
  63.   WFLG_BORDERLESS|
  64.   WFLG_DRAGBAR,
  65.   NULL,         /* FirstGadget  Ei käyttöä. */
  66.   NULL,         /* CheckMark    Ei customia */
  67.   title,
  68.   NULL,         /* Screen       Asetetaan screenin avauksen jälkeen*/
  69.   NULL,         /* BitMap       Ei custom-bitmappia */
  70.   0,            /* MinWidth     Ei käyttöä koska ei SIZEGADGETtiä*/
  71.   0,            /* MinHeight    */
  72.   0,            /* MaxWidth     */
  73.   0,            /* MaxHeight    */
  74.   CUSTOMSCREEN  /* Type*/
  75. };
  76. struct TextFont *myfont;
  77. struct Library *DiskfontBase;
  78. #define fkor 8
  79. #define RP   Win->RPort
  80. #define BText Text(RP,buf1,strlen(buf1))
  81. struct TextAttr myfontattr=           /* vaihda toinen fontti...*/
  82. {      /* tässä käytetään Topazia vain siksi että ohjelma ei kaadu fontin
  83.                                                              puutteeseen*/
  84.   "Topaz.font", /* Name of the font.  */
  85.   fkor,          /* Height (in pixels) */
  86.   FS_NORMAL,   /* Style              */
  87.   FPF_DISKFONT /* Exist on Disk.     */
  88. };
  89.  
  90. int lkm=20; //0..lkm-1
  91. int maxx=MAXX-5*fkor;
  92. double yk,yma,ymy;
  93. double tx,mtx,mtx0;
  94. double tl;
  95. double i,a=0.8,b=0.9,fi;
  96.  
  97. void myexit(void);
  98. void Piirra(void);
  99. void main(ac, av)
  100. int ac;
  101. char *av[];
  102. {
  103.     short notDone = 1;
  104.     atexit(myexit);
  105.  
  106.     if (ac == 1) {
  107.         puts("cantor1.exe <lkm> <a> <b>, jossa a<b esim. cantor1.exe 20 18.3 18.8");
  108.         exit(1);
  109.     }
  110.     lkm=atof(av[1]);
  111.     a=atof(av[2]);
  112.     b=atof(av[3]);
  113.    
  114.  
  115.     if (a>=b) {
  116.         puts("a<b");
  117.         exit(1);
  118.     }
  119.  
  120.     if (lkm<1) {
  121.         puts("lkm>1");
  122.         exit(1);
  123.     }
  124.  
  125.     if (Scr = OpenScreen(&Ns)) {
  126.         Nw.TopEdge= 0;
  127.         Nw.Height = Scr->Height - Nw.TopEdge;
  128.         Nw.Width  = Scr->Width;
  129.  
  130.         Nw.Screen = Scr;
  131.         cols=fpow(2.0,depth);
  132.  
  133.         if (Win = OpenWindow(&Nw))  {
  134. //Tehty RGBmaker:lla
  135. SetRGB32(&Scr->ViewPort,0,0<<24,0<<24,0<<24);
  136. SetRGB32(&Scr->ViewPort,1,255<<24,255<<24,255<<24);
  137. SetRGB32(&Scr->ViewPort,2,255<<24,251<<24,246<<24);
  138. SetRGB32(&Scr->ViewPort,3,255<<24,247<<24,237<<24);
  139. SetRGB32(&Scr->ViewPort,4,255<<24,243<<24,228<<24);
  140. SetRGB32(&Scr->ViewPort,5,255<<24,239<<24,219<<24);
  141. SetRGB32(&Scr->ViewPort,6,255<<24,234<<24,209<<24);
  142. SetRGB32(&Scr->ViewPort,7,255<<24,230<<24,200<<24);
  143. SetRGB32(&Scr->ViewPort,8,255<<24,226<<24,191<<24);
  144. SetRGB32(&Scr->ViewPort,9,255<<24,222<<24,182<<24);
  145. SetRGB32(&Scr->ViewPort,10,255<<24,218<<24,173<<24);
  146. SetRGB32(&Scr->ViewPort,11,255<<24,214<<24,164<<24);
  147. SetRGB32(&Scr->ViewPort,12,255<<24,210<<24,155<<24);
  148. SetRGB32(&Scr->ViewPort,13,255<<24,206<<24,146<<24);
  149. SetRGB32(&Scr->ViewPort,14,255<<24,202<<24,137<<24);
  150. SetRGB32(&Scr->ViewPort,15,255<<24,197<<24,127<<24);
  151. SetRGB32(&Scr->ViewPort,16,255<<24,193<<24,118<<24);
  152. SetRGB32(&Scr->ViewPort,17,255<<24,189<<24,109<<24);
  153. SetRGB32(&Scr->ViewPort,18,255<<24,185<<24,100<<24);
  154. SetRGB32(&Scr->ViewPort,19,255<<24,181<<24,91<<24);
  155. SetRGB32(&Scr->ViewPort,20,255<<24,177<<24,82<<24);
  156. SetRGB32(&Scr->ViewPort,21,255<<24,173<<24,73<<24);
  157. SetRGB32(&Scr->ViewPort,22,255<<24,169<<24,64<<24);
  158. SetRGB32(&Scr->ViewPort,23,255<<24,165<<24,55<<24);
  159. SetRGB32(&Scr->ViewPort,24,255<<24,161<<24,46<<24);
  160. SetRGB32(&Scr->ViewPort,25,255<<24,156<<24,36<<24);
  161. SetRGB32(&Scr->ViewPort,26,255<<24,152<<24,27<<24);
  162. SetRGB32(&Scr->ViewPort,27,255<<24,148<<24,18<<24);
  163. SetRGB32(&Scr->ViewPort,28,255<<24,144<<24,9<<24);
  164. SetRGB32(&Scr->ViewPort,29,255<<24,140<<24,0<<24);
  165. SetRGB32(&Scr->ViewPort,30,153<<24,50<<24,204<<24);
  166. SetRGB32(&Scr->ViewPort,31,139<<24,0<<24,0<<24);
  167.  
  168.  
  169.             DiskfontBase = (struct DiskfontBase *)
  170.             OpenLibrary( "diskfont.library", 0 );
  171.  
  172.             myfont = (struct TextFont *)
  173.             OpenDiskFont( &myfontattr );
  174.  
  175.             SetFont(RP, myfont );
  176.                     Pointer=AllocRaster(RASX,MAXY);
  177.             InitTmpRas(&TmpRas,Pointer,RASSIZE(RASX,MAXY));
  178.             RP->TmpRas=&TmpRas;
  179.  
  180.             InitArea(&AreaInfo,AreaBuffer,(4+1)*5);
  181.             RP->AreaInfo=&AreaInfo;
  182.             Piirra();
  183.               while (notDone) {
  184.                       IntuiMessage *imsg;
  185.                       WaitPort(Win->UserPort);
  186.                       while (imsg = (IntuiMessage *)GetMsg(Win->UserPort)) {
  187.                           switch(imsg->Class) {
  188.                           case IDCMP_CLOSEWINDOW:
  189.                               notDone = 0;
  190.                               break;
  191.                           case IDCMP_MOUSEBUTTONS:
  192.                               a=a++;b++;
  193.                               Piirra();
  194.                           }
  195.                           ReplyMsg(&imsg->ExecMessage);
  196.                          
  197.                       }
  198.               } //while notDone
  199.         } // Win
  200.     }//Scr
  201.  
  202. }
  203. void myexit(void)
  204. {
  205.  
  206.     if (Win) {
  207.         CloseWindow(Win);
  208.         Win = NULL;
  209.     }
  210.     if (Scr) {
  211.         CloseScreen(Scr);
  212.         Scr = NULL;
  213.     }
  214.  
  215.     if (Pointer) FreeMem(Pointer,RASSIZE(RASX,MAXY));
  216.  
  217.     if( myfont )
  218.     CloseFont( myfont );
  219.  
  220.     if( DiskfontBase )
  221.     CloseLibrary( DiskfontBase );
  222. }
  223.  
  224. void Piirra(void)
  225. {
  226.             SetAPen(RP,0);
  227.             RectFill(RP,0,10,MAXX,MAXY);
  228.             yk=MAXY/3;
  229.             ymy=12+3*fkor;
  230.             yma=yk+yk-ymy;
  231.             mtx0=0.05*maxx;
  232.             tl=TextLength(RP,"f(345)=89.012345BB",17);
  233.  
  234.              SetAPen(RP,cols-1);
  235.               AreaMove(RP,mtx0,yma-4);
  236.               AreaDraw(RP,maxx-mtx0,yma-4);
  237.               AreaDraw(RP,(b/lkm)*maxx,yk+2);
  238.               AreaDraw(RP,(a/lkm)*maxx,yk+2);
  239.               AreaDraw(RP,mtx0,yma-4);
  240.               AreaEnd(RP);
  241.  
  242.               RectFill(RP,koord[0],koord[1],koord[2],koord[3]+4);
  243.               SetAPen(RP,cols-2);
  244.               RectFill(RP,koord[6],koord[7]-4,koord[4],koord[5]);
  245.               SetAPen(RP,cols-1);
  246.  
  247.               AreaMove(RP,(a/lkm)*maxx,yk-2);
  248.               AreaDraw(RP,(b/lkm)*maxx,yk-2);
  249.               AreaDraw(RP,maxx-mtx0,ymy+4);
  250.               AreaDraw(RP,mtx0,ymy+4);
  251.               AreaDraw(RP,(a/lkm)*maxx,yk-2);
  252.               AreaEnd(RP);
  253.  
  254.               RectFill(RP,koord[6],ymy+1,koord[4],ymy+4);
  255.               int rlkm=(MAXY-yma)/fkor-1;
  256.               for (i=0;i<=lkm;i++)
  257.               {
  258.                   fi=b-((b-a)/(i+1.0));
  259.                   tx=maxx*(fi-a)/(b-a);
  260.                   mtx=mtx0+0.9*maxx*(fi-a)/(b-a);
  261.  
  262.                   //f(n)=14.23434
  263.  
  264.                   SetAPen(RP,(int)(i+1.1));
  265.                   sprintf(buf1,"f(%d)=%lf",(int)i,fi);
  266.                   Move(RP,mtx,yma+2*fkor+fkor*((int)i%rlkm));
  267.                   BText; //Text(RP,buf1,strlen(buf1));
  268.  
  269.                   Move(RP,(i/lkm)*maxx,yk-2);     //kokonaisluku koord
  270.                   Draw(RP,(i/lkm)*maxx,yk+2);
  271.  
  272.                   
  273.                   Draw(RP,mtx,yma-4); //murtoluku koord  ok
  274.                   Draw(RP,mtx,yma+4);
  275.  
  276.                   Move(RP,(fi/lkm)*maxx,yk+2); //murtoluku koord kokonaislukuaks.
  277.                   Draw(RP,(fi/lkm)*maxx,yk-2);
  278.                   Draw(RP,mtx,ymy+4);
  279.                   Draw(RP,mtx,ymy-4);
  280.               }
  281.  
  282.  
  283.               for (i=0;i<=lkm;i++)
  284.               {
  285.                   SetAPen(RP,(int)(i+1));           //kokonaisluvut siirrettiin alusta tänne
  286.                   sprintf(buf1,"%d",(int)i);
  287.                   Move(RP,(i/lkm)*maxx,yk-fkor);
  288.                   BText;
  289.  
  290.               }
  291.  
  292.               SetAPen(RP,cols-3);    //  Vaakaviivat
  293.               Move(RP,0,yk);    //kokonaisluvut
  294.               Draw(RP,MAXX,yk);
  295.  
  296.               SetAPen(RP,cols-2);
  297.               Move(RP,mtx0,yma);    //murtoluvut alh.
  298.               Draw(RP,maxx-mtx0,yma);
  299.  
  300.               SetAPen(RP,cols-2);
  301.               Move(RP,mtx0,ymy);    //murtoluvut ylh.
  302.               Draw(RP,maxx-mtx0,ymy);
  303.  
  304.               for(i=a; i<=b; i=i+((b-a)/10))
  305.               {
  306.                  //murtoluku-akseli ylhäällä
  307.                  mtx=mtx0+0.9*maxx*(i-a)/(b-a);
  308.  
  309.                  sprintf(buf1,"%1.2lf",i);
  310.                  Move(RP,mtx,ymy-fkor);
  311.                  BText;
  312.  
  313.                  Move(RP,mtx,ymy+2);
  314.                  Draw(RP,mtx,ymy-2);
  315.  
  316.                  //murtolukuakseli alhaalla
  317.                  sprintf(buf1,"%1.2lf",i);
  318.                  Move(RP,mtx,yma+fkor);
  319.                  BText;
  320.  
  321.                  Move(RP,mtx,yma+2);
  322.                  Draw(RP,mtx,yma-2);
  323.               }
  324.               //-------------------------------------------------------------
  325.               
  326.               i=lkm; 
  327.               rlkm=rlkm-1;
  328.               int sara;
  329.               do {
  330.                   fi=b-((b-a)/i);
  331.                   
  332.                   SetAPen(RP,1+(int)i%(cols-4));
  333.                   sprintf(buf1,"f(%d)=%lf",(int)(i+1),fi);
  334.                   sara=(int)((i-lkm)/rlkm);
  335.                   Move(RP,tl*sara,yma+(3+sara)*fkor+fkor*((int)(i-lkm)%rlkm));
  336.                   BText;
  337.                   i++;
  338.               } while(tl*(int)((i-lkm)/rlkm)<maxx-20*fkor);
  339.               //
  340.  
  341.  
  342.  
  343. }
  344.  
  345.