0

我正在研究 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);
            }
        }
    }
}

谢谢你的帮助!!

4

1 回答 1

1

Java2D 坐标与您想象的不同。在 Java2D 中,左上角是 (0,0)。左下角是 (0, HEIGHT)。

参考官方教程:http ://docs.oracle.com/javase/tutorial/2d/overview/coordinate.html

for (int h = 0, maxH=this.screenPixel.getSizeY(); h < maxH; h++)
{   
    if (screenPixel.matrix[i][h] != 0)
    {
        rectX = i * xElemSize;
        rectY = (maxH-h) * yElemSize;      
        // ... 
    }
}
于 2014-05-12T13:30:21.673 回答