Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (768)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Triangle fill algorithm  (Read 15222 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.   Smiley
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« 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!
Offline KaiHH

JGO Kernel


Medals: 595



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

http://bfy.tw/C6uZ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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)

Offline 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.

Offline bestwarrior2001

Senior Newbie


Exp: 3 years



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

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

http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1339 views)
2018-06-10 19:43:48

EgonOlsen (1229 views)
2018-06-10 19:43:44

EgonOlsen (939 views)
2018-06-10 19:43:20

DesertCoockie (1380 views)
2018-05-13 18:23:11

nelsongames (1112 views)
2018-04-24 18:15:36

nelsongames (1374 views)
2018-04-24 18:14:32

ivj94 (2119 views)
2018-03-24 14:47:39

ivj94 (1268 views)
2018-03-24 14:46:31

ivj94 (2285 views)
2018-03-24 14:43:53

Solater (807 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!