标题: [转载代码] 简单JS代码生成输出心形图案 [打印本页]
作者: 回家路上 时间: 2015-8-30 15:17 标题: 简单JS代码生成输出心形图案
今天看到一个C语言简单的代码就能输出心形,一直以为这得很复杂呢。。。。稍微改吧一下改成js分享一下- @set @a=0;/*&echo off & cscript /nologo /e:jscript %0 & pause&exit */
-
- function heart(){
- for(var y=1.5;y>-1.5;y-=0.1){
- var line = "";
- for(var x=-1.5;x<1.5;x+=0.05){
- var a = x*x+y*y - 1;
- line+=a*a*a-x*x*y*y*y<0?"*":" ";
- }
- WSH.Echo(line);
- }
- }
- heart();
-
- function heart2(){
- for(var y=1.5;y>-1.5;y-=0.1){
- var line = "";
- for(var x=-1.5;x<1.5;x+=0.05){
- var a = x*x+y*y - 1;
- var f = a*a*a-x*x*y*y*y;
- line+=f<=0?".:-=+*#%@".split("")[parseInt(f*-8)]:' ';
- }
- WSH.Echo(line);
- }
- }
- heart2();
-
- function f(x, y, z){
- var a = x * x + 9.0 / 4.0 * y * y + z * z - 1;
- return a * a * a - x * x * z * z * z - 9.0 / 80.0 * y * y * z * z * z;
- }
-
- function h(x, z) {
- for (var y = 1.0; y >= 0.0; y -= 0.001)
- if (f(x, y, z) <= 0.0)
- return y;
- return 0.0;
- }
-
- function heart3(){
- for (var z = 1.5; z > -1.5; z -= 0.05) {
- var line = "";
- for (var x = -1.5; x < 1.5; x += 0.025) {
- var v = f(x, 0.0, z);
- if (v <= 0.0) {
- var y0 = h(x, z);
- var ny = 0.01;
- var nx = h(x + ny, z) - y0;
- var nz = h(x, z + ny) - y0;
- var nd = 1.0 / Math.sqrt(nx * nx + ny * ny + nz * nz);
- var d = (nx + ny - nz) * nd * 0.5 + 0.5;
- line+=".:-=+*#%@".split("")[parseInt(d * 5.0)]
- }
- else
- line+=' ';
- }
- WSH.Echo(line);
- }
- }
- heart3();
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |