PDA

View Full Version : [Lape] Curve method for TMufasaBitmap



Dgby714
06-03-2013, 09:23 AM
I've written a Curve method for TMufasaBitmap in Lape.

It can take any number of TPoints, it will go from the first to the last (Only they will be touched, unless Straight).

I've also written a Squircle method for TMufasaBitmap that uses the Curve method.

Here's an example of what it does. (Blue: Squircle; Green: Points; Red: Curve)
http://i.imgur.com/Vow6LKU.png

In this example, I made a Squircle, then generated an array of 3 random points, Called Curve, and then painted the points.

procedure Curve(const Points: TPointArray; const Color: TColor; const Sharpness: UInt32 = 30);
Sharpness - Technically, is just the number of lines in the curve.

procedure Squircle(const X1, Y1, X2, Y2: UInt32; const Color: TColor; const Size: UInt32 = 20; const Sharpness: UInt32 = 30);
Size - Number of pixels to remove from the lines for the curve.
Sharpness - Same as above.

procedure TMufasaBitmap.Curve(const Points: TPointArray; const Color: TColor; const Sharpness: UInt32 = 30);
function getB(const H, K: UInt32): UInt32;
begin
Result := 1;
if ((K <> 0) and (K <> H)) then
Result := getB(H - 1, K - 1) + getB(H - 1, K);
end;
var
I, K, H: UInt32;
U, B, X, Y: Double;
Pos: TPoint;
begin
H := High(Points);

for I := 0 to Sharpness do
begin
X := Y := 0;
U := I / Sharpness;

for K := 0 to H do
begin
B := getB(H, K) * Pow(U, K) * Pow(1 - U, H - K);

X := X + Points[K].X * B;
Y := Y + Points[K].Y * B;
end;

if (I = 0) then
Pos := [Round(X), Round(Y)];

LineTo(Pos, [Round(X), Round(Y)], Color);
Pos := [Round(X), Round(Y)];
end;
end;

procedure TMufasaBitmap.Squircle(const X1, Y1, X2, Y2: UInt32; const Color: TColor; const Size: UInt32 = 20; const Sharpness: UInt32 = 30);
begin
LineTo(Point(X1, Y1 + Size), Point(X1, Y2 - Size), Color);
Curve([Point(X1, Y2 - Size), Point(X1, Y2), Point(X1 + Size, Y2)], Color, Sharpness);

LineTo(Point(X1 + Size, Y2), Point(X2 - Size, Y2), Color);
Curve([Point(X2 - Size, Y2), Point(X2, Y2), Point(X2, Y2 - Size)], Color, Sharpness);

LineTo(Point(X2, Y2 - Size), Point(X2, Y1 + Size), Color);
Curve([Point(X2, Y1 + Size), Point(X2, Y1), Point(X2 - Size, Y1)], Color, Sharpness);

LineTo(Point(X2 - Size, Y1), Point(X1 + Size, Y1), Color);
Curve([Point(X1 + Size, Y1), Point(X1, Y1), Point(X1, Y1 + Size)], Color, Sharpness);
end;

procedure TMufasaBitmap.Squircle(const Box: TBox; const Color: TColor; const Size: UInt32 = 20; const Sharpness: UInt32 = 30); overload;
begin
Squircle(Box.X1, Box.Y1, Box.X2, Box.Y2, Color, Size, Sharpness);
end;

procedure TMufasaBitmap.Squircle(const P1, P2: TPoint; const Color: TColor; const Size: UInt32 = 20; const Sharpness: UInt32 = 30); overload;
begin
Squircle(P1.X, P1.Y, P2.X, P2.Y, Color, Size, Sharpness);
end;

rj
06-03-2013, 12:18 PM
Oo this looks nice, looks like a lot of math haha