畫圓形演算法
A. c語言用Bresenham演算法畫圓,哪位高手教教,主要是演算法里的內容,謝謝!
的確哈,關鍵在於對delta的理解
可以看到,都是delta=2*(1-radius)這樣的,起作用應該是判斷要畫的點x、y坐標的變化趨勢,先把我注釋了的代碼貼下,加了getch();可以看到畫的過程
-----------------------------------------------------------------
#include<graphics.h>
#include<stdio.h>
void BresenhemCircle(int centerx, int centery, int radius, int color, int type);
void main()
{
int drive=DETECT,mode;
int i,j;
initgraph(&drive,&mode,"");
BresenhemCircle(300,200,100,15,0);
getch();
}
void BresenhemCircle(int centerx, int centery, int radius, int color, int type)
{
int x =type = 0;/*初始橫坐標為原點*/
int y = radius; /*初始縱坐標遠離原點*/
int delta = 2*(1-radius);
int direction;
while (y >= 0)
{
getch();
if (!type)/*執行*/
{
/*在上半圓畫兩點*/
putpixel(centerx+x, centery+y, color);
putpixel(centerx-x, centery+y, color);
/*在下半圓畫兩點*/
putpixel(centerx-x, centery-y, color);
putpixel(centerx+x, centery-y, color);
getch();
}
else/*不執行*/
{
line(centerx+x, centery+y, centerx+x, centery-y);
line(centerx-x, centery+y, centerx-x, centery-y);
getch();
}
/*以下代碼設置下次四點的位置,圓是對稱的,且此方法相當於同時畫四個圓弧
觀察右上方圓弧可知,前一半是x增的要快些,後一半是y減的快些*/
if (delta < 0)
{
if ((2*(delta+y)-1) < 0)
direction = 1; /*選擇橫向加*/
else
direction = 2;
}
else if(delta > 0)
{
if ((2*(delta-x)-1) > 0)
direction = 3; /*選擇縱向減*/
else
direction = 2;
}
else
direction=2;
switch(direction)
{
case 1:
x++;/*只橫坐標遠離原點*/
delta += (2*x+1); /*小執行到這,所以加*/
break;
case 2:
x++;
y--;/*橫向遠離,同時縱向靠近*/
delta += 2*(x-y+1); /*即(2*x+1)+(-2*y+1)*/
break;
case 3:
y--;/*只縱坐標靠近原點*/
delta += (-2*y+1); /*大執行到這,所以減*/
break;
}
}
}
B. matlab如何畫圓
使用MATLAB畫圓的方法有兩種,分別如下:
1、啟動MATLAB,新建腳本,輸入以下代碼:
close all; clear all; clc
r=2; theta=0:pi/100:2*pi;
x=r*cos(theta); y=r*sin(theta);
rho=r*sin(theta);
figure(1)
plot(x,y,'-')
hold on; axis equal
fill(x,y,'c')
figure(2)
h=polar(theta,rho);
set(h,'LineWidth',2)
C. 怎樣用C語言畫圓
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
char arg[50]={0};
arg[0]= '\ " ';
strcpy(arg+1,argv[0]);
int len=int(strlen(arg));
arg[len]= '\ " ';
HWND hWnd=FindWindow(NULL,arg); //找到程序運行窗口的句柄
HDC hDC=GetDC(hWnd);//通過窗口句柄得到該窗口的設備場境句柄
HPEN hPen,hOldPen; //畫筆
int i=0;
for(;i <500;++i)
SetPixel(hDC,10+i,10+i,0x0000ff);//用畫點的辦法畫一根線,最後一個參數是顏色(32位)
hPen=CreatePen(PS_SOLID,2,0x00ff00);//生成綠色畫筆
hOldPen=(HPEN)SelectObject(hDC,hPen);//把畫筆引入設備場境
MoveToEx(hDC,20,50,NULL); //設置畫線起點
LineTo(hDC,520,550); //畫到終點
Arc(hDC,100,100,300,300,350,500,350,500);//畫圓
SelectObject(hDC,hOldPen);
ReleaseDC(hWnd,hDC);
//下面是對比,表明它確實是控制台程序
printf( "hello console ");
system( "pause ");
return 0;
}
D. 怎樣畫圓
是不是編程題?
#include <windows.h>
#include <stdio.h>
#include <math.h>
typedef struct
{
HWND *lphWnd;
DWORD dwExStyle; // extended window style
LPCTSTR lpClassName; // pointer to registered class name
LPCTSTR lpWindowName; // pointer to window name
DWORD dwStyle; // window style
int x; // horizontal position of window
int y; // vertical position of window
int nWidth; // window width
int nHeight; // window height
HMENU hMenu; // handle to menu, or child-window identifier
}childstru;
HANDLE hInst;
HFONT hFont;
HWND MainhWnd,button1,button2,button3,button4;
HDC mDc;
HBITMAP hbmp;
char *titletext="兩種畫圓演算法",*classname="HeLiang";
BOOL InitApplication(HANDLE hInstance);
BOOL InitInstance(HANDLE hInstance,int nCmdShow);
//------------------------------------------------
childstru CldWin[]=
{
{
&button1,
0,// extended window style
"Button",// pointer to registered class name
"Midpoint",// pointer to window name
WS_VISIBLE| WS_CHILD |BS_PUSHBUTTON,// window style
0,// horizontal position of window
0,// vertical position of window
80,// window width
21,// window height
NULL,// handle to menu, or child-window identifier
},
{
&button2,
0,// extended window style
"Button",// pointer to registered class name
"Bresenham",// pointer to window name
WS_VISIBLE| WS_CHILD |BS_PUSHBUTTON,// window style
0,// horizontal position of window
0,// vertical position of window
80,// window width
21,// window height
NULL,// handle to menu, or child-window identifier
},
{
&button3,
0,// extended window style
"Button",// pointer to registered class name
"Clear",// pointer to window name
WS_VISIBLE| WS_CHILD |BS_PUSHBUTTON,// window style
0,// horizontal position of window
0,// vertical position of window
80,// window width
21,// window height
NULL,// handle to menu, or child-window identifier
},
{
&button4,
0,// extended window style
"Button",// pointer to registered class name
"Line",// pointer to window name
WS_VISIBLE| WS_CHILD |BS_PUSHBUTTON,// window style
0,// horizontal position of window
0,// vertical position of window
80,// window width
21,// window height
NULL,// handle to menu, or child-window identifier
},
{
NULL,
0,// extended window style
NULL,// pointer to registered class name
NULL,// pointer to window name
0,// window style
0,// horizontal position of window
0,// vertical position of window
0,// window width
0,// window height
NULL,// handle to menu, or child-window identifier
}
};
//------------------------------------------------
void __fastcall MidLine(HDC hDc,int x0,int y0,int x1,int y1,COLORREF color)
{
int a,b,dt1,dt2,d,x,y,ystp=1;
if(abs(x1-x0)>abs(y1-y0))
{
if(x0>x1)
{
x=x0;x0=x1;x1=x;
y=y0;y0=y1;y1=y;
}
a=y0-y1;
b=x1-x0;
if(a>0){a=-a;ystp=-1;}else ystp=1;
d=(a<<1)+b;
dt1=a<<1;
dt2=(a+b)<<1;
x=x0;
y=y0;
SetPixel(hDc,x,y,color);
while(x<x1)
{
if(d<0)
{
x++;y+=ystp;d+=dt2;
}else
{
x++;
d+=dt1;
}
SetPixel(hDc,x,y,color);
}
}else
{
if(y0>y1)
{
x=x0;x0=x1;x1=x;
y=y0;y0=y1;y1=y;
}
a=x0-x1;
b=y1-y0;
if(a>0){a=-a;ystp=-1;}else ystp=1;
d=(a<<1)+b;
dt1=a<<1;
dt2=(a+b)<<1;
x=x0;
y=y0;
SetPixel(hDc,x,y,color);
while(y<y1)
{
if(d<0)
{
y++;x+=ystp;d+=dt2;
}else
{
y++;
d+=dt1;
}
SetPixel(hDc,x,y,color);
}
}
}
//------------------------------------------------
void __fastcall cirpixel(HDC hDc,int x0,int y0,int x,int y,COLORREF color)
{
SetPixel(hDc,x0+x,y0+y,color);
SetPixel(hDc,x0+y,y0+x,color);
SetPixel(hDc,x0-x,y0+y,color);
SetPixel(hDc,x0+y,y0-x,color);
SetPixel(hDc,x0+x,y0-y,color);
SetPixel(hDc,x0-y,y0+x,color);
SetPixel(hDc,x0-x,y0-y,color);
SetPixel(hDc,x0-y,y0-x,color);
}
//------------------------------------------------
void __fastcall bpixel(HDC hDc,int x0,int y0,int x,int y,COLORREF color)
{
SetPixel(hDc,x0+x,y0+y,color);
SetPixel(hDc,x0-x,y0+y,color);
SetPixel(hDc,x0+x,y0-y,color);
SetPixel(hDc,x0-x,y0-y,color);
}
//------------------------------------------------
void __fastcall BresenhamCircle(HDC hDc,int x0,int y0,int r,COLORREF color)
{
int x,y,dt,dt1,dt2,dr;
x=0;y=r;dt=2-r-r;
while(y>=0)
{
bpixel(hDc,x0,y0,x,y,color);
if(dt<0)
{
dt1=dt+dt+y+y-1;
if(dt1<=0)dr=1;
else dr=2;
}
else if(dt>0)
{
dt2=dt+dt-x-x-1;
if(dt2<=0)dr=2;
else dr=3;
}else dr=2;
switch(dr)
{
case 1: x++;dt+=x+x+1;
break;
case 2: x++;y--;dt+=x+x-y-y-1;
break;
case 3: y--;dt+=1-y-y;
break;
}
}
}
//------------------------------------------------
void __fastcall MidpointCircle(HDC hDc,int x,int y,int r,COLORREF color)
{
int xx,yy,dtx,dty,d;
xx=0;yy=r;dtx=3,dty=2-r-r;d=1-r;
cirpixel(hDc,x,y,xx,yy,color);
while(xx<yy)
{
if(d<0)
{
d+=dtx;dtx+=2;xx++;
}else
{
d+=dtx+dty;dtx+=2;dty+=2;xx++;yy--;
}
cirpixel(hDc,x,y,xx,yy,color);
}
}
//------------------------------------------------
long PASCAL MainWnProc(HWND hWnd,unsigned int Message,unsigned int wParam,long lParam)
{
static HANDLE hDc;
RECT rect;
PAINTSTRUCT ps;
WNDPROC prevWinProc;
int i;
switch(Message)
{
case WM_CREATE:
SendMessage(hWnd,WM_SETFONT,(WPARAM)hFont,MAKELPARAM(true, 0));
i=0;
while(CldWin[i].lphWnd!=NULL)
{
*(CldWin[i].lphWnd)=CreateWindowEx(CldWin[i].dwExStyle,CldWin[i].lpClassName,
CldWin[i].lpWindowName,CldWin[i].dwStyle,CldWin[i].x,CldWin[i].y,
CldWin[i].nWidth,CldWin[i].nHeight,hWnd,CldWin[i].hMenu,hInst,NULL);
SendMessage(*(CldWin[i].lphWnd),WM_SETFONT,(WPARAM)hFont,MAKELPARAM(true, 0));
i++;
}
return 0;
case WM_PAINT:
hDc=BeginPaint(hWnd,&ps);
EndPaint(hWnd,&ps);
return 0;
case WM_SIZE:
GetClientRect(hWnd,&rect);
MoveWindow(button1,rect.right-240,rect.bottom-30,70,21,true);
MoveWindow(button2,rect.right-160,rect.bottom-30,70,21,true);
MoveWindow(button3,rect.right-80,rect.bottom-30,70,21,true);
MoveWindow(button4,rect.right-320,rect.bottom-30,70,21,true);
InvalidateRect(hWnd,NULL,true);
break;
case WM_COMMAND:
if(LOWORD(lParam)==(unsigned short)button1)
{
hDc=GetDC(MainhWnd);
for(int r=200;r>=0;r--)
MidpointCircle(hDc,260,260,r,RGB(255,0,0xff-r));
MidpointCircle(hDc,260,260,250,RGB(255,0,250));
ReleaseDC(MainhWnd,hDc);
}
if(LOWORD(lParam)==(unsigned short)button2)
{
hDc=GetDC(MainhWnd);
for(int r=200;r>=0;r--)
BresenhamCircle(hDc,260,260,r,RGB(255,0xff-r,0));
BresenhamCircle(hDc,260,260,250,RGB(255,250,0));
ReleaseDC(MainhWnd,hDc);
}
if(LOWORD(lParam)==(unsigned short)button4)
{
hDc=GetDC(MainhWnd);
for(int i=0;i<700;i++)
MidLine(hDc,300,200,i,50,RGB(255,0,i*255/700));
for(int i=0;i<700;i++)
MidLine(hDc,300,200,i,350,RGB(i*255/700,255,0));
for(int i=50;i<350;i++)
MidLine(hDc,300,200,0,i,RGB(0,255,i*255/350));
for(int i=50;i<350;i++)
MidLine(hDc,300,200,700,i,RGB(0,i*255/350,255));
ReleaseDC(MainhWnd,hDc);
}
if(LOWORD(lParam)==(unsigned short)button3)
{
InvalidateRect(hWnd,NULL,true);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,Message,wParam,lParam);
}
//------------------------------------------------
int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
LOGFONT LogFont;
MSG msg;
HDC hDc;
hFont=GetStockObject(DEFAULT_GUI_FONT);
if(!hPrevInstance)
if(!InitApplication(hInstance)) return (FALSE);
if(!InitInstance(hInstance,nCmdShow)) return(FALSE);
while(GetMessage(&msg,NULL,NULL,NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DeleteObject(hFont);
return(msg.wParam);
}
//------------------------------------------------
BOOL InitApplication(HANDLE hInstance)
{
WNDCLASS wc;
wc.style=NULL;
wc.lpfnWndProc=MainWnProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(hInstance,"MAINICON");
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=GetSysColorBrush(COLOR_WINDOW);
wc.lpszMenuName=""; //Name of menu resource in .RC file
wc.lpszClassName=classname; //Name used in call to CreateWindow.
return RegisterClass(&wc);
}
//------------------------------------------------
BOOL InitInstance(HANDLE hInstance,int nCmdShow)
{
HWND hWnd;
hInst=hInstance;
hWnd=CreateWindowEx(WS_EX_APPWINDOW,classname,titletext,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
MainhWnd=hWnd;
if(!hWnd)return(FALSE);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return(TRUE);
}
//------------------------------------------------
E. C++畫圓(計算機圖形學)
#include<math.h>
#include<graphics.h>/*預定義庫函數*/
voidcirclePoint(intx,inty)/*八分法畫圓程序*/
{
circle(320x*20,240y*20,3);
circle(320y*20,240x*20,3);
circle(320-y*20,240x*20,3);
circle(320-x*20,240y*20,3);
circle(320-x*20,240y*20,3);
circle(320-x*20,240-y*20,3);
circle(320-y*20,240-x*20,3);
circle(320y*20,240-x*20,3);
circle(320x*20,240-y*20,3);
}
voidMidBresenhamcircle(intr)/*中點Bresenham演算法畫圓的程序*/
{
intx,y,d;
x=0;y=r;d=1-r;/*計算初始值*/
while(x<y)
{circlePoint(x,y);/*繪制點(x,y)及其在八分圓中的另外7個對稱點*/
if(d<0)d=2*x3;/*根據誤差項d的判斷,決定非最大位移方向上是走還是不走*/
else
{d=2*(x-y)5;
y--;
}
x;
delay(900000);
}/*while*/
}
main()
{
inti,j,r,graphmode,graphdriver;
detectgraph(&graphdriver,&graphmode);
initgraph(&graphdriver,&graphmode,"");
printf("中點Bresenhamcircle演算法畫圓的程序 ");/*提示信息*/
printf("注意|r|<=11");
printf(" 輸入半徑值r:");
scanf("%d",&r);
printf("按任意鍵顯示圖形...");
getch();
cleardevice();
setbkcolor(BLACK);
for(i=20;i<=620;i=20)/*使用雙循環畫點函數畫出表格中的縱坐標*/
for(j=20;j<=460;j)
putpixel(i,j,2);
for(j=20;j<=460;j=20)/*使用雙循環畫點函數畫出表格中的橫坐標*/
for(i=20;i<=620;i)
putpixel(i,j,2);
outtextxy(320,245,"0");/*原點坐標*/
outtextxy(320-5*20,245,"-5");circle(320-5*20,240,2);/*橫坐標值*/
outtextxy(3205*20,245,"5");circle(3205*20,240,2);
outtextxy(320-10*20,245,"-10");circle(320-10*20,240,2);
outtextxy(32010*20,245,"10");circle(32010*20,240,2);
outtextxy(320-15*20,245,"-15");circle(320-15*20,240,2);
outtextxy(32015*20,245,"15");circle(32015*20,240,2);
outtextxy(320,240-5*20,"-5");circle(320,240-5*20,2);/*縱坐標值*/
outtextxy(320,2405*20,"5");circle(320,2405*20,2);
outtextxy(320,240-10*20,"-10");circle(320,240-10*20,2);
outtextxy(320,24010*20,"10");circle(320,24010*20,2);
outtextxy(20,10,"Thecenterofthecircleis(0,0)");/*坐標軸左上角顯示提示信息*/
setcolor(RED);/*標記坐標軸*/
line(20,240,620,240);outtextxy(32015*20,230,"X");
line(320,20,320,460);outtextxy(330,20,"Y");
setcolor(YELLOW);
MidBresenhamcircle(r);
setcolor(BLUE);/*繪制圓*/
circle(320,240,r*20);
setcolor(2);
getch();
closegraph();
}
F. c語言中如何用line畫一個圓
#include <math.h>
#include <graphics.h> /*預定義庫函數*/
void circlePoint(int x,int y) /*八分法畫圓程序*/
{
circle(320+x*20,240+y*20,3);
circle(320+y*20,240+x*20,3);
circle(320-y*20,240+x*20,3);
circle(320-x*20,240+y*20,3);
circle(320-x*20,240+y*20,3);
circle(320-x*20,240-y*20,3);
circle(320-y*20,240-x*20,3);
circle(320+y*20,240-x*20,3);
circle(320+x*20,240-y*20,3);
}
void MidBresenhamcircle(int r) /* 中點Bresenham演算法畫圓的程序 */
{
int x,y,d;
x=0;y=r;d=1-r; /* 計算初始值 */
while(x<y)
{ circlePoint(x,y); /* 繪制點(x,y)及其在八分圓中的另外7個對稱點 */
if(d<0) d+=2*x+3; /* 根據誤差項d的判斷,決定非最大位移方向上是走還是不走 */
else
{ d+=2*(x-y)+5;
y--;
}
x++;
delay(900000);
} /* while */
}
main()
{
int i,j,r,graphmode,graphdriver;
detectgraph(&graphdriver,&graphmode); initgraph(&graphdriver,&graphmode," ");
printf("中點Bresenhamcircle演算法畫圓的程序\n"); /*提示信息*/
printf("注意 |r|<=11");
printf("\n輸入半徑值 r:");
scanf("%d",&r);
printf("按任意鍵顯示圖形...");
getch(); cleardevice(); setbkcolor(BLACK);
for(i=20;i<=620;i+=20) /*使用雙循環畫點函數畫出表格中的縱坐標*/
for(j=20;j<=460;j++)
putpixel(i,j,2);
for(j=20;j<=460;j+=20) &n歡迎光臨學網,收藏本篇文章 [1] [2]
$False$
bsp; /*使用雙循環畫點函數畫出表格中的橫坐標*/
for(i=20;i<=620;i++)
putpixel(i,j,2); outtextxy(320,245,"0"); /*原點坐標*/
outtextxy(320-5*20,245,"-5");circle(320-5*20,240,2); /*橫坐標值*/
outtextxy(320+5*20,245,"5");circle(320+5*20,240,2);
outtextxy(320-10*20,245,"-10");circle(320-10*20,240,2);
outtextxy(320+10*20,245,"10");circle(320+10*20,240,2);
outtextxy(320-15*20,245,"-15");circle(320-15*20,240,2);
outtextxy(320+15*20,245,"15");circle(320+15*20,240,2);
outtextxy(320,240-5*20,"-5");circle(320,240-5*20,2); /*縱坐標值*/
outtextxy(320,240+5*20,"5");circle(320,240+5*20,2);
outtextxy(320,240-10*20,"-10");circle(320,240-10*20,2);
outtextxy(320,240+10*20,"10");circle(320,240+10*20,2);
outtextxy(20,10,"The center of the circle is (0,0) "); /*坐標軸左上角顯示提示信息*/
setcolor(RED); /*標記坐標軸*/
line(20,240,620,240); outtextxy(320+15*20,230,"X");
line(320,20,320,460); outtextxy(330,20,"Y");
setcolor(YELLOW);
MidBresenhamcircle(r);
setcolor(BLUE); /*繪制圓*/
circle(320,240,r*20);
setcolor(2);
getch(); closegraph();
}
G. 用C語言寫出畫一個圓形的代碼
可以參考下面的代碼:
#include<math.h>
main()
{
double y;
int x,m;
for(y=10;y>=-10;y--)
{
m=2.5*sqrt(100-y*y);
for(x=1;x<50-m;x++)
printf(" ");
printf("*");
for(;x<50+m;x++)
printf(" ");
printf("* ");
}
}
(7)畫圓形演算法擴展閱讀:
for循表達式為:for(單次表達式;條件表達式;末尾循環體){中間循環體;}。
其中,表示式皆可以省略,但分號不可省略,因為「;」可以代表一個空語句,省略了之後語句減少,即為語句格式發生變化,則編譯器不能識別而無法進行編譯。for循環小括弧里第一個「;」號前為一個為不參與循環的單次表達式。
H. 請問中點bresenham演算法畫圓與bresenham演算法畫圓有區別嗎
Bresenham演算法畫圓:
Bresenham演算法用來畫直線非常方便,但上次也說了,Bresenham演算法也可以用來顯示圓和其他曲線,只需要把直線方程改成圓方程或者其他曲線的方程就行,具體的推理過程就不演示了,大體跟直線的差不多!但由推算的結果可以看出,用Bresenham演算法來畫圓的確是不大明智的做法,要計算的步驟太多,計算速度比專門的畫圓方法慢很多!並且在斜率越大的地方像素的間距就越大,當然我們可以在畫某個像素之前先判斷一下這一點跟前面一點的連線的斜率,然後在適當的時候交換x、y的坐標,但這樣計算量必將增加!
直接給出Bresenham畫圓的代碼:
#include<gl/glut.h>
#include<math.h>
#include<stdio.h>
voiddraw_pixel(intix,intiy)
{
glBegin(GL_POINTS);
glVertex2i(ix,iy);
glEnd();
}
//intinlineround(constfloata){returnint(a+0.5);}
voidBresenham(intx1,inty1,intr,doublea,doubleb,doublec)/*圓心在(x1,y1),半徑為r的圓*/
{
glColor3f(a,b,c);
intdx=r;//intdy=abs(yEnd-y1);
//intp=2*dy-dx;
//inttwoDy=2*dy,twoDyMinusDx=2*dy-2*dx;
intx,y,d1,d2;
/*if(x1>xEnd)
{
x=xEnd;y=yEnd;
xEnd=x1;
}
else
{
x=x1;
y=y1;
}
*/
x=x1;
y=y1+r;
draw_pixel(x1,y1);
draw_pixel(x,y);//起始點裝入幀緩存,起始點是圓的最上面一點,然後按順時針來畫
while(x<=x1+dx)
{
d1=y1+sqrt(pow(r,2)-pow(x-x1,2));/*lower*/
x++;
d2=2*(y1+sqrt(pow(r,2)-pow(x-x1,2)))-2*d1-1;/*lower-upper*/
if(1)
{
y=d1;
draw_pixel(x,y);
draw_pixel(x,2*y1-y);
draw_pixel(2*x1-x,y);
draw_pixel(2*x1-x,2*y1-y);
}
else
{
y++;
//p+=twoDyMinusDx;
draw_pixel(x,y);
}
}
}
voiddisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
Bresenham(250,250,200,0.0,0.0,1.0);
Bresenham(300,250,150,1.0,0.0,0.0);
Bresenham(200,250,150,0.0,1.0,0.0);
//Bresenham(250,300,150,0.8,0.4,0.3);
//Bresenham(250,200,150);
glFlush();
}
voidmyinit()
{
glClearColor(0.8,1.0,1.0,1.0);
//glColor3f(0.0,0.0,1.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0,0.0,500.0);
}
voidmain(intargc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(200.0,200.0);
glutCreateWindow("CG_test_Bresenham_Circleexample");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
以下為程序運行效果:
中點畫圓:
用光柵畫圓的不足在上次已經用實例表示的很明白了,上次畫的那個圓怎麼都不能算滿意,雖然可以通過修改演算法來得到改善,但本來計算步驟就已經很多了,交換坐標重新計算將會大大增加計算機的就是負擔,為此我們採用另一種更加常用的畫圓演算法——中點畫圓演算法,之所以叫做「中點」畫圓演算法是由於它不是像Bresenham演算法那樣所繪像素不是(xk+1,yk)就是(xk+1,yk+1),而是根據這兩個點的中點來判斷是(xk+1,yk)還是(xk+1,yk-1)更接近於圓!
對於給定的半徑r和圓心(x0,y0),我們先計算圓心在原點(0,0)的點,然後將其平移到圓心(x0,y0)處即可,跟Bresenham演算法一樣,我們也可以藉助圓的高度對稱性來減少計算機的計算步驟,在這里我們可以先計算出八分之一圓的像素點,然後根據對稱性繪出其他點。這樣可以大大加快畫圓的速度!
跟光柵化方法一樣,我們還是採用步進的方法來逐點描繪,但這里的決策參數計算方式跟Bresenham不大一樣,設決策參數為p,則:
P=x2+y2-r2
對於任一個點(x,y),可以根據p的符號來判斷點是在圓內還是圓外還是在圓上,這里不多說,假設我們在(xk,yk)處繪制了一個像素,下一步需要確定的是(xk+1,yk)還是(xk+1,yk-1)更接近於圓,在此代入這兩個點的中點來求出決策參數:
Pk=(xk+1)2+(yk-1/2)2-r2
如果Pk<0,則yk上的像素更接近於圓,否則就是yk-1更接近於圓
同理可以推出Pk+1=Pk+2(xk+1)+(yk+12-yk2)-(yk+1-yk)+1
給出一個示例,這個圓比用Bresenham畫出來的好看多了:
#include<glglut.h>
classscreenPt
{
private:
intx,y;
public:
screenPt(){x=y=0;}
voidsetCoords(GLintxCoordValue,GLintyCoordValue)
{
x=xCoordValue;
y=yCoordValue;
}
GLintgetx()const
{
returnx;
}
GLintgety()const
{
returny;
}
voidincrementx(){x++;}
voiddecrementy(){y--;}
};
voiddraw_pixel(intxCoord,intyCoord)
{
glBegin(GL_POINTS);
glVertex2i(xCoord,yCoord);
glEnd();
}
voidcircleMidpoint(GLintxc,GLintyc,GLintradius)
{
screenPtcircPt;
GLintp=1-radius;
circPt.setCoords(0,radius);
voidcirclePlotPoints(GLint,GLint,screenPt);
circlePlotPoints(xc,yc,circPt);
while(circPt.getx()<circPt.gety())
{
circPt.incrementx();
if(p<0)
p+=2*circPt.getx()+1;
else
{
circPt.decrementy();
p+=2*(circPt.getx()-circPt.gety())+1;
}
circlePlotPoints(xc,yc,circPt);
}
}
voidcirclePlotPoints(GLintxc,GLintyc,screenPtcircPt)//描繪八分圓各點
{
draw_pixel(xc+circPt.getx(),yc+circPt.gety());
draw_pixel(xc-circPt.getx(),yc+circPt.gety());
draw_pixel(xc+circPt.getx(),yc-circPt.gety());
draw_pixel(xc-circPt.getx(),yc-circPt.gety());
draw_pixel(xc+circPt.gety(),yc+circPt.getx());
draw_pixel(xc-circPt.gety(),yc+circPt.getx());
draw_pixel(xc+circPt.gety(),yc-circPt.getx());
draw_pixel(xc-circPt.gety(),yc-circPt.getx());
}
voiddisplay()
{
//screenPtPt;
glClear(GL_COLOR_BUFFER_BIT);
circleMidpoint(250,250,200);
glFlush();
}
voidmyinit()
{
glClearColor(0.8,1.0,1.0,1.0);
glColor3f(0.0,0.0,1.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0,0.0,500.0);
}
voidmain(intargc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(200.0,200.0);
glutCreateWindow("CG_test_中點畫圓example");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
運行效果:
I. 急求C語言中怎樣畫一個圓形圖形
#include<stdlib.h>
#include<graphics.h>
int main()
{
int gdriver, gmode, i;
gdriver=DETECT;
initgraph(&gdriver, &gmode, "c:\\caic\\bgi");
setbkcolor(BLUE);/*設背景顏色*/
cleardevice();
setcolor(12); /*設置作圖顏色*/
arc(100, 100, 0, 360, 50); /*畫一個以(100,100)為圓心,50為半徑,從0度到360度的圓*/
getch();
closegraph();
return 0;
}
/*turbo c2.0下編譯通過*/
J. 什麼是「直角坐標畫圓」演算法
就是根據圓心在直角坐標系裡的位置和半徑,依次計算
圓周
各點
坐標,並依次連接
這些點
「畫圓」
圓方程:
(x-x0)*(x-x0)
+
(y-y0)*(y-y0)
=
R*R
x0,y0
是圓心座標,R
是
半徑。
x
范圍是
x0-R
到
x0+R
y
范圍是
y0-R
到
y0+R
或:
(x-x0)
=
R
cos(A);
(y-y0)
=
R
sin(A);
你可以給一個步長(例如dA=1度),計算圓周
各點
的
坐標。
這就是
「直角坐標畫圓」演算法。