- import java.math.BigDecimal;
- public class Circle
- {
- /**
- * 半径
- */
- public int radius;
- /**
- * 一排点数 = 半径*2+1
- */
- public double point;
- public static void main(String[] args)
- {
- Circle circle = new Circle(6);
- circle.show();
- }
- Circle(double dou)
- {
- radius = changeValue(dou);//四舍五入
- point = radius*2 +1;//一排点数 = 半径*2+1
- }
- /**
- * 打印圆
- *
- */
- private void show()
- {
- for(int i = 0;i<point;i++)
- {
- for(int j = 0;j<point;j++)
- {
- if(isCircle(j,i))
- System.out.print(" *");
- else
- System.out.print(" ");
- }
- System.out.print("\n");
- }
- }
- /**
- * 判断某点是否是圆的点
- * @return true:是;false:不是
- */
- private boolean isCircle(int x, int y)
- {
- x = x-radius;
- y = y-radius;
- double temp = square(radius) - square(y);
- int z = ((int)Math.sqrt(temp)); // 用圆的公式求星号离x轴的长度
- if(x == z || x== -z)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /**
- * 四舍五入
- * @param dou double
- * @return double
- */
- public int changeValue(double dou)
- {
- BigDecimal bdBigDecimal = new BigDecimal(String.valueOf(dou)).setScale(0, BigDecimal.ROUND_HALF_UP);
- return bdBigDecimal.intValue();
- }
- /**
- * 求平方
- * @param x int
- * @return 平方值
- */
- public double square(int x)
- {
- return (double)(x*x);
- }
- }
复制代码 |