Im trying to do a scan line fill on a triangle and I know Im close but still so very far away. Can I get some help please..........
void Triangle::FillTriangle(DrawingManager* draw){ Vector3f v[3]; Vector3f temp; Vector3f lineLeftPos; Vector3f lineRightPos; v[0] = changedVertices[0]; v[1] = changedVertices[1]; v[2] = changedVertices[2]; //Sort vertices by ascending y value if (v[0].y > v[1].y) { temp = v[0]; v[0] = v[1]; v[1] = temp; } if (v[1].y > v[2].y) { temp = v[1]; v[1] = v[2]; v[2] = temp; if (v[0].y > v[1].y) { temp = v[0]; v[0] = v[1]; v[1] = temp; } } if (v[0].y > v[2].y) { temp = v[0]; v[0] = v[2]; v[2] = temp; } for(int i = v[0].y; i < v[1].y; i++) { if (v[1].x < v[0].x) { lineLeftPos = GetScanLine(&v[0], &v[2], i); lineRightPos = GetScanLine(&v[0], &v[1], i); } else { lineLeftPos = GetScanLine(&v[0], &v[1], i); lineRightPos = GetScanLine(&v[0], &v[2], i); } //draw->DrawBresenhamLine(&v[0], &v[1]); draw->DrawBresenhamLine(&lineLeftPos, &lineRightPos); } if (v[2].y-v[1].y!=0) //Make sure we only continue drawing the rest of the triangle if the remaining area actually exists { for(int i = v[1].y; i < v[2].y; i++) { if (v[1].x < v[2].x) { lineLeftPos = GetScanLine(&v[0], &v[2], i); lineRightPos = GetScanLine(&v[1], &v[2], i); } else { lineLeftPos = GetScanLine(&v[1], &v[2], i); lineRightPos = GetScanLine(&v[0], &v[2], i); } //draw->DrawBresenhamLine(&v[1], &v[2]); draw->DrawBresenhamLine(&lineLeftPos, &lineRightPos); } }}Vector3f Triangle::GetScanLine(Vector3f* v1, Vector3f* v2, int yValue){ Vector3f returnVec = *v1; float dx = abs(v2->x - v1->x); float dy = abs(v2->y - v1->y); float slope = 0; if(dy != 0 && dx != 0) { slope = dy / dx; } returnVec.x = v1->x - (slope * yValue); returnVec.y = yValue; return returnVec;}
Many Thanks