class Circle {
public static void main(String[] args) throws Exception {
//以(x,y)为圆心,显示一个半径为r的圆
int x = 20;
int y = 30;
int r = 15;
//显示空行
for (int e=1; e<(y-r); e++)
{
System.out.println();
}
//显示切线
StringBuffer cutLine = new StringBuffer();
for (int e=1; e<x; e++) {
cutLine.append(" ");
}
cutLine.append("**");
System.out.println(cutLine);
//####################################################下面的不明白
//显示圆的投影
for (int row=(y-r+1); row<=(y+r-1); row++) {
StringBuffer line = new StringBuffer();
//计算弦长/2 Math.round(double a) 返回最接近参数的 long。结果将舍入为整数:加上 1/2,(long)Math.floor(a + 0.5d)
long temp = Math.round(Math.sqrt((r*r - (y-row)*(y-row))));
//计算当前行的空格
for (int i=1; i<=(x-temp); i++) {
line.append(" ");
}
//计算当前行的**
for (int i=1; i<=(2*temp); i++) {
line.append("**");
}
System.out.println(line);
}
//########################################################上面的不明白
//显示切线
StringBuffer cutLine2 = new StringBuffer();
for (int i=1; i<x; i++) {
cutLine2.append(" ");
}
cutLine2.append("**");
System.out.println(cutLine2);
}
}
public static void main(String[] args) throws Exception {
//以(x,y)为圆心,显示一个半径为r的圆
int x = 20;
int y = 30;
int r = 15;
//显示空行
for (int e=1; e<(y-r); e++)
{
System.out.println();
}
//显示切线
StringBuffer cutLine = new StringBuffer();
for (int e=1; e<x; e++) {
cutLine.append(" ");
}
cutLine.append("**");
System.out.println(cutLine);
//####################################################下面的不明白
//显示圆的投影
for (int row=(y-r+1); row<=(y+r-1); row++) {
StringBuffer line = new StringBuffer();
//计算弦长/2 Math.round(double a) 返回最接近参数的 long。结果将舍入为整数:加上 1/2,(long)Math.floor(a + 0.5d)
long temp = Math.round(Math.sqrt((r*r - (y-row)*(y-row))));
//计算当前行的空格
for (int i=1; i<=(x-temp); i++) {
line.append(" ");
}
//计算当前行的**
for (int i=1; i<=(2*temp); i++) {
line.append("**");
}
System.out.println(line);
}
//########################################################上面的不明白
//显示切线
StringBuffer cutLine2 = new StringBuffer();
for (int i=1; i<x; i++) {
cutLine2.append(" ");
}
cutLine2.append("**");
System.out.println(cutLine2);
}
}
R = X + Y因此
___________
/ 2 2
X = V R - Y
long temp = Math.round(Math.sqrt((r*r - (y-row)*(y-row))));
它用的是三角形的计算公式
2 2 2
R = X + Y其中:
row=(y-r+1) ,是上下的一条边
r是连到圆周上的一条边
temp是连接圆周和row的一条边别的地方就简单了,我就不说了