我试图用水平线填充三角形,我无法弄清楚我当前的方法有什么问题。在任何人说要使用fillPolygon之前,我不能使用它。我需要用线填充它。它似乎在某些情况下工作正常,而在其他情况下则完全打破。
它应该是这样的。但后来我尝试将我的方法应用于旋转的3D立方体......
我不知道出了什么问题。此外,红色边框也是我的三角形方法之一。这些工作完美,填充三角形和轮廓三角形具有相同的顶点输入。
public void filledTri(int x1,int y1,int x2,int y2,int x3,int y3){
int[] xs = {x1,x2,x3};
int[] ys = {y1,y2,y3};
//Sort vertices in vertical order so A/1 is highest and C/3 is lowest
int I,tempx,tempy;
for(int i=1;i<3;i++){
I = i-1;
tempx = xs[i];
tempy = ys[i];
while(I>=0&&tempy<ys[I]){
xs[I+1] = xs[I];
ys[I+1] = ys[I];
I--;
}
xs[I+1] = tempx;
ys[I+1] = tempy;
}
//Set left and right edges
linepts ab = new linepts(xs[0],ys[0],xs[1],ys[1]),
ac = new linepts(xs[0],ys[0],xs[2],ys[2]);
linepts[] lines = {ab.getEndX() < ac.getEndX() ? ab : ac,
ab.getEndX() > ac.getEndX() ? ab : ac,
new linepts(xs[1],ys[1],xs[2],ys[2])};
//Fill triangle
int startY = ys[0],endY = ys[2];
for(int y=startY;y<=endY;y++){
if(y>ys[1])
horizontalLine((int)Math.round(lines[2].getX(y)),
y,
(int)Math.round(lines[1].getX(y)));
else
horizontalLine((int)Math.round(lines[0].getX(y)),
y,
(int)Math.round(lines[1].getX(y)));
}
getX(int y)获取x坐标,其中线穿过y值。如果它是一条水平线,它只返回线的起始x
A点是屏幕上最高的值,最低值,B是中间值,C是屏幕上最低值和最高值
我正在jframe上使用缓冲图像来绘制它,如果这有帮助。
我已经看到你在Software Renderer tutorial做了什么。在this和this剧集中对此进行了解释。
他所做的是扫描最长的线以获得该线上的每个像素,它存储最小X值和最大X值(由其他2条线给出)。他最初为特定三角形制作它,但随后他升级代码以接受通用三角形。
我假设您正在经历的是因为将3D三角形投影到2D中(剪切,三角形获得无限坐标,或者因为您的程序不需要太空的三角形。
一种方法是将线条绘制到图像,然后在TexturePaint
中使用该图像来填充Shape
(在这种情况下为三角形)。
它可能看起来像这样:(如果您使用包含一条红线的单个图像,将其放在随机的BG颜色上,并使用平滑的1.5像素笔划将形状本身绘制为蓝色)。
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.*;
public class LinesFillShape {
private JComponent ui = null;
LinesFillShape() {
initUI();
}
public final void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
ui.add(new JLabel(new ImageIcon(getImage())));
}
private void drawPolygon(Graphics2D g, int sz, Random r) {
int[] xpoints = {
r.nextInt(sz), r.nextInt(sz), r.nextInt(sz)
};
int[] ypoints = {
r.nextInt(sz), r.nextInt(sz), r.nextInt(sz)
};
Polygon p = new Polygon(xpoints, ypoints, 3);
Color bg = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
g.setColor(bg);
g.fill(p);
g.setPaint(
new TexturePaint(getTexture(),
new Rectangle2D.Double(0, 0, 8, 8)));
g.fill(p);
g.setStroke(new BasicStroke(1.5f));
g.setColor(Color.BLUE);
g.draw(p);
}
private BufferedImage getImage() {
int sz = 600;
BufferedImage bi = new BufferedImage(sz, sz, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Random r = new Random();
drawPolygon(g, sz, r);
drawPolygon(g, sz, r);
drawPolygon(g, sz, r);
g.dispose();
return bi;
}
private BufferedImage getTexture() {
BufferedImage bi = new BufferedImage(8, 8, BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.getGraphics();
g.setColor(Color.RED);
// TODO: something more interesting here..
g.drawLine(0, 0, 0, 8);
g.dispose();
return bi;
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
LinesFillShape o = new LinesFillShape();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}
我没有仔细检查你的代码,但我可以告诉你,你并不总是加入与相关方的交叉点。
你可以按如下方式工作:
对于给定的扫描线(一些Y
),
Y0-Y1
将有零或两面;使用条件Y1-Y2
(索引模3),没有其他,Y2-Y0
的两侧,计算交叉点。您将从Y
扫描到(Yi > Y) != (Yi+1 > Y)
,每次都加入两个十字路口。