Scan Line Fill

Started by
2 comments, last by Monkan 13 years, 4 months ago
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

"To know the road ahead, ask those coming back."

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

"To know the road ahead, ask those coming back."

Why do you need Bresenham just for a horizontal line? It could be just a very simple loop.
I know but I had the function already so was quicker for me to make sure I got how it worked 1st.

"To know the road ahead, ask those coming back."

This topic is closed to new replies.

Advertisement