import java.applet.*; /** Bresenham algorithm for drawing a line */ public class Bresenham extends DDA { /* these need to be instance variables so that we can display them with the showState() method */ int x; int y; int p; int const1; int const2; public String showState() { return "x = " + x + "\ny = " + y + "\np = " + p + "\nconst1 = " + const1 + "\nconst2 = " + const2; } void drawLine(int x1, int y1, int x2, int y2) { int dx = x2 - x1; int dy = y2 - y1; const1 = 2*dy; const2 = 2*(dy-dx); p = 2*dy - dx; y = y1; for (x = x1; x <= x2; x++) { drawPixel(x,y); if (step()) return; if (p < 0) { p += const1; } else { p += const2; y++; } } } }