程序员的浪漫–java打印心形图案

谁说程序员不能浪漫

单纯的心

双心

加些点缀

代码所用方程:

心:((0.05*x)^2 + (0.1*y)^2-1)^3-(0.05*x)^2 * (0.1*y)^3 < = 0

箭:4*y-x == 0

原理:根据方程,打印出空格或者文字

注意:

本程序使用idea编写时打印,如果用其他ide或者命令行中可能会变形

使用中根据汉字或者英文需要进行微调

代码不够简洁,抱歉

代码:

public static void main(String[] args) {

//单心

heart(15,0.9,”love”);

//双心

heartTwo(15,0.9,”爱”,”梁山伯”,”祝英台”);

//点缀

heartTwoWithXK(15,0.9,”爱”,”梁山伯”,”祝英台”);

}

private static void heart(int r,double size,String req){

size=1/(1.5*r*size);

StringBuilder sb=new StringBuilder();

for (int y = r; y > -r; y–,sb.append(“\n”))

for (int x = -2*r; x < 2*r; x++ ) {

char msg=(req + req).charAt((x – y) % req.length() + req.length());

sb.append((inHeart(size,x,y)?msg+ ” ” : ” “));

}

System.err.println(sb.toString());

}

private static void heartTwo(int r,double size,String center,String left,String right){

size=1/(1.5*r*size);

StringBuilder sb=new StringBuilder();

for (int y = r; y > -r; y–,sb.append(“\n”))

for (int x = -2*r; x <4*r; x++ ) {

boolean isLeft=inHeart(size,x,y);

boolean isRight=inHeart(size,x-25,y-3);

//双空格

String req=null;

if(isLeft && isRight) req=center;

else if(isLeft) req=left;

else if (isRight) req=right;

if(req!=null) sb.append((req + req).charAt((x – y) % req.length() + req.length()));

else sb.append(” “);//双空格

}

System.err.println(sb.toString());

}

private static void heartTwoWithXK(int r,double size,String center,String left,String right){

size=1/(1.5*r*size);

StringBuilder sb=new StringBuilder();

for (int y = r; y >=-r; y–,sb.append(“\n”))

for (int x = -2*r; x <= 4*r; x++ ) {

boolean isLeft=inHeart(size,x,y+3);

boolean isRight=inHeart(size,x-25,y);

//双空格

String req=null;

String w=””;

if(isLeft && isRight) req=center;

else if(isLeft) req=left;

else if (isRight) req=right;

else if((y==-r || y==r)) {

if (x < 3 * r – 7) {

req = “♥”;

w = ” “;

}

}

else if(x==4*r || x==-2*r || line(x,y+3)) req=”♥”;

if(req!=null) sb.append((req + req).charAt((x – y) % req.length() + req.length()) + w);

else sb.append(” “);//双空格

}

System.err.println(sb.toString());

}

private static boolean inHeart(double size,int x,int y){

return Math.pow(Math.pow(x * size, 2) + Math.pow(y * 2*size, 2) – 1, 3) – Math.pow(x * size, 2) * Math.pow(y * 2*size, 3) <= 0;

}

private static boolean line(int x,int y){

return 4*y-x == 0;

}

转载请注明:《程序员的浪漫–java打印心形图案