Question
this code draw a line with |slope| <1 , i need to modify it draw a line with slope>1 , using the same algorithm (bersenahm
this code draw a line with |slope|<1 , i need to modify it draw a line with slope>1 , using the same algorithm (bersenahm algorithm )
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.ObjectOutputStream.PutField;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
static JFrame frame;
static JPanel panel;
static BufferedImage image;
public static void main(String[] args) {
image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
panel = new JPanel() {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
};
frame = new JFrame();
frame.setSize(500, 500);
frame.add(panel);
frame.setVisible(true);
// draw_line_Bresenham(image.getRaster(), 80, 50, 80, 100, new int[] {
// 255, 255, 255 });
draw_line_Bresenham(image.getRaster(), 50, 30, 80, 20, new int[] { 255, 255, 255 });
}
private static void draw_line_Bresenham(WritableRaster raster, int xa, int ya, int xb, int yb, int[] ms) {
int dx = Math.abs(xa - xb), dy = Math.abs(ya - yb), p = 2 * dy - dx, twody = 2 * dy, twodxdy = 2 * (dy - dx),
twodx = 2 * dx;
int x = 0, y = 0, xend = 0, yend = 0;
if (xa > xb) {
x = xb;
y = yb;
xend = xa;
} else {
x = xa;
y = ya;
xend = xb;
}
while (x < xend) {
x++;
if (p < 0)
p += twody;
else {
if (ya > yb)
y++;
else
y--;
p += twodxdy;
}
raster.setPixel(x, y, ms);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started