Janilabo

Untitled

Sep 18th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.68 KB | None | 0 0
  1. program new;
  2.  
  3. procedure SetPixels(bmp: Integer; TPA: TPointArray; color: Integer);
  4. var
  5.   a, z, w, h: Integer;
  6. begin
  7.   z := High(TPA);
  8.   if (z > -1) then
  9.   begin
  10.     GetBitmapSize(bmp, w, h);
  11.     for a := 0 to z do
  12.       if ((TPA[a].X >= 0) and (TPA[a].Y >= 0) and (TPA[a].X < w) and (TPA[a].Y < h)) then
  13.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y, color);
  14.   end;
  15. end;
  16.  
  17.  
  18. //Walk around current, from previous.
  19. procedure RotatingAdjecent8Way(var adj:TPointArray;const Curr:TPoint; const Prev:TPoint);
  20. var
  21.   i: Integer;
  22.   dx,dy,x,y:Single;
  23. begin
  24.   x := Prev.x; y := Prev.y;
  25.   adj[7] := Prev;
  26.   for i:=0 to 6 do
  27.   begin
  28.     dx := x - Curr.x;
  29.     dy := y - Curr.y;
  30.     x := ((dy * 0.7070) + (dx * 0.7070)) + Curr.x;
  31.     y := ((dy * 0.7070) - (dx * 0.7070)) + Curr.y;
  32.     adj[i] := Point(Round(x),Round(y));
  33.   end;
  34. end;
  35.  
  36. procedure TPALine(var TPA:TPointArray; P1, P2: TPoint);
  37. var
  38.   dx,dy,step,I,H: Integer;
  39.   rx,ry,x,y: Extended;
  40. begin
  41.   H := Length(TPA);
  42.   if (p1.x = p2.x) and (p2.y = p1.y) then
  43.   begin
  44.     SetLength(TPA, H+1);
  45.     TPA[H] := P1;
  46.     Exit;
  47.   end;
  48.  
  49.   dx := (P2.x - P1.x);
  50.   dy := (P2.y - P1.y);
  51.   if (Abs(dx) > Abs(dy)) then step := Abs(dx)
  52.   else step := Abs(dy);
  53.   SetLength(TPA, (H+step+1));
  54.  
  55.   rx := dx / step;
  56.   ry := dy / step;
  57.   x := P1.x;
  58.   y := P1.y;
  59.  
  60.   TPA[H] := Point(P1.x, P1.y);
  61.   for I:=1 to step do
  62.   begin
  63.     x := x + rx;
  64.     y := y + ry;
  65.     TPA[(H+i)] := Point(Round(x),Round(y));
  66.   end;
  67. end;
  68.  
  69.  
  70. function lines(TPA:TPointArray; Distance:Integer): TPointArray;
  71. var
  72.   i,j,h,x,y,lx,hx,ly,hy,l,hit:Integer;
  73.   Matrix:T2DIntegerArray;
  74.   Area: TBox;
  75.   pt,start,prev,endpt:TPoint;
  76.   adj:TPointArray;
  77.   bmp:Integer;
  78. begin
  79.   Area := GetTPABounds(TPA);
  80.   Area.X2 := (Area.X2 - Area.X1) + 1;  //Width
  81.   Area.Y2 := (Area.Y2 - Area.Y1) + 1;  //Height
  82.   H := High(TPA);
  83.  
  84.   SetLength(Matrix, Area.Y2);
  85.   for i:=0 to (Area.Y2-1) do
  86.     SetLength(Matrix[i], Area.X2);
  87.   for i:=0 to H do
  88.     Matrix[(TPA[i].y-Area.Y1)][(TPA[i].x-Area.X1)] := 1;
  89.  
  90.   for i:=0 to H do
  91.   begin
  92.     pt.x := TPA[i].x-Area.X1;
  93.     pt.y := TPA[i].y-Area.Y1;
  94.     if Matrix[pt.y][pt.x] = 1 then
  95.     begin
  96.       Matrix[pt.y][pt.x] := 0;
  97.       lx := Max(pt.x - Distance, 0);
  98.       hx := Min(pt.x + Distance, Area.X2-1);
  99.       ly := Max(pt.y - Distance, 0);
  100.       hy := Min(pt.y + Distance, Area.Y2-1);
  101.       for x:=lx to hx do
  102.         for y:=ly to hy do
  103.           if Matrix[y][x] = 1 then
  104.             TPALine(Result, Point((pt.x),(pt.y)), point(x,y));
  105.     end;
  106.   end;
  107.   {
  108.   bmp := CreateBitmap(400,400);
  109.   SetPixels(bmp,Result,255);
  110.   DisplayDebugImgWindow(400,400);
  111.   DrawBitmapDebugImg(bmp);
  112.   }
  113.   //Add lines to matrix, and find start.
  114.   start := Point(Area.X2, Area.Y2);
  115.   for i:=0 to High(Result) do begin
  116.     Matrix[Result[i].y][Result[i].x] := 255;
  117.     if Result[i].y < start.y then
  118.         start := Result[i];
  119.   end;
  120.  
  121.   //Rooooooling: http://www.youtube.com/watch?v=qCRae5mRoRE
  122.   l := High(Result);
  123.   SetLength(Result, 0);
  124.   SetLength(adj, 8);
  125.   endpt := start;
  126.   prev := Point(start.x, start.y-1);
  127.   i := 0; hit := 0;
  128.   while True do begin
  129.     Inc(i);
  130.     RotatingAdjecent8Way(adj, start, prev);
  131.     for j:=0 to 7 do begin
  132.       if (adj[j].x >= 0) and (adj[j].x < Area.X2) and
  133.          (adj[j].y >= 0) and (adj[j].y < Area.Y2) then
  134.         if Matrix[adj[j].y][adj[j].x] >= 255 then
  135.         begin
  136.           prev := start;
  137.           start := adj[j];
  138.           hx := Matrix[start.y][start.x];
  139.           Matrix[start.y][start.x] := hx+1;
  140.           if hx = 255 then begin
  141.             SetLength(Result, Length(result)+1);
  142.             Result[High(result)] := prev;
  143.           end;
  144.           break;
  145.         end;
  146.     end;
  147.     if ((endpt.x = prev.x) and (endpt.y = prev.y) and (i>1)) or (i>l) then
  148.     begin
  149.       Inc(hit);
  150.       if hit = 2 then Break;
  151.     end;
  152.   end;
  153.   {
  154.   SetPixels(bmp,Result,16777215);
  155.   DrawBitmapDebugImg(bmp);
  156.   FreeBitmap(bmp);}
  157. end;
  158.  
  159.  
  160.  
  161. //----------------------------------------------
  162.  
  163.  
  164. function RandomTPA(Amount:Integer; MinX,MinY,MaxX,MaxY:Integer): TPointArray;
  165. var i:Integer;
  166. begin
  167.   SetLength(Result, Amount);
  168.   for i:=0 to Amount-1 do
  169.     Result[i] := Point(RandomRange(MinX, MaxX), RandomRange(MinY, MaxY));
  170. end;
  171.  
  172. procedure DebugBitmap(bmp: Integer);
  173. var
  174.   w, h: Integer;
  175. begin
  176.   GetBitmapSize(bmp, w, h);
  177.   DisplayDebugImgWindow(w, h);
  178.   DrawBitmapDebugImg(bmp);
  179. end;
  180.  
  181.  
  182. var
  183.   TPA,TPA2: TPointArray;
  184.   t, bmp:Integer;
  185. begin
  186.   bmp := CreateBitmap(500, 500);
  187.   TPA := RandomTPA(1000, 20,20, 400,400);
  188.   DrawTPABitmap(bmp, TPA, 16777215);
  189.   t:=GetSystemTime;
  190.   TPA2 := lines(TPA, 30);
  191.   WriteLn('used '+ToStr(GetSystemTime - t)+'ms');
  192.   DrawTPABitmap(bmp, TPA2, 255);
  193.   DebugBitmap(bmp);
  194. end.
Advertisement
Add Comment
Please, Sign In to add comment