4

How can I draw an anti-aliased rounded corners rectangle using Graphics32?

I managed to make a normal rectangle with TPolygon onto the bitmap32 canvas, but I cant find any reference to drawing rounded corners. Would appreciate some code.

hikari
  • 3,393
  • 1
  • 33
  • 72

1 Answers1

6
function GetRoundedFixedRectanglePoints(const Rect: TFloatRect; dx, dy: single): TArrayOfFixedPoint;
var
  i, j, k, arcLen: integer;
  arcs: array [0 .. 3] of TArrayOfFixedPoint;
begin
  //nb: it's simpler to construct the rounded rect in an anti-clockwise
  //direction because that's the direction in which the arc points are returned.

  with Rect do
  begin
    arcs[0] := GetArcPointsEccentric(
      FloatRect(Left, Bottom -dy*2, Left+dx*2, Bottom), rad180, rad270);
    arcs[1] := GetArcPointsEccentric(
      FloatRect(Right-dx*2, Bottom -dy*2, Right, Bottom), rad270, 0);
    arcs[2] := GetArcPointsEccentric(
      FloatRect(Right - dx*2, Top, Right, Top + dy*2), 0, rad90);
    arcs[3] := GetArcPointsEccentric(
      FloatRect(Left, top, Left+dx*2, Top+dy*2), rad90, rad180);

    //close the rectangle
    SetLength(arcs[3], Length(arcs[3])+1);
    arcs[3][Length(arcs[3])-1] := arcs[0][0];
  end;

  //calculate the final number of points to return
  j := 0;
  for i := 0 to 3 do
    Inc(j, Length(arcs[i]));
  SetLength(Result, j);

  j := 0;
  for i := 0 to 3 do
  begin
    arcLen := Length(arcs[i]);
    for k := 0 to arcLen -1 do
      Result[j+k] := arcs[i][k];
    Inc(j, arcLen);
  end;
end;

Usage:

var
  pts: TArrayOfFixedPoint;
begin    
  pts := GetRoundedFixedRectanglePoints(FloatRect(Left+2, Top+2, Left + Width-2, Top + Height-2), 5, 5);

  SimpleFill(ABitmap32, pts, 0, Color32(clBlack));  
pani
  • 1,075
  • 6
  • 13
  • 1
    Where are all these procs from? it doesn't seem to be part of GR32. – hikari Dec 27 '12 at 08:36
  • 1
    GR32_Lines library for Graphics32 (GR32_Misc unit). You can download it from http://www.angusj.com/delphi/gr32_lines.php – pani Dec 27 '12 at 09:57
  • I see, thanks. Need a little time for this, seems the extension needs the latest SVN files rather than the official 1.91 which removes TPolygon32 and conflicts with all my code -.- will check and get back to you later today. – hikari Dec 27 '12 at 13:40