Janilabo

Untitled

Jul 20th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.09 KB | None | 0 0
  1. procedure TPAEdge(var TPA: TPointArray);
  2. var
  3.   v, x, y, h, r: Integer;
  4.   B: array of TBoolArray;
  5.   bx: TBox;
  6. begin;
  7.   h := High(TPA);
  8.   if (h > 3) then
  9.   begin
  10.     bx := IntToBox(TPA[0].X, TPA[0].Y, TPA[0].X, TPA[0].Y);
  11.     if (h > 0) then
  12.     for v := 1 to h do
  13.     begin
  14.       if (TPA[v].X < bx.X1) then
  15.         bx.X1 := TPA[v].X
  16.       else
  17.         if (TPA[v].X > bx.X2) then
  18.           bx.X2 := TPA[v].X;
  19.       if (TPA[v].Y < bx.Y1) then
  20.         bx.Y1 := TPA[v].Y
  21.       else
  22.         if (TPA[v].Y > bx.Y2) then
  23.           bx.Y2 := TPA[v].Y;
  24.     end;
  25.     bx := IntToBox((bx.X1 - 1), (bx.Y1 - 1), (bx.X2 + 1), (bx.Y2 + 1));
  26.     SetLength(B, ((bx.X2 - bx.X1) + 1));
  27.     for v := 0 to (bx.X2 - bx.X1) do
  28.       SetLength(B[v], ((bx.Y2 - bx.Y1) + 1));
  29.     for v := 0 to h do
  30.       if not B[(TPA[v].X - bx.X1)][(TPA[v].Y - bx.Y1)] then
  31.         B[(TPA[v].X - bx.X1)][(TPA[v].Y - bx.Y1)] := True;
  32.     for y := 1 to ((bx.Y2 - bx.Y1) - 1) do
  33.       for x := 1 to ((bx.X2 - bx.X1) - 1) do
  34.         if B[x][y] then
  35.           if not (B[(x - 1)][y] and B[x][(y - 1)] and B[(x + 1)][y] and B[x][(y + 1)]) then
  36.           begin
  37.             TPA[r].X := (x + bx.X1);
  38.             TPA[r].Y := (y + bx.Y1);
  39.             Inc(r);
  40.           end;
  41.     SetLength(TPA, r);
  42.     SetLength(B, 0);
  43.   end;
  44. end;
  45.  
  46. {------------------------------------------------------------------------------]
  47.  Check if a point is within a polygon/shape by the given outline points (poly)
  48.  The points must be in order, as if you would draw a line trough each point.
  49. [------------------------------------------------------------------------------}
  50. function InPoly(pt:TPoint; poly:TPointArray): Boolean;
  51. var
  52.   N,I, xint: Integer;
  53.   p1,p2: TPoint;
  54.   e: TPointArray;
  55. begin
  56.   N := Length(poly);
  57.   Result := False;
  58.   if N>2 then
  59.   begin
  60.     e := CopyTPA(poly);
  61.     TPAEdge(e);
  62.     Result := PointInTPA(pt, e);
  63.     if Result then
  64.       Exit;
  65.     p1 := poly[0];
  66.     for I:=0 to N do
  67.     begin
  68.       p2 := poly[I mod N];
  69.       if (pt.y > min(p1.y,p2.y))  and
  70.          (pt.y <= max(p1.y,p2.y)) and
  71.          (pt.x <= max(p1.x,p2.x)) then
  72.       begin
  73.         if (p1.y <> p2.y) then
  74.           xint := (pt.y - p1.y) * (p2.x - p1.x) div (p2.y - p1.y) + p1.x;
  75.         if (p1.x = p2.x) or (pt.x < xint) then
  76.           Result := Not(Result);
  77.       end;
  78.       p1 := p2;
  79.     end;
  80.   end;
  81. end;
  82.  
  83. //******* DIRTY EXAMPLE BELLOW: *******//
  84. procedure SetPixels(bmp: Integer; TPA: TPointArray; color: Integer);
  85. var
  86.   a, z, w, h: Integer;
  87. begin
  88.   z := High(TPA);
  89.   if (z > -1) then
  90.   begin
  91.     GetBitmapSize(bmp, w, h);
  92.     for a := 0 to z do
  93.       if ((TPA[a].X >= 0) and (TPA[a].Y >= 0) and (TPA[a].X < w) and (TPA[a].Y < h)) then
  94.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y, color);
  95.   end;
  96. end;
  97.  
  98. procedure SetPixelsMP(bmp: Integer; TPA: TPointArray; color: Integer);
  99. var
  100.   a, z, w, h: Integer;
  101. begin
  102.   z := High(TPA);
  103.   if (z > -1) then
  104.   begin
  105.     GetBitmapSize(bmp, w, h);
  106.     for a := 0 to z do
  107.       if ((TPA[a].X > 0) and (TPA[a].Y > 0) and (TPA[a].X < w-1) and (TPA[a].Y < h-1)) then
  108.       begin
  109.         FastSetPixel(bmp, TPA[a].X-1, TPA[a].Y, color);
  110.         FastSetPixel(bmp, TPA[a].X+1, TPA[a].Y, color);
  111.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y+1, color);
  112.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y-1, color);
  113.       end;
  114.   end;
  115. end;
  116.  
  117. function FillShape(Shape:TPointArray): TPointArray;
  118. var
  119.   TBA: TBox;
  120.   x,y: Integer;
  121. begin
  122.   TBA:=GetTPABounds(Shape);
  123.   for x := TBA.x1 to TBA.x2 do
  124.     for y := TBA.y1 to TBA.y2 do
  125.       if InPoly(Point(x,y), Shape) then
  126.       begin
  127.         SetLength(Result, Length(Result)+1);
  128.         Result[High(Result)] := Point(x,y);
  129.       end;
  130. end;
  131.  
  132. function Xagon(Sides:Integer;C,S:TPoint): TPointArray;
  133. var
  134.   ptx,pty,dx,dy,i: Integer;
  135.   rads:Extended;
  136. begin
  137.   SetLength(Result,Sides+1);
  138.   ptx := S.x;
  139.   pty := S.y;
  140.   rads := Radians(360/sides);
  141.   for i:=0 to Sides do
  142.   begin
  143.     dx := ptx - c.x;
  144.     dy := pty - c.y;
  145.     ptx := Round((dy * Sin(rads)) + (dx * Cos(rads)) + c.x);
  146.     pty := Round((dy * Cos(rads)) - (dx * Sin(rads)) + c.y);
  147.     Result[i] := Point(ptx,pty);
  148.   end;
  149. end;
  150.  
  151. //******* Plot a few shapes using this algorithm *******//
  152. var
  153.   Shape, TPA: TPointArray;
  154.   bmp: Integer;
  155.   ShowExample,ShowPoints: Integer;
  156. begin
  157.   //Dynamic shapes from Example 6 and down:
  158.   ShowExample := 0; //1 - 9
  159.   ShowPoints := 0;  //0 or 1
  160.  
  161.   Case ShowExample of
  162.   0: Shape := [Point(50, 50), Point(51, 50), Point(50, 51), Point(51, 51)];
  163.   1: // Manual: Arrow like shape //
  164.      Shape := [Point(70,90),Point(185,90),Point(185,116), Point(70,116), //Square +
  165.                Point(70,140),Point(35,105),Point(70,70)];                //Triangle
  166.  
  167.   2: // Manual: Triangle //
  168.      Shape := [Point(50,50),Point(0,100),Point(100,150)];
  169.  
  170.   3: // Manual: Raped pentagon //
  171.      Shape := [Point(10,25),Point(35,5),Point(82,22),Point(50,90),Point(4,60)];
  172.  
  173.   4: // Manual: Octagon //
  174.      Shape := [Point(100,30), Point(155,50),  //Top-Right
  175.                Point(180,110),Point(155,160), //Right-Bottom
  176.                Point(100,180),Point(45,160),  //Bottom-Left
  177.                Point(20,100), Point(45,50)];  //Left-Top
  178.  
  179.   5: // Manual: S-Shape //
  180.      Shape := [Point(135,50),Point(123,70),Point(103,58),Point(85,70),Point(85,85),
  181.                 Point(122,105),Point(132,125),Point(120,155),Point(78,164),Point(55,147),
  182.                 Point(64,128),Point(80,140),Point(100,140),Point(96,116),Point(62,98),
  183.                 Point(60,55),Point(111,35)];
  184.  
  185.   6: // Xagon: 15 corners //
  186.      Shape := Xagon(15, Point(100,100), Point(60,100));
  187.  
  188.   7: // Xagon: 9 corners //
  189.      Shape := Xagon(9, Point(100,100), Point(60,50));
  190.  
  191.   8: // Xagon: 5 corners AKA Pentagon //
  192.      Shape := Xagon(5, Point(100,100), Point(75,50));
  193.  
  194.   9: // Xagon: 3 corners AKA Triangle //
  195.      Shape := Xagon(3, Point(100,100), Point(48,48));
  196.   end;
  197.  
  198.   bmp := CreateBitmap(200, 200);
  199.   TPA := FillShape(Shape);
  200.   //TPAEdge(TPA);
  201.   SetPixels(bmp, TPA, 357435);
  202.   if ShowPoints=1 then
  203.     SetPixelsMP(bmp, Shape, 16777215);
  204.   DisplayDebugImgWindow(200, 200);
  205.   DrawBitmapDebugImg(bmp);
  206.   FreeBitmap(bmp);
  207. end.
Advertisement
Add Comment
Please, Sign In to add comment