C语言打印心形

#include <stdio.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 a = x * x + y * y - 1;
putchar(a * a * a - x * x * y * y * y <= 0.0f ? ‘*‘ : ‘ ‘);
}
putchar(‘\n‘);
}
}

*********************************************************************************************

#include <stdio.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‘);
}
}

*********************************************************************************************

#include <stdio.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‘);
}
}

*********************************************************************************************

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.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);
}

*********************************************************************************************

#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);
}
}

原文地址:https://www.cnblogs.com/LyShark/p/9064141.html

时间: 2024-08-02 10:04:36

C语言打印心形的相关文章

打印心形---print 的基础使用

#!/bin/usr/env python#coding=utf-8'''用学习到的print语句,完成如下图形的打印工作打印心形'''print " *** *** "print " ********* *********"print "************ ************"print "************* *************"print "************** *******

c++打印心形

用c++打印一个心形的图案: 1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 int main() 5 { 6     float x, y; 7     for (y = 1.5f; y >-1.5f; y -= 0.1f) 8     { 9         for (x = -1.5f; x <1.5f; x += 0.05f)10         {11             fl

打印心形

#include <stdio.h> #include <stdlib.h> int isEven(int number); void printLovingHeart(int number,int left); //打印爱心 void printUpLovingHeart(int number,int left); //打印爱心的上半部分 void printDownTri(int number, int left); //打印边长为number的向下正三角形,左边空left列

知乎上的文章, 用 C 打印心形

//normal #include <stdio.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 a = x * x + y * y - 1; putchar(a * a * a - x * x * y * y * y <= 0.0f ? '*' : ' '); } putchar('\n'); }

一个C语言实现的心形闪烁

前期准备: 1.打印出心形需要使用 \3 进行输出 2.想更改终端(命令行)同一个位置的输出字符,需要使用 \b 退格将光标回退,之后写入空格,再回退即可 3.想要闪烁的话需要调用系统进程休眠函数,在windows下使用Sleep(ms);函数,需包含<windows.h>头文件 4.具体逻辑看代码 1 #include<stdio.h> 2 #include<windows.h> 3 int main() 4 { 5 int a=100; 6 while(a) 7 {

心形java2019/10/17

在网上无意中看到这个代码,学习了一下心形函数的知识:http://mathworld.wolfram.com/HeartCurve.html package dada; /** * 了解Heart Curve函数中的 一种:(X的平方+Y的平方-1)-X的平方乘以Y的三次方 */ public class HeartTest { public static void main(String[] args) { //i对应X j对应Y for (float i = 1.5f; i > -1.5f;

心形动画

<!doctype html><html><head><meta charset="utf-8"><title>心形动画代码</title><script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script><style>body { background:black;}.heart

C语言打印记事本内搜索字符串所在行信息

本程序采用C语言编写,使用方法: 1.双击“甲骨文字符串查询作品.exe”运行程序; 2.运行前请确保此可执行程序目录下有1.txt文件. 3.根据提示输入一个字符串,程序将显示存在所搜索字符串的所有行! 程序如果问题,请联系[email protected]! 2014-7-31日安阳师范学院机房完成. 程序截图: 源码如下: #include <stdio.h> #include<string.h> #include<stdlib.h> #include<co

[控件] 心形加载的view

心形加载的view 效果: 素材图片: 源码: StarView.h 与 StarView.m // // StarView.h // Star // // Created by XianMingYou on 15/3/13. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import <UIKit/UIKit.h> @interface StarView : UIView @property (nonatomic,