![](/uploads/1/2/5/5/125513405/920135076.jpg)
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.
Sep 17, 2012 Filling the lower-triangular portion of a matrix with random numbers is not just an example that demonstrates this technique, it is also the first step in the simulation of variates from a Wishart distribution. You can use the r and c matrices to define the indices for. Given b = 12, c = 15 and A = 60° in triangle ABC, use the Law of Cosines to solve for a. Fill in the blank(s) to complete each step. If applicable, be sure to enter all decimal numbers with a zero in the ones place.
**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----//
![How to fill a triangle with circles in python How to fill a triangle with circles in python](https://bzdww.com/cms_static/v2-461b1adc554cecf60b4b2f7221562ab2_b.jpg)
dx = dy;
dy = myTemp;
//--- swap stepX, stepY----//
myTemp = stepX;
stepX = stepY;
stepY = myTemp;
![How to fill triangle in chemistry How to fill triangle in chemistry](/uploads/1/2/5/5/125513405/117512649.jpg)
}
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;
}
Another fractal that exhibits the property of self-similarity is theSierpinski triangle. An example is shown in Figure 3. TheSierpinski triangle illustrates a three-way recursive algorithm. Theprocedure for drawing a Sierpinski triangle by hand is simple. Startwith a single large triangle. Divide this large triangle into four newtriangles by connecting the midpoint of each side. Ignoring the middletriangle that you just created, apply the same procedure to each of thethree corner triangles. Each time you create a new set of triangles, yourecursively apply this procedure to the three smaller corner triangles.You can continue to apply this procedure indefinitely if you have asharp enough pencil. Before you continue reading, you may want to trydrawing the Sierpinski triangle yourself, using the method described.
Since we can continue to apply the algorithm indefinitely, what is thebase case? We will see that the base case is set arbitrarily as thenumber of times we want to divide the triangle into pieces. Sometimes wecall this number the “degree” of the fractal. Each time we make arecursive call, we subtract 1 from the degree until we reach 0. When wereach a degree of 0, we stop making recursive calls. The code thatgenerated the Sierpinski Triangle in Figure 3 is shown inActiveCode 1.
The program in ActiveCode 1 follows the ideas outlined above. Thefirst thing
sierpinski
does is draw the outer triangle. Next, thereare three recursive calls, one for each of the new corner triangles weget when we connect the midpoints. Once again we make use of thestandard turtle module that comes with Python. You can learn all thedetails of the methods available in the turtle module by usinghelp('turtle')
from the Python prompt.Look at the code and think about the order in which the triangles willbe drawn. While the exact order of the corners depends upon how theinitial set is specified, let’s assume that the corners are orderedlower left, top, lower right. Because of the way the
sierpinski
function calls itself, sierpinski
works its way to the smallestallowed triangle in the lower-left corner, and then begins to fill outthe rest of the triangles working back. Then it fills in the trianglesin the top corner by working toward the smallest, topmost triangle.Finally, it fills in the lower-right corner, working its way toward thesmallest triangle in the lower right.Sometimes it is helpful to think of a recursive algorithm in terms of adiagram of function calls. Figure 4 shows that the recursivecalls are always made going to the left. The active functions areoutlined in black, and the inactive function calls are in gray. Thefarther you go toward the bottom of Figure 4, the smaller thetriangles. The function finishes drawing one level at a time; once it isfinished with the bottom left it moves to the bottom middle, and so on.
The
sierpinski
function relies heavily on the getMid
function.getMid
takes as arguments two endpoints and returns the pointhalfway between them. In addition, ActiveCode 1 has a function thatdraws a filled triangle using the begin_fill
and end_fill
turtlemethods.![](/uploads/1/2/5/5/125513405/920135076.jpg)