473,487 Members | 2,483 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Creating Triangles

I have a problem creating triangles with this program it creates
rectangles and squares but not triangles. For example I would like to
create a triangle with the vertices (1,1), (31,1), (31,31) and have it
be the color red. Here is the code your insights will be very helpful
thanks a lot.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct Pixel {
unsigned char R, G, B; // Red, Green, Blue
};

class ColorImage {
Pixel *pPixel;
int xRes, yRes;
public:
ColorImage();
~ColorImage();
void init(int xSize, int ySize);
void clear(Pixel background);
Pixel readPixel(int x, int y);
void writePixel(int x, int y, Pixel p);
void outputPPM(char *filename);
};

ColorImage::ColorImage()
{
pPixel = 0;
}

ColorImage::~ColorImage()
{
if (pPixel) delete[] pPixel;
pPixel = 0;
}

void ColorImage::init(int xSize, int ySize)
{
Pixel p = {0,0,0};
xRes = xSize;
yRes = ySize;
pPixel = new Pixel[xSize*ySize];
clear(p);
}

void ColorImage::clear(Pixel background)
{
int i;

if (! pPixel) return;
for (i=0; i<xRes*yRes; i++) pPixel[i] = background;
}

Pixel ColorImage::readPixel(int x, int y)
{
assert(pPixel); // die if image not initialized
return pPixel[x + y*yRes];
}

void ColorImage::writePixel(int x, int y, Pixel p)
{
assert(pPixel); // die if image not initialized
pPixel[x + y*yRes] = p;
}

void ColorImage::outputPPM(char *filename)
{
FILE *outFile = fopen(filename, "wb");

assert(outFile); // die if file can't be opened

fprintf(outFile, "P6 %d %d 255\n", xRes, yRes);
fwrite(pPixel, 1, 3*xRes*yRes, outFile );

fclose(outFile);
}

// A test program that generates varying shades of reds.
int main(int argc, char* argv[])
{
ColorImage image;
int x, y;
Pixel p={0,0,0};

image.init(512, 512);
for (y=0; y<300; y++) {
for (x=0; x<30; x++) {
p.R = y;
image.writePixel(x, y, p);
}
}

image.outputPPM("reds.ppm");
return 0;
}

Sep 9 '05 #1
5 4792
George wrote:
I have a problem creating triangles with this program it creates
rectangles and squares but not triangles. For example I would like to
create a triangle with the vertices (1,1), (31,1), (31,31) and have it
be the color red. Here is the code your insights will be very helpful
thanks a lot.


Here is some insight: start by trying to create a triangle throught
(1,1) - (3,3) - (1,3). What does it mean? Draw it on a piece of
graph paper. Which pixels need to be colored and why? Then change
it to (1,1) - (3,4) - (1,4). What is different? If that is too
difficult, start with (1,1) - (2,1) - (1,2). Then move the two
last vertices farther out. Then try to come up with a general
approach.

BTW, this is not a C++ langauge question, it's a matter of coming
up with an algorithm. Once you have the algorithm, come back, and
we'll help you translate it into C++.

V
Sep 10 '05 #2
I have this algorithm and have tried to work it into my code but am
unable to grasp exactly what I should do. I get lost and delete
everything and try again and again. Could someone help me incorporate
this algorithm into my code thanks a lot.

alg:

void ScanTriangle(Traingle T, Pixel p)
{
for each pair
{
initialize xl, xr;
compute dxl/dyl and dxr/dyr;
for each scanline at y
for(int x=xl;x<=xr;x++)
SetPixel(x,y,p);
xl+=dxl/dyl;
xr+=dxr/dyr;
}
}

ps this is using the code in the beginning of this post thanks a lot.

Sep 10 '05 #3
George wrote:
I have this algorithm and have tried to work it into my code but am
unable to grasp exactly what I should do. I get lost and delete
everything and try again and again. Could someone help me incorporate
this algorithm into my code thanks a lot.

alg:

void ScanTriangle(Traingle T, Pixel p)
{
for each pair
For each pair of what?
{
initialize xl, xr;
xl and xr are what? Left and right?
compute dxl/dyl and dxr/dyr;
Where did dxl, dxr, dyl and dyr come from?
for each scanline at y
for(int x=xl;x<=xr;x++)
SetPixel(x,y,p);
The above three lines look like a rectangle to me
xl+=dxl/dyl;
xr+=dxr/dyr;
I give up.
}
}

ps this is using the code in the beginning of this post thanks a lot.


The above is not an algorithm it is a meaningless piece of pseudo-code
that (probably) would draw a rectangle anyway.

The first thing you need for an algorithm is some inputs, in your case
the imputs would tell you where the triangle is going to be drawn (and
maybe some other stuff like what colour it would be). So think first
about the inputs to the algorithm.

Now I'm going to take a guess and assume that you specify the triangle
as four numbers, xleft, xright, ytop, and ybottom. The first two are
x-coordinates and the last two are y-coordinates. If you drew every
pixel between those coordinates you would get a rectangle.

Here's the start of an algorithm.

void DrawTriangle(int xleft, int xright, int ytop, int ybottom)
{
for (int x = xleft; x <= xright; ++x)
{
for (int y = ytop; y <= ybottom; ++y)
{
if (x, y) is inside the triangle
draw pixel
}
}
}

Now the only bit you have to solve is how to tell if the (x, y) point is
insode the triangle or not. That is the whole point of the exercise you
have been given, the rest is just distraction. Try drawing xleft,
xright, ytop, ybottom on a piece of paper, then draw where you want the
traingle to be, and then see if you can work out what the formula is for
telling whether a point (x, y) is inside the triangle or not.

