public class Test{
成都創(chuàng)新互聯(lián)公司秉承實(shí)現(xiàn)全網(wǎng)價(jià)值營(yíng)銷的理念,以專業(yè)定制企業(yè)官網(wǎng),網(wǎng)站建設(shè)、做網(wǎng)站,小程序定制開(kāi)發(fā),網(wǎng)頁(yè)設(shè)計(jì)制作,手機(jī)網(wǎng)站開(kāi)發(fā),營(yíng)銷型網(wǎng)站建設(shè)幫助傳統(tǒng)企業(yè)實(shí)現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級(jí)專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對(duì)客戶都以感恩的心態(tài)奉獻(xiàn)自己的專業(yè)和所長(zhǎng)。
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控制循環(huán)內(nèi)高度,j控制每行前面空格,k控制輸出的*數(shù)
// t控制高層星號(hào)中間空格,m記錄高層最底行的星數(shù),以下逐行增4
// n記錄頂行空個(gè)數(shù),以下逐行減4;count記錄高層高度,與high無(wú)關(guān)
m = (wide - 4) / 2;
do {
count++;
m -= 4;
} while (m 4);
if ((wide - 4) % 2 == 0)// 區(qū)別對(duì)待奇偶寬度,奇數(shù)中間最小空1個(gè),偶數(shù)最小空兩個(gè)
{
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// 計(jì)算所需高度
{
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);
}
}
用方程的話肯定輸出不會(huì)和你的圖完全一致
心形線方程如圖
用上面行第二個(gè)比較簡(jiǎn)單,程序如下
#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函數(shù)是判斷(x,y)坐標(biāo)是否在心形范圍內(nèi)
主函數(shù)循環(huán),y取值±1.5之間,步長(zhǎng)0.1,x取值±1.2之間,步長(zhǎng)0.05
如果(x,y)坐標(biāo)在心形范圍外打印空格
如果(x,y)坐標(biāo)在心形范圍內(nèi),由于這個(gè)心是空心的,要繼續(xù)判斷是不是心形邊緣,判斷周圍4個(gè)點(diǎn)坐標(biāo),如果都在范圍內(nèi),表示(x,y)坐標(biāo)不會(huì)是邊緣,打印空格,否則是邊緣,打印星號(hào)
最終結(jié)果如圖