Advertisement
Janilabo

Untitled

Jan 11th, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.89 KB | None | 0 0
  1. function InTriangle(const Pt, v1, v2, v3:TPoint): Boolean;
  2. var
  3.   b1,b2,b3: Boolean;
  4.   p1,p2,p3: TPoint;
  5. begin
  6.   p1:=v1; p2:=v2; p3:=v3;
  7.   if p3.y < p1.y then tSwap(p1,p3);
  8.   if p1.x < p2.x then tSwap(p1,p2);
  9.   b1 := (pt.x - p2.x) * (p1.y - p2.y) - (p1.x - p2.x) * (pt.y - p2.y) < 0;
  10.   b2 := (pt.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (pt.y - p3.y) < 0;
  11.   if (b1 <> b2) then Exit;
  12.   b3 := (pt.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (pt.y - p1.y) < 0;
  13.   if (b2 <> b3) then Exit;
  14.   Result := True;
  15. end;
  16.  
  17. function TPAFromTriangle(P1, P2, P3: TPoint): TPointArray;
  18. var
  19.   x, y, r: Integer;
  20.   b: TBox;
  21.   p: TPoint;
  22. begin
  23.   b := IntToBox(Min(Min(P1.X, P2.X), P3.X), Min(Min(P1.Y, P2.Y), P3.Y), Max(Max(P1.X, P2.X), P3.X), Max(Max(P1.Y, P2.Y), P3.Y));
  24.   SetLength(Result, (((b.X2 - b.X1) + 1) * ((b.Y2 - b.Y1) + 1)));
  25.   r := 0;
  26.   for y := b.Y1 to b.Y2 do
  27.     for x := b.X1 to b.X2 do
  28.     begin
  29.       p.X := x;
  30.       p.Y := y;
  31.       if InTriangle(p, P1, P2, P3) then
  32.       begin
  33.         Result[r] := p;
  34.         r := (r + 1);
  35.       end;
  36.     end;
  37.   SetLength(Result, r);
  38. end;
  39.  
  40. procedure DebugBitmap(bmp: Integer);
  41. var
  42.   w, h: Integer;
  43. begin
  44.   GetBitmapSize(bmp, w, h);
  45.   DisplayDebugImgWindow(w, h);
  46.   DrawBitmapDebugImg(bmp);
  47. end;
  48.  
  49. var
  50.   triangle: array[0..2] of TPoint;
  51.   x, y, i: Integer;
  52.   bmp: Integer;
  53.   t: TPointArray;
  54.   s: Integer;
  55.  
  56. begin
  57.   for i := 0 to 2 do
  58.   begin
  59.     triangle[i].X := (50 + Random(700));
  60.     triangle[i].Y := (50 + Random(500));
  61.   end;
  62.   s := GetSystemTime;
  63.   t := TPAFromTriangle(triangle[0], triangle[1], triangle[2]);
  64.   WriteLn(IntToStr(GetSystemTime - s) + ' ms.');
  65.   bmp := CreateBitmap(900, 600);
  66.   DrawTPABitmap(bmp, t, 255);
  67.   FastSetPixel(bmp, triangle[0].X, triangle[0].Y, 16777215);
  68.   FastSetPixel(bmp, triangle[1].X, triangle[1].Y, 16024898);
  69.   FastSetPixel(bmp, triangle[2].X, triangle[2].Y, 32768);
  70.   DebugBitmap(bmp);
  71. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement