Draw 2D Traingle Pixel by Pixel

Started by
7 comments, last by Sharlin 18 years, 3 months ago
Hi, does anyone know how to draw a simple equalateral triangle pixel by pixel? The reason I need to draw it pixel by pixel is that each pixel could have a different colour or effect applied to it. I can draw a circle (or donut) easily cause there's a simple formula, but the formula to draw a triangle evades me. What I currently do, is create a CRgn and creates a polygon region using 3 points. With this I test whether a point is inside the region and to draw it if it is. Unfortunately, this means I cannot interpolate the edges with the background because I don't know where the edges are. Please help. :) Thanks.
Advertisement
When I made something a few years ago that drew in the insides of a triangle it stored "scanlines" as it was drawing the line and then used those scanlines to perform the drawing. This is commonly used for polygon filling, you can search with google.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
You have to scan all three edges into a list of horizontal strips. There's a good tutorial here.
here is what I pulled up with "polygon scanline" in google:

http://www.xs4all.nl/~kholwerd/algdoc/filling/filling.html
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
Quote:Original post by Chrominium
Hi, does anyone know how to draw a simple equalateral triangle pixel by pixel? The reason I need to draw it pixel by pixel is that each pixel could have a different colour or effect applied to it.


Hrm. How are you going to specify the colour/effect for a given pixel, if you don't already know the set of pixels you're going to draw (in which case you wouldn't be asking)?

It's usual to assign effects to the corners or vertices of a triangle, and interpolate them internally - and 3D APIs normally do all of that sort of thing automatically. See what happens in OpenGL for example when you assign a different colour at each vertex of a triangle. You might also want to google things like "Gouraud shading" (there are others but they don't come to mind :( ).
Thanks for the help! I didn't expect such a quick reply. :) I didn't know what to search for so I'm glad I know now. I need some kind of 2D graphics technique article or something.

Quote:Original post by Zahlman
Hrm. How are you going to specify the colour/effect for a given pixel, if you don't already know the set of pixels you're going to draw (in which case you wouldn't be asking)?


The only thing I know are the 3 points in screen coordinates, and the colour for the various parts of the triangle. Working out what colour each pixel should be is easy, but I found it difficult to find out if that pixel should be drawn (indes or outside the triangle).

It's for a application and part of the UI, so I'm trying to avoid 3D APIs such as OpenGL or DirectX.

What kind of application is it? Couldn't you just draw the thing ahead of time and display a picture resource?
Let v1, v2 and v3 be 3 vectors.
Every pixel on the triangel is given with
v=a.v1+b.v2+c.v3 where a,b and c are posetiv reals and a+b+c=1
So, you'll need to write a software Gouraud triangle rasterizer. This is how 3D games were done before accelerated hardware. It isn't too hard, fortunately, and the Net is full of good resources for it. Check out, for instance, the legendary (for Finnish demo coders, anyway :p) 3DIca 3D programming tutorial (the link takes you directly to the relevant portion of the document).

This topic is closed to new replies.

Advertisement