繪制一個(gè)簡易愛心
#include <stdio.h>
#include <Windows.h>
int main()
{
for (float y = 1.5f; y > -1.5f; y -= 0.1f)
{
for (float x = -1.5f; x < 1.5f; x += 0.05f)
{
float z = x * x + y * y - 1;
float f = z * z * z - x * x * y * y * y;
putchar(f <= 0.0f ? ".:-=+*#%@"[(int)(f * -8.0f)] : ' ');
}
putchar('\n');
}
system("pause");
return 0;
}
繪制一個(gè)精致愛心
#include <stdio.h>
#include <Windows.h>
#include <math.h>
float f(float x, float y, float z)
{
float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}
float h(float x, float z)
{
for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
if (f(x, y, z) <= 0.0f)
return y;
return 0.0f;
}
int main()
{
for (float z = 1.5f; z > -1.5f; z -= 0.05f)
{
for (float x = -1.5f; x < 1.5f; x += 0.025f)
{
float v = f(x, 0.0f, z);
if (v <= 0.0f)
{
float y0 = h(x, z);
float ny = 0.01f;
float nx = h(x + ny, z) - y0;
float nz = h(x, z + ny) - y0;
float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
putchar(".:-=+*#%@"[(int)(d * 5.0f)]);
}
else
putchar(' ');
}
putchar('\n');
}
system("pause");
return 0;
}
另一個(gè)好的版本
#include<bits/stdc++.h>
#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
using namespace std;
double q,w,e,r,t,u,o,p,s,d,f,g,h,j,l,z,c,v,b,n,m,i;
double k,x,y;
long long a[1000][1000];
int main (int argc,char argv[])
{
Sleep(1000);
HWND hwnd=NULL;
RECT rect;
hwnd=GetForegroundWindow();
GetClientRect(hwnd,&rect);
if(hwnd!=NULL)
{
MoveWindow(hwnd,0,0,0,0,true);Sleep(20);
}
system("mode con cols=300 lines=150");
system("color f4");
for(i=40;i>=-32;i--)
{
for(j=-40;j<=40;j++)
{
y=i/30;
x=j/30;
if(pow(x*x+y*y-1,3)-x*x*y*y*y<=0)
{
cout<<"██";
}
else cout<<" ";
}
cout<<endl;
}
return 0;
}
一個(gè)最花里胡哨的
#include<bits/stdc++.h>
#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
using namespace std;
double q,w,e,r,t,u,o,p,s,d,f,g,h,j,l,z,c,v,b,n,m,i;
double k,x,y;
long long a[1000][1000];
int main()
{
system("mode con cols=100 lines=50");
system("color f4");
for(i=20;i>=-16;i--)
{
for(j=-20;j<=20;j++)
{
y=i/15;
x=j/15;
if(pow(x*x+y*y-1,3)-x*x*y*y*y<=0)
{
cout<<"██";
}
else cout<<" ";
}
cout<<endl;
}
Sleep(5000);
system("cls");
for(i=20;i>=-20;i--)
{
for(j=-20;j<=20;j++)
{
y=i/15;
x=j/15;
if(pow(x*x+y*y-1,3)-x*x*y*y*y<=0)
{
cout<<"愛";
}
else cout<<" ";
}
cout<<endl;
}
Sleep(5000);
system("cls");
for(i=20;i>=-20;i--)
{
for(j=-20;j<=20;j++)
{
y=i/15;
x=j/15;
if(pow(x*x+y*y-1,3)-x*x*y*y*y<=-0.8)
cout<<"康";
else if(pow(x*x+y*y-1,3)-x*x*y*y*y<=-0.6)
cout<<"喜";
else if(pow(x*x+y*y-1,3)-x*x*y*y*y<=-0.48)
cout<<"吉";
else if(pow(x*x+y*y-1,3)-x*x*y*y*y<=-0.32)
cout<<"壽";
else if(pow(x*x+y*y-1,3)-x*x*y*y*y<=-0.18)
cout<<"順";
else if(pow(x*x+y*y-1,3)-x*x*y*y*y<=-0.1)
cout<<"安";
else if(pow(x*x+y*y-1,3)-x*x*y*y*y<=-0.06)
cout<<"樂";
else if(pow(x*x+y*y-1,3)-x*x*y*y*y<=-0.03)
cout<<"貴";
else if(pow(x*x+y*y-1,3)-x*x*y*y*y<=0)
cout<<"福";
else cout<<" ";
}
cout<<endl;
}
return 0;
}
繪制紅心并保存到文件
#include <stdio.h>
#include <Windows.h>
#include <math.h>
float f(float x, float y, float z)
{
float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}
float h(float x, float z)
{
for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
if (f(x, y, z) <= 0.0f)
return y;
return 0.0f;
}
int main()
{
FILE* fp = fopen("heart.ppm", "w");
int sw = 512, sh = 512;
fprintf(fp, "P3\n%d %d\n255\n", sw, sh);
for (int sy = 0; sy < sh; sy++)
{
float z = 1.5f - sy * 3.0f / sh;
for (int sx = 0; sx < sw; sx++)
{
float x = sx * 3.0f / sw - 1.5f;
float v = f(x, 0.0f, z);
int r = 0;
if (v <= 0.0f)
{
float y0 = h(x, z);
float ny = 0.001f;
float nx = h(x + ny, z) - y0;
float nz = h(x, z + ny) - y0;
float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
float d = (nx + ny - nz) / sqrtf(3) * nd * 0.5f + 0.5f;
r = (int)(d * 255.0f);
}
fprintf(fp, "%d 0 0 ", r);
}
fputc('\n', fp);
}
fclose(fp);
system("pause");
}
繪制跳動(dòng)的心臟:注此版本只能在Windows系統(tǒng)下編譯運(yùn)行。
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <tchar.h>
float f(float x, float y, float z)
{
float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}
float h(float x, float z)
{
for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
if (f(x, y, z) <= 0.0f)
return y;
return 0.0f;
}
int main()
{
HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
_TCHAR buffer[25][80] = { _T(' ') };
_TCHAR ramp[] = _T(".:-=+*#%@");
for (float t = 0.0f;; t += 0.1f)
{
int sy = 0;
float s = sinf(t);
float a = s * s * s * s * 0.2f;
for (float z = 1.3f; z > -1.2f; z -= 0.1f)
{
_TCHAR* p = &buffer[sy++][0];
float tz = z * (1.2f - a);
for (float x = -1.5f; x < 1.5f; x += 0.05f)
{
float tx = x * (1.2f + a);
float v = f(tx, 0.0f, tz);
if (v <= 0.0f)
{
float y0 = h(tx, tz);
float ny = 0.01f;
float nx = h(tx + ny, tz) - y0;
float nz = h(tx, tz + ny) - y0;
float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
*p++ = ramp[(int)(d * 5.0f)];
}
else
*p++ = ' ';
}
}
for (sy = 0; sy < 25; sy++)
{
COORD coord = { 0, sy };
SetConsoleCursorPosition(o, coord);
WriteConsole(o, buffer[sy], 79, NULL, 0);
}
Sleep(33);
}
}
升級(jí)版
include <stdio.h>
include <math.h>
include <windows.h>
include <tchar.h>
float f(float x, float y, float z) {
float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}
float h(float x, float z) {
for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
if (f(x, y, z) <= 0.0f)
return y;
return 0.0f;
}
int main() {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x0C);
HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
_TCHAR buffer[25][80] = { _T(' ') };
_TCHAR ramp[] = _T(".:-=+*#%@");
int count= 0;
int count1=0;
//system("color F4");
for (float t = 0.0f;; t += 0.1f) {
int sy = 0;
float s = sinf(t);
float a = s * s * s * s * 0.2f;
for (float z = 1.3f; z > -1.2f; z -= 0.1f) {
_TCHAR* p = &buffer[sy++][0];
float tz = z * (1.2f - a);
for (float x = -1.5f; x < 1.5f; x += 0.05f) {
float tx = x * (1.2f + a);
float v = f(tx, 0.0f, tz);
if (v <= 0.0f) {
float y0 = h(tx, tz);
float ny = 0.01f;
float nx = h(tx + ny, tz) - y0;
float nz = h(tx, tz + ny) - y0;
float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
*p++ = ramp[(int)(d * 5.0f)];
}
else
*p++ = ' ';
}
}
for (sy = 0; sy < 25; sy++) {
COORD coord = { 0, sy };
SetConsoleCursorPosition(o, coord);
WriteConsole(o, buffer[sy], 79, NULL, 0);
}
if(count <= 10){
printf("I Love You ———.Mua~\n") ;//表白內(nèi)容
printf(" By 編程愛我");// 表白者的名字
count++;
} else{
printf("You Are My Best Lover.\n");
printf(" Stephen Ge");
count++;
if(count>=20){
count =0;
}
}
Sleep(33);
}
}
其他做法html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<canvas width="400" height="400"></canvas>
<script>
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
context.lineWidth = 3;
// 將畫布的原點(diǎn)(0,0),移動(dòng)到(200,200)
// 移動(dòng)原點(diǎn)是為了能讓整個(gè)心形顯示出來
context.translate(200,200);
// t 代表弧度
var t=0;
// maxt 代表 t 的最大值
var maxt = 2*Math.PI;
// vt 代表 t 的增量
var vt = 0.01;
// 需要循環(huán)的次數(shù)
var maxi = Math.ceil(maxt/vt);
// 保存所有點(diǎn)的坐標(biāo)的數(shù)組
var pointArr=[];
// x 用來暫時(shí)保存每次循環(huán)得到的 x 坐標(biāo)
var x=0;
// y 用來暫時(shí)保存每次循環(huán)得到的 y 坐標(biāo)
var y=0;
// 根據(jù)方程得到所有點(diǎn)的坐標(biāo)
for(var i=0;i<=maxi;i++){
// x=a*(2*sin(t)+sin(2*t))
x=50*(2*Math.sin(t)+Math.sin(2*t));
// y=a*(2*cos(t)+cos(2*t))
y=50*(2*Math.cos(t)+Math.cos(2*t));
t+=vt;
pointArr.push([x,y]);
}
// 根據(jù)點(diǎn)的坐標(biāo),畫出心形線
context.moveTo(pointArr[0][0],pointArr[0][1]);
draw();
function draw(){
context.fillStyle='#c00';
// 把每個(gè)點(diǎn)連接起來
for(var i=1;i<pointArr.length;i++){
x = pointArr[i][0];
y = pointArr[i][1];
context.lineTo(x,y);
}
context.fill();
}
</script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<canvas width="400" height="400"></canvas>
<script>
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
// 將畫布的原點(diǎn)(0,0),移動(dòng)到(200,100)
// 移動(dòng)原點(diǎn)是為了能讓整個(gè)心形顯示出來
context.translate(200, 100);
// 畫心形
draw();
function draw() {
// 畫圓弧時(shí),圓的半徑
var r = 0;
// start 代表畫弧線時(shí)的 起始角
var start = 0;
// end 代表畫弧線時(shí)的 結(jié)束角
var end = 0;
// 一個(gè)常數(shù),用來控制心形的大小
var a = 100;
context.fillStyle = '#e21f27';
//連續(xù)的畫圓弧
for (var q = 0; q < 500; q++) {
start += Math.PI * 2 / 500;
// 當(dāng) 結(jié)束角 是 Math.PI * 2 時(shí)也就是已經(jīng)畫了一圈了,心形就出來了
end = start + Math.PI * 2 / 500;
// 根據(jù)極坐標(biāo)方程 r=a(1+sinθ),得到 r(半徑)
r = a * (1 + Math.sin(start));
// 畫弧線
context.arc(0, 0, r, start, end, false);
}
context.fill();
}
</script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<canvas width="400" height="400"></canvas>
<script>
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
context.lineWidth = 3;
// 將畫布的原點(diǎn)(0,0),移動(dòng)到(200,200)
// 移動(dòng)原點(diǎn)是為了能讓整個(gè)心形顯示出來
context.translate(200,200);
// t 代表弧度
var t=0;
// vt 代表 t 的增量
var vt = 0.01;
// maxt 代表 t 的最大值
var maxt = 2*Math.PI;
// 需要循環(huán)的次數(shù)
var maxi = Math.ceil(maxt/vt);
// 保存所有點(diǎn)的坐標(biāo)的數(shù)組
var pointArr=[];
// 控制心形大小
var size = 10;
// x 用來暫時(shí)保存每次循環(huán)得到的 x 坐標(biāo)
var x=0;
// y 用來暫時(shí)保存每次循環(huán)得到的 y 坐標(biāo)
var y=0;
// 根據(jù)方程得到所有點(diǎn)的坐標(biāo)
for(var i=0;i<=maxi;i++){
// x=16 * (sin(t)) ^ 3;
var x = 16 * Math.pow(Math.sin(t),3);
// y=13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t)
var y = 13 * Math.cos(t) - 5 * Math.cos(2 * t) -2 * Math.cos(3 * t)- Math.cos(4 * t);
t+=vt;
pointArr.push([x*size,-y*size]);
}
// 根據(jù)點(diǎn)的坐標(biāo),畫出心形線
context.moveTo(pointArr[0][0],pointArr[0][1]);
draw();
function draw(){
context.fillStyle='#c00';
// 把每個(gè)點(diǎn)連接起來
for(var i=1;i<pointArr.length;i++){
x = pointArr[i][0];
y = pointArr[i][1];
context.lineTo(x,y);
}
context.fill();
}
</script>
</body>
</html>
文章來源:http://www.zghlxwxcb.cn/news/detail-716231.html
也許我們還可以再做點(diǎn)什么,比如加點(diǎn)動(dòng)畫,看看下面這個(gè)吧。
點(diǎn)這里下載源碼,里面已經(jīng)加了很詳細(xì)的注釋了。文章來源地址http://www.zghlxwxcb.cn/news/detail-716231.html
到了這里,關(guān)于C++ 實(shí)現(xiàn)命令行畫心形代碼,有多個(gè)愛心代碼,簡單可調(diào)數(shù)據(jù)和字符,可裝X,也可用于浪漫的表白,可實(shí)現(xiàn)跳動(dòng)、保存等功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!