Bitmap/Bresenham's line algorithm: Difference between revisions
Content added Content deleted
m (→version 1: corrected comment label for an END statement.) |
(→{{header|Java}}: added Java) |
||
Line 1,130: | Line 1,130: | ||
viewRGB myimg=: (Square;0 0 255) drawLines myimg NB. draw 4 blue lines to form a square |
viewRGB myimg=: (Square;0 0 255) drawLines myimg NB. draw 4 blue lines to form a square |
||
viewRGB (Diamond;255 0 0) drawLines (Square;0 0 255) drawLines myimg</lang> |
viewRGB (Diamond;255 0 0) drawLines (Square;0 0 255) drawLines myimg</lang> |
||
=={{header|Java}}== |
|||
<lang java>import java.awt.*; |
|||
import javax.swing.*; |
|||
public class Bresenham extends JFrame { |
|||
BresenhamPanel panel; |
|||
public static void main(String[] args) { |
|||
JFrame f = new Bresenham(); |
|||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
f.setVisible(true); |
|||
} |
|||
public Bresenham() { |
|||
Container content = getContentPane(); |
|||
content.setLayout(new BorderLayout()); |
|||
panel = new BresenhamPanel(); |
|||
content.add(panel, BorderLayout.CENTER); |
|||
setTitle("Bresenham"); |
|||
pack(); |
|||
setLocationRelativeTo(null); |
|||
} |
|||
} |
|||
class BresenhamPanel extends JPanel { |
|||
final int w, h, centerX, centerY; |
|||
public BresenhamPanel() { |
|||
w = 600; |
|||
h = 500; |
|||
centerX = w / 2; |
|||
centerY = h / 2; |
|||
setPreferredSize(new Dimension(w, h)); |
|||
setForeground(Color.black); |
|||
setBackground(Color.white); |
|||
} |
|||
@Override |
|||
public void paintComponent(Graphics g) { |
|||
super.paintComponent(g); |
|||
drawLine(g, 0, 0, 8, 19); // NNE |
|||
drawLine(g, 0, 0, 19, 8); // ENE |
|||
drawLine(g, 0, 0, 19, -8); // ESE |
|||
drawLine(g, 0, 0, 8, -19); // SSE |
|||
drawLine(g, 0, 0, -8, -19); // SSW |
|||
drawLine(g, 0, 0, -19, -8); // WSW |
|||
drawLine(g, 0, 0, -19, 8); // WNW |
|||
drawLine(g, 0, 0, -8, 19); // NNW |
|||
} |
|||
private void plot(Graphics g, int x, int y) { |
|||
g.setColor(Color.black); |
|||
g.drawOval(centerX + (x * 10), centerY + (-y * 10), 10, 10); |
|||
} |
|||
private void drawLine(Graphics g, int x1, int y1, int x2, int y2) { |
|||
// delta of exact value and rounded value of the dependant variable |
|||
int d = 0; |
|||
int dy = Math.abs(y2 - y1); |
|||
int dx = Math.abs(x2 - x1); |
|||
int dy2 = (dy << 1); // slope scaling factors to avoid floating |
|||
int dx2 = (dx << 1); // point |
|||
int ix = x1 < x2 ? 1 : -1; // increment direction |
|||
int iy = y1 < y2 ? 1 : -1; |
|||
if (dy <= dx) { |
|||
for (;;) { |
|||
plot(g, x1, y1); |
|||
if (x1 == x2) |
|||
break; |
|||
x1 += ix; |
|||
d += dy2; |
|||
if (d > dx) { |
|||
y1 += iy; |
|||
d -= dx2; |
|||
} |
|||
} |
|||
} else { |
|||
for (;;) { |
|||
plot(g, x1, y1); |
|||
if (y1 == y2) |
|||
break; |
|||
y1 += iy; |
|||
d += dx2; |
|||
if (d > dy) { |
|||
x1 += ix; |
|||
d -= dy2; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
}</lang> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |