September 08 Admission, July 12 graduation, ended my happy and rich university life in the Software Institute. This series is a review of the four-year professional curriculum study, indexed in: http://blog.csdn.net/xiaowei_cqu/article/details/7747205
characteristics of the Circle
A circle is defined as a set of points to the given central position (XC,YC) distance of R. The circle at the origin of the center has four axis x=0,y=0, x=y and X=-y. If a point (X,y) is known on a circular arc, the other 7 points about the four symmetric axes can be obtained, which is called eight-point symmetry. Therefore, as long as the scanning transformation of one-eighth arc, we can find the entire arc of the pixel set.
An algorithm that displays eight symmetric points on an arc:
void circlepoints (int x,int y,int color)
{putpixel (x,y,color); Putpixel (Y,x,color);
Putpixel (-x,y,color); Putpixel (Y,-x,color);
Putpixel (X,-y,color); Putpixel (-y,x,color);
Putpixel (-x,-y,color); Putpixel (-y,-x,color);
}
Algorithm of midpoint drawing circle
The result of our constructor F (x,y) =X2+Y2-R2 is f (x,y) = 0 for points on the Circle, and F (x,y) >0 for dots outside of the circle, for Point F (x,y) <0 within the circle. As with the midpoint line method, the construction of discriminant:
D=f (M) =f (xp+1,yp-0.5) = (xp+1) hydrate (yp-0.5) 2-R2
If the d<0, then should take P1 as the next pixel, and the next pixel discriminant is:
D=f (xp+2,yp-0.5) = (xp+2) (yp-0.5) 2-r2=d+2xp+3
If the d≥0, then should take P2 for the next pixel, and the next pixel discriminant is
D=f (xp+2,yp-1.5) = (xp+2) hydrate (yp-1.5) 2-r2=d+2 (XP-YP) +5
The first pixel we're talking about here is (0,r), and the initial value of discriminant D is:
D0=f (1,r-0.5) =1.25-r
"Algorithm Flowchart"
"Algorithmic Code"
void Paintarea::d rawcirclemiddle (qpainter &painter,const qpoint¢er, int r)
{
int x,y,deltax,deltay,d;
X=0;y=r;
Deltax=3;deltay=2-3-3;d=1-r;
while (X<y)
{
if (d<0)
{
d+=deltax;
deltax+=2;
x + +;
}
else
{
d+= (deltax+deltay);
deltax+=2;deltay+=2;
x++;y++;
}
Painter.drawpoint (Center.x () +x,center.y () +y);
Painter.drawpoint (Center.x () +x,center.y ()-y);
Painter.drawpoint (Center.x ()-x,center.y () +y);
Painter.drawpoint (Center.x ()-x,center.y ()-y);
Painter.drawpoint (Center.x () +y,center.y () +x);
Painter.drawpoint (Center.x () +y,center.y ()-X);
Painter.drawpoint (Center.x ()-y,center.y () +x);
Painter.drawpoint (Center.x ()-y,center.y ()-X);
}
Bresenham algorithm for drawing circle
Thought see Bresenham of straight Lines "computer graphics" basic graphic elements: line generation algorithm "algorithm flowchart"
"Algorithmic Code"
void Paintarea::d Rawcirclebresenham (qpainter &painter,const qpoint¢er, int r)
{
int x,y,delta,delta1, delta2,direction;
X=0;y=r;
delta=2* (1-r);
while (y>=0)
{
painter.drawpoint (x,y);
if (delta<0)
{ delta1=2* (delta+y)-1;
if (delta1<=0) direction=1; else direction=2;
else if (delta>0)
{ delta2=2* (delta-x)-1;
if (delta2<=0) direction=2; else direction=3;
else direction=2;
switch (direction)
{Case 1:
x + +;d elta+=2*x+1;
Case 2:
x + + y--; delta+=2* (x-y+1);
Case 3:
y--;d elta+= ( -2*y+1); break;
}
}
}
An algorithm for generating elliptic arcs
The basic same ARC algorithm, just the equation becomes complex f (x,y) = (BX) ^2+ (ay) ^2-(AB) ^2.
Symmetry: 4 points symmetrical, draw first quadrant
Paragraph by: Slope to a point
Upper Arc:
Next Arc:
"Ellipse midpoint algorithm flowchart"
"Algorithmic Code"
void Paintarea::d rawellipsemiddle (qpainter &painter,int xcenter,int ycenter, int Rx, int Ry) {int rx2=rx*rx;
int ry2=ry*ry;
int tworx2=2*rx2;
int Twory2=2*ry2;
int p,x=0,y=ry,px=0,py=tworx2*y;
void Ellipseplotpoints (Qpainter&,int,int,int,int);
Ellipseplotpoints (Painter,xcenter,ycenter,x,y);
Region1 P=round (ry-(rx2*ry) + (0.25*rx2));
while (px<py) {x + +;
Px+=twory2;
if (p<0) p+=ry2+px;
else{y--;
py-=tworx2;
P+=ry2+px-py;
} ellipseplotpoints (Painter,xcenter,ycenter,x,y);
}//region2 P=round (ry2* (x+0.5) * (x+0.5) +rx2* (y-1) * (y-1)-rx2*ry2);
while (y>0) {y--;
py-=tworx2;
if (p>0) p+=rx2-py;
else{x + +;
Px+=twory2;
P+=RX2-PY+PX;
} ellipseplotpoints (Painter,xcenter,ycenter,x,y); } void Ellipseplotpoints (Qpainter &painter,int xcenter,int yCenter,int X,int y) {painter.drawpoint (xcenter+x,ycenter+y);
Painter.drawpoint (Xcenter-x,ycenter+y);
Painter.drawpoint (XCENTER+X,YCENTER-Y);
Painter.drawpoint (XCENTER-X,YCENTER-Y);
}
Software screenshot
This drawing software is written in Qt, I will write another introduction to the programming structure, to be continued ~
Reprint Please indicate the source: http://blog.csdn.net/xiaowei_cqu/article/details/7909607