public static void FillTriangle(this SpriteBatch batch, Vector2 p1, Vector2 p2,
Vector2 p3, Color color)
{
Vector2 tp;
if (p2.Y > p1.Y) { tp = p1; p1 = p2; p2 = tp; }
if (p3.Y > p1.Y) { tp = p1; p1 = p3; p3 = tp; }
if (p3.Y > p2.Y) { tp = p2; p2 = p3; p3 = tp; }
var steps13 = (int)(p1.Y - p3.Y);
var steps12 = (int)(p1.Y - p2.Y);
var steps23 = (int)(p2.Y - p3.Y);
float sx13 = (p1.X - p3.X) / steps13;
float sx12 = (p1.X - p2.X) / steps12;
float sx23 = (p2.X - p3.X) / steps23;
float x13 = p1.X;
float x12 = p1.X;
float dx = 1;
for (int i = 0; i < steps12; i++)
{
x13 -= sx13;
x12 -= sx12;
dx = x13 - x12;
if (dx > 0)
batch.FillRectangle((int)x12, (int)p1.Y - i, (int)Math.Abs(dx), 1, color);
else
batch.FillRectangle((int)x13, (int)p1.Y - i, (int)Math.Abs(dx), 1, color);
}
float x23 = p2.X;
for (int i = 0; i < steps23; i++)
{
x13 -= sx13;
x23 -= sx23;
dx = x13 - x23;
if (dx > 0)
batch.FillRectangle((int)x23, (int)p2.Y - i, (int)Math.Abs(dx), 1, color);
else
batch.FillRectangle((int)x13, (int)p2.Y - i, (int)Math.Abs(dx), 1, color);
}
}