PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Polyline/Polygon Routines

Does anyone have any routines to draw polylines and polygon's?
For polyline/polygon I simply use a for loop and connect the last point with the first (if the number of points > 2).

A polygon fill is a whole other story. I havn't had the time to write mine yet. =)
Wow, I am in mild shock right now...

I just converted the filled polygon routine I wrote almost a year ago for my own GAPI library to PF. Took all of five minutes! Here it is...

Code:

unsigned int PFE_FPStartX[320];
unsigned int PFE_FPEndX[320];
typedef struct {
int x;
int y;
} PFE_Point;

void PFE_PolyLineCalc(int x1, int y1, int x2, int y2) {
int tmp;
int y;
long x;
long m;
if (y2 != y1) {
if (y2 < y1) {
tmp = y1;
y1 = y2;
y2 = tmp;
tmp = x1;
x1 = x2;
x2 = tmp;
}
x = (long)x1<<8;
m = ((long)(x2 - x1) << 8) / ((long)(y2 - y1));
x += m;
y1++;
for (y = y1; y <= y2; y++) {
if ((y >= 0) & (y < 320)) {
if (PFE_FPStartX[y] == -16000) {
PFE_FPStartX[y] = x >> 8;
} else {
PFE_FPEndX[y] = x >> 8;
}
}
x += m;
}
}
}

void PFE_DrawFilledPolygon(DisplayDevice* display, PFE_Point PFEI_VertexList[], int PFEI_NumVertex, int PFEI_colR, int PFEI_colG, int PFEI_colB) {
int i;
PFE_Point *curpt;
PFE_Point *nextpt;
curpt = PFEI_VertexList;
nextpt = PFEI_VertexList + 1;
for (i = 0; i < 320; i++) {
PFE_FPStartX[i] = -16000;
PFE_FPEndX[i] = -16000;
}
for (i = 1; i < PFEI_NumVertex; i++) {
PFE_PolyLineCalc(curpt->x, curpt->y, nextpt->x, nextpt->y);
curpt += 1;
nextpt += 1;
}
nextpt = PFEI_VertexList;
PFE_PolyLineCalc(curpt->x, curpt->y, nextpt->x, nextpt->y);
for (i = 0; i < 320; i++) {
if (PFE_FPStartX[i] != -16000) {
if (PFE_FPEndX[i] == -16000) {
PFE_FPEndX[i] = PFE_FPStartX[i];
}
display->DrawLine(PFE_FPStartX[i], i, PFE_FPEndX[i], i, Color(PFEI_colR, PFEI_colG, PFEI_colB));
}
}
}

And here's an example GameLoop to use it:

Code:

void MyGame::GameLoop() {
DisplayDevice* display = GetDisplay();
int numPoints = (unsigned short int)(rand() * 10 / (RAND_MAX + 1));
PFE_Point FPPoints[12];
unsigned short int cR;
unsigned short int cG;
unsigned short int cB;
int i;
cR = (unsigned short int)(rand() * 256 / (RAND_MAX + 1));
cG = (unsigned short int)(rand() * 256 / (RAND_MAX + 1));
cB = (unsigned short int)(rand() * 256 / (RAND_MAX + 1));
for (i = 0; i < numPoints; i++) {
FPPoints[i].x = (unsigned short int)(rand() * 240 / (RAND_MAX + 1));
FPPoints[i].y = (unsigned short int)(rand() * 320 / (RAND_MAX + 1));
}
PFE_DrawFilledPolygon(display, FPPoints, numPoints, cR, cG, cB);
display->Update();
}

It actually works! I'm sure it could be optimized considerably, and I'm sure actually integrating it into PF would be better, but feel free to use, modify or whatever you want with it. Just dump the first block of code in a header, include it and you should be good to go.

It's pretty self-explanatory, but in case your like me and hate looking at unexplained code:

You create an array of PFE_Point structures. This array defines all the vertecies (sp?) of your polygon. You then pass the value returned by PF's GetDisplay() method, along with the array, followed by the number of vertecies in the array and the red, green and blue color components to draw the polygon in, and that's it.

If anyone takes and makes this better, please give back to myself and everyone else!
Great work Framzetti. I'll have a look at it this afternoon (or perhapps tomorrow) and C if I can find any obvious faults and/or spice it up a bit.

This will for shure (provided it's fast enough) save me a lot of time. In any case, it's better to start from somewhere than from scratch.
I havn't tried the code yet, but it is really straight forward and easy. It looks like it will work perfectly for polygons with 3 or more points (and that's the definition of a polygon I guess). The soft spot is a single point polygon, where it will produce an index out of bounds error, and any horizontal two point polygon, where it won't draw the line at all.

Another thing is that it only works on a resolution/window size with height <= 320 pixels. Since most PPC:s use the 240x320 this is not a problem. But for me (and perhaps others) who have a maximum resolution of 800x600, it will be =)

The algoritm only manages convex polygons. Any concave polygon will be drawn as convex with it's concavities removed. This is to be considered as a flaw if you have concave polygons, otherwise not.

