home *** CD-ROM | disk | FTP | other *** search
/ Amiga Magazin: Amiga-CD 2000 April & May / AMIGA_2000_04.iso / pd-disketten / dms-gepackt / 6_96 / apd-6-96-2.dms / apd-6-96-2.adf / Skalieren / line.c < prev    next >
C/C++ Source or Header  |  1996-03-21  |  2KB  |  101 lines

  1. // 
  2. //    line.c: (C) Clemens Marschner, 1996
  3. //
  4.  
  5. #include <pragma/graphics_lib.h>
  6. #include <pragma/exec_lib.h>
  7. #include <pragma/intuition_lib.h>
  8. #include <pragma/dos_lib.h>
  9.  
  10. struct GfxBase         *GfxBase;
  11. struct IntuitionBase *IntuitionBase;
  12.  
  13. void Zeichne(struct RastPort*);
  14.  
  15. void main() {
  16.     struct Window *win;
  17.     if(GfxBase = (struct GfxBase*)OpenLibrary("graphics.library",0l)) 
  18.     {
  19.         if(IntuitionBase = (struct IntuitionBase*)OpenLibrary("intuition.library",0l))
  20.         {
  21.             if(win = OpenWindowTags(0, WA_Title, "Linien", TAG_END, 0))
  22.             {
  23.                 Zeichne(win->RPort);
  24.                 Delay(4*50);        /* vier Sekunden warten */
  25.                 CloseWindow(win);
  26.             }
  27.             CloseLibrary((struct Library*)IntuitionBase);
  28.         }
  29.         CloseLibrary((struct Library*)GfxBase);
  30.     }
  31. }
  32.  
  33.  
  34. int round(double v) {
  35.     return (int)(v+0.5);
  36. }
  37.  
  38. // Folgende Routinen nur gültig für 0<=m<=1 (Linie zw. 0 und 45°)
  39. // in Wirklichkeit noch Spiegelungen notwendig
  40.  
  41. void Line_1(struct RastPort *rp, int x1, int y1, int x2, int y2)
  42. {
  43.     double m,y;
  44.     int x;
  45.     m = ((double)(y2-y1))/(x2-x1); /* durch "float" werden die Koordinaten in 
  46.                                                 Fließkommazahlen umgewandelt */
  47.     for(x=x1,y=y1; x<=x2; x++) 
  48.     {
  49.         WritePixel(rp, x, round(y));
  50.         y += m;
  51.     }
  52. }
  53.  
  54. void Line_2(struct RastPort *rp, int x1, int y1, int x2, int y2) 
  55. {
  56.     double m,error = 0;
  57.     m = ((double)(y2-y1))/(x2-x1);
  58.     int y = y1;
  59.     for(int x = x1; x<=x2; x++) {
  60.         WritePixel(rp, x,y);
  61.         error += m;
  62.         if (error >= 0.5) {
  63.             y++; 
  64.             error--;
  65.         }
  66.     }
  67. }
  68.  
  69. void Line_3(struct RastPort *rp, int x1, int y1, int x2, int y2) 
  70. {
  71.     int m = 2*(y2-y1), error = 0;
  72.     int y = y1;
  73.     for(int x = x1; x<=x2; x++) {
  74.         WritePixel(rp, x,y);
  75.         error += m;
  76.         if (error >= x2-x1) {
  77.             y++; 
  78.             error-= 2*(x2-x1);
  79.         }
  80.     }
  81. }
  82.  
  83.  
  84.  
  85. void Zeichne(struct RastPort*rp) {
  86.     int i;
  87.     SetAPen(rp,1);
  88.     for(i=0; i<60; i+=2) {
  89.         Line_1(rp, 10+6*i,2*i+20, 600,4*i+20);
  90.     }
  91.     SetAPen(rp,2);
  92.     for(i=0; i<60; i+=2) {
  93.         Line_2(rp, 10+6*i,2*i+20, 600,4*i+20);
  94.     }
  95.     SetAPen(rp,3);
  96.     for(i=0; i<60; i+=2) {
  97.         Line_2(rp, 10+6*i,2*i+20, 600,4*i+20);
  98.     }
  99. }
  100.  
  101.