c语言画正方体,C语言绘制实心正方形

发布时间:2022-11-27

本文目录一览:

1、C语言怎样画正方体
2、c 语言 在vc下 用直线画一个正方形(要直线,不是星号或者有间隔的,要完整的)
3、用符号“□”“■”C语言画出正方形和正方形对角线
4、用c语言来画1个正方体,一个简单的程序。。
5、怎样用c语言来编写一个任意立方体的表面积和体积?
6、C语言编写一个程序输出一个正方形

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,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("设置左侧缩进: ");
            cin>>LEN;
            set_prefix_str( PREFIX, LEN, ' ' );
            continue;
        }
        if ('1'==option) {
            print_pix("正方形边长: ");
            cin>>rectY; rectX=rectY;
            println_str("");
        }
        if ('2'==option) {
            print_pix("矩形长: ");
            cin>>rectX;
            println_str("");
            print_pix("矩形高: ");
            cin>>rectY;
            println_str("");
        }
        if (rectX<0 or rectX>64) {
            println_str("X参数范围错误");
            system("pause");
            continue;
        }
        if (rectY<0 or rectY>64) {
            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为边界的颜色,也就是封闭图形轮廓的
颜色。调用了该函数后,将用规定的颜色和图模填满整个封闭图形。
#include stdlib.h
#include graphics.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语言来编写一个任意立方体的表面积和体积?

#include stdio.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;i<n;i++){
        for(j=0;j<n;j++){
            if(i==0||i==n-1||j==0||j==n-1)
                printf("*");
            else 
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

输出:

5
*****
*   *
*   *
*   *
*****