码迷,mamicode.com
首页 > 其他好文 > 详细

绘制指定半径的圆

时间:2015-06-13 19:55:34      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

package day01;

/**
 * Created by sherry on 000019/3/19 13:55.
 */

/*直角坐标*/
class Coord{
    /*横坐标*/
    private int i;
    /*纵坐标*/
    private int j;

    /*两个坐标之间的距离(坐标值必须都是正数)*/
    public double getRange(Coord coord){
        return Math.sqrt(Math.pow(Math.abs(this.i-coord.getI()),2)+Math.pow(Math.abs(this.j-coord.getJ()),2));
    }

    @Override
    public String toString() {
        return "Coord{" +
                "i=" + i +
                ", j=" + j +
                ‘}‘;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null){
            return false;
        }
        Coord coord = (Coord) o;
        if (this.i == coord.getI()
                &&this.j == coord.getJ()){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int result = i;
        result = 31 * result + j;
        return result;
    }

    public Coord(int i, int j) {
        this.i = i;
        this.j = j;
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    public int getJ() {
        return j;
    }

    public void setJ(int j) {
        this.j = j;
    }
}
public class DrawCircle {
    public static void main(String[] args) {
        drawCircle(20);
    }

    private static void drawCircle(int r) {
        /*边框长度*/
        int d = 2*r+1;
        /*圆心坐标*/
        Coord centerOfCircle = new Coord(r,r);

        /*任意一点坐标*/
        Coord coord;
        for (int i = 0;i < d;i++){
            for (int j = 0;j < d;j++){
                coord = new Coord(j,i);
                if (coord.equals(centerOfCircle)){
                    System.out.print("A ");
                } else if (Math.abs(coord.getRange(centerOfCircle)-r)<0.5){
                    System.out.print("B ");
                } else {
                    System.out.print("* ");
                }
            }
            System.out.println();
        }
    }
}

 

绘制指定半径的圆

标签:

原文地址:http://www.cnblogs.com/sherrykid/p/4573895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!