疯狂java第四章画近似圆_疯狂java 讲义第三章练习题---画圆

public class PaintRound{

/**

* @author Dream_e.

* @version v1.0

*/

private int r;//圆的半径

PaintRound(int r){

this.r = r;

}

public void paint(){

int y = 2*r;//y的最大值为半径的2倍

int x = 0;

int c = 0;

int z = 2;//坐标轴递减量.

for(int j = y; j >= 0; j-=z){

x = getX(r,y);

System.out.print(getSpace(x)+"*");

c = (r-x)*2;

System.out.println(getSpace(c)+"*");

y-=z;

}

}

//计算X坐标.

private int getX(int r, int y){

double temp = Math.sqrt(r*r-(r-y)*(r-y));//根据圆内的正角三角形计算出圆上某点到圆心的X轴距离

int x = (int)Math.round(r-temp);

return x;

}

//计算要打印的空格数量.

private String getSpace(int c){

String space = " ";

for(int i = 0; i < c; i++){

space += " ";

}

return space;

}

public static void main(String[] args){

new PaintRound(10).paint();

}

}

原文:http://blog.csdn.net/dream_e/article/details/25802499

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值