Janilabo

progress2

Jul 20th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 8.81 KB | None | 0 0
  1. { Given a TPA - This function will connect each point, from first to last }
  2. function ConnectPts(pts: TPointArray): TPointArray;
  3. var
  4.   i, j, h, l, r, a: Integer;
  5.   q, p: TPoint;
  6. begin
  7.   if (High(pts) > -1) then
  8.   begin
  9.     h := High(pts);
  10.     for i := 0 to h do
  11.     begin
  12.       j := (i + 1);
  13.       if (i = h) then
  14.         j := 0;
  15.       q := pts[i];
  16.       p := pts[j];
  17.       r := Length(Result);
  18.       if ((q.X <> p.X) or (q.Y <> p.Y)) then
  19.       begin
  20.         l := Max(Round(Abs(q.X - p.X)), Round(Abs(q.Y - p.Y)));
  21.         SetLength(Result, ((r + l) + 1));
  22.         for a := 0 to l do
  23.           Result[(r + a)] := Point((q.X + Round((p.X - q.X) * (a / Extended(l)))), (q.Y + Round((p.Y - q.Y) * (a / Extended(l)))));
  24.       end else
  25.       begin
  26.         SetLength(Result, (r + 1));
  27.         Result[r] := q;
  28.       end;
  29.     end;
  30.   end else
  31.     SetLength(Result, 0);
  32. end;
  33.  
  34. {------------------------------------------------------------------------------]
  35.  Check if a point is within a polygon/shape by the given outline points (poly)
  36.  The points must be in order, as if you would draw a line trough each point.
  37. [------------------------------------------------------------------------------}
  38. function InPoly(pt: TPoint; poly: TPointArray): Boolean;
  39. var
  40.   n, i: Integer;
  41.   z: Extended;
  42.   a, b: TPoint;
  43. begin
  44.   n := Length(poly);
  45.   Result := False;
  46.   case n of
  47.     0: Exit;
  48.     1: Result := ((pt.X = poly[0].X) and (pt.Y = poly[0].Y));
  49.     2: Result := (((pt.X = poly[0].X) and (pt.Y = poly[0].Y)) or ((pt.X = poly[1].X) and (pt.Y = poly[1].Y)));
  50.   else
  51.     begin
  52.       for i := 0 to (n - 1) do
  53.         if ((pt.X = poly[i].X) and (pt.Y = poly[i].Y)) then
  54.           Break;
  55.       Result := (i < n);
  56.       if not Result then
  57.       begin
  58.         a := poly[0];
  59.         for i := 0 to n do
  60.         begin
  61.           b := poly[(i mod n)];
  62.           if (pt.Y > Min(a.Y, b.Y)) then
  63.             if (pt.Y <= Max(a.Y, b.Y)) then
  64.               if (pt.X <= Max(a.X, b.X)) then
  65.               begin
  66.                 if (a.y <> b.y) then
  67.                   z := ((pt.Y - a.Y) * (b.X - a.X) / Extended((b.Y - a.Y)) + a.X);
  68.                 if ((a.X = b.X) or (pt.X < z)) then
  69.                   Result := not Result;
  70.               end;
  71.           a := b;
  72.         end;
  73.       end;
  74.     end;
  75.   end;
  76. end;
  77.  
  78. //******* DIRTY EXAMPLE BELLOW: *******//
  79. procedure SetPixels(bmp: Integer; TPA: TPointArray; color: Integer);
  80. var
  81.   a, z, w, h: Integer;
  82. begin
  83.   z := High(TPA);
  84.   if (z > -1) then
  85.   begin
  86.     GetBitmapSize(bmp, w, h);
  87.     for a := 0 to z do
  88.       if ((TPA[a].X >= 0) and (TPA[a].Y >= 0) and (TPA[a].X < w) and (TPA[a].Y < h)) then
  89.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y, color);
  90.   end;
  91. end;
  92.  
  93. procedure SetPixelsMP(bmp: Integer; TPA: TPointArray; color: Integer);
  94. var
  95.   a, z, w, h: Integer;
  96. begin
  97.   z := High(TPA);
  98.   if (z > -1) then
  99.   begin
  100.     GetBitmapSize(bmp, w, h);
  101.     for a := 0 to z do
  102.       if ((TPA[a].X > 0) and (TPA[a].Y > 0) and (TPA[a].X < (w - 1)) and (TPA[a].Y < (h - 1))) then
  103.       begin
  104.         FastSetPixel(bmp, (TPA[a].X - 1), TPA[a].Y, color);
  105.         FastSetPixel(bmp, (TPA[a].X + 1), TPA[a].Y, color);
  106.         FastSetPixel(bmp, TPA[a].X, (TPA[a].Y + 1), color);
  107.         FastSetPixel(bmp, TPA[a].X, (TPA[a].Y - 1), color);
  108.       end;
  109.   end;
  110. end;
  111.  
  112. { Fills an polygon by the given outlines }
  113. function FillShape(shape: TPointArray): TPointArray;
  114. var
  115.   b: TBox;
  116.   x, y, h, i, v, l, r, z: Integer;
  117.   o: TPointArray;
  118.   f: Boolean;
  119.   t: array of TBoolArray;
  120.   e: Extended;
  121.   q, p, d: TPoint;
  122. begin
  123.   h := High(shape);
  124.   if (h > -1) then
  125.   begin
  126.     for i := 0 to h do
  127.     begin
  128.       v := (i + 1);
  129.       if (i = h) then
  130.         v := 0;
  131.       q := shape[i];
  132.       p := shape[v];
  133.       r := Length(o);
  134.       if ((q.X <> p.X) or (q.Y <> p.Y)) then
  135.       begin
  136.         l := Max(Round(Abs(q.X - p.X)), Round(Abs(q.Y - p.Y)));
  137.         SetLength(o, ((r + l) + 1));
  138.         for z := 0 to l do
  139.           o[(r + z)] := Point((q.X + Round((p.X - q.X) * (z / Extended(l)))), (q.Y + Round((p.Y - q.Y) * (z / Extended(l)))));
  140.       end else
  141.       begin
  142.         SetLength(o, (r + 1));
  143.         o[r] := q;
  144.       end;
  145.       if (i > 0) then
  146.       begin
  147.         if (shape[i].X < b.X1) then
  148.           b.X1 := shape[i].X
  149.         else
  150.           if (shape[i].X > b.X2) then
  151.             b.X2 := shape[i].X;
  152.         if (shape[i].Y < b.Y1) then
  153.           b.Y1 := shape[i].Y
  154.         else
  155.           if (shape[i].Y > b.Y2) then
  156.             b.Y2 := shape[i].Y;
  157.       end else
  158.         b := IntToBox(shape[0].X, shape[0].Y, shape[0].X, shape[0].Y);
  159.     end;
  160.     SetLength(t, ((b.X2 - b.X1) + 1));
  161.     for i := 0 to (b.X2 - b.X1) do
  162.       SetLength(t[i], ((b.Y2 - b.Y1) + 1));
  163.     l := Length(o);
  164.     for i := 0 to (l - 1) do
  165.       if not t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] then
  166.         t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] := True;
  167.     for y := 0 to (b.Y2 - b.Y1) do
  168.       for x := 0 to (b.X2 - b.X1) do
  169.       begin
  170.         f := t[x][y];
  171.         if not f then
  172.         begin
  173.           d := Point((x + b.X1), (y + b.Y1));
  174.           for i := 0 to h do
  175.             if ((d.X = shape[i].X) and (d.Y = shape[i].Y)) then
  176.               Break;
  177.           f := (i < (h + 1));
  178.           if not f then
  179.           begin
  180.             q := shape[0];
  181.             for i := 0 to (h + 1) do
  182.             begin
  183.               p := shape[(i mod (h + 1))];
  184.               if (d.Y > Min(q.Y, p.Y)) then
  185.                 if (d.Y <= Max(q.Y, p.Y)) then
  186.                   if (d.X <= Max(q.X, p.X)) then
  187.                   begin
  188.                     if (q.y <> p.y) then
  189.                       e := ((d.Y - q.Y) * (p.X - q.X) / Extended((p.Y - q.Y)) + q.X);
  190.                     if ((q.X = p.X) or (d.X < e)) then
  191.                       f := not f;
  192.                   end;
  193.               q := p;
  194.             end;
  195.           end;
  196.         end;
  197.         if f then
  198.         begin
  199.           l := Length(Result);
  200.           SetLength(Result, (l + 1));
  201.           Result[l] := Point((x + b.X1), (y + b.Y1));
  202.         end;
  203.       end;
  204.     SetLength(o, 0);
  205.     SetLength(t, 0);
  206.   end else
  207.     SetLength(Result, 0);
  208. end;
  209.  
  210. { Creates all the points needed to define a simple polygon }
  211. function Xagon(Sides: Integer; C, S: TPoint): TPointArray;
  212. var
  213.   ptx, pty, dx, dy, v: Integer;
  214.   SinR, CosR: Extended;
  215. begin
  216.   SetLength(Result,Sides);
  217.   ptx := S.x;
  218.   pty := S.y;
  219.   SinR := Sin(Radians(360.0 / sides));
  220.   CosR := Cos(Radians(360.0 / sides));
  221.   for v := 0 to (Sides - 1) do
  222.   begin
  223.     dx := (ptx - c.x);
  224.     dy := (pty - c.y);
  225.     ptx := Round(((dy * SinR) + (dx * CosR) + c.x));
  226.     pty := Round(((dy * CosR) - (dx * SinR) + c.y));
  227.     Result[v] := Point(ptx, pty);
  228.   end;
  229. end;
  230.  
  231. //******* Plot a few shapes using this algorithm *******//
  232. var
  233.   shape, TPA: TPointArray;
  234.   bmp, t: Integer;
  235.   ShowExample: Integer;
  236.   ShowPoints: Boolean;
  237.  
  238. begin
  239.   //Dynamic shapes from Example 6 and down:
  240.   ShowExample := 1; //0 - 9
  241.   ShowPoints := True;  //0 or 1
  242.   case ShowExample of
  243.     // Microshape ------//
  244.     //           TOP            LEFT           BOTTOM         RIGHT
  245.     0: shape := [Point(50, 50), Point(60, 50), Point(51, 51), Point(50, 51)];
  246.     // Arrow like shape //
  247.     1: shape := [Point(70,90),Point(185,90),Point(185,116), Point(70,116), //Square +
  248.                  Point(70,140),Point(35,105),Point(70,70)];                //Triangle
  249.     // Triangle --------//
  250.     2: shape := [Point(50,50),Point(0,100),Point(100,150)];
  251.     // Raped pentagon --//
  252.     3: shape := [Point(10,25),Point(35,5),Point(82,22),Point(50,90),Point(4,60)];
  253.     // Octagon ---------//
  254.     4: shape := [Point(100,30), Point(155,50),  //Top-Right
  255.                  Point(180,110),Point(155,160), //Right-Bottom
  256.                  Point(100,180),Point(45,160),  //Bottom-Left
  257.                  Point(20,100), Point(45,50)];  //Left-Top
  258.     // S-shape ---------//
  259.     5: shape := [Point(135,50),Point(123,70),Point(103,58),Point(85,70),Point(85,85),
  260.                  Point(122,105),Point(132,125),Point(120,155),Point(78,164),Point(55,147),
  261.                  Point(64,128),Point(80,140),Point(100,140),Point(96,116),Point(62,98),
  262.                  Point(60,55),Point(111,35)];
  263.     // Xagon: 15 corners //
  264.     6: shape := Xagon(15, Point(100,100), Point(60,100));
  265.     // Xagon: 9 corners //
  266.     7: shape := Xagon(9, Point(100,100), Point(60,50));
  267.     // Xagon: 5 corners AKA Pentagon //
  268.     8: shape := Xagon(5, Point(100,100), Point(74,50));
  269.     // Xagon: 3 corners AKA Triangle //
  270.     9: shape := Xagon(3, Point(100,100), Point(48,48));
  271.   end;
  272.   bmp := CreateBitmap(200, 200);
  273.   t := GetSystemTime;
  274.   TPA := FillShape(shape);
  275.   WriteLn(IntToStr(GetSystemTime - t) + ' ms.');
  276.   SetPixels(bmp, TPA, 357435);
  277.   if ShowPoints then
  278.     SetPixelsMP(bmp, shape, 16777215);
  279.   DisplayDebugImgWindow(200, 200);
  280.   DrawBitmapDebugImg(bmp);
  281.   FreeBitmap(bmp);
  282.   SetLength(TPA, 0);
  283. end.
Advertisement
Add Comment
Please, Sign In to add comment