programming of line segment drawing algorithms

Asked 1 years ago, Updated 1 years ago, 316 views

The following line segment drawing algorithm does not seem to work properly unless the absolute value of slope a is less than or equal to 1. Please tell me why and how to respond to any slope.

void drawLine(int x0, inty0, /* starting point*/int x1, inty1, /* ending point*/
 char, charg, charb)/* Line segment color (RGB)*/{
   double x, y, a;
   a = (double)(y1-y0)/(x1-x0);
   /* If it is not x0<x1
   Replace the coordinates of the starting and ending points here*/
   /* writePixel(): dot drawing function */
   writePixel(x0,y0,r,g,b); // Start point drawing
    x = x0;
    y = y0;
    while(x<x1){
     x = x + 1.0;
     y = y + a;
     writePixel(x, (int) (y+0.5), r, g, b);
    }
 }

Also, I would like you to tell me how to improve the above program and draw curves.
Thank you for your cooperation.

c

2022-09-30 21:59

1 Answers

A1. You always do x+=1.0; in the loop. If the slope (absolute value) exceeds 1.0, y goes only a when x goes one.Then, the point you hit will jump and it doesn't look like you're drawing a straight line from a human point of view.x is 1/a when the inclination is 1.0, which means y increases only after 2 or 10 dots. y is 1 so the dot column does not break = looks straight.

A2. To support a>1, simply replace x and y (homework)

A3. There are so many curves that I can't explain.

The primitive "microcomputer" does not have a floating point arithmetic circuit, so even if you write a code that uses double like the example shown, it will not perform well.You can use digital diffraction algorithm or Bresenhams algorithm to perform this kind of drawing purely with integer operations.
# I did it a long time ago, it's kind of cool
# Well, modern PCs have graphics support hardware, so you don't have to do this with the CPU at all (it's much faster to leave it to them)

If you need a commentary on Blessenham, please ask another question.


2022-09-30 21:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.