Write a program in java containing a function that draws a line using the \"Bres
ID: 3541285 • Letter: W
Question
Write a program in java containing a function that draws a line using the "Bresenham" algorithm. Implement the function to draw N short lines (N is provided by the user) at positions (i.e., end coordinates) determined by a random number generator
Explanation / Answer
// draw a line from point x1,y1 into x2,y2 public void bresenham_line(int x1, int y1, int x2, int y2) { // if point x1, y1 is on the right side of point x2, y2, change them if ((x1 - x2) > 0) {bresenham_line(x2, y2, x1, y1); return;} // test inclination of line // function Math.abs(y) defines absolute value y if (Math.abs(y2 - y1) > Math.abs(x2 - x1)) { // line and y axis angle is less then 45 degrees // that swhy go on the next procedure bresteepline(y1, x1, y2, x2); return; } // line and x axis angle is less then 45 degrees, so x is guiding // auxiliary variables int x = x1, y = y1, sum = x2 - x1, Dx = 2 * (x2 - x1), Dy = Math.abs(2 * (y2 - y1)); int prirastokDy = ((y2 - y1) > 0) ? 1 : -1; // draw line for (int i = 0; i 0) {bresteepline(x4, y4, x3, y3); return;} int x = x3, y = y3, sum = x4 - x3, Dx = 2 * (x4 - x3), Dy = Math.abs(2 * (y4 - y3)); int prirastokDy = ((y4 - y3) > 0) ? 1 : -1; for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.