Advertisement
Janilabo

Bounds of Shapes

Nov 20th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.25 KB | None | 0 0
  1. // Original SCARExt-based concept developed by slacky (Jarl)
  2.  
  3. {$loadlib pumbaa.dll}
  4.  
  5. var
  6.   TPA: TPointArray;
  7.   TBA: TBoxArray;
  8.   bmp, i, t: Integer;
  9.   area: TBox;
  10.   shapes: T2DPointArray;
  11.  
  12. procedure SetupShapes;
  13. begin
  14.   SetLength(shapes, 4);
  15.   shapes[0] := TPAFromBox(IntToBox(40, 10, 80, 90));
  16.   shapes[1] := TPAFromBox(IntToBox(20, 30, 60, 70));
  17.   shapes[2] := pp_TPAFromCircle(Point(80, 80), 15.5);
  18.   shapes[3] := pp_TPAFromEllipse(Point(60, 20), 15, 6);
  19. end;
  20.  
  21. procedure DebugBitmap(bmp: Integer);
  22. var
  23.   w, h: Integer;
  24. begin
  25.   GetBitmapSize(bmp, w, h);
  26.   DisplayDebugImgWindow(w, h);
  27.   DrawBitmapDebugImg(bmp);
  28. end;
  29.  
  30. function GetTPABoxes(TPA: TPointArray): TBoxArray;
  31. var
  32.   w, h, i, l, r: Integer;
  33.   a: TBox;
  34.   b: T2DBoolArray;
  35.   m, p, c: TPointArray;
  36.   o: T2DPointArray;
  37. begin
  38.   r := 0;
  39.   l := Length(TPA);
  40.   if (l > 0) then
  41.   begin
  42.     c := pp_Clone(TPA);
  43.     a := pp_TPABounds(c);
  44.     pp_BoxExpand(a);
  45.     pp_BoxDimensions(a, w, h);
  46.     SetLength(b, w, h);
  47.     p := pp_TPAFloodFill(c, Point(a.X1, a.Y1), sd4Ways, a);
  48.     m := pp_Merge(c, p);
  49.     SetLength(p, 0);
  50.     SetLength(c, 0);
  51.     pp_TPAInvert(m);
  52.     o := pp_TPABlob(m, sd4ways);
  53.     SetLength(m, 0);
  54.     l := Length(o);
  55.     SetLength(Result, l);
  56.     for i := 0 to (l - 1) do
  57.     begin
  58.       Result[i] := pp_TPABounds(o[i]);
  59.       pp_BoxExpand(Result[i]);
  60.     end;
  61.     SetLength(o, 0);
  62.   end else
  63.     SetLength(Result, 0);
  64. end;
  65.  
  66. function CreateShapes(shapes: T2DPointArray): TPointArray;
  67. var
  68.   h, i: Integer;
  69.   c: TPointArray;
  70. begin
  71.   SetLength(Result, 0);
  72.   h := High(shapes);
  73.   for i := 0 to h do
  74.   begin
  75.     c := pp_Clone(shapes[i]);
  76.     if (i > 0) then
  77.       pp_TPAFilterPoints(Result, shapes[i]);
  78.     pp_TPAEdge(c);
  79.     pp_Append(Result, c);
  80.     SetLength(c, 0);
  81.   end;
  82. end;
  83.  
  84. begin
  85.   area := pp_TPABounds(shapes);
  86.   SetupShapes;
  87.   bmp := CreateBitmap((area.X2 + 150), (area.Y2 + 150));
  88.   TPA := CreateShapes(shapes);
  89.   DrawTPABitmap(bmp, TPA, 255);
  90.   DebugBitmap(bmp);
  91.   t := GetSystemTime;
  92.   TBA := GetTPABoxes(TPA);
  93.   WriteLn('GetTPABoxes took ' + IntToStr(GetSystemTime - t) + ' ms.');
  94.   Wait(1000);
  95.   for i := 0 to High(TBA) do
  96.     DrawTPABitmap(bmp, TPAFromBox(TBA[i]), Random(16777215));
  97.   DebugBitmap(bmp);
  98.   FreeBitmap(bmp);
  99. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement