Question

The book by Foley et al. presents a version of the Bresenham algorithm, that works for...

The book by Foley et al. presents a version of the Bresenham algorithm, that works for lines with slope m such that 0 ≤ m ≤ 1. Below, the algorithm follows:

void MidPointLine(int x0, int y0, // line start point

                              int x1, int y1) { // line final poin

   int dx = x1 - x0;

int dy = y1 - y0;

int d = 2 * dy - dx;

int incrE = 2 * dy;

int incrNE = 2 * (dy - dx);

int x = x0;

int y = y0;

PutPixel(x, y); // firs pixel of the line

while (x

if (d<=0) {

d += incrE;

x++;

    } else {

d += incrNE;

x++;

y++;

}

      PutPixel(x, y);

}

}

Assuming that the function above was invoked with the MidPointLine parameters (0,0,5,3): list the coordinate pairs (x, y) of all pixels that will be painted as a result of this call. Note: the first pixel to be painted will be the coordinates (0,0).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
void MidPointLine(int x0, int y0, int x1, int y1)
{
    int dx = x1 - x0;
    int dy = y1 - y0;
    int d = 2 * dy - dx;
    int incrE = 2 * dy;
    int incrNE = 2 * (dy - dx);
    int x = x0;
    int y = y0;
    printf("(%d, %d)\n", x, y);
    while (x < x1)
    {
        if (d <= 0)
        {
            d += incrE;
            x++;
        }
        else
        {
            d += incrNE;
            x++;
            y++;
        }
        printf("(%d, %d)\n", x, y);
    }
}

int main()
{
    MidPointLine(0, 0, 5, 3);
    return 0;
}

Output:

(0, 0)
(1, 1)
(2, 1)
(3, 2)
(4, 2)
(5, 3)
Add a comment
Know the answer?
Add Answer to:
The book by Foley et al. presents a version of the Bresenham algorithm, that works for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A random walk is a particular kind of probabilistic (pseudo-random) simulation that models certai...

    A random walk is a particular kind of probabilistic (pseudo-random) simulation that models certain statistical systems, such as Brownian motion of particles or molecules. Coin flipping is an example of a one-dimensional random walk--one dimensional because you only can go forward (when you flip heads) or backward (when you flip tails) along a straight line. Suppose you take a random walk of nsteps. How many steps away from your starting point would you expect to end up on average, if...

  • Python. Just work in the def sierpinski. No output needed. Will give thumbs up for any attempt beginning this code. Your task is to implement this algorithm in Python, returning a random collection of...

    Python. Just work in the def sierpinski. No output needed. Will give thumbs up for any attempt beginning this code. Your task is to implement this algorithm in Python, returning a random collection of inum-100, 000 points. You should then plot the points to see the structure. Please complete the following function: def sierpinski (po, v, f, inum) The four arguments are ·po the initial point. You may assume this is the origin, i.e., po = [0, 0] . v:...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • Write a MATLAB Graphical User Interface (GUI) to simulate and plot the projectile motion – the...

    Write a MATLAB Graphical User Interface (GUI) to simulate and plot the projectile motion – the motion of an object projected into the air at an angle. The object flies in the air until the projectile returns to the horizontal axis (x-axis), where y=0. This MATLAB program should allow the user to try to hit a 2-m diameter target on the x-axis (y=0) by varying conditions, including the lunch direction, the speed of the lunch, the projectile’s size, and the...

  • Game Description: Most of you have played a very interesting game “Snake” on your old Nokia...

    Game Description: Most of you have played a very interesting game “Snake” on your old Nokia phones (Black & White). Now it is your time to create it with more interesting colors and features. When the game is started a snake is controlled by up, down, left and right keys to eat food which appears on random locations. By eating food snake’s length increases one unit and player’s score increases by 5 points. Food disappears after 15 seconds and appears...

  • 2. Consider a mass m moving in R3 without friction. It is fasten tightly at one...

    2. Consider a mass m moving in R3 without friction. It is fasten tightly at one end of a string with length 1 and can swing in any direction. In fact, it moves on a sphere, a subspace of R3 1 0 φ g 2.1 Use the spherical coordinates (1,0,) to derive the Lagrangian L(0,0,0,0) = T-U, namely the difference of kinetic energy T and potential energy U. (Note r = 1 is fixed.) 2.2 Calculate the Euler-Lagrange equations, namely...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT