Jump to content
  • Advertisement
Sign in to follow this  
Monkan

Scan Line Fill

This topic is 2752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!