Skip to content

Commit

Permalink
DrawRaster: Added RasterRect and RasterGradient
Browse files Browse the repository at this point in the history
  • Loading branch information
BasmanovDaniil committed Jan 7, 2021
1 parent 991348d commit e119921
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions Runtime/DrawRaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace ProceduralToolkit
/// </summary>
public static partial class Draw
{
#region RasterLine

/// <summary>
/// Draws a line and calls <paramref name="draw"/> on every pixel
/// </summary>
Expand Down Expand Up @@ -56,6 +58,10 @@ public static void RasterLine(int x0, int y0, int x1, int y1, Action<int, int> d
}
}

#endregion RasterLine

#region RasterAALine

/// <summary>
/// Draws an anti-aliased line and calls <paramref name="draw"/> on every pixel
/// </summary>
Expand Down Expand Up @@ -117,6 +123,10 @@ public static void RasterAALine(int x0, int y0, int x1, int y1, Action<int, int,
}
}

#endregion RasterAALine

#region RasterCircle

/// <summary>
/// Draws a circle and calls <paramref name="draw"/> on every pixel
/// </summary>
Expand Down Expand Up @@ -171,6 +181,10 @@ public static void RasterCircle(int x0, int y0, int radius, Action<int, int> dra
}
}

#endregion RasterCircle

#region RasterFilledCircle

/// <summary>
/// Draws a filled circle and calls <paramref name="draw"/> on every pixel
/// </summary>
Expand Down Expand Up @@ -242,5 +256,78 @@ private static void DrawHorizontalLine(int fromX, int toX, int y, Action<int, in
draw(x, y);
}
}

#endregion RasterFilledCircle

#region RasterRect

/// <summary>
/// Draws a filled rectangle and calls <paramref name="draw"/> on every pixel
/// </summary>
public static void RasterRect(RectInt rect, Action<int, int> draw)
{
RasterRect(rect.x, rect.y, rect.width, rect.height, draw);
}

/// <summary>
/// Draws a filled rectangle and calls <paramref name="draw"/> on every pixel
/// </summary>
public static void RasterRect(int x0, int y0, int width, int height, Action<int, int> draw)
{
for (int y = y0; y < y0 + height; y++)
{
for (int x = x0; x < x0 + width; x++)
{
draw(x, y);
}
}
}

#endregion RasterRect

#region RasterGradient

/// <summary>
/// Draws a gradient rectangle and calls <paramref name="draw"/> on every pixel
/// </summary>
private static void RasterGradient(RectInt rect, Directions direction, Action<int, int, float> draw)
{
RasterGradient(rect.x, rect.y, rect.width, rect.height, direction, draw);
}

/// <summary>
/// Draws a gradient rectangle and calls <paramref name="draw"/> on every pixel
/// </summary>
private static void RasterGradient(int x0, int y0, int width, int height, Directions direction, Action<int, int, float> draw)
{
Func<int, int, float> getGradient;
switch (direction)
{
case Directions.Left:
getGradient = (x, y) => 1 - (float) (x - x0)/(float) width;
break;
case Directions.Right:
getGradient = (x, y) => (float) (x - x0)/(float) width;
break;
case Directions.Down:
getGradient = (x, y) => 1 - (float) (y - y0)/(float) height;
break;
case Directions.Up:
getGradient = (x, y) => (float) (y - y0)/(float) height;
break;
default:
throw new ArgumentException("Not supported direction: " + direction, nameof(direction));
}

for (int y = y0; y < y0 + height; y++)
{
for (int x = x0; x < x0 + width; x++)
{
draw(x, y, getGradient(x, y));
}
}
}

#endregion RasterGradient
}
}

0 comments on commit e119921

Please sign in to comment.