Janilabo

absolute borders

Sep 24th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.14 KB | None | 0 0
  1. {$loadlib pumbaa.dll}
  2.  
  3. const
  4.   DELAY = 550; // Delay for drawing detected border points.
  5.  
  6. // V== CODE FOR GENERATING RANDOM SHAPE ==V
  7.  
  8. procedure TPALine(var TPA:TPointArray; P1, P2: TPoint);
  9. var
  10.   dx,dy,step,I,H: Integer;
  11.   rx,ry,x,y: Extended;
  12. begin
  13.   H := Length(TPA);
  14.   if (p1.x = p2.x) and (p2.y = p1.y) then
  15.   begin
  16.     SetLength(TPA, H+1);
  17.     TPA[H] := P1;
  18.     Exit;
  19.   end;
  20.  
  21.   dx := (P2.x - P1.x);
  22.   dy := (P2.y - P1.y);
  23.   if (Abs(dx) > Abs(dy)) then step := Abs(dx)
  24.   else step := Abs(dy);
  25.   SetLength(TPA, (H+step+1));
  26.  
  27.   rx := dx / step;
  28.   ry := dy / step;
  29.   x := P1.x;
  30.   y := P1.y;
  31.  
  32.   TPA[H] := Point(P1.x, P1.y);
  33.   for I:=1 to step do
  34.   begin
  35.     x := x + rx;
  36.     y := y + ry;
  37.     TPA[(H+i)] := Point(Round(x),Round(y));
  38.   end;
  39. end;
  40.  
  41. function lines(TPA:TPointArray; Distance:Integer): TPointArray;
  42. var
  43.   i,h,x,y,lx,hx,ly,hy:Integer;
  44.   Matrix:Array of TBoolArray;
  45.   Area: TBox;
  46.   pt:TPoint;
  47.  
  48. begin
  49.   Area := GetTPABounds(TPA);
  50.   Area.X2 := (Area.X2 - Area.X1) + 1;  //Width
  51.   Area.Y2 := (Area.Y2 - Area.Y1) + 1;  //Height
  52.   H := High(TPA);
  53.  
  54.   SetLength(Matrix, Area.Y2);
  55.   for i:=0 to (Area.Y2-1) do
  56.     SetLength(Matrix[i], Area.X2);
  57.  
  58.   for i:=0 to H do
  59.     Matrix[(TPA[i].y-Area.Y1)][(TPA[i].x-Area.X1)] := True;
  60.  
  61.   for i:=0 to H do
  62.   begin
  63.     pt.x := TPA[i].x-Area.X1;
  64.     pt.y := TPA[i].y-Area.Y1;
  65.     if Matrix[pt.y][pt.x] = True then
  66.     begin
  67.       Matrix[pt.y][pt.x] := False;
  68.       lx := Max(pt.x - Distance, 0);
  69.       hx := Min(pt.x + Distance, Area.X2-1);
  70.       ly := Max(pt.y - Distance, 0);
  71.       hy := Min(pt.y + Distance, Area.Y2-1);
  72.       for x:=lx to hx do
  73.         for y:=ly to hy do
  74.           if Matrix[y][x] = True then
  75.             TPALine(Result, Point((pt.x+Area.X1),(pt.y+Area.y1)), point(x+Area.x1,y+Area.y1));
  76.     end;
  77.   end;
  78.   SetLength(Matrix, 0);
  79. end;
  80.  
  81. function RandomTPA(Amount:Integer; MinX,MinY,MaxX,MaxY:Integer): TPointArray;
  82. var i:Integer;
  83. begin
  84.   SetLength(Result, Amount);
  85.   for i:=0 to Amount-1 do
  86.     Result[i] := Point(RandomRange(MinX, MaxX), RandomRange(MinY, MaxY));
  87. end;
  88.  
  89. procedure SetPixels(bmp: Integer; TPA: TPointArray; color: Integer);
  90. var
  91.   a, z, w, h: Integer;
  92. begin
  93.   z := High(TPA);
  94.   if (z > -1) then
  95.   begin
  96.     GetBitmapSize(bmp, w, h);
  97.     for a := 0 to z do
  98.       if ((TPA[a].X >= 0) and (TPA[a].Y >= 0) and (TPA[a].X < w) and (TPA[a].Y < h)) then
  99.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y, color);
  100.   end;
  101. end;
  102.  
  103. function BuildShape: TPointArray;
  104. var
  105.   shape: TPointArray;
  106. begin
  107.   shape := RandomTPA(8000, 50,50, 1150,650);
  108.   Result := lines(shape, 10);
  109. end;
  110.  
  111. // ^== CODE FOR GENERATING RANDOM SHAPE ==^
  112.  
  113. procedure Test;
  114. var
  115.   TPA: TPointArray;
  116.   t, h, i, bmp: Integer;
  117. begin
  118.   ClearDebug;
  119.   bmp := CreateBitmap(1200, 700);
  120.   DisplayDebugImgWindow(1200, 700);
  121.   //TPA := TPAFromCircle(250, 250, 144);
  122.   TPA := BuildShape;
  123.   DrawTPABitmap(bmp, TPA, 5367616);
  124.   DrawBitmapDebugImg(bmp);
  125.   t := GetSystemTime;
  126.   pp_TPABorders(TPA);
  127.   WriteLn('TPABorder took ' + IntToStr(GetSystemTime - t) + ' ms.');
  128.   h := High(TPA);
  129.   Wait(DELAY);
  130.   DrawTPABitmap(bmp, TPA, 255);
  131.   DrawBitmapDebugImg(bmp);
  132. end;
  133.  
  134. begin
  135.   Test;
  136. end.
Advertisement
Add Comment
Please, Sign In to add comment