Take our survey and automatically be enter to win anyone of the following:

Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Become a Premium Member and unlock a new, free course in leading technologies each month.

Solved

Posted on 2004-09-14

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???

please give me some suggestions.

Thanks For All the Replys

***********************************/

}

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;

}

**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???

please give me some suggestions.

Thanks For All the Replys

**************************

}

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;

}

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

- Help others & share knowledge
- Earn cash & points
- Learn & ask questions

4 Comments

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.

/\

@ @

/ \

/ \

----------------

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

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.

http://cyl.cs.cf.ac.uk/teaching/graphics/CG-12.pdf

http://www.cs.virginia.edu/~gfx/Courses/2003/Intro.spring.03/lecture15.ppt

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Course of the Month8 days, 5 hours left to enroll

Join the community of 500,000 technology professionals and ask your questions.