Skip to content Skip to sidebar Skip to footer

bresenham line drawing algorithm 3d

Original address: https://www.cnblogs.com/llsq/p/7506597.html

Programming surroundings: codeblocks + EGE library

The part used: putpixel (int x1, int y1, int color) Utilize a sure color to highlight a coordinate betoken.

These two algorithms are used to draw a directly line on the calculator, so why don't we directly use the direct line equation to separately point and so highlight it, considering each coordinate bespeak in the figurer is an integer, and the straight line is It 'southward a combination of pixels, then it 'south good to round the coordinate points to integers, indeed, this is a method, but the rounding of floating-point numbers in the computer will make the calculation efficiency worse, and so the real These two methods are used when drawing straight lines.

1. Midpoint cartoon method

Only consider the situation when the slope of the direct line | k | <one, assuming that in that location is now a straight line (x1, y1, x2, y2), and then the first point must be (x1, y1) no doubt, the x coordinate of the next point For x1 + 1, the y coordinate is either y1 or y1 +. The fundamental is that each time a bespeak is taken, is the previous y1 or y1 + 1 taken? At this fourth dimension, the betoken closest to the point on the line must be taken, and the midpoint is used to determine which point to accept. We will The midpoint is substituted into the straight line d = F (x1 + 1, y1 + 0.five) = a * (x1 + 1) + b * (y1 + 0.5) + c.

(1) If the straight line d> = 0, the point on the side is (x1 + 1, y1). (2) If the straight line d <0, the upper betoken is taken equally (x1 + one, y1 + i).

Its bodily procedure is to gauge where the adjacent indicate is based on the previous bespeak each time, and then highlight it, simply it is likewise troublesome to substitute the direct line equation calculation for each judgment. Nosotros volition substitute these two situations into the straight line separately. The law can exist plant in the equation:

(i) When the direct line is> = 0, d1 = d + a is obtained subsequently dissolution;

(2) When the directly line is <0, d2 = d + a + b will exist resolved after the solution;

(three) Initial value d0 = a + 0.5b.

In other words, the increment of each time is either a or a + b, so it is much simpler to judge in this mode, because nosotros only approximate its positive and negative every time. So multiply the equation by two at the same time, and convert the floating-point number 0.v into an integer, and then that the hardware operation is undoubtedly faster.

Code:

                          ane            #include <iostream>                          2            #include <graphics.h>                          iii            using            namespace                          std;                                      4            //            Midpoint line drawing                          v            void            line1(int            x1,int            y1,int            x2,int                          y2){                                      6                          7            int                          10,y,d0,d1,d2,a,b;                                      8            y=y1;                                      9            a=y1-y2;            //            Algorithm of a in straight line equation            10            b=x2-x1;            //            Algorithm of b in straight line equation            11            d0=2*a+b;            //            Incremental initial value            12            d1=2*a;            //            Increase when> = 0            13            d2=two*(a+b);            //            Increment when <0            fourteen            for(x=x1;x<=x2;x++){                        fifteen            putpixel(x,y,GREEN);            //            Brighten            16            if(d0<0            ){                        17            y++;                        18            d0+=d2;                        19            }else            {                        20            21            d0+=d1;                        22                          }                        23            24                          }                        25            }                        26            //            Bresenham line drawing algorithm            27            void            line2(int            x1,int            y1,int            x2,int                          y2){                        28            29            int                          x,y,dx,dy,d;                        xxx            y=y1;                        31            dx=x2-x1;                        32            dy=y2-y1;                        33            d=2*dy-dx;            //            Initial value of increment d            34            for(10=x1;x<=x2;x++){                        35            putpixel(x,y,Light-green);            //            Brighten            36            if(d<0            ){                        37            d+=two*dy;                        38            }else            {                        39            y++;                        xl            d+=two*dy-2*dx;                        41                          }                        42            43            44            45                          }                        46            47            }                        48            int                          main()                        49            {                        50            initgraph(640,480);            //            Plow on EGE initialization            51            line1(200,160,400,400);            //            Line drawing            52            getch();            //            Expect for user operation            53            closegraph();            //            Close graphics            54            return            0            ;                        55            }

2. Bresenham line drawing algorithm

The thought of ​​this line drawing algorithm is the same every bit that of midpoint cartoon. Information technology is but when judging which betoken is taken, instead of looking at whether it is in a higher place or below the midpoint, it subtracts the distance between these 2 points and the bespeak on the line. Judging whether it is positive or negative, if the altitude from the lower point to the actual indicate of the directly line is far from d1-d2> = 0, and so take the upper point y1 + one, which is also substituted into the straight line solution to depict the following conclusion:

(i) When d1-d2 <0, d = d + 2 * dy.

(2) When d1-d2> = 0, d = d + 2 * dy-2 * dx.

The initial value of (three) d is d = 2 * dy-dx.

The code is shown above. Run screenshot as follows:

Sharing brings fun to learning, come on, come on!
Support original creation.

bradleyrusoody.blogspot.com

Source: https://programmersought.com/article/72063518731/

Postar um comentário for "bresenham line drawing algorithm 3d"