home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Libraries / GrafSys 2.0 / GrafSys 2.0 source / Triangles.p < prev   
Encoding:
Text File  |  1993-07-28  |  4.1 KB  |  187 lines  |  [TEXT/PJMM]

  1. unit triangles;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Matrix, Transformations, OffscreenCore, GrafSysCore, GrafSysScreen, GrafSysObject, Resources, OffScreenGraphics, GrafSysC;
  7.  
  8.     function Triangle (p1, p2, p3: point; theColor: integer): integer;
  9.  
  10.  
  11. implementation
  12.  
  13. (* Top-Down :    Pascal version of the triangle top-down fill algorithm *)
  14. (*                    just provided to test and demonstrate the algorithm *)
  15. (*                    calling convention :     b is highest point, a and d have same     *)
  16. (*                                            vertical component                         *)
  17.  
  18.     procedure TPTopDown (a, b, d: point);
  19.         var
  20.             deltaxleft, deltaxright: fixed;
  21.             dx, dy: integer;
  22.             ys, ye: integer;
  23.             xs, xe: fixed;
  24.  
  25.     begin
  26.         dy := a.v - b.v;
  27.         dx := a.h - b.h;
  28.         deltaXleft := FixRatio(dx, dy); (* ATTN: dy may be zero ! *)
  29.         dx := d.h - b.h;
  30.         deltaXright := FixRatio(dx, dy);
  31. (* Not implemented : plot b itself *)
  32.         xs := FixRatio(b.h, 1);
  33.         ys := b.v;
  34.         xe := FixRatio(b.h, 1);
  35.         ye := b.v; (* now both point to the start point *)
  36.         while ye < a.v do
  37.             begin (* go down one line and plot from s to e horizontal line *)
  38.                 ys := ys + 1;
  39.                 ye := ye + 1;
  40.                 xs := xs + deltaXleft;
  41.                 xe := xe + deltaXright;
  42.                 MoveTo(HiWord(xs), ys);
  43.                 LineTo(HiWord(xe), ye);
  44.             end;
  45.     end;
  46.  
  47.  
  48.     procedure TPBottomUp (a, d, c: point);
  49.  
  50.         var
  51.             deltaxleft, deltaxright: fixed;
  52.             dx, dy: integer;
  53.             ys, ye: integer;
  54.             xs, xe: fixed;
  55.  
  56.  
  57.     begin
  58.         dy := a.v - c.v;
  59.         dx := a.h - c.h;
  60.         deltaXleft := FixRatio(dx, dy); (* ATTN: dy may be zero ! *)
  61.         dx := d.h - c.h;
  62.         deltaXright := FixRatio(dx, dy);
  63. (* Not implemented : plot b itself *)
  64.         xs := FixRatio(c.h, 1);
  65.         ys := c.v;
  66.         xe := FixRatio(c.h, 1);
  67.         ye := c.v; (* now both point to the start point *)
  68.         while ye > a.v do
  69.             begin (* go down one line and plot from s to e horizontal line *)
  70.                 ys := ys - 1;
  71.                 ye := ye - 1;
  72.                 xs := xs - deltaXleft;
  73.                 xe := xe - deltaXright;
  74.                 MoveTo(HiWord(xs), ys);
  75.                 LineTo(HiWord(xe), ye);
  76.             end;
  77.     end;
  78.  
  79.     procedure ButtonWait;
  80.     begin
  81.         repeat
  82.         until button;
  83.         repeat
  84.         until not button;
  85.     end;
  86.  
  87.     function Triangle (p1, p2, p3: point; theColor: integer): integer;
  88.  
  89.         var
  90.             a, b, c, d: point; (* the four points that define the two scan-parallel triangles *)
  91.             temp: point;
  92.  
  93.             dx, dy: integer;
  94.             deltax: fixed;
  95.             deltaY: fixed;
  96.             thePort: GrafPtr;
  97.  
  98.  
  99.     begin
  100. (* sanity check : do we have a pixmap allocated ? *)
  101. (* if currOSPixMap = nil then *)
  102.         if false then
  103.             begin
  104.                 Triangle := cNoActiveOSPixMap;
  105.                 Exit(Triangle);
  106.             end;
  107.  
  108.         GetPort(thePort);
  109.  
  110. (* Step 1 : sort p1, p2, p3 so that they conform with the scan-parallel definition *)
  111.     (* Step 1.1 : look for b (highest) and place a and c accordingly *)
  112.         if (p1.v <= p2.v) and (p1.v <= p3.v) then
  113.             begin
  114.                 b := p1; (* p1 is highest *)
  115.                 if p2.v >= p3.v then (* p2 is lowest, p3 is middle *)
  116.                     begin
  117.                         c := p2;
  118.                         a := p3;
  119.                     end
  120.                 else
  121.                     begin (* p3 is lowest, p2 is middle *)
  122.                         c := p3;
  123.                         a := p2;
  124.                     end;
  125.             end
  126.         else if (p2.v <= p1.v) and (p2.v <= p3.v) then
  127.             begin
  128.                 b := p2; (* p2 is highest *)
  129.                 if p1.v >= p3.v then
  130.                     begin
  131.                         c := p1;
  132.                         a := p3;
  133.                     end
  134.                 else
  135.                     begin
  136.                         c := p3;
  137.                         a := p1;
  138.                     end;
  139.             end
  140.         else (* p3.v is highest *)
  141.             begin
  142.                 b := p3;
  143.                 if p1.v >= p2.v then
  144.                     begin
  145.                         c := p1;
  146.                         a := p2;
  147.                     end
  148.                 else
  149.                     begin
  150.                         c := p2;
  151.                         a := p1;
  152.                     end;
  153.             end;
  154.  
  155. (* step 2 : calculate point d : it's v is the same as a's but it resides on bc *)
  156.         dx := c.h - b.h;
  157.         dy := c.v - b.v;
  158.         deltaX := FixRatio(dx, dy);
  159.         deltaY := FixRatio(a.v - b.v, 1);
  160.         d.v := a.v;
  161.         d.h := b.h + HiWord(FixMul(deltaY, deltax));
  162.  
  163. (* step 4 : sanity check --> exchange a and d if d.h < a.h *)
  164.         if d.h < a.h then
  165.             begin
  166.                 temp := a;
  167.                 a := d;
  168.                 d := temp;
  169.             end;
  170.  
  171. (* step 3 : draw it. this takes two steps*)
  172. (*                step 3.1 draw upper triangle top-down *)
  173. (*                step 3.2 draw lower triangle bottom-up *)
  174.         ButtonWait;
  175.         MoveTo(a.h, a.v); (* jst draw outline. Draw upper scan-parallel *)
  176.         LineTo(b.h, b.v);
  177.         LineTo(d.h, d.v);
  178.         MoveTo(a.h, a.v); (* draw lower scan-parallel *)
  179.         LineTo(c.h, c.v);
  180.         LineTo(d.h, d.v);
  181.         ButtonWait;
  182.         FillTri(a, b, d, c, CGrafPtr(thePort)^.portPixMap, theColor);
  183. (* Finish up *)
  184.         Triangle := noErr;
  185.     end; (* Triangle *)
  186.  
  187. end.