Janilabo

Untitled

Jul 20th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 7.51 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.   g: Boolean;
  55.   e: TPointArray;
  56. begin
  57.   N := Length(poly);
  58.   Result := False;
  59.   if N>2 then
  60.   begin
  61.     e := CopyTPA(poly);
  62.     WriteLn('BEFORE: ' + ToStr(e));
  63.     TPAEdge(e);
  64.     WriteLn('AFTER: ' + ToStr(e));
  65.     p1 := poly[0];
  66.     for I:=0 to N do
  67.     begin
  68.       p2 := poly[I mod N];
  69.       g := PointInTPA(pt, e);
  70.       if (g or (pt.y > min(p1.y,p2.y)))  and
  71.          (g or (pt.y <= max(p1.y,p2.y))) and
  72.          (g or (pt.x <= max(p1.x,p2.x))) then
  73.       begin
  74.         if not g then
  75.           if (p1.y <> p2.y) then
  76.             xint := (pt.y - p1.y) * (p2.x - p1.x) div (p2.y - p1.y) + p1.x;
  77.         if (g or ((p1.x = p2.x) or (pt.x < xint))) then
  78.           Result := Not(Result);
  79.       end;
  80.       p1 := p2;
  81.     end;
  82.   end;
  83. end;
  84.  
  85. //******* DIRTY EXAMPLE BELLOW: *******//
  86. procedure SetPixels(bmp: Integer; TPA: TPointArray; color: Integer);
  87. var
  88.   a, z, w, h: Integer;
  89. begin
  90.   z := High(TPA);
  91.   if (z > -1) then
  92.   begin
  93.     GetBitmapSize(bmp, w, h);
  94.     for a := 0 to z do
  95.       if ((TPA[a].X >= 0) and (TPA[a].Y >= 0) and (TPA[a].X < w) and (TPA[a].Y < h)) then
  96.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y, color);
  97.   end;
  98. end;
  99.  
  100. procedure SetPixelsMP(bmp: Integer; TPA: TPointArray; color: Integer);
  101. var
  102.   a, z, w, h: Integer;
  103. begin
  104.   z := High(TPA);
  105.   if (z > -1) then
  106.   begin
  107.     GetBitmapSize(bmp, w, h);
  108.     for a := 0 to z do
  109.       if ((TPA[a].X > 0) and (TPA[a].Y > 0) and (TPA[a].X < w-1) and (TPA[a].Y < h-1)) then
  110.       begin
  111.         FastSetPixel(bmp, TPA[a].X-1, TPA[a].Y, color);
  112.         FastSetPixel(bmp, TPA[a].X+1, TPA[a].Y, color);
  113.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y+1, color);
  114.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y-1, color);
  115.       end;
  116.   end;
  117. end;
  118.  
  119. function FillShape(shape: TPointArray): TPointArray;
  120. var
  121.   z: TBox;
  122.   x, y, r, l, i, n, c: Integer;
  123.   a, b: TPoint;
  124.   v, g: Boolean;
  125.   p: TPoint;
  126.   e: TPointArray;
  127. begin
  128.   l := Length(shape);
  129.   if (l > 2) then
  130.   begin
  131.     z := IntToBox(shape[0].X, shape[0].Y, shape[0].X, shape[0].Y);
  132.     if (l > 1) then
  133.     for i := 1 to (l - 1) do
  134.     begin
  135.       if (shape[i].X < z.X1) then
  136.         z.X1 := shape[i].X
  137.       else
  138.         if (shape[i].X > z.X2) then
  139.           z.X2 := shape[i].X;
  140.       if (shape[i].Y < z.Y1) then
  141.         z.Y1 := shape[i].Y
  142.       else
  143.         if (shape[i].Y > z.Y2) then
  144.           z.Y2 := shape[i].Y;
  145.     end;
  146.     e := CopyTPA(shape);
  147.     TPAEdge(e);  
  148.     SetLength(Result, (((z.X1 * z.X2) + 1) * ((z.Y1 * z.Y2) + 1)));
  149.     n := Length(Result);
  150.     if (n > 0) then
  151.     begin
  152.       for y := z.y1 to z.y2 do
  153.         for x := z.x1 to z.x2 do
  154.         begin
  155.           p := Point(x, y);
  156.           v := False;
  157.           a := shape[0];
  158.           v := PointInTPA(p, e);
  159.           if not v then
  160.           for i := 0 to l do
  161.           begin
  162.             b := shape[Round(i mod l)];
  163.             if ((p.y > Min(a.y, b.y)) and (p.y <= Max(a.y, b.y)) and (p.x <= Max(a.x, b.x))) then
  164.             begin
  165.               if (a.y <> b.y) then
  166.                 c := ((p.y - a.y) * (b.x - a.x) div (b.y - a.y) + a.x);
  167.               if ((a.x = b.x) or (p.x <= c)) then
  168.               v := not v;
  169.             end;
  170.             a := b;
  171.           end;
  172.           if v then
  173.           begin
  174.             Result[r] := Point(x, y);
  175.             Inc(r);
  176.           end;
  177.         end;
  178.       SetLength(Result, r);
  179.     end;
  180.   end else
  181.     SetLength(Result, 0);
  182. end;
  183.  
  184. function Xagon(Sides:Integer;C,S:TPoint): TPointArray;
  185. var
  186.   ptx,pty,dx,dy,i: Integer;
  187.   rads:Extended;
  188. begin
  189.   SetLength(Result,Sides+1);
  190.   ptx := S.x;
  191.   pty := S.y;
  192.   rads := Radians(360/sides);
  193.   for i:=0 to Sides do
  194.   begin
  195.     dx := ptx - c.x;
  196.     dy := pty - c.y;
  197.     ptx := Round((dy * Sin(rads)) + (dx * Cos(rads)) + c.x);
  198.     pty := Round((dy * Cos(rads)) - (dx * Sin(rads)) + c.y);
  199.     Result[i] := Point(ptx,pty);
  200.   end;
  201. end;
  202.  
  203. //******* Plot a few shapes using this algorithm *******//
  204. var
  205.   Shape, TPA: TPointArray;
  206.   bmp: Integer;
  207.   ShowExample,ShowPoints: Integer;
  208. begin
  209.   //Dynamic shapes from Example 6 and down:
  210.   ShowExample := 0; //1 - 9
  211.   ShowPoints := 0;  //0 or 1
  212.  
  213.   Case ShowExample of
  214.   0: Shape := [Point(50, 50), Point(51, 50), Point(51, 50), Point(51, 51)];
  215.   1: // Manual: Arrow like shape //
  216.      Shape := [Point(70,90),Point(185,90),Point(185,116), Point(70,116), //Square +
  217.                Point(70,140),Point(35,105),Point(70,70)];                //Triangle
  218.  
  219.   2: // Manual: Triangle //
  220.      Shape := [Point(50,50),Point(0,100),Point(100,150)];
  221.  
  222.   3: // Manual: Raped pentagon //
  223.      Shape := [Point(10,25),Point(35,5),Point(82,22),Point(50,90),Point(4,60)];
  224.  
  225.   4: // Manual: Octagon //
  226.      Shape := [Point(100,30), Point(155,50),  //Top-Right
  227.                Point(180,110),Point(155,160), //Right-Bottom
  228.                Point(100,180),Point(45,160),  //Bottom-Left
  229.                Point(20,100), Point(45,50)];  //Left-Top
  230.  
  231.   5: // Manual: S-Shape //
  232.      Shape := [Point(135,50),Point(123,70),Point(103,58),Point(85,70),Point(85,85),
  233.                 Point(122,105),Point(132,125),Point(120,155),Point(78,164),Point(55,147),
  234.                 Point(64,128),Point(80,140),Point(100,140),Point(96,116),Point(62,98),
  235.                 Point(60,55),Point(111,35)];
  236.  
  237.   6: // Xagon: 15 corners //
  238.      Shape := Xagon(15, Point(100,100), Point(60,100));
  239.  
  240.   7: // Xagon: 9 corners //
  241.      Shape := Xagon(9, Point(100,100), Point(60,50));
  242.  
  243.   8: // Xagon: 5 corners AKA Pentagon //
  244.      Shape := Xagon(5, Point(100,100), Point(75,50));
  245.  
  246.   9: // Xagon: 3 corners AKA Triangle //
  247.      Shape := Xagon(3, Point(100,100), Point(48,48));
  248.   end;
  249.  
  250.   bmp := CreateBitmap(200, 200);
  251.   TPA := FillShape(Shape);
  252.   //TPAEdge(TPA);
  253.   SetPixels(bmp, TPA, 357435);
  254.   if ShowPoints=1 then
  255.     SetPixelsMP(bmp, Shape, 16777215);
  256.   DisplayDebugImgWindow(200, 200);
  257.   DrawBitmapDebugImg(bmp);
  258.   FreeBitmap(bmp);
  259. end.
Advertisement
Add Comment
Please, Sign In to add comment