User Tag List

Results 1 to 2 of 2

Thread: Triangles!

  1. #1
    Lynel
    ZC Developer
    pkmnfrk's Avatar
    Join Date
    Jan 2004
    Location
    Toronto
    Age
    37
    Posts
    1,248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    3,141
    Level
    18
    vBActivity - Bars
    Lv. Percent
    10.37%

    Triangles!

    Code:
    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);
    }
    Code:
    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:

    Code:
    //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();
    }
    Tale of the Cave - a web comic - Updates Monday, Wednesday, Friday
    ZC Tutorials - Tutorials and Script Library - Updated July 30, 2008
    ZeldaGuard - Corruption in my save files? It's more likely than you think!
    I do script requests!

  2. #2
    Gel tursiops's Avatar
    Join Date
    Dec 2007
    Age
    45
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    604
    Level
    8
    vBActivity - Bars
    Lv. Percent
    74.63%

    Re: Triangles!

    Quote Originally Posted by pkmnfrk View Post
    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
    Code:
    sy = by;
    to be changed to
    Code:
    sy = by+1;
    (like it is in the 'if(dx1 > dx2)' block above it).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Armageddon Games is a game development group founded in 1997. We are extremely passionate about our work and our inspirations are mostly drawn from games of the 8-bit and 16-bit era.
Social