Link to home
Start Free TrialLog in
Avatar of slamjam2000
slamjam2000

asked on

Fill Triangle with or multi colors

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;
}
SOLUTION
Avatar of wayside
wayside

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slamjam2000
slamjam2000

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial