空心
图示

代码
public class xin1 {
public static void main(String[] args) {
float x,y,a;
for(y=1.3f;y>-1.1f;y-=0.06f){
for(x=-1.2f;x<=1.2f;x+=0.025f){
a=x*x+y*y-1;
System.out.print(a*a*a-x*x*y*y*y<=0.0f?' ':'s');
}
System.out.println();
}
}
}
实心

import static java.lang.Math.*;
public class xin2 {
public static void main(String[] args) {
double x,y,a;
char s[]=new char[]{'I',' ','l','o','v','e',' ','y','o','u','!'};
int index=0;
for(y=1.3f;y>-1.1f;y-=0.06f){
index=0;
for(x=-1.1f;x<=1.1f;x+=0.025f){
double result=x*x+pow((5.0*y/4.0-sqrt(abs(x))),2);
if(result<=1){
System.out.print((s[index]));
index=(index+1)%11;
}
else{
System.out.print(' ');
}
}
System.out.println(" ");
}
}
}
具体原理请请教数学老师

这两段代码分别展示了如何用Java绘制空心和实心的几何图形。空心图形通过浮点数循环遍历坐标,根据坐标点距离圆心的距离判断是否打印字符。实心图形使用了一个字符数组,根据坐标点位置和特定公式确定是否填充字符来形成图形。


被折叠的 条评论
为什么被折叠?



