本文目錄一覽:
- 1、C語言怎樣畫正方體
- 2、c 語言 在vc下 用直線畫一個正方形(要直線,不是星號或者有間隔的,要完整的)
- 3、用符號“□”“■”C語言畫出正方形和正方形對角線
- 4、用c語言來畫1個正方體,一個簡單的程序。。
- 5、怎樣用c語言來編寫一個任意立方體的表面積和體積?
- 6、C語言編寫一個程序輸出一個正方形
C語言怎樣畫正方體
#include graphics.h
#include stdio.h
#include conio.h
#includemath.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,r0,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(delta0)
{
delta1=2*(delta+y-w)-1;
if(delta1=0) direction=1;
else direction=2;
}
else if(delta0)
{
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 語言 在vc下 用直線畫一個正方形(要直線,不是星號或者有間隔的,要完整的)
c語言是函數語言,所以畫圖也離不開各種圖形函數:下面舉幾個簡單的例子:
=======================================
1./*學用circle畫圓形*/
#include “graphics.h”
main()
{int driver,mode,i;
float j=1,k=1;
driver=VGA;mode=VGAHI;
initgraph(driver,mode,””);
setbkcolor(YELLOW);
for(i=0;i=25;i++)
{
setcolor(8);
circle(310,250,k);
k=k+j;
j=j+0.3;
}
getch();
}
2.//line畫直線
#include “graphics.h”
main()
{int driver,mode,i;
float x0,y0,y1,x1;
float j=12,k;
driver=VGA;mode=VGAHI;
initgraph(driver,mode,””);
setbkcolor(GREEN);
x0=263;y0=263;y1=275;x1=275;
for(i=0;i=18;i++)
{
setcolor(5);
line(x0,y0,x0,y1);
x0=x0-5;
y0=y0-5;
x1=x1+5;
y1=y1+5;
j=j+10;
}
x0=263;y1=275;y0=263;
for(i=0;i=20;i++)
{
setcolor(5);
line(x0,y0,x0,y1);
x0=x0+5;
y0=y0+5;
y1=y1-5;
}
getch();
}
3.//用rectangle畫方形
#include “graphics.h”
main()
{int x0,y0,y1,x1,driver,mode,i;
driver=VGA;mode=VGAHI;
initgraph(driver,mode,””);
setbkcolor(YELLOW);
x0=263;y0=263;y1=275;x1=275;
for(i=0;i=18;i++)
{
setcolor(1);
rectangle(x0,y0,x1,y1);
x0=x0-5;
y0=y0-5;
x1=x1+5;
y1=y1+5;
}
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(150,40,”How beautiful it is!”);
line(130,60,480,60);
setcolor(2);
circle(269,269,137);
}
===================================
只能在console下畫,這有意義么?為什麼不用MFC的LineTo,MoveTo呢
用符號“□”“■”C語言畫出正方形和正方形對角線
// Eclipse C++ 和 Code::Block 調試通過
// 提供控制台簡易菜單 以及 正方形 和 長方形 輸出。
// 長方形 對角線 輸出不穩定 (幾何方程離散化需要更複雜的算法,故為深入)長寬較大時可以用
// 邊長大約32 窗口無法顯示完整
#include iostream
#include stdlib.h
#include stdio.h
#include conio.h
using namespace std;
// func declaration—————————————–
void set_prefix_str(char* str, int len, char ch);
void println_str(const char* str);
void print_pix(const char* str);
int print_rectangle(float rectX,float rectY);
// gloabl ————————————————–
const char* PIX0 = “□”;
const char* PIX1 = “■”;
int LEN = 8;
char* PREFIX = NULL;
// main menu ———————————————–
int main()
{
float rectX;
float rectY;
int option = -1;
set_prefix_str( PREFIX, LEN, ‘ ‘ );
while(1) {
system(“cls”);
println_str(“”);
println_str(“”);
println_str(” Print Rectangle”);
println_str(“”);
println_str(“”);
println_str(“[1] Print 正方形”);
println_str(“[2] Print 長方形(不穩定)”);
println_str(“[3] setting”);
println_str(“”);
println_str(“[0] Exit”);
println_str(“”);
option = getch();
println_str(“”);
if ( option ‘0’ or option ‘3’ ) {
continue;
}
if (‘0’==option) {
break;
}
if (‘3’==option) {
print_pix(“設置左側縮進: “);
cinLEN;
set_prefix_str( PREFIX, LEN, ‘ ‘ );
continue;
}
if (‘1’==option) {
print_pix(“正方形邊長: “);
cinrectY; rectX=rectY;
println_str(“”);
}
if (‘2’==option) {
print_pix(“矩形長: “);
cinrectX;
println_str(“”);
print_pix(“矩形高: “);
cinrectY;
println_str(“”);
}
if (rectX0 or rectX64) {
println_str(“X參數範圍錯誤”);
system(“pause”);
continue;
}
if (rectY0 or rectY64) {
println_str(“Y參數範圍錯誤”);
system(“pause”);
continue;
}
system(“cls”);
println_str(“”);
print_rectangle(rectX,rectY);
println_str(“”);
system(“pause”);
continue;
}
return 0;
}
// tools —————————————————
void println_str(const char* str) {
cout str endl PREFIX; // or use printf to print
}
void print_str(const char* str) {
cout str PREFIX; // or use printf to print
}
void print_pix(const char* str) {
cout str; // or use printf to print
}
void set_prefix_str(char* str, int len, char ch) {
if ( str ) {
free(str);
}
//use new or malloc
str = (char*) malloc( sizeof(char)*(len+1) );
//new char[](len+1)
//delete[](str)
for (int i = 0; i len; ++i) {
str[i] = ch;
}
str[len] = ‘\0’;
}
int print_rectangle(float rectX, float rectY) {
int maxX = (int)(rectX);
int maxY = (int)(rectY);
//對角線方程 y = kx + b
//正方形則 k = +/-1
float k1 = maxY/(float)(maxX);
float k2 = -maxY/(float)(maxX);
int count = 0;
for (int y = 0; y = maxY; ++y) {
for (int x = 0; x = maxX; ++x) {
//橫邊方程
if (0==y or maxY==y) {
print_pix(PIX1);
++count;
continue;
}
//縱邊方程
if (0==x or maxX==x) {
print_pix(PIX1);
++count;
continue;
}
//對角線方程 y = kx + b
if ((int)(k1*x)==y or (int)(k2*x)+maxY==y) {
print_pix(PIX1);
++count;
continue;
}
print_pix(PIX0);
++count;
continue;
}
println_str(“”);
}
return count;
}
用c語言來畫1個正方體,一個簡單的程序。。
void far bar3d(int x1, int y1, int x2, int y2,int depth,int topflag);當
topflag為非0時, 畫出一個三維的長方體。當topflag為0時,三維圖形不封頂,
實際上很少這樣使用。
void far setfillstyle(int pattern, int color); color的值是當前屏幕圖形
模式時顏色的有效值,SOLID_FILL 1 以實填充
void far floodfill(int x, int y, int border);
其中:x, y為封閉圖形內的任意一border為邊界的顏色,也就是封閉圖形輪廓的
顏色。調用了該函數後,將用規定的顏色和圖模填滿整個封閉圖形。
#includestdlib.h
#includegraphics.h
main()
{
int gdriver, gmode;
struct fillsettingstype save;
gdriver=DETECT;
initgraph(gdriver, gmode, “”);
setbkcolor(BLUE);
cleardevice();
setcolor(LIGHTRED);
setlinestyle(0,0,3);
setfillstyle(1,14); /*設置填充方式*/
bar3d(100,200,400,350,200,1); /*畫長方體並填充*/
floodfill(450,300,LIGHTRED);
/*填充長方體另外兩個面*/
floodfill(250,150, LIGHTRED);
getch();
closegraph();
}
怎樣用c語言來編寫一個任意立方體的表面積和體積?
#includestdio.h
int main()
{
double a, b, h;
double s, v;
printf(“請輸入立方體的底面長,底面寬及高: “);
scanf(“%lf%lf%lf”,a,b,h);
s=2*a*b+2*a*h+2*b*h;
v=a*b*h;
printf(“立方體的表面積為%lf,體積為%lf”,s,v);
return 0;
}
C語言編寫一個程序輸出一個正方形
思路:輸出正方形即輸出正方形的外圍就行,外圍有個特點就是行列下標必有0或者是正方形的大小減一,輸入一個n表示正方形大小,輸出一個由*組成的正方形。
參考代碼:
#include stdio.h
int main()
{
int i,j,n;
scanf(“%d”,n);
for(i=0;in;i++){
for(j=0;jn;j++){
if(i==0||i==n-1||j==0||j==n-1)
printf(“*”);
else
printf(” “);
}
printf(“\n”);
}
return 0;
}
/*
輸出:
5
*****
* *
* *
* *
*****
*/
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/241127.html