Consider the following line drawing code: int Image[20][20]; void draw_line(int
ID: 3739709 • Letter: C
Question
Consider the following line drawing code:
int Image[20][20];
void draw_line(int x1, int y1, int x2, int y2, int value) {
// Calculate step size double dx = x2 - x1; double dy = y2 - y1;
// Draw points on line
double x = x1 + 0.5;
for (int y = y1; y <= y2; y++) {
Image[y][(int)x] = value;
x += dx/dy; }
}
a) What pixels in the image will be filled in when drawing a line from (10,5) to (15,15)?
b) What will happen if we try to draw a line with dx < 0?
c) What will happen if we try to draw a line with dy < 0?
d) What will happen if we try to draw a line with dy = 0?
Explanation / Answer
a)
x1 = 10 y1 = 5 x2 = 15 y2 = 15
dx = 5 dy = 10
x = x1+0.5 = 10.5
For Loop:
1. y=5
pixel = 5 10
x = 10.5+(5/10) = 11
2. y=6
pixel = 6 11
x = 11+0.5 == 11.5
3. y=7
pixel = 7 11
The loop will iterate in this manner, in each iteration value of y increments by 1 & an step of 0.5 is added to x. So the pixels are:
5 10, 6 11, 7 11, 8 12, 9 12, 10 13, 11 13, 12 14, 13 14, 14 15, 15 15
b&c) If any one of dx/dy is less than 0 then step value would become negative & thus value of x would decrease with every iteration i.e. step function becomes negative.
d) dy = 0 will result in an exception bcoz division by 0 is not allowed in any arithmetic calculation.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.