# Scan Line Fill

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

## 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 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 on other sites
Why do you need Bresenham just for a horizontal line? It could be just a very simple loop.

##### Share on other sites
I know but I had the function already so was quicker for me to make sure I got how it worked 1st.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633729
• Total Posts
3013578
×