❶ 用c語言畫矩形
每行起始和結束字元均是你的第3個參數
矩形第1行和最後一行中間是第3個參數,其他行根據第4個參數決定是空格或者第3個參數
程序可以這樣寫:
...
for ( m=0;m<a;m++ )
{
printf("%c",c); //第1列
if ( m==0 || m==a-1 ) //第1行和最後一行
for ( n=1;n<b-1;n++ ) printf("%c",c);
else //中間的行
for ( n=1;n<b-1;n++ ) if ( d==0 ) printf(" "); else printf("%c",c); //空心或否
printf("%c\n",c); //最後1列
}
或者可以寫:
for ( m=0;m<a;m++ )
{
printf("%c",c); //第1列
if ( m==0 || m==a-1 || d!=0) for ( n=1;n<b-1;n++ ) printf("%c",c);
else for ( n=1;n<b-1;n++ ) printf(" ");
printf("%c\n",c); //最後1列
}
❷ 各位大大,我本來想用C語言畫一個長方形,但是我的那麼多FOR語句為什麼只執行第一條啊.源代碼如下
第一次循環之後L_B的值已經大於等於R_B了,導致後面幾個for循環判斷出了問題,重新初始化一下L_B
❸ C語言怎樣畫正方體
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
main()
{
int r,q,w,color;
int gdriver=DETECT,gmode; /*設置圖形驅動*/
int Bresenham_Ciecle(); /*定義子函數*/
printf("please input the centre of a circle x0,y0\n");
scanf("%d,%d",&q,&w);
if(q>=320||q<=-320||w<=-250||w>=250)
{printf("please input the centre of a circle again x0,y0\n");
scanf("%d,%d",&q,&w);} /*輸入圓心位置越界輸出信息*/
printf("please input the numble of radius r=");
scanf("%d",&r);
if(r<=0||r>=500)
{
printf("r is error,r>0,please input the numble of r=");
scanf("%d",&r);
}/*輸入半徑*/
printf("please input the numble of color=");
scanf("%d",&color);
initgraph(&gdriver,&gmode,"D:\\TC");
setcolor(color);/*設置圖形顏色*/
Bresenham_Ciecle(q,w,r,color); /*繪圖*/
getch();
}
Bresenham_Ciecle(int q,int w,int r,int color)
{
int x,y,t,v,delta,delta1,delta2,direction;
char buffera[20];
char bufferb[20];
t=getmaxx()/2;
v=getmaxy()/2; /*選定圓心*/
sprintf(buffera, "(%d,%d)", q,w); /*列印圓心坐標*/
sprintf(bufferb, "(0,0) R=%d",r); /*列印原點坐標及半徑*/
moveto(t,v+4);
outtext(bufferb); /*圓點坐標定位輸出*/
q=q+t;w=v-w;
moveto(q,w);
outtext(buffera);/*原點坐標定位輸出*/
line(1,v,2*t,v);
line(t,2*v,t,1);/*畫坐標*/
x=q; y=r+w;
line(q, w, x, y); /*畫半徑*/
delta=2*(1-r);
while(y-w>=0)
{
putpixel(x,y,color); /*右下方1/4個圓*/
putpixel(2*q-x,y,color);/*右上方1/4個圓(從右下方1/4個圓對稱復制)*/
putpixel(x,2*w-y,color);/*左下方1/4個圓(從右下方1/4個圓對稱復制)*/
putpixel(2*q-x,2*w-y,color);/*左上方1/4個圓(從右下方1/4個圓對稱復制)*/
if(delta<0)
{
delta1=2*(delta+y-w)-1;
if(delta1<=0) direction=1;
else direction=2;
}
else if(delta>0)
{
delta2=2*(delta-x+q)-1;
if(delta2<=0) direction=2;
else direction=3;
}
else
direction=2;
switch(direction)
{
case 1: x++;
delta+=2*(x-q)+1;
break;
case 2: x++;
y--;
delta+=2*(1+x-y-q+w);
break;
case 3: y--;
delta+=-2*(y-w)+1;
break;
}
}
}
❹ c語言中,line()畫在bar()上,為什麼會有陰影
我曾經用滑鼠驅動在DOS下寫過類似PAINT的軟體,你可以嘗試一下。
❺ 如何用c語言編程用虛線畫一個長方形然後長方形內部有一個由"*"組成的三角形
最笨的方法 按照最長的那行定義每行的長度 然後其他行用空格填充
❻ C語言如何畫圖
framebuffer(幀緩沖)。
幀的最低數量為24(人肉眼可見)(低於24則感覺到畫面不流暢)。
顯卡與幀的關系:由cpu調節其數據傳輸速率來輸出其三基色的配比。
三基色:RGB(紅綠藍)。
在沒有桌面和圖形文件的系統界面,可以通過C語言的編程來實現在黑色背景上畫圖!
用下面的代碼,在需要的地方(有注釋)適當修改,就能畫出自己喜歡的圖形!
PS:同樣要編譯運行後才能出效果。
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <stdlib.h>
#define RGB888(r,g,b) ((r & 0xff) <<16 | (g & 0xff) << 8 | (b & 0xff))
#define RGB565(r,g,b) ((r & 0x1f) <<11 | (g & 0x3f) << 5 | (b & 0x1f))
int main()
{
int fd = open("/dev/fb0", O_RDWR);
if(fd < 0){
perror("open err. ");
exit(EXIT_FAILURE);
printf("xres: %d ", info.xres);
printf("yres: %d ", info.yres);
printf("bits_per_pixel: %d ", info.bits_per_pixel);
size_t len = info.xres*info.yres*info.bits_per_pixel >> 3;
unsigned long* addr = NULL;
addr = mmap(NULL, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
if(addr == (void*)-1){
perror("mmap err. ");
❼ 用C語言畫矩形
/* 旋轉的立體橢球 */
#include <graphics.h>
#include <math.h>
#include <conio.h>
#define PI 3.14159
#define T PI/180
#define NN 36
#define DT PI/NN
#define DIST 0.8
#define R 190
static int n=1;
void trans(t,s)
float t[3];
float s[3][3];
{
s[0][0]=cos(t[1])*cos(t[2]); s[0][1]=cos(t[1])*sin(t[2]);
s[0][2]=-sin(t[1]);
s[1][0]=sin(t[0])*sin(t[1])*cos(t[2])-cos(t[0])*sin(t[2]);
s[1][1]=sin(t[0])*sin(t[1])*sin(t[2])+cos(t[0])*cos(t[2]);
s[1][2]=sin(t[0])*cos(t[1]);
s[2][0]=cos(t[0])*sin(t[1])*cos(t[2])+sin(t[0])*sin(t[2]);
s[2][1]=cos(t[0])*sin(t[0])*sin(t[2])-sin(t[0])*cos(t[2]);
s[2][2]=cos(t[0])*cos(t[1]);
}
void draw(m,da,db)
float m[3][3],da,db;
{
float f[3],f0 , x0,y0,x1,y1,x2,y2;
f[0]=R*sin(da)*cos(db); f[1]=R*sin(da)*sin(db);
f[2]=R*cos(da);
f0=f[0]*m[0][2]+f[1]*m[1][2]+f[2]*m[2][2];
if(f0<=0) n=1;
else
{
x0=300.0;y0=165.0;
x2=(m[0][0]*f[0]+m[1][0]*f[1]+m[2][0]*f[2])+x0;
y2=(m[0][1]*f[0]+m[1][1]*f[1]+m[2][1]*f[2])*DIST+y0;
if(n==1) {n=2;x1=x2;y1=y2;}
else
{
line(x1,y1,x2,y2);
x1=x2;y1=y2;
}
}
}
main()
{
float d[3],r[3][3],ta,tb;
char k;
int gdriver=VGA,gmode=VGAMED, i,p=1;
initgraph(&gdriver,&gmode,"c:\\tc");
setbkcolor(BLACK);setcolor(GREEN);
d[1]=30*T; d[2]=10*T;
do{
for(i=0;i<=361;i+=1)
{
k=kbhit();
if(k!=0)break;
setactivepage(p);
d[0]=i*T;
cleardevice();
trans(d,r);
for(tb=0.0;tb<PI;tb+=DT)
{
n=1;
for(ta=0.0;ta<2.1*PI;ta+=DT) draw(r,ta,tb);}
for(ta=0.0;ta<PI;ta+=DT)
{
n=1;
for(tb=0.0;tb<2.1*PI;tb+=DT) draw(r,ta,tb);
}
setvisualpage(p);delay(150);p=1-p;
}
}
while(k==0);
getch();closegraph();
}
這是旋轉橢球的
❽ c語言中line函數內的參數的含義
line(x1,y1,x2,y2);其中兩個數為一個坐標,表示從(x1,y1)畫線到(x2,y2)
❾ C語言怎麼畫矩形
用lineto函數畫矩形
#include<graphics.h>
main()
{int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc");
cleardevice();
moveto(160,120);
lineto(480,120);
lineto(160,360);
lineto(160120);
getch();
closegraph();
}
圖形和圖像函數包含在graphics.h裡面
rectangle() 畫矩形函數
功能: 函數rectangle() 用當前繪圖色、線型及線寬,畫一個給定左上角與右下角的矩形(正方形或長方形)。
用法: 此函數調用方式為void rectangle(int left,int top,int right,int bottom);
說明: 參數left,top是左上角點坐標,right,bottom是右下角點坐標。如果有一個以上角點不在當前圖形視口內,且裁剪標志clip設置的是真(1),那麼調用該函數後,只有在圖形視口內的矩形部分才被畫出。
這個函數對應的頭文件為graphics.h
返回值: 無
例: 下面的程序畫一些矩形實例:
#i nclude<graphics.h>
void main()
{
int driver,mode;
driver=DETECT;
mode=0;
initgrpah(&driver,&mode,"");
rectangle(80,80,220,200);
rectangle(140,99,180,300);
rectangle(6,6,88,88);
rectangle(168,72,260,360);
getch();
restorecrtmode();
}
❿ 用c語言編寫一個簡單的立方體
#include<stdio.h>
#include"graphics.h" /*畫圖需要的*/
void main()
{
int graphdriver=0;
int graphmode=0;
initgraph(&graphdriver,&graphmode,"");
cleardevice(); /*以上是圖形驅動*/
setbkcolor(1); /*設置背景顏色*/
setcolor(RED); /*設置前景顏色*/
setfillstyle(1,3); /*設置顏色填充模式和顏色*/
bar3d(100,200,400,350,100,1); /*畫立方體和填充正面*/
floodfill(450,300,WHITE);
floodfill(250,150,WHITE);
getch();
closegraph();
}
以上是一個簡單的長方體,立方體是一個道理,根據你自己要求改嘛
可以參考C語言的高級編程