Colorear un triángulo con 3 colores

MrJab:

Izquierda lo que está archivada, Justo lo que quiero archivo:

introducir descripción de la imagen aquí

Por favor me ayude a colorear el triángulo en el lado derecho. Lo tengo como un problema para Computer Graphics, Usar lenguaje de programación Java

Hola este es el código: Estoy intentando conseguir el triángulo de la derecha, pero es difícil de mezclar colores para conseguirlo. Por favor, si va a resolverlo (aproximarla) lo más que puede enviar a mí, lo tengo como tarea y 5-6 días plazo

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
public class NotFullVersion2 extends JApplet {

public static void main(String s[]) {

  JFrame frame = new JFrame();
  frame.setTitle("Colors");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JApplet applet = new NotFullVersion2();
  applet.init();
  applet.addMouseListener(
     new MouseAdapter(){  
        public void mousePressed(MouseEvent e) { 
           System.out.println(e.getX() + " " + e.getY());
        }});

  frame.getContentPane().add(applet);
  frame.pack();
  frame.setVisible(true);
}

ColorPanel panel;

public void init() {

  panel = new ColorPanel();
  Container cp = getContentPane();
  cp.setLayout(new BorderLayout());
  cp.add(panel, BorderLayout.CENTER);
  JPanel p = new JPanel();
  cp.add(p,BorderLayout.EAST);  
}


}

class ColorPanel extends JPanel  {

//int red = 100,green = 100, blue = 100;

public ColorPanel() {

  setPreferredSize(new Dimension(500, 500));
  setBackground(Color.black);
}

public void paintComponent(Graphics g) {

  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D)g;


  for(int i = 0; i < 256; i++) {

     int start = 399; 

     g2.setColor(new Color(0,i,255-i));   

     for(int j = 0; j < 200; j ++) {

        Rectangle rec = new Rectangle(150+j,start - i,1,1);
        g2.fill(rec);

     }

  }


  for(int j = 0; j < 100; j++) {

     int start = 100;

     for(int i = 0; i < 300; i++) {

        if(i < 22) {

           g2.setColor(new Color(255,0,0));
           Rectangle rec = new Rectangle(100 + i,start + j,1,1);
           g2.fill(rec);

        } else if(i > 21 && i < 278) {

           g2.setColor(new Color(255-(i-22),(i-22),0));
           Rectangle rec = new Rectangle(100 + i,start + j,1,1);
           g2.fill(rec);

        } else if(i < 300) {

           g2.setColor(new Color(0,255,0));
           Rectangle rec = new Rectangle(100 + i,start + j,1,1);
           g2.fill(rec);

        }
     }

  }

  GeneralPath closePath1a = new GeneralPath();

  g2.setColor(new Color(0,0,0));

  closePath1a.moveTo(100,100);
  closePath1a.lineTo(100,400);
  closePath1a.lineTo(250,400);
  closePath1a.closePath();

  g2.fill(closePath1a);


  GeneralPath closePath2a = new GeneralPath();

  g2.setColor(new Color(0,0,0));

  closePath2a.moveTo(400,100);
  closePath2a.lineTo(400,400);
  closePath2a.lineTo(250,400);
  closePath2a.closePath();

  g2.fill(closePath2a);


  GeneralPath closePath3a = new GeneralPath();

  g2.setColor(new Color(0,0,0));

  closePath3a.moveTo(100,100);
  closePath3a.lineTo(100,50);
  closePath3a.lineTo(400,50);
  closePath3a.lineTo(400,100);
  closePath3a.closePath();

  g2.fill(closePath3a);



 }  


 }
ybungalobill:

Colorear un triángulo como esto es equivalente a calcular las coordenadas baricéntricas de cada píxel dentro del triángulo. La siguiente no probado código calcula las coordenadas baricéntricas para cada píxel dentro del triángulo ABC, y luego lo utiliza para que el color de píxeles:

private float area(float Ax, float Ay, float Bx, float By, float Cx, float Cy) {
    return  0.5*((Ax - Cx)*(By - Ay) - (Ax - Bx)*(Cy - Ay));
}

private void paintTriangle(Graphics g, float Ax, float Ay, float Bx, float By, float Cx, float Cy) {
    // calculate the bounding box of the triangle:
    int minX = Math.round(Math.min(Ax, Math.min(Bx, Cx)));
    int minY = Math.round(Math.min(Ay, Math.min(By, Cy)));
    int maxX = Math.round(Math.max(Ax, Math.max(Bx, Cx)));
    int maxY = Math.round(Math.max(Ay, Math.max(By, Cy)));

    // loop for each pixel in the bounding box of the triangle
    for(int y = minY; y < maxY; ++y) {
        for(int x = minX; x < maxX; ++x) {
            // center of the pixel (x,y)
            float Px = x + 0.5, Py = y + 0.5;

            // barycentric coordinates of P
            float denom = area(Ax, Ay, Bx, By, Cx, Cy);
            float b0 = area(Px, Py, Bx, By, Cx, Cy)/denom;
            float b1 = area(Ax, Ay, Px, Py, Cx, Cy)/denom;
            float b2 = area(Ax, Ay, Bx, By, Px, Py)/denom;

            // discard pixels outside the triangle
            if(b0 < 0 || b1 < 0 || b2 < 0)
                continue;

            // paint a pixel of color (b0,b1,b2) at (x,y)
            g.setColor(new Color(b0,b1,b2));
            g.fillRect(x,y,1,1));
        }
    }
}

Lo dejo a usted para probar e integrar este código.

Puede leer más acerca de las coordenadas baricéntricas en Wikipedia ;

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=410070&siteId=1
Recomendado
Clasificación