Sign in to follow this  
Monkan

Scan Line Fill

Recommended Posts

Hi,

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

Share this post


Link to post
Share on other sites
No Problem, figured it out:


Vector3f Triangle::GetScanLine(Vector3f* v1, Vector3f* v2, int yValue)
{
Vector3f returnVec;
float dx = (v2->x - v1->x);
float dy = (v2->y - v1->y);
float slope = 0;
if(dy != 0 && dx != 0)
{
slope = dx / dy;
}

returnVec.x = v1->x + slope * (yValue - v1->y);
returnVec.y = yValue;

return returnVec;
}



Cheers Anyway

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this