Draw 2D Traingle Pixel by Pixel

This topic is 4705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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.

Share on other sites
You have to scan all three edges into a list of horizontal strips. There's a good tutorial here.

Share on other sites
Quote:
 Original post by ChrominiumHi, 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 :( ).

Share on other sites
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 ZahlmanHrm. 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.

Share on other sites
What kind of application is it? Couldn't you just draw the thing ahead of time and display a picture resource?

Share on other sites
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

Share on other sites
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).

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 21
• 10
• 33
• Forum Statistics

• Total Topics
633438
• Total Posts
3011882
• Who's Online (See full list)

There are no registered users currently online

×