john
Sep 10 '05 #4
"George" <bu*******@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I have a problem creating triangles with this program it creates
rectangles and squares but not triangles. For example I would like to
create a triangle with the vertices (1,1), (31,1), (31,31) and have it
be the color red. Here is the code your insights will be very helpful
thanks a lot.


[code snipped]

(1,1), (31,1), (31,1). something like this:

xxxxxxxxxxx
xxxxxxxxx
xxxxxxx
xxxx
x

?

First think about a for loop to draw the above.
Notice each row doesnt' start at 1, but one more over.
Easy to do when you can hard code the coordinates and figure it out before
hand.

Not so easy to do when the values are arbitrary (user input).

Sep 11 '05 #5

"John Harrison" <jo*************@hotmail.com> wrote in message
news:vp******************@newsfe4-win.ntli.net...
George wrote:
I have this algorithm and have tried to work it into my code but am
unable to grasp exactly what I should do. I get lost and delete
everything and try again and again. Could someone help me incorporate
this algorithm into my code thanks a lot.

alg:

void ScanTriangle(Traingle T, Pixel p)
{
for each pair
For each pair of what?
{
initialize xl, xr;


xl and xr are what? Left and right?


Left and right endpoints of a given "scan line", which is one horizontal
line in the triangle.
compute dxl/dyl and dxr/dyr;
Where did dxl, dxr, dyl and dyr come from?


He just said... compute them! :-) Those describe the slopes of the left and
right lines which border the triangle. If you need to know, they're the
difference in x and y between the top and bottom of each of the left and
right lines that define the triangle. Not so hard to figure out, (if you
recognize the algorithm being described).
for each scanline at y
for(int x=xl;x<=xr;x++)
SetPixel(x,y,p);
The above three lines look like a rectangle to me
xl+=dxl/dyl;
xr+=dxr/dyr;


I give up.


The above two lines of code are intended to be within the "for x" loop
above. They modify the endpoints of the horizontal line to be drawn at each
y position, using the slope formula for the left and right edges of the
triangle. That gives you the new enpoints of the line at the following y
position. (But care needs to be taken here, because divisions like this
imply floating-point operations, while the resulting left and right x
potisions must be integers. Some rounding scheme is appropriate, which is
dependant on how you decide to relate your pixel display to the real world.)
}
}

ps this is using the code in the beginning of this post thanks a lot.

The above is not an algorithm it is a meaningless piece of pseudo-code
that (probably) would draw a rectangle anyway.


Not if the loop contains those two lines. (And what's so wrong with using
pseudo-code to describe an algorithm?)

The first thing you need for an algorithm is some inputs, in your case the
imputs would tell you where the triangle is going to be drawn (and maybe
some other stuff like what colour it would be). So think first about the
inputs to the algorithm.

Now I'm going to take a guess and assume that you specify the triangle as
four numbers, xleft, xright, ytop, and ybottom. The first two are
x-coordinates and the last two are y-coordinates. If you drew every pixel
between those coordinates you would get a rectangle.

Here's the start of an algorithm.

void DrawTriangle(int xleft, int xright, int ytop, int ybottom)
{
for (int x = xleft; x <= xright; ++x)
{
for (int y = ytop; y <= ybottom; ++y)
{
if (x, y) is inside the triangle
draw pixel
}
}
}

Now the only bit you have to solve is how to tell if the (x, y) point is
insode the triangle or not. That is the whole point of the exercise you
have been given, the rest is just distraction. Try drawing xleft, xright,
ytop, ybottom on a piece of paper, then draw where you want the traingle
to be, and then see if you can work out what the formula is for telling
whether a point (x, y) is inside the triangle or not.


That's a very inefficient way to draw a triangle.

The algorithm given wasn't bad, but needs more specifics as to how the xl
and xr values are initialized, and how/when they change. In similar code
I've written, a triangle _always_ has either the top as a point and the
bottom as a line, or the top as a line and the bottom as a point. To draw
triangles that don't fit that mold, you split it into a top and bottom
triangle that _do_ fit that mold.

So the algorithm draws all pixels between a line of some non-zero slope on
the left, and another on the right, moving down (or up) one "scan line" at a
time. Fast, and easy.

(But as Victor said, not really a C++ question.)

-Howard
Sep 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
2924
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
15
6715
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
1
1355
by: R | last post by:
Hey guys. I have a conundrum. I am trying to create a square, consisting of 2 right triangles (credit goes to: http://www.infimum.dk/HTML/slantinfo.html for the idea). Across the middle of the 2...
4
2762
by: asif929 | last post by:
I have another program to write, i will appreciate if somebody can help......prompts the user to enter positive integer, and then prints out four triangles For Example: If we enter 4 it should...
2
2127
by: js06am | last post by:
I can't for the life of me work out just how to draw a triangle in java. Using the g.fillRect etc has been easy for rectangles but I have absolutely no idea where to start with triangles. Any...
0
1335
by: Grant Edwards | last post by:
I posted this question to the vtk mailing list last week: I've been Googling and wandering through the class references most of the afternoon, but I can't figure out how to get the triangles out...
1
1393
by: MHJisHere | last post by:
Hi I'm trying to create a Shadow Volume of a 3ds model using C++ and CG and have ended up hitting a wall. I can create volumes of basic shapes such as triangles and squares but the closest i have...
5
3352
by: luxor1275bc | last post by:
I am trying to google for how to create click-down triangles with either CSS or some other method but not having much luck. It's probably because my choice of words to describe this is incorrect....
1
3568
Markus
by: Markus | last post by:
Recently got PS CS4 (looking good). However, when I create a new image, be it white BG, transparent, etc. I'm greeted with a few triangles of what appears to be transparency. I'm unable to draw into...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.