『壹』 用c語言編寫程序 在屏幕上畫一個矩形
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
void polygon(int n, int x, int y, int r, int color, float arg, int fillstyle);
int main()
{
int GraphDriver;
int GraphMode;
float arg = 45, argd;
int a;
int direction;
int r;
int n = 4;
FILE *fp;
char szfilename[255] = {"c:\\cube.txt"};
GraphDriver = DETECT;
printf("Input size of cube: ");
scanf("%d", &r);
printf("Input direction(0-1): ");
scanf("%d", &direction);
if (direction == 0)
{
argd = 45;
}
else
{
argd = -45;
}
initgraph(&GraphDriver, &GraphMode, "");
polygon(n, 300, 200, r, 12, arg, 0);
while(1)
{
while(kbhit())
{
a = getch();
if (a == 27)
{
if ((fp = fopen(szfilename, "wt")) != NULL)
{
fprintf(fp, "%d\n%d\n", r, direction);
fclose(fp);
}
closegraph();
return 0;
}
if (a == 0)
{
getch();
polygon(n, 300, 200, r, 0, arg, 0);
arg += argd;
polygon(n, 300, 200, r, 12, arg, 0);
}
else
{
polygon(n, 300, 200, r, 0, arg, 0);
arg += argd;
polygon(n, 300, 200, r, 12, arg, 0);
}
}
}
}
void polygon(int n, int x, int y, int r, int color, float arg, int fillstyle)
{
double pi;
int i;
float x1[10], y1[10];
setcolor(color);
pi = atan(1) * 4;
arg = atan(1) / 45 * arg;
x1[1] = x + r * cos(2 * pi / n + arg);
y1[1] = y + r * sin(2 * pi / n + arg);
moveto(x1[1], y1[1]);
for (i = 2; i <= n; i++)
{
x1[i] = x + r * cos(2 * pi * i / n + arg);
y1[i] = y + r * sin(2 * pi * i / n + arg);
lineto(x1[i], y1[i]);
}
lineto(x1[1], y1[1]);
if (fillstyle != 0)
{
setfillstyle(SOLID_FILL, color);
floodfill(x, y, color);
}
}
『貳』 用c語言編矩形
#include<stdio.h>
intmain(){
intwidth,height,fill;
charchr;
scanf("%d%d%c%d",&height,&width,&chr,&fill);
if(height>10||height<3){
perror("Heightmustbetween3and10!");
return1;
}
if(width>10||width<5){
perror("Widthmustbetween5and10!");
return1;
}
for(inti=0;i<height;i++){
if(i==0||i==height-1||fill){
for(intj=0;j<width;j++){
printf("%c",chr);
}
}else{
printf("%c",chr);
for(intj=0;j<width-2;j++){
printf("");
}
printf("%c",chr);
}
printf(" ");
}
return0;
『叄』 C語言:星號繪制的矩形程序問題
改成這樣就行了
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int x = 0; /*長度*/
int y = 0; /*寬度*/
int i = 1;
int j = 1;
on:
printf("x=");
scanf("%d", &x);
printf("y=");
scanf("%d", &y);
if(x < 2 || y < 0)
{
printf("Please enter a right number");
goto on;
}
for( ; i <= x ; i++)
printf("*"); /*先輸出頂端的一條邊*/
x -= 2; /*用來輸出空格*/
for( ; j <= y - 2 ; j++) /*寬度*/
{
printf("\n*"); /*先輸出一個星號,後面輸出空格*/
for( i = 1; i <= x ; i++)
printf(" "); /*這個空格木有輸出,問題出在哪裡?*/
printf("*"); /*輸出完空格後要輸出一個星號*/
}
printf("\n");
x += 2;
for( i = 1; i <= x ; i++)
printf("*"); /*先輸出頂端的一條邊*/
system("pause");
return 0;
}
『肆』 用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語言畫矩形
/* 旋轉的立體橢球 */
#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語言怎麼畫矩形
用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語言畫一個矩形
每行起始和結束字元均是你的第3個參數 矩形第1行和最後一行中間是第3個參數,其他行根據第4個參數決定是空格或者第3個參數 程序可以這樣寫: ... for ( m=0;m
『捌』 如何利用c語言程序設計繪制一個形狀(圓,橢圓,矩形都可以),用線條動態地填充其內部
你的c 編譯器需帶 繪圖函數庫 才行。
c++ API 程序 可以繪圖。只要得到窗口句柄,就可在該窗畫圖。畫直線,多邊形,圓,橢圓,扇形 等 都是基本函數。
下面程序在桌面窗口畫線:
#include <Afxwin.h>
#include <Windows.h>
#pragma comment (lib, "User32.lib")
int main(void){
HWND hWnd = ::GetDesktopWindow();
HDC hdc = ::GetDC(hWnd);
// HDC hdc = ::GetDC(NULL);
RECT rect;
::GetWindowRect(hWnd, &rect);
::MoveToEx(hdc, 0, 0, NULL);
::LineTo(hdc, rect.right, rect.bottom);
::MoveToEx(hdc, rect.right, 0, NULL);
::LineTo(hdc, 0, rect.bottom);
::ReleaseDC(hWnd, hdc);
system("PAUSE");
return 0;
}
===
用「刷子」畫填充的封閉圖形 (參數是 花紋,顏色)
HBRUSH Brush[5];
case WM_PAINT:
Brush[0] = CreateHatchBrush(HS_BDIAGONAL, RGB(0, 0, 255));
Brush[1] = CreateHatchBrush(HS_CROSS, RGB(200, 0, 0));
Brush[2] = CreateHatchBrush(HS_DIAGCROSS, RGB(0, 128, 0));
Brush[3] = CreateHatchBrush(HS_FDIAGONAL, RGB(0, 128, 192));
Brush[4] = CreateHatchBrush(HS_DIAGCROSS, RGB(255, 128, 0));
----
編譯器 MS VC++ 6.0.
API 程序羅嗦,這里就不列出來了。
『玖』 用C語言模擬畫圖行(直線、 橢圓形、 三角形、 矩形、 梯形)求寫編程
#include <stdio.h>
#define PI 3.14159
float erea_round(float r)
{
//圓形面積
return r * r * PI;
}
float erea_tri(float d,float h)
{
//三角形 面積
return 0.5 * d * h;
}
float erea_rec(float a,float b)
{
//矩形面積
return a * b;
}
int main()
{
float r,a,b,d,h;
int choose;
while (1)
{
printf("請選擇要計算的圖像:\n");
printf("1、圓形\n");
printf("2、三角形\n");
printf("3、矩形\n");
printf("4、退出\n");
scanf("%d",&choose);
if (choose == 1)
{
printf("請輸入圓的半徑:\n");
scanf("%f",&r);
printf("該圓面積為:%.3f\n",erea_round(r));
}
else if (choose == 2)
{
printf("請輸入三角形的底和高:\n");
scanf("%f%f",&d,&h);
printf("該三角形面積為:%.3f\n",erea_tri(d,h));
}
else if (choose == 3)
{
printf("請輸入矩形的長和寬:\n");
scanf("%f%f",&a,&b);
printf("該矩形面積為:%.3f\n",erea_rec(a,b));
}
else return 0;
}
return 0;
}