Frage

I have three points in my code and I would like to fill the area between them, or in other words draw and fill a triangle between 3 points.

I thought about simply drawing lines with a for loop (from x1 to x2) but I don't think that will be efficient, is there any other way to do this that is more efficient?

War es hilfreich?

Lösung

I would draw a Polygon with three Points:

Graphics.fillPolygon(int[], int[], int)

Andere Tipps

You can use a buffered image and paint pixels one at a time. BufferedImage is MUCH faster than using the paintcomponent of a gui object. Use the WriteableRaster in a BufferedImage, then loop through the pixels one at a time, marking the ones that fall within your triangle.

Your algorithm looks something like:

equation for line 1
equation for line 2
equation for line 3
then for each point as you loop through writeableraster:  
    check if x and y are each between the three lines

Alternatively, you could try this example which is more of a paint approach.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top