RosettaCodeData/Task/Bitmap-Bresenhams-line-algorithm/Java/bitmap-bresenhams-line-algorithm.java

116 lines
3.2 KiB
Java
Raw Permalink Normal View History

2018-06-22 20:57:24 +00:00
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
2015-02-20 00:35:01 -05:00
2018-06-22 20:57:24 +00:00
public class Bresenham {
2015-02-20 00:35:01 -05:00
public static void main(String[] args) {
2018-06-22 20:57:24 +00:00
SwingUtilities.invokeLater(Bresenham::run);
}
private static void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setTitle("Bresenham");
f.getContentPane().add(new BresenhamPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
2015-02-20 00:35:01 -05:00
}
}
class BresenhamPanel extends JPanel {
2018-06-22 20:57:24 +00:00
private final int pixelSize = 10;
BresenhamPanel() {
setPreferredSize(new Dimension(600, 500));
setBackground(Color.WHITE);
2015-02-20 00:35:01 -05:00
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
2018-06-22 20:57:24 +00:00
int w = (getWidth() - 1) / pixelSize;
int h = (getHeight() - 1) / pixelSize;
int maxX = (w - 1) / 2;
int maxY = (h - 1) / 2;
int x1 = -maxX, x2 = maxX * -2 / 3, x3 = maxX * 2 / 3, x4 = maxX;
int y1 = -maxY, y2 = maxY * -2 / 3, y3 = maxY * 2 / 3, y4 = maxY;
drawLine(g, 0, 0, x3, y1); // NNE
drawLine(g, 0, 0, x4, y2); // ENE
drawLine(g, 0, 0, x4, y3); // ESE
drawLine(g, 0, 0, x3, y4); // SSE
drawLine(g, 0, 0, x2, y4); // SSW
drawLine(g, 0, 0, x1, y3); // WSW
drawLine(g, 0, 0, x1, y2); // WNW
drawLine(g, 0, 0, x2, y1); // NNW
2015-02-20 00:35:01 -05:00
}
private void plot(Graphics g, int x, int y) {
2018-06-22 20:57:24 +00:00
int w = (getWidth() - 1) / pixelSize;
int h = (getHeight() - 1) / pixelSize;
int maxX = (w - 1) / 2;
int maxY = (h - 1) / 2;
int borderX = getWidth() - ((2 * maxX + 1) * pixelSize + 1);
int borderY = getHeight() - ((2 * maxY + 1) * pixelSize + 1);
int left = (x + maxX) * pixelSize + borderX / 2;
int top = (y + maxY) * pixelSize + borderY / 2;
2015-02-20 00:35:01 -05:00
g.setColor(Color.black);
2018-06-22 20:57:24 +00:00
g.drawOval(left, top, pixelSize, pixelSize);
2015-02-20 00:35:01 -05:00
}
private void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
2018-06-22 20:57:24 +00:00
// delta of exact value and rounded value of the dependent variable
2015-02-20 00:35:01 -05:00
int d = 0;
int dx = Math.abs(x2 - x1);
2018-06-22 20:57:24 +00:00
int dy = Math.abs(y2 - y1);
2015-02-20 00:35:01 -05:00
2018-06-22 20:57:24 +00:00
int dx2 = 2 * dx; // slope scaling factors to
int dy2 = 2 * dy; // avoid floating point
2015-02-20 00:35:01 -05:00
int ix = x1 < x2 ? 1 : -1; // increment direction
int iy = y1 < y2 ? 1 : -1;
2018-06-22 20:57:24 +00:00
int x = x1;
int y = y1;
if (dx >= dy) {
while (true) {
plot(g, x, y);
if (x == x2)
2015-02-20 00:35:01 -05:00
break;
2018-06-22 20:57:24 +00:00
x += ix;
2015-02-20 00:35:01 -05:00
d += dy2;
if (d > dx) {
2018-06-22 20:57:24 +00:00
y += iy;
2015-02-20 00:35:01 -05:00
d -= dx2;
}
}
} else {
2018-06-22 20:57:24 +00:00
while (true) {
plot(g, x, y);
if (y == y2)
2015-02-20 00:35:01 -05:00
break;
2018-06-22 20:57:24 +00:00
y += iy;
2015-02-20 00:35:01 -05:00
d += dx2;
if (d > dy) {
2018-06-22 20:57:24 +00:00
x += ix;
2015-02-20 00:35:01 -05:00
d -= dy2;
}
}
}
}
}