Filling the area between three points (java.awt.Point) with Graphics (java.awt.Graphics)

StackOverflow https://stackoverflow.com/questions/20447814

  •  30-08-2022
  •  | 
  •  

Pergunta

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?

Foi útil?

Solução

I would draw a Polygon with three Points:

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top