PDA

View Full Version : Triangles!



pkmnfrk
12-20-2008, 09:21 PM
void DrawTriangle(int layer, int x1, int y1, int x2, int y2, int x3, int y3, int color, bool filled, int trans) {
if(!filled) {
Screen->Line(layer, x1, y1, x2, y2, color, 1, 0, 0, 0, trans);
Screen->Line(layer, x2, y2, x3, y3, color, 1, 0, 0, 0, trans);
Screen->Line(layer, x1, y1, x3, y3, color, 1, 0, 0, 0, trans);
} else {
int ax; int ay; int bx; int by; int cx; int cy;

if(y2 < y1) {
if(y3 < y2) {
ax = x3; ay = y3;
bx = x2; by = y2;
cx = x1; cy = y1;
} else {
ax = x2; ay = y2;
bx = x1; by = y1;
cx = x3; cy = y3;
}

} else {
if(y2 < y3) {
bx = x2; by = y2;
cx = x3; cy = y3;
} else {
if(y1 < y3) {
ax = x1; ay = y1;
bx = x3; by = y3;
} else {
ax = x3; ay = y3;
bx = x1; by = y1;
}
cx = x2; cy = y2;
}

}

int dx1;
if(by-ay > 0) dx1 = (bx - ax)/(by-ay); else dx1 = bx - ax;

int dx2;
if(cy-ay > 0) dx2 = (cx - ax)/(cy-ay); else dx2 = 0;

int dx3;
if(cy-by > 0) dx3 = (cx - bx)/(cy-by); else dx3 = 0;

int sx; int sy; int ex; int ey;
sx = ax; sy = ay; ex = ax; ey = ay;
if(dx1 > dx2) {
while(sy <= by) {
horizline(layer, sx, ex, sy, color, trans);
sy++; ey++; sx+=dx2;ex+=dx1;
}
ex = bx;
ey = by+1;
while(sy <= cy) {
horizline(layer, sx, ex, sy, color, trans);
sy++; ey++; sx+=dx2;ex+=dx3;
}
} else {
while(sy <= by) {
horizline(layer, sx, ex, sy, color, trans);
sy++; ey++; sx+=dx1;ex+=dx2;
}
sx = bx;
sy = by;
while(sy <= cy) {
horizline(layer, sx, ex, sy, color, trans);
sy++; ey++; sx+=dx3;ex+=dx2;
}
}
}
}

void horizline(int layer, int x1, int x2, int y, int color, int trans) {
Screen->Line(layer, x1, y, x2, y, color, 1, 0, 0, 0, trans);
}


void DrawTriangle(int layer, int x1, int y1, int x2, int y2, int x3, int y3, int color, bool filled, int trans)

* layer - the drawing layer. Same as the built in drawing primitives
* x/y1/2/3 - The three verticies of the triangle. They can be specified in any order you want.
* color, trans - same as the drawing primitives.

Draw a triangle, amaze your friends! The three-sided polygon can be the basis of advanced graphics effects, including 3d objects!

Use it just like a built in primitive (except that you don't call it with Screen->), by pasting it at the top of your script file.

Note: It does not support scaling or rotation. That would be quite difficult. However, you are free to rotate/scale the verticies yourself.

Note 2: It has a small bug whereby it will, on occasion, draw a certain scanline twice. Normally, this isn't a problem. But, if you draw it partially transparent (trans=64), then the line will show up darker. I can't figure out what's causing this, but if you can figure it out, a cookie awaits you!

Usage example:


//draw a spotlight on Link
const int lightoff = 8;
while(true) {
DrawTriangle(0, 16, 0, Link->X - 8 - lightoff, Link->Y + 12, Link->X + 24 - lightoff, Link->Y + 12, 227, true, 64);
DrawTriangle(0, 240, 0, Link->X - 8 + lightoff, Link->Y + 12, Link->X + 24 + lightoff, Link->Y + 12, 227, true, 64);
Screen->Circle(0, Link->X + 8 - lightoff, Link->Y+12, 16, 227, 1, 0,0,0, true, 128);
Screen->Circle(0, Link->X + 8 + lightoff, Link->Y+12, 16, 227, 1, 0,0,0, true, 128);
Waitframe();
}

tursiops
12-22-2008, 01:53 PM
Note 2: It has a small bug whereby it will, on occasion, draw a certain scanline twice. Normally, this isn't a problem. But, if you draw it partially transparent (trans=64), then the line will show up darker. I can't figure out what's causing this, but if you can figure it out, a cookie awaits you!


I haven't tested it (since I'm kinda lazy), but... pretty sure the 'else' block near the end needs the line
sy = by; to be changed to
sy = by+1; (like it is in the 'if(dx1 > dx2)' block above it).