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

  1.  
  2. /* Käytetty Cantor artikkelin (Zöözi kanttori) kuvien tuottamiseen
  3. parametrina annetaan - tai ei anneta - 1, 2 tai 3...*/
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <intuition/intuition.h>
  9. #include <intuition/screens.h>
  10. #include <clib/exec_protos.h>
  11. #include <clib/graphics_protos.h>
  12. #include <clib/intuition_protos.h>
  13. #include <graphics/rastport.h>
  14. #include <graphics/view.h>
  15. typedef struct Screen Screen;
  16. typedef struct Window Window;
  17. typedef struct IntuiMessage IntuiMessage;
  18. Screen *Scr;
  19. Window *Win;
  20. #define MAXX 640
  21. #define MAXY 256
  22. #define depth 4
  23. #define scrid  0x8000 //0x0000 // //0x89024
  24. #define RP   Win->RPort
  25. int cols;
  26. struct TmpRas TmpRas;
  27. PLANEPTR Pointer;
  28. #define RASX MAXX
  29. double koord[10]; 
  30. UBYTE AreaBuffer[(5+1)*5];
  31. struct AreaInfo AreaInfo;
  32.  
  33. struct Library *DiskfontBase;
  34. #define fkor 8
  35. struct TextAttr myfontattr=     /* vaihda toinen fontti ... */
  36. {
  37.   "Topaz.font", /* Name of the font.  */
  38.   fkor,          /* Height (in pixels) */
  39.   FS_NORMAL,   /* Style              */
  40.   FPF_DISKFONT /* Exist on Disk.     */
  41. };
  42.  
  43. struct TextFont *myfont;
  44. char title[256]={"Murtolukujen numerointi."};
  45. struct NewScreen Ns = {
  46.   0,            /* LeftEdge */
  47.   0,            /* TopEdge */
  48.   MAXX,         /* Width   */
  49.   MAXY,         /* Height  */
  50.   depth,        /* Depth   */
  51.   0,            /* DetailPen */
  52.   4,            /* BlockPen  */
  53.   scrid,      /* ViewModes */
  54.   CUSTOMSCREEN, /* Type */
  55.   NULL,         /* Font */
  56.   title,         /* Title */
  57.   NULL,         /* Gadget */
  58.   NULL          /* BitMap */
  59. };
  60. struct NewWindow Nw =  {
  61.   0,            /* LeftEdge */
  62.   0,            /* TopEdge  */
  63.   MAXX,         /* Width  */
  64.   MAXY,         /* Height */
  65.   0,            /* DetailPen */
  66.   1,            /* BlockPen   */
  67.   IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS, /* IDCMPFlags */
  68.   WFLG_CLOSEGADGET|  /* Flags*/
  69.   WFLG_ACTIVATE|
  70.   WFLG_BORDERLESS,
  71.   NULL,         /* FirstGadget  Ei käyttöä. */
  72.   NULL,         /* CheckMark    Ei customia */
  73.   title,        /* Title */
  74.   NULL,         /* Screen       Asetetaan screenin avauksen jälkeen*/
  75.   NULL,         /* BitMap       Ei custom-bitmappia */
  76.   0,            /* MinWidth     Ei käyttöä koska ei SIZEGADGETtiä*/
  77.   0,            /* MinHeight    */
  78.   0,            /* MaxWidth     */
  79.   0,            /* MaxHeight    */
  80.   CUSTOMSCREEN  /* Type*/
  81. };
  82. double slev0,slev,rkor,rkor0;
  83. float sX(float s)
  84. {
  85.     return(slev0+s*slev);
  86. }
  87. float rY(float r)
  88. {
  89.     return(rkor0+r*rkor);
  90.  
  91. }
  92. float kiertoX(float alfa,float x,float y)
  93. {
  94.     x=slev*x;
  95.     y=slev*y;
  96.     return(cos(alfa)*x+sin(alfa)*y*1.9);
  97.  
  98. }
  99. float kiertoY(float alfa,float x,float y)
  100. {
  101.     x=slev*x;
  102.     y=slev*y;
  103.     return(-sin(alfa)*x/1.9+cos(alfa)*y);
  104.  
  105. }
  106. void myexit(void);
  107. main(ac, av)
  108. int ac;
  109. char *av[];
  110. {
  111.     printf("Cantor2.exe 1, 2 tai 3\n");
  112.     int para=atoi(av[1]);
  113.     if (para < 1 || para>3 || ac==1) para=3;
  114.  
  115.     short notDone = 1;
  116.     atexit(myexit);
  117.     if (Scr = OpenScreen(&Ns)) {
  118.         Nw.Screen = Scr;
  119.         cols=fpow(2.0,depth);
  120.         if (Win = OpenWindow(&Nw)) {
  121.             Pointer=AllocRaster(RASX,MAXY);
  122.             InitTmpRas(&TmpRas,Pointer,RASSIZE(RASX,MAXY));
  123.             RP->TmpRas=&TmpRas;
  124.  
  125.             InitArea(&AreaInfo,AreaBuffer,(4+1)*5);
  126.             RP->AreaInfo=&AreaInfo;
  127. //Tehty RGBmaker:lla
  128. SetRGB32(&Scr->ViewPort,0,28<<24,63<<24,163<<24);
  129. SetRGB32(&Scr->ViewPort,1,6<<24,90<<24,185<<24);
  130. SetRGB32(&Scr->ViewPort,2,246<<24,162<<24,109<<24);
  131. SetRGB32(&Scr->ViewPort,3,236<<24,246<<24,223<<24);
  132. SetRGB32(&Scr->ViewPort,4,83<<24,183<<24,144<<24);
  133. SetRGB32(&Scr->ViewPort,5,255<<24,255<<24,255<<24);
  134. SetRGB32(&Scr->ViewPort,6,126<<24,73<<24,67<<24);
  135. SetRGB32(&Scr->ViewPort,7,200<<24,89<<24,57<<24);
  136. SetRGB32(&Scr->ViewPort,8,236<<24,221<<24,11<<24);
  137. SetRGB32(&Scr->ViewPort,9,51<<24,178<<24,86<<24);
  138. SetRGB32(&Scr->ViewPort,10,79<<24,255<<24,0<<24);
  139. SetRGB32(&Scr->ViewPort,11,222<<24,186<<24,188<<24);
  140. SetRGB32(&Scr->ViewPort,12,113<<24,165<<24,28<<24);
  141. SetRGB32(&Scr->ViewPort,13,10<<24,228<<24,132<<24);
  142. SetRGB32(&Scr->ViewPort,14,155<<24,49<<24,255<<24);
  143. SetRGB32(&Scr->ViewPort,15,24<<24,139<<24,214<<24);
  144. SetRGB32(&Scr->ViewPort,16,0<<24,0<<24,0<<24);
  145.  
  146.             SetAPen(RP,0);                  /* ruudun tyhjennys */
  147.             RectFill(RP,0,0,MAXX-1 ,MAXY-1);
  148.  
  149.             DiskfontBase = (struct DiskfontBase *)
  150.             OpenLibrary( "diskfont.library", 0 );
  151.             if( !DiskfontBase )
  152.             myexit();
  153.  
  154.             
  155.             myfont = (struct TextFont *) OpenDiskFont( &myfontattr );
  156.             if (!myfont) {
  157.                 printf("Fontti puuttuu\n"); myexit();
  158.             }
  159.             SetFont(RP, myfont );
  160.             SetDrMd(RP,0);
  161.  
  162.             double maxx=MAXX,maxy=MAXY;
  163.             char buf1[256];
  164.             int rvari=5,svari=3;
  165.  
  166.             
  167.  
  168.             slev=1.2*TextLength(RP,"99/99",5);
  169.             slev0=TextLength(RP,"99",2);
  170.             rkor=(0.2+para+(para==3))*fkor;
  171.             rkor0=12+fkor;
  172.  
  173.             double sara=maxx/slev, rivi=maxy/rkor;
  174.  
  175.             int i,s=1,r=1, edr=1,eds=1,dr=-1,ds=1,p;
  176.             float nkx,nky,alfa;
  177.             short neg;
  178.             i=1;
  179.             if (para==1) goto yli1;
  180.             do {
  181.                 SetAPen(RP,7);              //suunta viivat
  182.  
  183.                 nky=(para==3)*0.25+0.25;   //0.25 tai 0.5
  184.                 nkx=fkor*(0.6+0.6*(para==3));
  185.                 for(p=-1;p<2;p++) {
  186.                 Move(RP,sX(s-0.5),rY(r-nky)+p);
  187.                 Draw(RP,sX(eds-0.5),rY(edr-nky)+p);
  188.                 }
  189.                 for(p=-2;p<3;p++) {
  190.                 Move(RP,p+sX(s-0.5),rY(r-nky));
  191.                 Draw(RP,p+sX(eds-0.5),rY(edr-nky));
  192.                 }
  193.                                             //järj. numero ellipsi
  194.                 AreaEllipse(RP,sX(s-0.5),rY(r-nky),1.5*fkor,(0.5+0.5*(para==3))*fkor);
  195.                 AreaEnd(RP);
  196.  
  197.                 if (r>1 || s>1) {
  198.                 nkx=(sX(eds-0.5)+sX(s-0.5))/2;
  199.                 float nkyapu=nky;
  200.                 nky=(rY(edr-nkyapu)+rY(r-nkyapu))/2;//suuntanuolet
  201.  
  202.                 if (eds != s ) alfa=fatan(-(r-edr)/(s-eds));
  203.                 else alfa=fatan(-99);
  204.                 neg=1-2*(eds>s);
  205.  
  206.                 SetAPen(RP,8);
  207.  
  208. Move(RP,nkx+neg*kiertoX(alfa,-0.1,0.07),nky+neg*kiertoY(alfa,-0.1,0.07));
  209. Draw(RP,nkx+neg*kiertoX(alfa,0.3,0),nky+neg*kiertoY(alfa,0.3,0));
  210. Draw(RP,nkx+neg*kiertoX(alfa,-0.1,-0.07),nky+neg*kiertoY(alfa,-0.1,-0.07));
  211. }
  212.  
  213.                 edr=r;
  214.                 eds=s;
  215.  
  216.                 r=r+dr;
  217.                 s=s+ds;
  218.  
  219.                 if (r<1) {r=1; dr=1; ds=-1;}
  220.                 if (s<1) {s=1; ds=1; dr=-1;}
  221.  
  222.             } while(r<rivi || s<sara);
  223.             //Sama luuppi uudestaan numerointia varten ja tutkitaan jaollisuus
  224.             double kkapu;
  225.             int a,b,c,syti;
  226.             r=1, s=1;
  227.             edr=1,eds=1,dr=-1,ds=1;
  228.             i=1; syti=0;
  229.             int dx,dy;
  230.             do {
  231.  
  232.                 SetAPen(RP,6);
  233.                 sprintf(buf1,"%d",i++);      //järjestysnumero
  234.                 kkapu=sX(s-1)+(slev-TextLength(RP,buf1,strlen(buf1)))/2;
  235.                 Move(RP,kkapu,rY(r-1)+2*fkor);
  236.                 Text(RP,buf1,strlen(buf1));
  237.                 if (para<3) goto yli3;
  238.                 // Tutkitaan onko yhteisiä tekijöitä, jolloin luku on
  239.                 // jo lueteltu: i/j luetellaan aina ennen kuin (n*i)/(n*j)
  240.                 // missä n=syt(i,j)
  241.                 a=r;b=s;
  242.                 if (a<b) {
  243.                     a=s;
  244.                     b=r;
  245.                 }
  246.                 c=1;
  247.                 while(c!=0) {    //a:han tulee a:n ja b:n suurin yhteinen
  248.                                  //tekijä joka voidaan sieventää a/b:stä
  249.                     c=a%b;
  250.                     a=b;
  251.                     b=c;
  252.                 }
  253.  
  254.                 if (a!=1) {
  255.                     b=s/a;
  256.                     a=r/a;
  257.                 }
  258.                 else   {
  259.                     a=r;
  260.                     b=s;
  261.                     syti++;
  262.                 }
  263.                 SetAPen(RP,2);
  264.                 sprintf(buf1,"%d",syti);   // uusi järjestysnumero
  265.                 kkapu=sX(s-1)+(slev-TextLength(RP,buf1,strlen(buf1)))/2;
  266.                 Move(RP,kkapu,rY(r-1)+3*fkor);
  267.                 Text(RP,buf1,strlen(buf1));
  268.  
  269.                 // sievennetyt r/s
  270.                 if (b!=1) {
  271.                 sprintf(buf1,"%d/%d",a,b);
  272.                 } else {
  273.                     sprintf(buf1,"%d",a);
  274.                 }
  275.                 kkapu=sX(s-1)+(slev-TextLength(RP,buf1,strlen(buf1)))/2;
  276.  
  277.                 sprintf(buf1,"%d",a);
  278.                 Move(RP,kkapu,rY(r-1)+4*fkor);
  279.  
  280.                 SetAPen(RP,rvari-1);
  281.                 Text(RP,buf1,strlen(buf1));
  282.                 if (b!=1) {
  283.                     SetAPen(RP,1);
  284.                     Text(RP,"/",1);
  285.                     sprintf(buf1,"%d",b);
  286.                     SetAPen(RP,svari+6);
  287.                     Text(RP,buf1,strlen(buf1));
  288.                 }
  289. yli3:           edr=r;
  290.                 eds=s;
  291.  
  292.                 r=r+dr;
  293.                 s=s+ds;
  294.                 if (r<1) {r=1; dr=1; ds=-1;}
  295.                 if (s<1) {s=1; ds=1; dr=-1;}
  296.             } while(r<rivi || s<sara);
  297.  
  298. yli1:
  299.  
  300.             for (r=0; r<rivi;r++) {
  301.                 SetAPen(RP,1);               //x-viivat
  302.                 Move(RP,slev0,rY(r));
  303.                 Draw(RP,maxx,rY(r));
  304.  
  305.                 SetAPen(RP,rvari);              //rivi-numerot 0-sarakkeella
  306.                 sprintf(buf1,"%d",r+1);
  307.                 Move(RP,0,rY(r+1)-(rkor-fkor)/2);
  308.                 Text(RP,buf1,strlen(buf1));
  309.             }
  310.  
  311.             for (s=0; s<sara;s++)  {        //y-viivat
  312.                 SetAPen(RP,1);
  313.                 Move(RP,sX(s),rkor0);
  314.                 Draw(RP,sX(s),maxy);
  315.  
  316.                 SetAPen(RP,svari);             //sarake-numerot 0-rivillä
  317.                 sprintf(buf1,"%d",s+1);
  318.                 Move(RP,sX(s)+(slev-TextLength(RP,buf1,strlen(buf1)))/2,0.9*rkor0);
  319.                 Text(RP,buf1,strlen(buf1));
  320.  
  321.                 int vi=7,vj=6;
  322.                 for (r=0; r<rivi;r++) {    // i/j
  323.                     /*       Tarvittiin neliöiden värityksessä kuvaan 5
  324.                     SetAPen(RP,0);
  325.                     if (s+1<vi+vj && r+1<vi+vj) {
  326.                         SetAPen(RP,9);
  327.                     }
  328.                     if(r+1<=vi && s+1<=vj) {
  329.                         SetAPen(RP,2);
  330.                     }
  331.                     if (r+s+2==vi+vj) {
  332.                         SetAPen(RP,vj);
  333.                     }
  334.                     if (s+1>vj && r+s+2<vi+vj) {
  335.                         SetAPen(RP,7);
  336.                     }
  337.                     if (r+1>vi && r+s+2<vi+vj) {
  338.                         SetAPen(RP,8);
  339.                     }
  340.                     if (r+1==vi && s+1==vj) {
  341.                         SetAPen(RP,10);
  342.                     }
  343.                    
  344.                     RectFill(RP,sX(s)+1,rY(r)+1,sX(s+1)-1,rY(r+1)-1);
  345.                     */
  346.  
  347.                     sprintf(buf1,"%d/%d",r+1,s+1);
  348.                     kkapu=sX(s)+(slev-TextLength(RP,buf1,strlen(buf1)))/2;
  349.  
  350.                     sprintf(buf1,"%d",r+1);
  351.                     Move(RP,kkapu,rY(r)+fkor);
  352.  
  353.                     SetAPen(RP,rvari);
  354.                     Text(RP,buf1,strlen(buf1));
  355.                     SetAPen(RP,1);
  356.                     Text(RP,"/",1);
  357.                     sprintf(buf1,"%d",s+1);
  358.                     SetAPen(RP,svari);
  359.                     Text(RP,buf1,strlen(buf1));
  360.  
  361.                 }
  362.             }
  363.  
  364. if (para<3) goto yli2;
  365.             // vielä kerran peittoamista varten
  366.  
  367.             r=1; s=1; dr=-1; ds=1; i=1; syti=0;
  368.             do {
  369.                 i++;
  370.                 a=r;b=s;
  371.                 if (a<b) {
  372.                     a=s;
  373.                     b=r;
  374.                 }
  375.                 c=1;
  376.                 while(c!=0) {    //a:han tulee a:n ja b:n suurin yhteinen
  377.                                  //tekijä joka voidaan sieventää a/b:stä
  378.                     c=a%b;
  379.                     a=b;
  380.                     b=c;
  381.                 }
  382.  
  383.                 if (a!=1) {
  384.                     b=s/a;
  385.                     a=r/a;
  386.                     if (r<=rivi && s<=sara) {
  387.                     SetAPen(RP,10);
  388.                     for(dy=rY(r-1); dy<=rY(r); dy=dy+2)
  389.                         for(dx=sX(s-1)+2*(dy%4); dx<=sX(s); dx=dx+8)
  390.                             WritePixel(RP,dx,dy);
  391.                     }
  392.                 }   /*
  393.                 else   {
  394.                     a=r;
  395.                     b=s;
  396.                     syti++;
  397.                 } */
  398.  
  399.                 edr=r;
  400.                 eds=s;
  401.  
  402.                 r=r+dr;
  403.                 s=s+ds;
  404.                 if (r<1) {r=1; dr=1; ds=-1;}
  405.                 if (s<1) {s=1; ds=1; dr=-1;}
  406.             } while(r<rivi || s<sara);
  407.  
  408.             //myfontattr.ta_YSize=11;
  409.             //myfont = (struct TextFont *) OpenDiskFont(&myfontattr);
  410.             //SetFont(RP, myfont );
  411. yli2:
  412.             while (notDone) {   // "Pääluuppi" jossa ei tehdä mitään
  413.                 IntuiMessage *imsg;
  414.                 WaitPort(Win->UserPort);
  415.                 while (imsg = (IntuiMessage *)GetMsg(Win->UserPort)) {
  416.                     switch(imsg->Class) {
  417.                     case IDCMP_CLOSEWINDOW:
  418.                         notDone = 0;
  419.                         break;
  420.                     }
  421.                     ReplyMsg(&imsg->ExecMessage);
  422.             }
  423.          }
  424.       }
  425.     }
  426.     return(0);
  427. }
  428.  
  429. void myexit(void)
  430. {
  431.     if (Win) {
  432.         CloseWindow(Win);
  433.         Win = NULL;
  434.     }
  435.     if (Scr) {
  436.         CloseScreen(Scr);
  437.         Scr = NULL;
  438.     }
  439.     if (Pointer) FreeMem(Pointer,RASSIZE(RASX,MAXY));
  440.  
  441.     if( myfont )
  442.     CloseFont( myfont );
  443.  
  444.     if( DiskfontBase )
  445.     CloseLibrary( DiskfontBase );
  446. }
  447.  
  448.