//DDA line methods public static void SetDDALine(Texture2D texture, Vector2 startPoint, Vector2 endPoint, Color color) { FloatDeltaValues deltaValues = new FloatDeltaValues(startPoint, endPoint); if (deltaValues.AbsDeltaX > deltaValues.AbsDeltaY) { SetDDALineIncrementingX(texture, startPoint, endPoint, deltaValues, color); } else if (deltaValues.AbsDeltaY > 0) { SetDDALineIncrementingY(texture, startPoint, endPoint, deltaValues, color); } else { SetPixel(texture, (int)startPoint.X, (int)startPoint.Y, color); } }
private static void SetDSCLineIncrementingY(Texture2D texture, Vector2 startPoint, Vector2 endPoint, FloatDeltaValues deltaValues, Color color) { SwapForYDrawingAsNeeded(ref startPoint, ref endPoint, ref deltaValues); float xInterval = deltaValues.DeltaX / deltaValues.DeltaY; for (int yMovement = 0; yMovement <= deltaValues.AbsDeltaY; yMovement++) { int currentX = (int)Math.Round(startPoint.X + xInterval * yMovement); int currentY = (int)Math.Round((startPoint.Y) + yMovement); SetPixel(texture, currentX, currentY, color); } }
private static void SwapForYDrawingAsNeeded(ref Vector2 startPoint, ref Vector2 endPoint, ref FloatDeltaValues deltaValues) { if (startPoint.Y > endPoint.Y) { SwapStartAndEnd(ref startPoint, ref endPoint); deltaValues = new FloatDeltaValues(startPoint, endPoint); } }
private static void SetDDALineIncrementingY(Texture2D texture, Vector2 startPoint, Vector2 endPoint, FloatDeltaValues deltaValues, Color color) { SwapForYDrawingAsNeeded(ref startPoint, ref endPoint, ref deltaValues); Vector2 incrementVector = new Vector2(deltaValues.DeltaX / deltaValues.DeltaY, 1); for (Vector2 currentPos = startPoint; currentPos.Y <= endPoint.Y; currentPos = Vector2.Add(currentPos, incrementVector)) { SetPixel(texture, (int)currentPos.X, (int)currentPos.Y, color); } }