gpt4 book ai didi

java - 用 3 种颜色给三角形上色

转载 作者:行者123 更新时间:2023-12-04 04:14:20 26 4
gpt4 key购买 nike

请帮助我如何用三种颜色给三角形上色。我把它当作学校的问题,使用Java编程语言

你好,这是代码:我试图用三种颜色填充三角形,但很难混合颜色来得到它。如果你要解决它(近似),请尽可能多地发送给我,我把它作为作业和 5-6 天的截止日期

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);



}


}

最佳答案

像这样给三角形上色相当于计算三角形内每个像素的重心坐标。以下未经测试代码计算三角形 ABC 内每个像素的重心坐标,然后使用它为该像素着色:

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));
}
}
}

我会留给您测试和集成此代码。

您可以在 wikipedia 上阅读更多关于重心坐标的信息;

关于java - 用 3 种颜色给三角形上色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61043652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com