/// <summary> /// Draws a line between two points using Bresenham's algorithm with a supplied color and thickness /// </summary> /// <param name="image"></param> /// <param name="x1"></param> /// <param name="y1"></param> /// <param name="x2"></param> /// <param name="y2"></param> /// <param name="color"></param> /// <param name="thickness"></param> public static void SetImageBytes(Span <Rgba32> span, int x1, int y1, int x2, int y2, Rgba32 color, int thickness, int width, int height) { if (thickness == 1) { XiaolinWuAlgoriithm.DrawLine(span, x1, y1, x2, y2, color, width, height); } else { IList <Point> normalPoints = GetLineStartingPoints(x1, y1, x2, y2, thickness); for (var i = 0; i < normalPoints.Count(); i++) { var point = normalPoints[i]; float targetX = x2 + (point.X - x1); float targetY = y2 + (point.Y - y1); //if these are the outer lines, use Xiaolin to anti-aliase them if (i == 0 || i == normalPoints.Count() - 1) { //XiaolinWuAlgoriithm.DrawLine(span, point.X, point.Y, targetX, targetY, color, width, height); } BresenhamLineAlgorithm.DrawLine(span, point.X, point.Y, targetX, targetY, color, width, height); } } }
/// <summary> /// Obtains the points of the normals of a line, up to a certain distance from the original line (and starting on x1,y1) /// </summary> /// <param name="x1"></param> /// <param name="y1"></param> /// <param name="x2"></param> /// <param name="y2"></param> /// <param name="thickness"></param> /// <returns></returns> public static IList <Point> GetLineStartingPoints(int x1, int y1, int x2, int y2, int thickness) { Vector2 unitVector = Vector2.Normalize(new Vector2(x2 - x1, y2 - y1)); Vector2 normalVectorRight = new Vector2(-unitVector.Y, unitVector.X); Vector2 normalVectorLeft = new Vector2(unitVector.Y, -unitVector.X); float normalSize = (thickness - 1) / 2f; var normalPoints = new List <Point>(); //Obtain the points of the normals Point startingPoint = BresenhamLineAlgorithm.GetNonDiagonalPointsOfLine(x1, y1, normalVectorRight, normalSize).Last(); normalPoints.Add(startingPoint); foreach (Point point in BresenhamLineAlgorithm.GetNonDiagonalPointsOfLine(startingPoint.X, startingPoint.Y, normalVectorLeft, thickness - 1)) { normalPoints.Add(point); } return(normalPoints); }