DrawLines with round corner

Jul 18, 2011 at 1:14 PM

Hi.

I write big line use few small lines

I write line from point A to B

then from B to C but when second line have angle a have white space between two line.

like this

http://dl.dropbox.com/u/9306268/Lines.png

And how can I draw line with thickness more then 1 px.

And How can I draw line with smooth(line must be without corners).

 

Thanks.

 

Aug 8, 2011 at 5:07 PM

You could do something like this, but it could be slow. I'm sure there are better ways.

var bmp = new WriteableBitmap(500, 500, 96, 96, PixelFormats.Bgra32, null);
bmp.FillPolygon(WriteableBitmapExtensions.GetThickLinePolyPoints(20, 50, 50, 100, 100, 4), Colors.Green);

public static int[] GetThickLinePolyPoints(int radius, int x1, int y1, int x2, int y2, int endRoundness = 1)
{
	var polypoints = new List<int>();

	int dX = x2 - x1;
	int dY = y2 - y1;
	double dL = Math.Sqrt(dX * dX + dY * dY);
	double ratio = radius / dL;
	int eX = -(int)(ratio * dY);
	int eY = (int)(ratio * dX);

	endRoundness = Math.Max(endRoundness, 1);

	double endRoundnessAngle = Math.PI / endRoundness;

	for (int i = 0; i <= endRoundness; i++)
	{
		double theta = -endRoundnessAngle * i;
		var arc = RotateArc(new Vector(-eX, -eY), theta);
		polypoints.Add((int)arc.X+x1);
		polypoints.Add((int)arc.Y+y1);
	}

	for (int i = 0; i <= endRoundness; i++)
	{
		double theta = -endRoundnessAngle * i;
		var arc = RotateArc(new Vector(eX, eY), theta);
		polypoints.Add((int)arc.X + x2);
		polypoints.Add((int)arc.Y + y2);
	}

	polypoints.Add(polypoints[0]);
	polypoints.Add(polypoints[1]);

	return polypoints.ToArray();
}

public static Vector RotateArc(Vector arc, double angle)
{
	Vector rotated = new Vector
	{
		X = arc.X * Math.Cos(angle) - arc.Y * Math.Sin(angle),
		Y = arc.X * Math.Sin(angle) + arc.Y * Math.Cos(angle)
	};
	return rotated;
}