Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (734) Games in Android Showcase (222) games submitted by our members Games in WIP (811) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Triangle fill algorithm  (Read 4166 times) 0 Members and 1 Guest are viewing this topic.
bestwarrior2001

Senior Newbie

Exp: 3 years

 « Posted 2017-05-31 20:19:58 »

Hi guys

I need a fast algorithm that fills a 2D triangle. I'm not using Graphics or Graphics2D for this.
This is what I want:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14 `class ColorBuffer {   private int[][] data;   public void plot(int x,int y,int color) {      data[x][y]=color;   }   // some other methods}class Renderer {   public ColorBuffer buffer;   // [...]   public void fillTriangle(Point v1,Point v2,Point v3) {      // that's what I need   }}`

Please include a source code if you can.
Riven

« JGO Overlord »

Medals: 1299
Projects: 4
Exp: 16 years

 « Reply #1 - Posted 2017-05-31 20:41:47 »

And I want a pony.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
KaiHH

JGO Kernel

Medals: 436

 « Reply #2 - Posted 2017-05-31 21:22:41 »

http://bfy.tw/C6uZ
Archive
 « Reply #3 - Posted 2017-05-31 22:49:57 »

If you're looking for speed, the first thing you should fix is your ColorBuffer. 2D arrays are slower than a 1D array. Plot the pixel at (x,y) by indexing into a 1D array of length (width*height) by using this formula (x + y * width)

ddyer
 « Reply #4 - Posted 2017-06-01 15:52:53 »

Decompose the triangle into two segments, "top half" and "bottom half" with appropriate
special cases.

Use the Bresenham algorithm to describe the left and right edges, draw one row at a time
from left to right.

This can be very fast (requires no multiplies) if implemented carefully, but be warned
that for extremely acute triangles, it can produce counterintuitive "triangles" that are
not one blob.

For extra credit, get the edge conditions correct so drawing a mesh of triangles
draws each pixel exactly once.  Otherwise, attempts to draw a mesh will leave
stray pixels undrawn.

bestwarrior2001

Senior Newbie

Exp: 3 years

 « Reply #5 - Posted 2017-06-01 15:58:49 »

That's exactly what I need. Thanks!
Damocles
 « Reply #6 - Posted 2017-06-06 12:29:59 »

http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html

Pages: [1]
 ignore  |  Print

 cybrmynd (28 views) 2017-08-02 12:28:51 cybrmynd (42 views) 2017-08-02 12:19:43 cybrmynd (56 views) 2017-08-02 12:18:09 Sralse (59 views) 2017-07-25 17:13:48 Archive (489 views) 2017-04-27 17:45:51 buddyBro (637 views) 2017-04-05 03:38:00 CopyableCougar4 (1117 views) 2017-03-24 15:39:42 theagentd (1126 views) 2017-03-24 15:32:08 Rule (1101 views) 2017-03-19 12:43:22 Rule (1078 views) 2017-03-19 12:42:17
 LiquidNitrogen 23x orange451 20x theagentd 17x KaiHH 12x dime26 11x kingroka123 11x princec 11x Riven 10x gouessej 9x KevinWorkman 6x Slyth2727 6x philfrei 6x Spasi 6x cybrmynd 5x orangepascal 5x Opiop 4x
 List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05SF/X Librariesby SkyAphid2017-03-02 06:38:56SF/X Librariesby SkyAphid2017-03-02 06:38:32SF/X Librariesby SkyAphid2017-03-02 06:38:05SF/X Librariesby SkyAphid2017-03-02 06:37:51
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org