Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {------------------------------------------------------------------------------]
- Check if a point is within a polygon/shape by the given outline points (poly)
- The points must be in order, as if you would draw a line trough each point.
- [------------------------------------------------------------------------------}
- function InPoly(pt:TPoint; poly:TPointArray): Boolean;
- var
- N,I, xint: Integer;
- p1,p2: TPoint;
- begin
- N := Length(poly);
- Result := False;
- if N>2 then
- begin
- p1 := poly[0];
- for I:=0 to N do
- begin
- p2 := poly[I mod N];
- if (pt.y > min(p1.y,p2.y)) and
- (pt.y <= max(p1.y,p2.y)) and
- (pt.x <= max(p1.x,p2.x)) then
- begin
- if (p1.y <> p2.y) then
- xint := (pt.y - p1.y) * (p2.x - p1.x) div (p2.y - p1.y) + p1.x;
- if (p1.x = p2.x) or (pt.x <= xint) then
- Result := Not(Result);
- end;
- p1 := p2;
- end;
- end;
- end;
- //******* DIRTY EXAMPLE BELLOW: *******//
- procedure SetPixels(bmp: Integer; TPA: TPointArray; color: Integer);
- var
- a, b, w, h: Integer;
- begin
- b := High(TPA);
- if (b > -1) then
- begin
- try
- GetBitmapSize(bmp, w, h);
- except
- Exit;
- end;
- for a := 0 to b do
- if ((TPA[a].X > -1) and (TPA[a].Y > -1) and (TPA[a].X < w) and (TPA[a].Y < h)) then
- FastSetPixel(bmp, TPA[a].X, TPA[a].Y, color);
- end;
- end;
- function FillShape(Shape:TPointArray): TPointArray;
- var
- TBA: TBox;
- x,y: Integer;
- begin
- TBA:=GetTPABounds(Shape);
- for x := TBA.x1 to TBA.x2 do
- for y := TBA.y1 to TBA.y2 do
- if InPoly(Point(x,y), Shape) then
- begin
- SetLength(Result, Length(Result)+1);
- Result[High(Result)] := Point(x,y);
- end;
- end;
- function Xagon(Sides:Integer;C,S:TPoint): TPointArray;
- var
- ptx,pty,dx,dy,i: Integer;
- rads:Extended;
- begin
- SetLength(Result,Sides+1);
- ptx := S.x;
- pty := S.y;
- rads := Radians(360/sides);
- for i:=0 to Sides do
- begin
- dx := ptx - c.x;
- dy := pty - c.y;
- ptx := Round((dy * Sin(rads)) + (dx * Cos(rads)) + c.x);
- pty := Round((dy * Cos(rads)) - (dx * Sin(rads)) + c.y);
- Result[i] := Point(ptx,pty);
- end;
- end;
- //******* Plot a few shapes using this algorithm *******//
- var
- Shape: TPointArray;
- bmp: Integer;
- ShowExample : Integer;
- begin
- //Dynamic shapes from Example 6 and down:
- ShowExample := 7;
- Case ShowExample of
- 1: // Manual: Arrow like shape //
- Shape := [Point(70,90),Point(185,90),Point(185,116), Point(70,116), //Square +
- Point(70,140),Point(35,105),Point(70,70)]; //Triangle
- 2: // Manual: Triangle //
- Shape := [Point(50,50),Point(0,100),Point(100,150)];
- 3: // Manual: Raped pentagon //
- Shape := [Point(10,25),Point(35,5),Point(82,22),Point(50,90),Point(4,60)];
- 4: // Manual: Octagon //
- Shape := [Point(100,30), Point(155,50), //Top-Right
- Point(180,110),Point(155,160), //Right-Bottom
- Point(100,180),Point(45,160), //Bottom-Left
- Point(20,100), Point(45,50)]; //Left-Top
- 5: // Manual: S-Shape //
- Shape := [Point(135,50),Point(123,70),Point(103,58),Point(85,70),Point(85,85),
- Point(122,105),Point(132,125),Point(120,155),Point(78,164),Point(55,147),
- Point(64,128),Point(80,140),Point(100,140),Point(96,116),Point(62,98),
- Point(60,55),Point(111,35)];
- 6: // Xagon: 15 corners //
- Shape := Xagon(15, Point(100,100), Point(60,100));
- 7: // Xagon: 9 corners //
- Shape := Xagon(9, Point(100,100), Point(60,50));
- 8: // Xagon: 5 corners AKA Pentagon //
- Shape := Xagon(5, Point(100,100), Point(75,50));
- 9: // Xagon: 3 corners AKA Triangle //
- Shape := Xagon(3, Point(100,100), Point(48,48));
- end;
- bmp := CreateBitmap(200, 200);
- SetPixels(bmp, FillShape(Shape), 255);
- DisplayDebugImgWindow(200, 200);
- DrawBitmapDebugImg(bmp);
- FreeBitmap(bmp);
- end.
Advertisement
Add Comment
Please, Sign In to add comment