---
title: "[学习DOS下的图形操作]第三天 绘制圆和球-匀速运动的圆和简单坐标的转换"
author: "k7_1983"
date: "2009-01-08T11:00:16Z"
tags: ["DOS"]
slug: "dos"
draft: false
lastmod: "2009-01-08T11:00:16Z"
id: 312290
url: "https://k7-1983.iteye.com/blog/312290"
---

一、画圆和椭圆
void circle( int x, int y, int radius );
画一个圆，圆心为(x,y),半径为radius.
void ellipse( int x, int y, int stangle, int endangle, int xradius, int yradius );
画一个椭圆，圆心为(x,y),开始角度为stangle，结束角度为endangle,x方向上的半径为xradius,y方向上的半径为yradius;
练习1.
画一个圆，以（4,3)为圆心，半径为3，为了画的清楚点，设置整个屏幕的左下角为原点，Y轴的最大值为6.X轴的最大值为8.

```java
#include <stdio.h>
#include <graphics.h>
#include <conio.h>//console io
#include <dos.h>  //sleep

#define SCALE 80
/*
        转换用户坐标系到屏幕坐标系
        用户坐标以屏幕左下标为原点，X轴最大值为8，Y轴最大值为6
        @param axis_x 用户的x坐标
        @param axis_y 用户的y坐标
        @param screen_x 返回对应的屏幕x坐标
        @param screen_y 返回对应的屏幕y坐标
*/
        
void change_axis( double axis_x, double axis_y, int * screen_x, int * screen_y )
{
        *screen_x = (int)( axis_x * SCALE );
        *screen_y = 480 - (int)(axis_y * SCALE );
        return ;
}

int main (void)
{
        int graph_driver = VGA;
        int graph_mode = VGAHI;
        char *graph_path = "D:\\DOS\\TC\\BGI";
        int x,y,radius;
        initgraph( &graph_driver, &graph_mode, graph_path);
        radius = 1 * SCALE;
        change_axis( 4.0, 3.0, &x, &y );
        circle( x, y, radius );
        getch();
        closegraph();
        return 0;    
}
```
二、画实心的圆
画实心的圆需要用到另外两个函数。
circle会用背景色填充。
setfillstyle( SOLID_FULL,color);
SOLID_FULL 表示使用单色填充，color则指明这种颜色。
fillellipse( int x, int y, int xradius, int yradius );
用来填充一个椭圆。
我们使用相等的xradius和yradius来画一个填充的圆。
练习2:
画一个匀速运动的小圆，从左边匀速运动到右边，然后匀速返回。
每sleep(1)一次，小圆运动10个px.小圆的大小为10px；


```java
#include <stdio.h>
#include <graphics.h>
#include <dos.h>
#include <conio.h>
/*
        画一个填充的圆
        @param x x坐标
        @param y y坐标
        @param radius 半径
        @param color 颜色
*/
void fillcircle( int x, int y, int radius, int color )
{
        setfillstyle( SOLID_FILL, color );
        setcolor( color );
        fillellipse( x, y, radius, radius );
}
int main(void)
{
        int graph_driver = VGA;
        int graph_mode = VGAHI;
        char *graph_path = "D:\\DOS\\TC\\BGI";

        int x, y;
        int v;

        initgraph( &graph_driver, &graph_mode, graph_path );

        x = 10;
        y = 240;
        v = 5;

        //如果没有按键
        while( kbhit()==0 )
        {
                fillcircle( x,y,10,BLACK);//用背景色填掉原来的圆
                if( x >= (640-10) )
                {
                        v = -5;//碰到右边界原速返回
                }
                else if( x <= 10 )
                {
                        v = 5;//碰到左边界原速返回
                }
                x = x + v;
                fillcircle( x, y, 10,RED );//重新画红色的圆
                sleep(1);
        }
        getch();
        closegraph();
        return 0;
}
```