Janilabo

example

Sep 18th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.21 KB | None | 0 0
  1. // Original algorithms by slacky, optimized by Janilabo
  2.  
  3. const
  4.   POLYGON_EXAMPLE = 2; // 0-9.
  5.  
  6. function BuildShape: TPointArray;
  7. begin
  8.   case POLYGON_EXAMPLE of
  9.     // SQUARE:    TOP          LEFT          BOTTOM         RIGHT
  10.     0: Result := [Point(20,20),Point(20,120),Point(120,120),Point(120,20)];
  11.     // "Arrow" shape----//
  12.     1: Result := [Point(70,90),Point(185,90),Point(185,116), Point(70,116), //Square +
  13.                   Point(70,140),Point(35,105),Point(70,70)];                //Triangle
  14.     // Triangle --------//
  15.     2: Result := [Point(50,150),Point(100,100),Point(150,150)];
  16.     // Raped pentagon --//
  17.     3: Result := [Point(10,25),Point(35,5),Point(82,22),Point(50,90),Point(4,60)];
  18.     // Octagon ---------//
  19.     4: Result := [Point(100,30), Point(155,50),  //Top-Right
  20.                   Point(180,110),Point(155,160), //Right-Bottom
  21.                   Point(100,180),Point(45,160),  //Bottom-Left
  22.                   Point(20,100), Point(45,50)];  //Left-Top
  23.     // S-shape ---------//
  24.     5: Result := [Point(135,50),Point(123,70),Point(103,58),Point(85,70),Point(85,85),
  25.                   Point(122,105),Point(132,125),Point(120,155),Point(78,164),Point(55,147),
  26.                   Point(64,128),Point(80,140),Point(100,140),Point(96,116),Point(62,98),
  27.                   Point(60,55),Point(111,35)];
  28.   else
  29.     TerminateScript;
  30.   end;
  31. end;
  32.  
  33. function TPAFromPolygon(shape: TPointArray): TPointArray;
  34. var
  35.   b: TBox;
  36.   x, y, h, i, l, r, z: Integer;
  37.   o: TPointArray;
  38.   f: Boolean;
  39.   t: array of TBoolArray;
  40.   e: Extended;
  41.   q, p, d: TPoint;
  42. begin
  43.   h := High(shape);
  44.   if (h > -1) then
  45.   begin
  46.     SetLength(o, 0);
  47.     b := IntToBox(shape[0].X, shape[0].Y, shape[0].X, shape[0].Y);
  48.     for i := 0 to h do
  49.     begin
  50.       q := shape[i];
  51.       if (i < h) then
  52.         p := shape[(i + 1)]
  53.       else
  54.         p := shape[0];
  55.       r := Length(o);
  56.       if ((q.X <> p.X) or (q.Y <> p.Y)) then
  57.       begin
  58.         l := Max(Round(Abs(q.X - p.X)), Round(Abs(q.Y - p.Y)));
  59.         SetLength(o, ((r + l) + 1));
  60.         for z := 0 to l do
  61.           o[(r + z)] := Point((q.X + Round((p.X - q.X) * (z / Extended(l)))), (q.Y + Round((p.Y - q.Y) * (z / Extended(l)))));
  62.       end else
  63.       begin
  64.         SetLength(o, (r + 1));
  65.         o[r] := q;
  66.       end;
  67.       if (shape[i].X < b.X1) then
  68.         b.X1 := shape[i].X
  69.       else
  70.         if (shape[i].X > b.X2) then
  71.           b.X2 := shape[i].X;
  72.       if (shape[i].Y < b.Y1) then
  73.         b.Y1 := shape[i].Y
  74.       else
  75.         if (shape[i].Y > b.Y2) then
  76.           b.Y2 := shape[i].Y;
  77.     end;
  78.     SetLength(t, ((b.X2 - b.X1) + 1));
  79.     for i := 0 to (b.X2 - b.X1) do
  80.       SetLength(t[i], ((b.Y2 - b.Y1) + 1));
  81.     l := Length(o);
  82.     for i := 0 to (l - 1) do
  83.       if not t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] then
  84.         t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] := True;
  85.     for y := 0 to (b.Y2 - b.Y1) do
  86.       for x := 0 to (b.X2 - b.X1) do
  87.       begin
  88.         f := t[x][y];
  89.         if not f then
  90.         begin
  91.           d := Point((x + b.X1), (y + b.Y1));
  92.           q := shape[0];
  93.           for i := 0 to (h + 1) do
  94.           begin
  95.             p := shape[(i mod (h + 1))];
  96.             if (d.Y > Min(q.Y, p.Y)) then
  97.               if (d.Y <= Max(q.Y, p.Y)) then
  98.                 if (d.X <= Max(q.X, p.X)) then
  99.                 begin
  100.                   if (q.y <> p.y) then
  101.                     e := ((d.Y - q.Y) * (p.X - q.X) / Extended((p.Y - q.Y)) + q.X);
  102.                   if ((q.X = p.X) or (d.X < e)) then
  103.                     f := not f;
  104.                 end;
  105.             q := p;
  106.           end;
  107.         end;
  108.         if f then
  109.         begin
  110.           l := Length(Result);
  111.           SetLength(Result, (l + 1));
  112.           Result[l] := Point((x + b.X1), (y + b.Y1));
  113.         end;
  114.       end;
  115.   end else
  116.     SetLength(Result, 0);
  117. end;
  118.  
  119. procedure Setup;
  120. var
  121.   TPA: TPointArray;
  122.   bmp: Integer;
  123.   a: TBox;
  124. begin
  125.   TPA := TPAFromPolygon(BuildShape);
  126.   a := GetTPABounds(TPA);
  127.   WriteLn(ToStr(A));
  128.   bmp := CreateBitmap(((a.X2 + a.X1) + 1), ((a.Y2 + a.Y1) + 1));
  129.   DrawTPABitmap(bmp, TPA, 357435);
  130.   DisplayDebugImgWindow((a.X2 + a.X1), (a.Y2 + a.Y1));
  131.   DrawBitmapDebugImg(bmp);
  132.   FreeBitmap(bmp);
  133. end;
  134.  
  135. begin
  136.   ClearDebug;
  137.   Setup;
  138. end.
Advertisement
Add Comment
Please, Sign In to add comment