Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function InTriangle(const Pt, v1, v2, v3:TPoint): Boolean;
- var
- b1,b2,b3: Boolean;
- p1,p2,p3: TPoint;
- begin
- p1:=v1; p2:=v2; p3:=v3;
- if p3.y < p1.y then tSwap(p1,p3);
- if p1.x < p2.x then tSwap(p1,p2);
- b1 := (pt.x - p2.x) * (p1.y - p2.y) - (p1.x - p2.x) * (pt.y - p2.y) < 0;
- b2 := (pt.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (pt.y - p3.y) < 0;
- if (b1 <> b2) then Exit;
- b3 := (pt.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (pt.y - p1.y) < 0;
- if (b2 <> b3) then Exit;
- Result := True;
- end;
- function TPAFromTriangle(P1, P2, P3: TPoint): TPointArray;
- var
- x, y, r: Integer;
- b: TBox;
- p: TPoint;
- begin
- 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));
- SetLength(Result, (((b.X2 - b.X1) + 1) * ((b.Y2 - b.Y1) + 1)));
- r := 0;
- for y := b.Y1 to b.Y2 do
- for x := b.X1 to b.X2 do
- begin
- p.X := x;
- p.Y := y;
- if InTriangle(p, P1, P2, P3) then
- begin
- Result[r] := p;
- r := (r + 1);
- end;
- end;
- SetLength(Result, r);
- end;
- procedure DebugBitmap(bmp: Integer);
- var
- w, h: Integer;
- begin
- GetBitmapSize(bmp, w, h);
- DisplayDebugImgWindow(w, h);
- DrawBitmapDebugImg(bmp);
- end;
- var
- triangle: array[0..2] of TPoint;
- x, y, i: Integer;
- bmp: Integer;
- t: TPointArray;
- s: Integer;
- begin
- for i := 0 to 2 do
- begin
- triangle[i].X := (50 + Random(700));
- triangle[i].Y := (50 + Random(500));
- end;
- s := GetSystemTime;
- t := TPAFromTriangle(triangle[0], triangle[1], triangle[2]);
- WriteLn(IntToStr(GetSystemTime - s) + ' ms.');
- bmp := CreateBitmap(900, 600);
- DrawTPABitmap(bmp, t, 255);
- FastSetPixel(bmp, triangle[0].X, triangle[0].Y, 16777215);
- FastSetPixel(bmp, triangle[1].X, triangle[1].Y, 16024898);
- FastSetPixel(bmp, triangle[2].X, triangle[2].Y, 32768);
- DebugBitmap(bmp);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement