home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 049.lha / 3dPlot / 3d-plot.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  6KB  |  275 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <functions.h>
  4. #include <exec/types.h>
  5. #include <intuition/intuition.h>
  6.  
  7. #define INTUITION_REV 0L  /* You must be sure this is correct */
  8. #define GRAPHICS_REV 0L
  9. #define RP Window->RPort
  10.  
  11. struct IntuitionBase *IntuitionBase;
  12. struct GfxBase *GfxBase;
  13. struct NewWindow NewWindow;
  14. struct Window *Window;
  15.  
  16. float hi,xx,yy,lo,xd,yd,y0,s,x0,x1;
  17. float xb,yb,ys,xa,ya,xs,lz,y1,z0,z1,x,y,z;
  18. float p1,p2,p3,p4,p5;
  19.  
  20. #define lz 20           /*  Linienzahl  */
  21. #define hh 192          /*  Vielfaches von 8  */
  22. #define br 384          /*     -"-         6  */
  23.  
  24. int o[br+1],u[br+1],px[lz+1][lz+1],py[lz+1][lz+1];
  25. int xp,yp,i,nx,ny,az,ph;
  26. char *klx,*grx,*kly,*gry,*klz,*grz;
  27. FILE *rwp;
  28.  
  29. main()
  30. {
  31.    IntuitionBase = (struct IntuitionBase *)
  32.       OpenLibrary("intuition.library",INTUITION_REV);
  33.    if(IntuitionBase == NULL)
  34.       exit(FALSE);
  35.  
  36.    GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",GRAPHICS_REV);
  37.    if(GfxBase == NULL)
  38.       exit(FALSE);
  39.  
  40.    /* Initialize the NewWindow structure for the call to OpenWindow() */
  41.  
  42.    NewWindow.LeftEdge = 0;
  43.    NewWindow.TopEdge = 0;
  44.    NewWindow.Width = 400;
  45.    NewWindow.Height = 200;
  46.    NewWindow.DetailPen = 0;
  47.    NewWindow.BlockPen = 1;
  48.    NewWindow.Title = NULL;
  49.    NewWindow.Flags = SMART_REFRESH;
  50.    NewWindow.IDCMPFlags = NULL;   /* Tell us about CLOSEWINDOW events */
  51.    NewWindow.Type = WBENCHSCREEN;
  52.    NewWindow.FirstGadget = NULL;
  53.    NewWindow.CheckMark = NULL;
  54.    NewWindow.Screen = NULL;
  55.    NewWindow.BitMap = NULL;
  56.    NewWindow.MinWidth = 100;
  57.    NewWindow.MinHeight = 25;
  58.    NewWindow.MaxWidth = 640;
  59.    NewWindow.MaxHeight = 200;
  60.  
  61.    klx = "Kleinstes x : " ; grx = "Größtes x : ";
  62.    kly = "Kleinstes y : " ; gry = "Größtes y : ";
  63.    klz = "KLeinstes z : " ; grz = "Größtes z : ";
  64.  
  65. /*    Eröffnen eines CON-Windows zur Eingabe  */
  66.  
  67.    rwp = fopen("CON:0/0/640/200/3D-Plot","w+");
  68.    Cbuffs[0]._unit = rwp->_unit;
  69.    Cbuffs[1]._unit = rwp->_unit;
  70.  
  71. Schleife:
  72.    printf("\f\n\n3D-Funktionen\n");
  73.  
  74.    for (i=0 ; i<=br-1 ; i++ )
  75.       o[i] = -1;
  76.  
  77.    printf("\n  Eingabe des Definitionsbereichs :\n");
  78.    printf("\n%s",klx) ; scanf("%f",&x0);
  79.    printf("%s",grx) ; scanf("%f",&x1);
  80.    printf("\n%s",kly) ; scanf("%f",&y0);
  81.    printf("%s",gry) ; scanf("%f",&y1);
  82.    printf("\nBetrachtungswinkel von der Seite:");
  83.    printf("\n(Azimut 0-80) ") ; scanf("%d",&az);
  84.    printf("\nBetrachtungswinkel Höhe (0-90): ") ; scanf("%d",&ph);
  85.  
  86.    p1=cos((float)(az*PI/180.0));
  87.    p2=sin((float)(az*PI/180.0));
  88.    p3=cos((float)(ph*PI/180.0));
  89.    p4=p1*sin((float)(ph*PI/180.0));
  90.    p5=p2*sin((float)(ph*PI/180.0));
  91.  
  92.    printf("\nIch suche nun Maximum und Minimum.\n");
  93.    xs=(x1-x0)/(float)lz ;
  94.    ys=(y1-y0)/(float)lz;
  95.    x=x0 ;
  96.    y=y0;
  97.  
  98.    Funktion();
  99.    xx=x*p4+y*p5-p3*z;
  100.    yy=y*p1-x*p2;
  101.  
  102.    z0=z ;
  103.    xa=xx ;
  104.    ya=yy;
  105.    z1=z ;
  106.    xb=xx ;
  107.    yb=yy;
  108.  
  109.    x=x0-xs;
  110.  
  111.    for (nx=0 ; nx<=lz ; nx++) {
  112.       x=x+xs ;
  113.       y=y0-ys;
  114.       for (ny=0 ; ny<=lz ; ny++) {
  115.          y=y+ys;
  116.  
  117.          Funktion();
  118.          xx=x*p4+y*p5-p3*z;
  119.          yy=y*p1-x*p2;
  120.  
  121.          if (z>z1)
  122.             z1=z;
  123.          else if (z<z0)
  124.             z0=z;
  125.  
  126.          if (xx<xa)
  127.             xa=xx;
  128.          else if (xx>xb)
  129.             xb=xx;
  130.  
  131.          if (yy<ya)
  132.             ya=yy;
  133.          else if (yy>yb)
  134.             yb=yy;
  135.  
  136.       }
  137.    }
  138.  
  139.    xd=(xb-xa)/(float)(hh-1) ;
  140.    yd=(yb-ya)/(float)(br-1);
  141.    x=x0-xs;
  142.  
  143.    for (nx=0 ; nx<=lz ; nx++) {
  144.       x=x+xs ;
  145.       y=y0-ys;
  146.       for (ny=0 ; ny<=lz ; ny++) {
  147.          y=y+ys;
  148.  
  149.          Funktion();
  150.          xx=x*p4+y*p5-p3*z;
  151.          yy=y*p1-x*p2;
  152.  
  153.          px[nx][ny]=(int)((xx-xa)/xd+.5);
  154.          py[nx][ny]=(int)((yy-ya)/yd+.5);
  155.  
  156.       }
  157.    }
  158.  
  159.    if (( Window=(struct Window *)OpenWindow(&NewWindow)) == NULL)
  160.       exit (FALSE);
  161.  
  162.    locate (5,52);printf("%s %f",klx,x0);
  163.    locate (6,52);printf("%s   %f",grx,x1);
  164.    locate (8,52);printf("%s %f",kly,y0);
  165.    locate (9,52);printf("%s   %f",gry,y1);
  166.    locate (11,52);printf("%s %f",klz,z0);
  167.    locate (12,52);printf("%s   %f",grz,z1);
  168.    locate (14,52);printf("Azimutwinkel : %d",az);
  169.    locate (15,52);printf("Höhe         : %d",ph);
  170.  
  171.    for (ny=lz-1 ; ny>=0 ; ny--) {
  172.       xa=(float)px[lz][ny+1] ; ya=(float)py[lz][ny+1];
  173.       xb=(float)px[lz][ny] ; yb=(float)py[lz][ny];
  174.       Ausgabe();
  175.    }
  176.  
  177.    for (nx=lz-1 ; nx>=0 ; nx--) {
  178.       xa=px[nx+1][lz] ; ya=py[nx+1][lz];
  179.       xb=px[nx][lz] ; yb=py[nx][lz];
  180.       Ausgabe();
  181.       for (ny=lz-1 ; ny>=0 ; ny--) {
  182.          xa=px[nx+1][ny] ; ya=py[nx+1][ny];
  183.          xb=px[nx][ny] ; yb=py[nx][ny];
  184.          xd=xb-xa ; yd=yb-ya;
  185.          s=sqrt((xd*xd+yd*yd)/2.0);
  186.  
  187.          if (s!=0.0) {
  188.             xb=round(xb-xd/s) ; yb=round(yb-yd/s);
  189.             Ausgabe();
  190.          }
  191.  
  192.          xa=px[nx][ny+1] ; ya=py[nx][ny+1];
  193.          xb=px[nx][ny] ; yb=py[nx][ny];
  194.          Ausgabe();
  195.       }
  196.    }
  197.  
  198.    locate (17,60);printf("Fertig !!!");
  199.    locate (19,60);printf("nochmal (j,n) ");
  200.    do
  201.       i = getchar();
  202.    while ( i== '\n' );
  203.    if (i=='j' || i=='y') {
  204.       CloseWindow(Window);
  205.       goto Schleife;
  206.    }
  207.    Ende();
  208. }
  209.  
  210. Ende()
  211. {
  212.    CloseWindow(Window);   /* Close the Window */
  213.    fclose (rwp);
  214.    exit(TRUE);            /* Exit */
  215. }
  216.  
  217. locate (x,y)
  218. int x,y;
  219. {
  220.    printf("\x1b[%d;%dH",x,y);
  221. }
  222.  
  223. Ausgabe()
  224. {
  225.    if (o[(int)yb] >= 0)
  226.       if (xb > o[(int)yb])
  227.          goto weiter1;
  228.  
  229.    if (xb > xa) {
  230.       x=xa;xa=xb;xb=x;y=ya;ya=yb;yb=y;
  231.    }
  232.  
  233.    goto weiter2;
  234.  
  235. weiter1:
  236.    if (xb < xa) {
  237.       x=xa;xa=xb;xb=x;y=ya;ya=yb;yb=y;
  238.    }
  239.  
  240. weiter2:
  241.    xd=xb-xa ; yd=yb-ya;
  242.    s=abs(xd);
  243.  
  244.    if (abs(yd) > s)
  245.       s=abs(yd);
  246.  
  247.    if (s == 0.0)
  248.       return();
  249.  
  250.    xd=xd/s ; yd=yd/s;
  251.    xx=xa-xd ; yy=ya-yd;
  252.  
  253.    for (i=0 ; i<=s ; i++) {
  254.       xx+=xd ; yy+=yd;
  255.       xp=(round(xx)) ; yp=(round(yy));
  256.       if (o[yp] < 0) {
  257.          o[yp]=xp;
  258.          u[yp]=xp;
  259.          SetAPen(RP,3L);
  260.          WritePixel(RP,(long)yp,(long)xp);
  261.       }
  262.       else if (xp > o[yp]) {
  263.          o[yp]=xp;
  264.          SetAPen(RP,2L);
  265.          WritePixel(RP,(long)yp,(long)xp);
  266.       }
  267.       else if (xp < u[yp]) {
  268.          u[yp]=xp;
  269.          SetAPen(RP,1L);
  270.          WritePixel(RP,(long)yp,(long)xp);
  271.       }
  272.    }
  273. }
  274.  
  275.