Regarding #2, this approach is called line rasterization. This is what chili covers in his 3D fundamentals tutorials. You arrange the points in ascending order based on their Y coordinate.
Let's name these 3 points as vertex A, B, C.
Let's name the edges that connect the vertices as AB, AC, BC, where the first letter means the edge starts there and ends on the second letter. AB would be the edge connecting A to B, BA would be the same edge, but going from B to A.
Let's also say, A is the top most vertex, B is the second highest and C is the lowest.
Once you have arranged the points, you need to get the slope of AC and multiply it by the height of AB. What this does is finds a point along AC that is at the same height as vertex B. It will break the triangle into two sections, one with a flat bottom and one with a flat top. It creates a new vertex, we'll call it D.
Once you have your flat top and flat bottom triangles, you can calculate the slope of AB ( X / Y instead of Y / X because you will be iterating one row at a time, so you really only need to calculate the X for each row ). Then, calculate the slope ( X / Y ) of AD. This will help calculate the X for each row to end your line.
Now that you have your left and right slope, start from the coordinates of vertex A and iterate down, from Ay to Dy. Use the distance from Ay ( y - Ay ) and multiply that by the slope of each edge to get your X positions for start and finish.
Code: Select all
A = {10, 10};
B = { 0, 20};
C = {15, 30};
// Calculate where the split happens
AB = B-A;
AC = C-A;
mAC = ACx / ACy;
height = By - Ay;
AD = {Ax + ( mAC * height ), By}
// Calculate AB and AD slope
mAB = ABx / ABy;
mAD = ADx / ADy;
// Iterate from Ay to Dy or Ay to By, the height should be the same
for y = Ay to Dy
xstart = (y - Ay) * mAB
xend = (y - Ay) * mAD
gfx.DrawLine( xstart, y, xend, y, color )