Solved

# Fill Triangle with or multi colors

Posted on 2004-09-14
2,901 Views
Hi, I am writing a code that draws some triangles that outputs into a .ppm file, and I want to write a fill function that will fill the triangle with one color or multi-color depending on the vertices.  My program can now draw the triangles but I can't figure out how to fill them with color.  One thing I'm thinking is choose 2 pixels on the side of the triangle and then draw a colored line in between those 2 pixels, and I'll do that for all the pixels along the edge of the triangle.

**Here's My Code**

#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

const int width = 512;
const int height = 512;
const int maxColor = 255;

int red[width][height];
int green[width][height];
int blue[width][height];

ofstream myFile("Triangle_C++.ppm");

void drawBLine(int x, int y, int x2, int y2, int r, int g, int b);
void inArray();
void fill(x1, y1, x2, y2, x3, y3,  redColor, greenColor, blueColor);

int main()
{
// Initialize arrays: red,  green,  blue:
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{      red[i][j] = 0;
green[i][j] = 0;
blue[i][j] = 0;
}
}

// Initialize output file:
if(!myFile)
{
cout << "Error opening output file" << endl;
return -1;
}

/*************************************************************************************************************************
A white triangle with vertices (5, 5), (35, 5), and (35, 35)
A red triangle with vertices (5, 500), (30, 470), and (55, 500)

A triangle with a red vertex at (430, 500), a green vertex at (500, 430), and a blue vertex at (510, 510)

white lines (use code from Homework 2) from (300, 399) to (400, 399) and from (299, 300) to (299, 400)
***************************************************************************************************************************/
// White Triangle without fill

drawBLine(5,5, 35, 5, 255, 255, 255);// white
drawBLine(35,5, 35, 35, 255, 255, 255);// white
drawBLine(35,35, 5, 5, 255, 255, 255);// white

//      fill(5, 5, 35, 5, 35, 35, 255, 255, 255);            // fills the triangle to white

drawBLine(5,500, 30, 470, 255, 0, 0);// red
drawBLine(30,470, 55, 500, 255, 0, 0);// red
drawBLine(55,500, 5, 500, 255, 0, 0);// red

//      fill(5, 500, 30, 470, 55, 500, 255, 0, 0);           // fills the triangle red

drawBLine(300,399, 400, 399, 255, 255, 255);// white line
drawBLine(299,300, 299, 400, 255, 255, 255);// white line

// Write PPM file:
myFile << "P3" << endl;
myFile << width << " " << height << endl;
myFile << maxColor << endl;

int count_line = 0;

/* normal view: */
inArray();

myFile.close();

cout << "File Ended" << endl;

return 0;
}

void fill(x1, y1, x2, y2, x3, y3,  redColor, greenColor, blueColor)
{
/***********************************
I don't know how to write this function
distance formula???
midpoint formula???
***********************************/
}

void inArray()
{
for(int j=height-1; j>=0; j--)
{      for(int i=0; i<width; i++)

myFile << red[i][j] << " " << green[i][j] << " " << blue[i][j] << "  " ;
}

}

// Bresenham's Line-Drawing Algorithm

void drawBLine(int x, int y, int x2, int y2, int r, int g, int b)
{
int d;
int dx, dy;
int x1, y1;
int inc_NE, inc_E;
int stepX, stepY;
int steep;

steep = 0;

dx = abs(x2-x);
dy = abs(y2-y);

if((x2 - x) > 0)
{      stepX = 1;
}
else
{      stepX = -1;
}

if((y2 - y) > 0)
{      stepY = 1;
}
else
{      stepY = -1;
}

if(dy > dx)
{
steep = 1;
int myTemp;

//--- swap x1, y1-----//
myTemp = x;
x = y;
y = myTemp;

//--- swap dx, dy----//
myTemp = dx;
dx = dy;
dy = myTemp;

//--- swap stepX, stepY----//
myTemp = stepX;
stepX = stepY;
stepY = myTemp;
}

d = 2*dy - dx;

for(int coord = 0; coord < dx; coord++)
{
if(steep)
{
red[y][x] = r;
green[y][x] = g;
blue[y][x] = b;
}
else
{
red[x][y] = r;
green[x][y] = g;
blue[x][y] = b;
}

while(d>=0)
{      y = y + stepY;
d = d - (dx * 2);
}

x = x + stepX;
d = d + (dy * 2);
}
red[x][y] = r;
green[x][y] = g;
blue[x][y] = b;
}
0
Question by:slamjam2000

LVL 14

Assisted Solution

wayside earned 35 total points
ID: 12058708
You've got the right idea, the scanline method works well for triangles.

Another idea is to use a flood fill - pick one pixel inside the triangle and recursively move in all eight directions where the pixel isn't the color you want, set the color, and repeat. Stop if the pixel you move to is already the correct color.

Flood algorithms have problems if you have overlapping shapes of the same color, though.

For more complex shapes this is a much tougher problem, however it has been solved many times. A little research or a look in a basic computer graphics textbook will give you some algorithms you can use.
0

LVL 1

Author Comment

ID: 12059289
do i need to sort the Y-Axis to know what kind of triangles I'm dealing with???  And split the triangles?

/\
@  @
/         \
/              \
----------------

How do I determin the two @ points (changes in left x-axis and right x-axis)???

This is where I'm trying to figure out right now, so I can draw a line through the pixels.

THANKS
0

Accepted Solution

mikeant78 earned 70 total points
ID: 12059556

You will need to first find the topmost point in the triangle (with smallest y value). Then you compare the points before and after it to determine which one goes to the right, and which one to the left. Then you can scan the lines simultaneously down, to determine the left and right coordinates for the scan-line fill. You continue so until you reach the bottom-most triangle point. Note that in a middle you may have to switch from one line segment to another as you ancounter an extra (third) trianagle point.

Although it might be possible to use it, the bresenham line algorithm may not be the best choice because it draws "cosmetic lines" stepping both left and right as it traces out the line (that is a line can have multiple y-coordinate values which you don't need when filling). A better alternative would be to just calculate the delta_x/delta_y value, i.e. the (x2-x1)/(y2-y1) value for each line, and add this value to the lines starting x coordinate each time you go down one unit in y. That way you will get the two x coordinate that you need to do the scan-line fill.
0

LVL 4

Assisted Solution

Fippy_Darkpaw earned 20 total points
ID: 12060038
These 2 links tell you exactly how to do it. The first one has better diagrams. The 2nd one has better explanation of half-space formula and even has example code you can use.

http://cyl.cs.cf.ac.uk/teaching/graphics/CG-12.pdf
http://www.cs.virginia.edu/~gfx/Courses/2003/Intro.spring.03/lecture15.ppt
0

## Featured Post

### Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

#### Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!