The most basic algorithm I use for drawing convex polygons is this:

have two arrays, from 0…479 or whatever your screen resolution is

Each array represents an edge. array 0 represents the left edge; array 1
represents the right edge. (or array one and two, depending how you think
:-))

for each edge (x1,y1)…(x2,y2), (x2,y2)…(x3,y3)… , scan it using a
simple slope algorithm to generate coordinates for that edge. Compare it to
the values in your array, which you have prestuffed with obscene values
(32767 for the left side, -32767 for the right side, for instance)… so, if
you know that point y=10 on slope (y2-y1)/(x2-x1) is greater than -32767,
then that’s your Y coordinate value.

Then blit the left over hlines.

Sample code available upon request.

Nicholas

Nicholas Vining “While you’re out there struggling
vining at pacificcoast.net with your computer, I’m naked,
icq: 20872003 clueless, and feeling good!”
- Ratbert

----- Original Message -----
From: johnm@victoriareal.co.uk (John Marshall)
To: sdl at lokigames.com
Date: Monday, February 07, 2000 5:12 AM
Subject: Re: [SDL] Polygon primitives
On Mon, Feb 07, 2000 at 10:48:37AM +0100, Enzo wrote:

Has anyone already written code for drawing filled polygons?
(3, 4 or, if you have, more points)

At http://reality.sgi.com/atul_asd/code/chapter.html you can find a
program to triangulate polygons. These polygones may have any size and
do not need to be convex, they may even have holes.
Painting the triangles you get from it should not be a problem…

Bye,
MartinOn Mon, 07 Feb 2000 10:48:37 +0100, Enzo wrote:

Has anyone already written code for drawing filled polygons?
(3, 4 or, if you have, more points)

I’m looking for algorythms for doing something like:

draw3poly (int x1, int y1, int x2, int y2, int x3, int y3)
draw4poly (int x1, int y1, int x2, int y2, int x3, int y3, int x4,
int y4)

Thank for any help.

Enzo

I have code for doing this under SDL, based on some really old code I
found in Mike Abrash’s columns in DDJ. Works for all types of polygons,
including polygons with holes and concave polygons. Even has a slightly
faster version for filling explicitly convex (monotone vertical)
polygons. Email me privately if you want a tarball.–

| Rafael R. Sevilla @Rafael_R_Sevilla |
| Instrumentation, Robotics, and Control Laboratory |

College of Engineering, University of the Philippines, Diliman