我正在研究 Java 中的 Bresenham 画线算法,我已经能够画线,但我的坐标有问题。我的行从屏幕的左上角开始,我希望它从左下角开始。我试过仿射变换但失败了。这是我的代码示例。
public void paintComponent( Graphics g )
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, getWidth(), getHeight());
int xElemSize = this.getWidth() / this.screenPixel.getSizeX();
int yElemSize = this.getHeight() / this.screenPixel.getSizeY();
int rectX = 0, rectY = 0;
for (int i = 0; i < this.screenPixel.getSizeX(); i++)
{
for (int h = 0; h < this.screenPixel.getSizeY(); h++)
{
if (screenPixel.matrix[i][h] != 0)
{
rectX = i * xElemSize;
rectY = h * yElemSize;
Rectangle2D rect = new Rectangle2D.Double(rectX, rectY, xElemSize, yElemSize);
g2d.setColor(Color.GREEN);
g2d.fill(rect);
g2d.draw (rect);
bresenham_Linie(x1, y1, x2, y2);
}
}
}
}
谢谢你的帮助!!