本文目錄一覽:
c語言,求輸出一個圓的圖形的程序
#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(xy)
{ circlePoint(x,y); /* 繪製點(x,y)及其在八分圓中的另外7個對稱點 */
if(d0) 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) /*使用雙循環畫點函數畫出表格中的橫坐標*/
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();
}
C語言簡單圖形題
/*編寫一程序P211.C實現以下功能
根據輸入的n在屏幕上顯示對應的以#組成的菱形圖案。編程可用素材:printf(“Please input n: “)…。
程序的運行效果應類似地如圖1和圖2所示,圖1中的1和圖2中的5是從鍵盤輸入的內容。
*/
#include stdio.h
int main(void)
{
int n, i=1, j, m,k=1;
printf(“Please input n: “);
scanf(“%d”, n);
do
{
for (j=i;j = n;j++)
{
printf(” “);
}
for (m=1;m = 2*i-1;m++)
{
printf(“#”);
}
printf(“\n”);
i++;
}while (i = n+1);
do
{
for (j=0;jk;j++)
{
printf(” “);
}
for (m=1;m = 2*n-1;m++)
{
printf(“#”);
}
printf(“\n”);
n–;k++;
}while (n);
return 0;
}
//德昂當同學的說法確實是你的錯誤所在,這就是你成功的一半了。
/*其實筆者建議你將上面的那個圖抽象成為一個線性規劃的問題:建立一個坐標系,之後提取出來更方便的數學關係式,例如上面的方程則是:| i-j |=n;和| i+j |n;之後將i,j分別當做兩個行列變量進行解決。請作者予以嘗試!*/
如何用C語言畫基本圖形
下面舉一個用drawpoly()函數畫箭頭的例子。#include
#include
int main()
{
int gdriver, gmode, i;
int arw[16]={200,102,300,102,300,107,330,br/100,300,93,300,98,200,98,200,102};
gdriver=DETECT;
initgraph(gdriver, gmode, “c:\\caic\\bgi”);
setbkcolor(BLUE);
cleardevice();
setcolor(12); /*設置作圖顏色*/
drawpoly(8, arw); /*畫一箭頭*/
getch();
closegraph();
return 0;
}
設定線型函數
在沒有對線的特性進行設定之前,TURBO C 用其默認值,即一點寬的實線,但TURBO C 也提供了可以改變線型的函數。線型包括:寬度和形狀。其中寬度只有兩種選擇:一點寬和三點寬。而線的形狀則有五種。下面介紹有關線型的設置函數。
void far setlinestyle(intlinestyle,unsigned upattern,int thickness); 該函數用來設置線的有關信息,其中linestyle是線形狀的規定,
見下表:
有關線的形狀(linestyle)
━━━━━━━━━━━━━━━━━━━━━━━━━
符號常數 數值 含義
─────────────────────────
SOLID_LINE 0 實線
DOTTED_LINE 1 點線
CENTER_LINE 2 中心線
DASHED_LINE 3 點畫線
USERBIT_LINE 4 用戶定義線
━━━━━━━━━━━━━━━━━━━━━━━━━
有關線寬(thickness)
thickness是線的寬度,見下表。
━━━━━━━━━━━━━━━━━━━━━━━━━
符號常數 數值 含義
─────────────────────────
NORM_WIDTH 1 一點寬
THIC_WIDTH 3 三點寬
━━━━━━━━━━━━━━━━━━━━━━━━━
對於upattern,只有linestyle選USERBIT_LINE 時才有意義 (選其它線型,uppattern取0即可)。此進uppattern的16位二進制數的每一位代表一個象元,如果那位為1,則該象元打開,否則該象元關閉。 void far getlinesettings(struct linesettingstypefar *lineinfo);該函數將有關線的信息存放到由lineinfo 指向的結構中,表中linesettingstype的結構如下:
struct linesettingstype
{
int linestyle;
unsigned upattern;
int thickness;
}
用c語言求多種圖形中任何一種圖形的面積。
#include stdio.h
#include math.h
double trianglearea( float a, float b, float c );
double squarearea( float a, float b );
double roundarea( float r );
int main()
{
int index = 0;
float a, b, c, r;
double S;
while(1)
{
printf(“請輸入圖的類型,三角形請輸入1,矩形請輸入2,圓形請輸入3。按0退出\n”);
printf(“請輸入圖形序號:”);
scanf(“%d”, index);
if(index == 0)
break;
else if(index == 1)
{
printf(“輸入三角形三邊長:”);
scanf(“%f %f %f”, a, b, c);
S = trianglearea(a, b, c);
}
else if(index == 2)
{
printf(“輸入矩形的長和寬:”);
scanf(“%f %f”, a, b);
S = squarearea(a, b);
}
else if(index == 3)
{
printf(“%輸入圓形的半徑:”);
scanf(“%f”, r);
S = roundarea(r);
}
else
;
printf(“所求面積為%lf\n\n”, S);
}
printf(“\nbye bye\n”);
return 0;
}
double trianglearea( float a, float b, float c )
{
double p = 0;
if( !(a+bc a+cb b+ca) )
{
printf(“這三條邊無法組成三角形。\n”);
return 0;
}
p = (a+b+c) / 2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
double squarearea( float a, float b )
{
return a*b;
}
double roundarea( float r )
{
return 3.14*r*r;
}
C語言輸出一個菱形圖案(有程序)
菱形,就是如下所示的圖形,總行數與總列數相等:
寫一個程序,根據用戶輸入的總行數,打印出菱形。
這個題目主要是找出規律,考察讀者的邏輯思維。
你可以從第一行開始,遍歷所有的列,也可以從第一列開始,遍歷所有的行。
下面的程序從第一行開始,遍歷所有的列。
設菱形的總行數為line,總列數為column,當前行為i,當前列為j。上半部分與下半部分的規律不一樣,應該分開討論。
我們着眼於星號(*),思考什麼條件下輸出星號,總結出如下的規律。
1) 對於上半部分(包括中間一行),當前行與當前列滿足如下關係輸出星號:
j=(column+1)/2-(i-1) (column+1)/2-(i-1)為第i行最左邊的星號
j=(column+1)/2+(i-1) (column+1)/2+(i-1)為第i行最右邊的星號
2) 對於下半部分,當前行與當前列滿足如下關係輸出星號:
j=(column+1)/2-(line-i) (column+1)/2-(line-i)為第i行最左邊的星號
j=(column+1)/2+(line-i) (column+1)/2+(line-i)為第i行最右邊的星號
不滿足上述條件,則輸出空格。
於是寫出如下的代碼:純文本複製
#include stdio.h#include stdlib.hint main(){int line; // 菱形總行數int column; // 菱形總列數int i; // 當前行int j; // 當前列printf(“請輸入菱形的行數(奇數):”);scanf(“%d”, line);if(line%2==0){ // 判斷是否是奇數printf(“必須輸入奇數!\n”);exit(1);}column = line; // 總行數和總列數相同for(i=1; i=line; i++){ // 遍歷所有行if(i(line+1)/2+1){ // 上半部分(包括中間一行)for(j=1; j=column; j++){ // 遍歷上半部分的所有列if( (column+1)/2-(i-1)=j j=(column+1)/2+(i-1) ){printf(“*”);}else{printf(” “);}}}else{ // 下半部分for(j=1; j=column; j++){ // 遍歷下半部分的所有列if( (column+1)/2-(line-i)=j j=(column+1)/2+(line-i) ){printf(“*”);}else{printf(” “);}}}printf(“\n”);}return 0;}
運行結果:請輸入菱形的行數(奇數):11
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/311378.html