/** * 显示填充圆 */ public void draw() { for (int y = -this.radius; y <= this.radius; y++) { int maxRelativeX = (int) Math.sqrt(this.radius * this.radius - y * y); for (int x = 0; x <= this.radius - maxRelativeX; x++) { System.out.print(' '); } for (int i = 0; i <= maxRelativeX + maxRelativeX; i++) { if(maxRelativeX == i && y == 0) { System.out.print('◆'); // 显示圆心 } else { System.out.print('◎'); } } System.out.println(); } } }
画出了圆 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); } }
import java.awt.Color;
import java.awt.Graphics;public class bbb extends Applet {
public void paint(Graphics g)
g.drawOval(100, 100, 100, 100);
g.setColor(Color.blue);
g.fillOval(100, 100, 100, 100);
}
}是这个意思不?
首先你确定原点坐标(x,y),半径为r;
这样对于任何一个点p(a,b)必须满足(a^2+b^2<=r^2)
a,b的区间为[x-r,x+r]和[y-r,y+r]。
对于满足(a^2+b^2<=r^2)的点打印*
明白了
// 用*填充的圆有些变形,就用 [全角空格]作背景,◎填充了,◆作为圆心
package test;/**
* @author - yy
* @time - Dec 15, 2008 5:24:31 PM
*/
public class Circle {
private int radius = 0;
public Circle(int radius) {
this.radius = radius;
} public static void main(String[] args) {
new Circle(10).draw(); // 显示半径为10的填充圆
// 输出
// ◎
// ◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◆◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎◎◎◎◎
// ◎◎◎◎◎◎◎◎◎
// ◎
}
/**
* 显示填充圆
*/
public void draw() {
for (int y = -this.radius; y <= this.radius; y++) {
int maxRelativeX = (int) Math.sqrt(this.radius * this.radius - y * y);
for (int x = 0; x <= this.radius - maxRelativeX; x++) {
System.out.print(' ');
}
for (int i = 0; i <= maxRelativeX + maxRelativeX; i++) {
if(maxRelativeX == i && y == 0) {
System.out.print('◆'); // 显示圆心
} else {
System.out.print('◎');
}
}
System.out.println();
}
}
}
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);
}
}