This perticular algoritm seems to be implemented almost as optimized as possible, using fixed point aritmetic instead of floating point. And it's so simple it's hard to reduce.

I do have some suggestions though. Why not keep track of the maximum and minimum y value. An average polygon (this is taken right out of the air) has six points. This will result in a maximum of 6x2 compares to see if the current yval is below global minimum or above global maximum. Then you won't have to loop through all the yvalues from 0 to 320 (or the given screen/window height). Just loop through the ones we know are occupied with the polygon. The 12 compares to find the ymin and ymax should be less than the amount of compares to see if a given yval has a polygon xval. Not shure if this will have a noticeable impact on performance, but it's an optimization never the less.

And, the DrawHLine might be faster than DrawLine, although I seem to recall (I currently have no access to pocketfrog so I can't double check) that DrawLine will call DrawHLine (or DrawVLine) if the line is horizontal (or vertical).

As some of you must have noticed by now, I'm picky about how the code is written. Although it will produce the same result, I always try to write code acording to a standard (my own =). For instance: finding good descriptive names, always use namespaces, never use global variables (unless it's absolutely necessary), comment a lot, etc, etc. Most of what I personally think is good coding style is also part of the C++ Coding Standard by Todd (or perhapps it's the other way around =) at the following link: http://www.possibility.com/Cpp/CppCodingStandard.html

Finding a good standard (whether it's strictly someone elses or a modified version of your own) and sticking to it will reduce the amount of errors you make, specially during larger projects.

For those who don't give a #¤&%#¤& about this, that's fine too. It's your problem =)

So, naturally, I have a few suggestions about the code in general, how it might be restructured and presented. For instance: it doesn't make any sense to have PFE_FPStartX (and Y) defined as unsigned int and then assign a negative value. And why is the int unsigned in the first place?

But like I said, it's just a matter of taste.

I need to test the code (and my sugestions) before I can say anything more (heh, like this wasn't enough. *cackle*). But basicly, it looks really good! I'll mail you any modifications I make (or perhaps post it at Pocketfrog's Devcorner and add a link to it from this thread)

Oh, one more thing. Sorry I misspelled you name in the previous note fzammetti. I'm a bit "word blind" sometimes.
You know, as I look back on that code, I can't even believe I wrote it because of the things you pointed out with regard to style... I'm usually a real anal retentive when it comes to coding style (like you, I just want each person to stick to a particular style, whatever it is). Looking at that code, it doesn't look anything like what I normally write. My only excuse is that I probably did it late at night with the intention of going back and cleaning it up but never did (I dropped development on my graphics library mid-stream, so this makes some sense, I do tend to do sloppy code and then clean it up later).

Ah well.

Your mods all make sense to me, although I very much doubt I'll do them myself. If you or anyone else wants to though, I'm in complete support of it.
Heh.. Didn't mean to come down on you. The code works great (for all simple polygons) and looks good to. Clear and easy to understand. And it's not much to be done to optimize (not something that will give large increase in performence anyways). Thus, I don't think the code you posted need any changes (especially since all the users of PocketFrog will use it together with the Game class, which draws a 240x320 window)

I will however, use your code in my 3D browser. And so I will rewrite it to fit into my project (making the mods I see fit). I have made a few other extentions to PocketFrog and put them all in a new namespace called MyFrog. If you want, I can email you the file when it's finished. You will get a BlitRotatedStretch() function as well.

I can't wait to finish rewriting the code and test it on my 3D browser. Thanks a bunch fzammetti. You just saved me a lot of valuable time and effort on my project. Respect!

Then, just to set the record straight, I made a few errors in my previous note:

adde wrote:The algoritm only manages convex polygons. Any concave polygon will be drawn as convex with it's concavities removed. This is to be considered as a flaw if you have concave polygons, otherwise not.


This is not entirely true. It does manage concave polygons, if they are verticaly concave (three points on a polygon side, x1-x3, where x2 > x1 and x3). It does NOT manage horizontally convace polygons (three points on a polygon side, y1-y3, where y2 > y1 and y3). The algoritm will produce two polygons in this case. Just a minor detail.

adde wrote:And, the DrawHLine might be faster than DrawLine, although I seem to recall (I currently have no access to pocketfrog so I can't double check) that DrawLine will call DrawHLine (or DrawVLine) if the line is horizontal (or vertical).

This is absolutely true. The first thing that DrawLine does it check whether it's a vertical line, and then if it's a horizontal line. Calling DrawVLine resp. DrawHLine. So, changing the code of DrawFilledPolygon to use DrawHLine will only save an extra function call and a check for vertical line, at the expense of readability.
Might I suggest that we hook up and combine all our PF extensions? I have a number of them myself, although they all are like these in that they aren't directly integrated in PF (I generally just stick them in a header and import it, nice and easy). I have some other functions that would be nice to convert over (horizontal and vertical surface flipping and surface shifts in all directions). If your interested let me know.