Janilabo

progress3

Jul 20th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 8.57 KB | None | 0 0
  1. // Original algorithms by slacky, optimized by Janilabo
  2.  
  3. { Given a TPA - This function will connect each point, from first to last }
  4. function ConnectPts(pts: TPointArray): TPointArray;
  5. var
  6.   i, j, h, l, r, a: Integer;
  7.   q, p: TPoint;
  8. begin
  9.   if (High(pts) > -1) then
  10.   begin
  11.     h := High(pts);
  12.     for i := 0 to h do
  13.     begin
  14.       j := (i + 1);
  15.       if (i = h) then
  16.         j := 0;
  17.       q := pts[i];
  18.       p := pts[j];
  19.       r := Length(Result);
  20.       if ((q.X <> p.X) or (q.Y <> p.Y)) then
  21.       begin
  22.         l := Max(Round(Abs(q.X - p.X)), Round(Abs(q.Y - p.Y)));
  23.         SetLength(Result, ((r + l) + 1));
  24.         for a := 0 to l do
  25.           Result[(r + a)] := Point((q.X + Round((p.X - q.X) * (a / Extended(l)))), (q.Y + Round((p.Y - q.Y) * (a / Extended(l)))));
  26.       end else
  27.       begin
  28.         SetLength(Result, (r + 1));
  29.         Result[r] := q;
  30.       end;
  31.     end;
  32.   end else
  33.     SetLength(Result, 0);
  34. end;
  35.  
  36. {------------------------------------------------------------------------------]
  37.  Check if a point is within a polygon/shape by the given outline points (poly)
  38.  The points must be in order, as if you would draw a line trough each point.
  39. [------------------------------------------------------------------------------}
  40. function InPoly(pt: TPoint; poly: TPointArray): Boolean;
  41. var
  42.   n, i: Integer;
  43.   z: Extended;
  44.   a, b: TPoint;
  45. begin
  46.   n := Length(poly);
  47.   Result := False;
  48.   if (n > -1) then
  49.   begin
  50.     for i := 0 to (n - 1) do
  51.       if ((pt.X = poly[i].X) and (pt.Y = poly[i].Y)) then
  52.         Break;
  53.     Result := (i < n);
  54.     if not Result then
  55.     begin
  56.       a := poly[0];
  57.       for i := 0 to n do
  58.       begin
  59.         b := poly[(i mod n)];
  60.         if (pt.Y > Min(a.Y, b.Y)) then
  61.           if (pt.Y <= Max(a.Y, b.Y)) then
  62.             if (pt.X <= Max(a.X, b.X)) then
  63.             begin
  64.               if (a.y <> b.y) then
  65.                 z := ((pt.Y - a.Y) * (b.X - a.X) / Extended((b.Y - a.Y)) + a.X);
  66.               if ((a.X = b.X) or (pt.X < z)) then
  67.                 Result := not Result;
  68.             end;
  69.         a := b;
  70.       end;
  71.     end;
  72.   end;
  73. end;
  74.  
  75. //******* DIRTY EXAMPLE BELLOW: *******//
  76. procedure SetPixels(bmp: Integer; TPA: TPointArray; color: Integer);
  77. var
  78.   a, z, w, h: Integer;
  79. begin
  80.   z := High(TPA);
  81.   if (z > -1) then
  82.   begin
  83.     GetBitmapSize(bmp, w, h);
  84.     for a := 0 to z do
  85.       if ((TPA[a].X >= 0) and (TPA[a].Y >= 0) and (TPA[a].X < w) and (TPA[a].Y < h)) then
  86.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y, color);
  87.   end;
  88. end;
  89.  
  90. procedure SetPixelsMP(bmp: Integer; TPA: TPointArray; color: Integer);
  91. var
  92.   a, z, w, h: Integer;
  93. begin
  94.   z := High(TPA);
  95.   if (z > -1) then
  96.   begin
  97.     GetBitmapSize(bmp, w, h);
  98.     for a := 0 to z do
  99.       if ((TPA[a].X > 0) and (TPA[a].Y > 0) and (TPA[a].X < (w - 1)) and (TPA[a].Y < (h - 1))) then
  100.       begin
  101.         FastSetPixel(bmp, (TPA[a].X - 1), TPA[a].Y, color);
  102.         FastSetPixel(bmp, (TPA[a].X + 1), TPA[a].Y, color);
  103.         FastSetPixel(bmp, TPA[a].X, (TPA[a].Y + 1), color);
  104.         FastSetPixel(bmp, TPA[a].X, (TPA[a].Y - 1), color);
  105.       end;
  106.   end;
  107. end;
  108.  
  109. { Fills an polygon by the given outlines }
  110. function FillShape(shape: TPointArray): TPointArray;
  111. var
  112.   b: TBox;
  113.   x, y, h, i, v, l, r, z: Integer;
  114.   o: TPointArray;
  115.   f: Boolean;
  116.   t: array of TBoolArray;
  117.   e: Extended;
  118.   q, p, d: TPoint;
  119. begin
  120.   h := High(shape);
  121.   if (h > -1) then
  122.   begin
  123.     for i := 0 to h do
  124.     begin
  125.       v := (i + 1);
  126.       if (i = h) then
  127.         v := 0;
  128.       q := shape[i];
  129.       p := shape[v];
  130.       r := Length(o);
  131.       if ((q.X <> p.X) or (q.Y <> p.Y)) then
  132.       begin
  133.         l := Max(Round(Abs(q.X - p.X)), Round(Abs(q.Y - p.Y)));
  134.         SetLength(o, ((r + l) + 1));
  135.         for z := 0 to l do
  136.           o[(r + z)] := Point((q.X + Round((p.X - q.X) * (z / Extended(l)))), (q.Y + Round((p.Y - q.Y) * (z / Extended(l)))));
  137.       end else
  138.       begin
  139.         SetLength(o, (r + 1));
  140.         o[r] := q;
  141.       end;
  142.       if (i > 0) then
  143.       begin
  144.         if (shape[i].X < b.X1) then
  145.           b.X1 := shape[i].X
  146.         else
  147.           if (shape[i].X > b.X2) then
  148.             b.X2 := shape[i].X;
  149.         if (shape[i].Y < b.Y1) then
  150.           b.Y1 := shape[i].Y
  151.         else
  152.           if (shape[i].Y > b.Y2) then
  153.             b.Y2 := shape[i].Y;
  154.       end else
  155.         b := IntToBox(shape[0].X, shape[0].Y, shape[0].X, shape[0].Y);
  156.     end;
  157.     SetLength(t, ((b.X2 - b.X1) + 1));
  158.     for i := 0 to (b.X2 - b.X1) do
  159.       SetLength(t[i], ((b.Y2 - b.Y1) + 1));
  160.     l := Length(o);
  161.     for i := 0 to (l - 1) do
  162.       if not t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] then
  163.         t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] := True;
  164.     SetLength(o, 0);
  165.     for y := 0 to (b.Y2 - b.Y1) do
  166.       for x := 0 to (b.X2 - b.X1) do
  167.       begin
  168.         f := t[x][y];
  169.         if not f then
  170.         begin
  171.           d := Point((x + b.X1), (y + b.Y1));
  172.           for i := 0 to h do
  173.             if ((d.X = shape[i].X) and (d.Y = shape[i].Y)) then
  174.               Break;
  175.           f := (i <= h);
  176.           if not f then
  177.           begin
  178.             q := shape[0];
  179.             for i := 0 to (h + 1) do
  180.             begin
  181.               p := shape[(i mod (h + 1))];
  182.               if (d.Y > Min(q.Y, p.Y)) then
  183.                 if (d.Y <= Max(q.Y, p.Y)) then
  184.                   if (d.X <= Max(q.X, p.X)) then
  185.                   begin
  186.                     if (q.y <> p.y) then
  187.                       e := ((d.Y - q.Y) * (p.X - q.X) / Extended((p.Y - q.Y)) + q.X);
  188.                     if ((q.X = p.X) or (d.X < e)) then
  189.                       f := not f;
  190.                   end;
  191.               q := p;
  192.             end;
  193.           end;
  194.         end;
  195.         if f then
  196.         begin
  197.           l := Length(Result);
  198.           SetLength(Result, (l + 1));
  199.           Result[l] := Point((x + b.X1), (y + b.Y1));
  200.         end;
  201.       end;
  202.     SetLength(t, 0);
  203.   end else
  204.     SetLength(Result, 0);
  205. end;
  206.  
  207. { Creates all the points needed to define a simple polygon }
  208. function Xagon(sides: Integer; M, F: TPoint): TPointArray;
  209. var
  210.   x, y, a, b, v: Integer;
  211.   s, c: Extended;
  212. begin
  213.   SetLength(Result, sides);
  214.   x := F.x;
  215.   y := F.y;
  216.   s := Sin(Radians(360.0 / sides));
  217.   c := Cos(Radians(360.0 / sides));
  218.   for v := 0 to (sides - 1) do
  219.   begin
  220.     a := (x - M.x);
  221.     b := (y - M.y);
  222.     x := Round(((b * s) + (a * c) + M.x));
  223.     y := Round(((b * c) - (a * s) + M.y));
  224.     Result[v] := Point(x, y);
  225.   end;
  226. end;
  227.  
  228. //******* Plot a few shapes using this algorithm *******//
  229. var
  230.   shape, TPA: TPointArray;
  231.   bmp, t: Integer;
  232.   ShowExample: Integer;
  233.   ShowPoints: Boolean;
  234.  
  235. begin
  236.   //Dynamic shapes from Example 6 and down:
  237.   ShowExample := 1; //0 - 9
  238.   ShowPoints := True;  //0 or 1
  239.   case ShowExample of
  240.     // Microshape ------//
  241.     //           TOP            LEFT           BOTTOM         RIGHT
  242.     0: shape := [Point(50, 50), Point(60, 50), Point(51, 51), Point(50, 51)];
  243.     // Arrow like shape //
  244.     1: shape := [Point(70,90),Point(185,90),Point(185,116), Point(70,116), //Square +
  245.                  Point(70,140),Point(35,105),Point(70,70)];                //Triangle
  246.     // Triangle --------//
  247.     2: shape := [Point(50,50),Point(0,100),Point(100,150)];
  248.     // Raped pentagon --//
  249.     3: shape := [Point(10,25),Point(35,5),Point(82,22),Point(50,90),Point(4,60)];
  250.     // Octagon ---------//
  251.     4: shape := [Point(100,30), Point(155,50),  //Top-Right
  252.                  Point(180,110),Point(155,160), //Right-Bottom
  253.                  Point(100,180),Point(45,160),  //Bottom-Left
  254.                  Point(20,100), Point(45,50)];  //Left-Top
  255.     // S-shape ---------//
  256.     5: shape := [Point(135,50),Point(123,70),Point(103,58),Point(85,70),Point(85,85),
  257.                  Point(122,105),Point(132,125),Point(120,155),Point(78,164),Point(55,147),
  258.                  Point(64,128),Point(80,140),Point(100,140),Point(96,116),Point(62,98),
  259.                  Point(60,55),Point(111,35)];
  260.     // Xagon: 15 corners //
  261.     6: shape := Xagon(15, Point(100,100), Point(60,100));
  262.     // Xagon: 9 corners //
  263.     7: shape := Xagon(9, Point(100,100), Point(60,50));
  264.     // Xagon: 5 corners AKA Pentagon //
  265.     8: shape := Xagon(5, Point(100,100), Point(74,50));
  266.     // Xagon: 3 corners AKA Triangle //
  267.     9: shape := Xagon(3, Point(100,100), Point(48,48));
  268.   end;
  269.   bmp := CreateBitmap(200, 200);
  270.   t := GetSystemTime;
  271.   TPA := FillShape(shape);
  272.   WriteLn(IntToStr(GetSystemTime - t) + ' ms.');
  273.   SetPixels(bmp, TPA, 357435);
  274.   if ShowPoints then
  275.     SetPixelsMP(bmp, shape, 16777215);
  276.   DisplayDebugImgWindow(200, 200);
  277.   DrawBitmapDebugImg(bmp);
  278.   FreeBitmap(bmp);
  279.   SetLength(TPA, 0);
  280. end.
Advertisement
Add Comment
Please, Sign In to add comment