home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1997 #3 / amigamamagazinepolishissue03-1 / ma_1995 / 07 / ami003a < prev    next >
Text File  |  1997-04-15  |  3KB  |  234 lines

  1. /**********************************************************/
  2.  
  3. /*  Program liczy rozkîad potencjaîu (np. grawitacyjnego) */
  4.  
  5. /*      Napisane dla Magazynu Amiga   (c) 1995 by BsZ     */
  6.  
  7. /**********************************************************/
  8.  
  9. #include <ctype.h>
  10.  
  11. #include <fctype.h>
  12.  
  13. #include <functions.h>
  14.  
  15. #include <math.h>
  16.  
  17. #include <stdio.h>
  18.  
  19. #include <stdlib.h>
  20.  
  21. #include <exec/types.h>
  22.  
  23. #include <intuition/intuition.h>
  24.  
  25. #include <time.h>
  26.  
  27. #define DEPTH 4
  28.  
  29. #define COLORS 16
  30.  
  31. #define WIDTH 640
  32.  
  33. #define HEIGHT 512
  34.  
  35. #define N_MAX 1000    /* maksymalna iloôê punktów */
  36.  
  37. struct Screen *scr;
  38.  
  39. struct IntuitionBase *IntuitionBase;
  40.  
  41. struct GfxBase *GfxBase;
  42.  
  43. struct NewScreen scr_data={
  44.  
  45.     0,0,WIDTH,HEIGHT,DEPTH,1,1,HIRES|LACE,CUSTOMSCREEN,0,
  46.  
  47.     (UBYTE *)"Pole",0,0};
  48.  
  49. int N,X,Y,x[N_MAX],y[N_MAX],m[N_MAX];
  50.  
  51. float BRIGHT;
  52.  
  53. void niedobrze(void)
  54.  
  55. {
  56.  
  57.     if (scr) CloseScreen(scr);
  58.  
  59.     if (GfxBase) CloseLibrary((struct Library *)GfxBase);
  60.  
  61.     if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  62.  
  63.     exit(0);
  64.  
  65. }
  66.  
  67. float RND(void)
  68.  
  69. {
  70.  
  71.     return (float)(rand()/(float)RAND_MAX);
  72.  
  73. }
  74.  
  75. void losuj(void)
  76.  
  77. {
  78.  
  79.     int licznik;
  80.  
  81.     long int t;
  82.  
  83.  
  84.  
  85.     time(&t);
  86.  
  87.     srand((UWORD)t);
  88.  
  89.     for(licznik=0;licznik<N;licznik++)
  90.  
  91.     {
  92.  
  93.         x[licznik]=(int)(RND()*639+1);
  94.  
  95.         y[licznik]=(int)(RND()*511+1);
  96.  
  97.         m[licznik]=(int)(RND()*29+1);
  98.  
  99.     }
  100.  
  101. }
  102.  
  103. void OpenAllLibraries(void)
  104.  
  105. {
  106.  
  107.     IntuitionBase=(struct IntuitionBase *)OldOpenLibrary("intuition.library");
  108.  
  109.     if (!IntuitionBase) exit(100);
  110.  
  111.     GfxBase=(struct GfxBase *)OldOpenLibrary("graphics.library");
  112.  
  113.     if (!GfxBase) exit(100);
  114.  
  115. }
  116.  
  117. void paleta(void)
  118.  
  119. {
  120.  
  121.     ULONG i;
  122.  
  123.     for(i=0;i<COLORS;i++)
  124.  
  125.         SetRGB4(&scr->ViewPort, i, i, i, i);
  126.  
  127. }
  128.  
  129. int kolor(float c)
  130.  
  131. {
  132.  
  133.     if(BRIGHT*c>15.0)
  134.  
  135.         return 15;
  136.  
  137.     else
  138.  
  139.         return (int)(BRIGHT*c);
  140.  
  141. }
  142.  
  143. float v(int index)
  144.  
  145. {
  146.  
  147.     float r;
  148.  
  149.     if( (x[index]==X) && (y[index]==Y) ) return 1000.0; /* biaîy */
  150.  
  151.     r=(float)(sqrt( (double)( (x[index]-X)*(x[index]-X)+(y[index]-Y)*(y[index]-Y))));
  152.  
  153.     return( (float)(m[index]/r) );
  154.  
  155. } /* v() */
  156.  
  157. void main()
  158.  
  159. {
  160.  
  161.     int i;
  162.  
  163.     float tempv;
  164.  
  165.     printf("Podaj iloôê punktów ");
  166.  
  167.     scanf("%d",&N);
  168.  
  169.     printf("Podaj jasnoôê ");
  170.  
  171.     scanf("%f",&BRIGHT);
  172.  
  173.     losuj();
  174.  
  175.     OpenAllLibraries();
  176.  
  177.     if(!(scr=OpenScreen(&scr_data))) niedobrze();
  178.  
  179.     paleta();
  180.  
  181.     SetRast(&scr->RastPort,0);
  182.  
  183.     for(Y=1;Y<=HEIGHT;Y++)
  184.  
  185.     {
  186.  
  187.         if(scr->MouseX<5 && scr->MouseY<5)
  188.  
  189.         {
  190.  
  191.             niedobrze();
  192.  
  193.             break;
  194.  
  195.         }
  196.  
  197.         for(X=1;X<=WIDTH;X++)
  198.  
  199.         {
  200.  
  201.             tempv=0.0;
  202.  
  203.             for(i=0;i<N;i++)
  204.  
  205.                 tempv+=v(i);
  206.  
  207.             SetAPen(&scr->RastPort,kolor(tempv));
  208.  
  209.             WritePixel(&scr->RastPort,X,Y);
  210.  
  211.         }
  212.  
  213.     }
  214.  
  215.     for(;;)
  216.  
  217.     {
  218.  
  219.         if(scr->MouseX<5 && scr->MouseY<5)
  220.  
  221.         {
  222.  
  223.             niedobrze();
  224.  
  225.             break;
  226.  
  227.         }
  228.  
  229.     }
  230.  
  231. }
  232.  
  233.  
  234.