Fill Triangle with or multi colors

Posted on 2004-09-14
Medium Priority
Last Modified: 2008-01-09
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:
            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: */

      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;
      {      stepX = -1;
      if((y2 - y) > 0)
      {      stepY = 1;
      {      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++)
                  red[y][x] = r;
                  green[y][x] = g;
                  blue[y][x] = b;
                  red[x][y] = r;
                  green[x][y] = g;
                  blue[x][y] = b;
            {      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;
Question by:slamjam2000
Welcome to Experts Exchange

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
LVL 14

Assisted Solution

wayside earned 105 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.

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.


Accepted Solution

mikeant78 earned 210 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.

Assisted Solution

Fippy_Darkpaw earned 60 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.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question