Janilabo

progress

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