public class Test{
成都创新互联公司秉承实现全网价值营销的理念,以专业定制企业官网,网站建设、做网站,小程序定制开发,网页设计制作,手机网站开发,营销型网站建设帮助传统企业实现“互联网+”转型升级专业定制企业官网,公司注重人才、技术和管理,汇聚了一批优秀的互联网技术人才,对客户都以感恩的心态奉献自己的专业和所长。
static float count(float x, float y) {
return (float) (Math.pow((x * x + y * y - 1), 3) - x * x * y * y * y);
}
public static void printEmpty() {
int height = 50, width = 100;
int heart[][] = new int[height][width];
float hx = 0.0f, hy = 0.0f;
for (int y = height / 2; y -height / 2; y--) {
for (int x = -width / 2; x width / 2; x++) {
hx = (float) x / (float) (width / 2.8f);
hy = (float) (y) / (float) (height / 2.8f);
if (count(hx, hy) = 0.0f) {
heart[height / 2 - y][x + width / 2] = 1;
}
}
}
for (int y = 1; y height - 1; y++) {
for (int x = 1; x width - 1; x++) {
if (heart[y][x] == 1
!(heart[y][x - 2] == 1 heart[y][x + 2] == 1
heart[y - 1][x] == 1 heart[y + 1][x] == 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
printEmpty();
}
}
package com.zeph.j2se.alg;
public class Heart {
int HighLevel(int wide) {
int i = 0, j = 0, k = 0, t = 0, m = 0, n = 0, count = 1;// i控制循环内高度,j控制每行前面空格,k控制输出的*数
// t控制高层星号中间空格,m记录高层最底行的星数,以下逐行增4
// n记录顶行空个数,以下逐行减4;count记录高层高度,与high无关
m = (wide - 4) / 2;
do {
count++;
m -= 4;
} while (m 4);
if ((wide - 4) % 2 == 0)// 区别对待奇偶宽度,奇数中间最小空1个,偶数最小空两个
{
n = 2 + 4 * (count - 1);
m--;
} else
n = 1 + 4 * (count - 1);
for (i = 0; i count; i++) {
for (j = (count - i) * 2; j 0; j--)
System.out.print(" ");
for (k = 0; k m; k++)
System.out.print("*");
for (t = 0; t n; t++)
System.out.print(" ");
for (k = 0; k m; k++)
System.out.print("*");
m += 4;
n -= 4;
System.out.print("\n");
}
return 0;
}
int LowLevel(int wide) {
int i = 0, j = 0, k = 0;// i控制输出行,j控制输出每行前的空格,k控制输出*
int high = 0, tmp = wide;
do// 计算所需高度
{
high++;
tmp -= 4;
} while (tmp 4);
high += 1;
for (i = 0; i high; i++) {
for (j = 0; j 2 * i; j++)
System.out.print(" ");
for (k = wide - 4 * i; k 0; k--)
System.out.print("*");
System.out.print("\n");
}
return 0;
}
public static void main(String[] args) {
int wide = 30;
Heart heart = new Heart();
heart.HighLevel(wide);
heart.LowLevel(wide);
}
}
用方程的话肯定输出不会和你的图完全一致
心形线方程如图
用上面行第二个比较简单,程序如下
#include stdio.h
bool draw(float x, float y)
{
float a = x * x + y * y - 1.0;
float b = x * x * y * y * y;
return a * a * a - b = 0;
}
int main(int argc, char* argv[])
{
for (float y = 1.5; y = -1.5; y -= 0.1)
{
for (float x = -1.2; x = 1.2; x += 0.05)
{
if (draw(x, y))
{
bool left = draw(x - 0.05, y);
bool right = draw(x + 0.05, y);
bool up = draw(x, y + 0.1);
bool down = draw(x, y - 0.1);
if (left right up down)
printf(" ");
else
printf("*");
}
else
printf(" ");
}
printf("\n");
}
return 0;
}
draw函数是判断(x,y)坐标是否在心形范围内
主函数循环,y取值±1.5之间,步长0.1,x取值±1.2之间,步长0.05
如果(x,y)坐标在心形范围外打印空格
如果(x,y)坐标在心形范围内,由于这个心是空心的,要继续判断是不是心形边缘,判断周围4个点坐标,如果都在范围内,表示(x,y)坐标不会是边缘,打印空格,否则是边缘,打印星号
最终结果如图
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款