• 【双十一特辑】爱心代码(程序员的浪漫)-李峋


    前言

    最近《点燃我温暖你》中李峋的爱心代码超级火,看着特别心动,这不,光棍节快到了,给兄弟们教学一波爱心代码,赶在双十一前表白,让这个双十一不在是孤单一个人!

    目录

    前言

    C语言简易爱心代码

    原理

    代码

    执行结果

    C语言动态爱心代码

    涉及知识点

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),字体色)

    sheep()

    代码

    执行结果

    Python简易爱心代码

    准备工作

    ​编辑

    涉及知识点

    np.linspace()

    np.sin(​)

    np.cos(​)

    plt.plot()

    plt.show()

    原理

     代码

     执行结果

    Python动态爱心代码

    代码

    执行结果

    HTML动态爱心代码

    代码

    执行结果

    真表白使用的(不懂编程也能学会)

    代码

    ​效果图

    彩蛋-红色炫酷爱心

    链接

    效果图


    C语言简易爱心代码

    原理

    心形线直角坐标式(x^2+y^2-1)^3=x^2*y^3

    让a=x^2+y^2-1,那么a*a*a就是(x^2+y^2-1)^3,有数学定理易得(x^2+y^2-1)^3<=x^2*y^3是为心形线里面的部分包括心形线,那么只要满足(x^2+y^2-1)^3<=x^2*y^3就输出某个指定符号,不满足就输出空格,就可以获得由这个字符组成的爱心,下面我使用的是'v'当指定字符,用三目运算符判断是否满足(x^2+y^2-1)^3<=x^2*y^3,注意输出完一行要换行。

    代码

    1. #include <stdio.h>
    2. int main() {
    3. for (float y = 2.0f; y > -2.0f; y -= 0.1f) {
    4. for (float x = -2.0f; x < 2.0f; x += 0.05f) {
    5. float a = x * x + y * y - 1;
    6. putchar(a * a * a - x * x * y * y * y <= 0.0f ? 'v' : ' ');
    7. }
    8. putchar('\n');
    9. }
    10. }

    执行结果

    C语言动态爱心代码

    涉及知识点

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),字体色)

    SetConsoleTextAttribute()是Windows系统中一个可以设置控制台窗口字体颜色和背景色的计算机函数,常用的几种颜色:

    0=黑色 1=蓝色 2=绿色 4=红色 3=湖蓝色 5=紫色 6=黄色 7=白色 8=灰色

    sheep()

    执行挂起一段时间

    代码

    1. #include <stdio.h>
    2. #include <math.h>
    3. #include <windows.h>
    4. #include <tchar.h>
    5. float f(float x, float y, float z) {
    6. float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
    7. return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
    8. }
    9. float h(float x, float z) {
    10. for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
    11. if (f(x, y, z) <= 0.0f)
    12. return y;
    13. return 0.0f;
    14. }
    15. int main() {
    16. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
    17. 0xc);//SetConsoleTextAttribute()是Windows系统中一个可以设置控制台窗口字体颜色和背景色的计算机函数
    18. HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);// GetStdHandle()检索指定标准设备的句柄(标准输入、标准输出或标准错误)
    19. _TCHAR buffer[25][80] = { _T(' ') };
    20. _TCHAR ramp[] = _T("vvvvvvvv");
    21. int count = 0;
    22. int count1 = 0;
    23. for (float t = 0.0f;; t += 0.1f) {
    24. int sy = 0;
    25. float s = sinf(t);
    26. float a = s * s * s * s * 0.2f;
    27. for (float z = 1.3f; z > -1.2f; z -= 0.1f) {
    28. _TCHAR *p = &buffer[sy++][0];
    29. float tz = z * (1.2f - a);
    30. for (float x = -1.5f; x < 1.5f; x += 0.05f) {
    31. float tx = x * (1.2f + a);
    32. float v = f(tx, 0.0f, tz);
    33. if (v <= 0.0f) {
    34. float y0 = h(tx, tz);
    35. float ny = 0.01f;
    36. float nx = h(tx + ny, tz) - y0;
    37. float nz = h(tx, tz + ny) - y0;
    38. float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
    39. float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
    40. *p++ = ramp[(int)(d * 5.0f)];
    41. } else
    42. *p++ = ' ';
    43. }
    44. }
    45. for (sy = 0; sy < 25; sy++) {
    46. COORD coord = { 0, sy };
    47. SetConsoleCursorPosition(o, coord);//作用是设置控制台(cmd)光标位置
    48. WriteConsole(o, buffer[sy], 79, NULL, 0);//从当前光标位置开始,将字符串写入控制台屏幕缓冲区
    49. }
    50. if (count <= 22) {
    51. printf("I Love You") ;//表白内容
    52. printf(" To CSDN");// 被表白者的名字
    53. count++;
    54. } else {
    55. printf("You Are My Best Lover.\n");
    56. count++;
    57. if (count >= 44) {
    58. count = 0;
    59. }
    60. }
    61. Sleep(36);//Sleep函数:执行挂起一段时间,也就是等待一段时间在继续执行
    62. }
    63. }

    执行结果

    Python简易爱心代码

    准备工作

    下载matplotlib软件包

    涉及知识点

    np.linspace()

    用于返回指定区间等间隔的数组,例如np.linspace(0,2*np.pi)就是0到2π等间隔的数组

    np.sin(θ)

    对中θ元素取正弦值 

    np.cos(θ)

    对中θ元素取余弦值 

    plt.plot()

    是matplotlib.pyplot模块下的一个函数, 用于画图,它可以绘制点和线

    plt.show()

    展示图像

    原理

    原始的心形线的极坐标方程为r=a(1-cosθ)

    与其对应的参数方程是:

    x(θ)=2r(sinθ-(sin2θ)/2)

    y(θ)= 2r(cosθ-(cos2θ)/2),(0<=θ<=2π)

     代码

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. t=np.linspace(0,2*np.pi)#用于返回指定区间等间隔的数组
    4. x=2*1*(np.cos(t)-np.cos(2*t)/2)
    5. y=2*1*(np.sin(t)-np.sin(2*t)/2)
    6. plt.plot(y,x,c='purple')#c=''控制颜色
    7. plt.show()

     执行结果

    Python动态爱心代码

    这个也是最还原的,代码过长,下面仅展示爱心的基础函数,需要的朋友可以去《点燃我温暖你》中李峋的同款爱心代码-Python文档类资源-CSDN文库下载,我设置的是免费下载

    代码

    1. x = 16 * (sin(t) ** 3)
    2. y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))

    执行结果

    HTML动态爱心代码

    代码

    由于代码过长,源码放在资源html网页做的动态爱心(超好看)-Javascript文档类资源-CSDN文库里,可以免费下载

    执行结果

    真表白使用的(不懂编程也能学会)

    直达:💗

    代码

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8" />
    5. <title>💗</title>
    6. <style>
    7. html,
    8. body {
    9. height: 100%;
    10. padding: 0;
    11. margin: 0;
    12. background: #000;
    13. }
    14. canvas {
    15. position: absolute;
    16. width: 100%;
    17. height: 100%;
    18. animation: anim 1.5s ease-in-out infinite;
    19. -webkit-animation: anim 1.5s ease-in-out infinite;
    20. -o-animation: anim 1.5s ease-in-out infinite;
    21. -moz-animation: anim 1.5s ease-in-out infinite;
    22. }
    23. #name {
    24. position: absolute;
    25. top: 50%;
    26. left: 50%;
    27. transform: translate(-50%, -50%);
    28. margin-top: -20px;
    29. font-size: 46px;
    30. color: #ea80b0;
    31. }
    32. @keyframes anim {
    33. 0% {
    34. transform: scale(0.8);
    35. }
    36. 25% {
    37. transform: scale(0.7);
    38. }
    39. 50% {
    40. transform: scale(1);
    41. }
    42. 75% {
    43. transform: scale(0.7);
    44. }
    45. 100% {
    46. transform: scale(0.8);
    47. }
    48. }
    49. @-webkit-keyframes anim {
    50. 0% {
    51. -webkit-transform: scale(0.8);
    52. }
    53. 25% {
    54. -webkit-transform: scale(0.7);
    55. }
    56. 50% {
    57. -webkit-transform: scale(1);
    58. }
    59. 75% {
    60. -webkit-transform: scale(0.7);
    61. }
    62. 100% {
    63. -webkit-transform: scale(0.8);
    64. }
    65. }
    66. @-o-keyframes anim {
    67. 0% {
    68. -o-transform: scale(0.8);
    69. }
    70. 25% {
    71. -o-transform: scale(0.7);
    72. }
    73. 50% {
    74. -o-transform: scale(1);
    75. }
    76. 75% {
    77. -o-transform: scale(0.7);
    78. }
    79. 100% {
    80. -o-transform: scale(0.8);
    81. }
    82. }
    83. @-moz-keyframes anim {
    84. 0% {
    85. -moz-transform: scale(0.8);
    86. }
    87. 25% {
    88. -moz-transform: scale(0.7);
    89. }
    90. 50% {
    91. -moz-transform: scale(1);
    92. }
    93. 75% {
    94. -moz-transform: scale(0.7);
    95. }
    96. 100% {
    97. -moz-transform: scale(0.8);
    98. }
    99. }
    100. </style>
    101. </head>
    102. <body>
    103. <canvas id="pinkboard"></canvas>
    104. <!-- 在下面加名字 -->
    105. <div id="name" style="color: blue;">CSDN</div>
    106. <script>
    107. var settings = {
    108. particles: {
    109. length: 500,
    110. duration: 2,
    111. velocity: 100,
    112. effect: -0.75,
    113. size: 30,
    114. },
    115. };
    116. (function () {
    117. var b = 0;
    118. var c = ["ms", "moz", "webkit", "o"];
    119. for (var a = 0; a < c.length && !window.requestAnimationFrame; ++a) {
    120. window.requestAnimationFrame = window[c[a] + "RequestAnimationFrame"];
    121. window.cancelAnimationFrame =
    122. window[c[a] + "CancelAnimationFrame"] ||
    123. window[c[a] + "CancelRequestAnimationFrame"];
    124. }
    125. if (!window.requestAnimationFrame) {
    126. window.requestAnimationFrame = function (h, e) {
    127. var d = new Date().getTime();
    128. var f = Math.max(0, 16 - (d - b));
    129. var g = window.setTimeout(function () {
    130. h(d + f);
    131. }, f);
    132. b = d + f;
    133. return g;
    134. };
    135. }
    136. if (!window.cancelAnimationFrame) {
    137. window.cancelAnimationFrame = function (d) {
    138. clearTimeout(d);
    139. };
    140. }
    141. })();
    142. var Point = (function () {
    143. function Point(x, y) {
    144. this.x = typeof x !== "undefined" ? x : 0;
    145. this.y = typeof y !== "undefined" ? y : 0;
    146. }
    147. Point.prototype.clone = function () {
    148. return new Point(this.x, this.y);
    149. };
    150. Point.prototype.length = function (length) {
    151. if (typeof length == "undefined")
    152. return Math.sqrt(this.x * this.x + this.y * this.y);
    153. this.normalize();
    154. this.x *= length;
    155. this.y *= length;
    156. return this;
    157. };
    158. Point.prototype.normalize = function () {
    159. var length = this.length();
    160. this.x /= length;
    161. this.y /= length;
    162. return this;
    163. };
    164. return Point;
    165. })();
    166. var Particle = (function () {
    167. function Particle() {
    168. this.position = new Point();
    169. this.velocity = new Point();
    170. this.acceleration = new Point();
    171. this.age = 0;
    172. }
    173. Particle.prototype.initialize = function (x, y, dx, dy) {
    174. this.position.x = x;
    175. this.position.y = y;
    176. this.velocity.x = dx;
    177. this.velocity.y = dy;
    178. this.acceleration.x = dx * settings.particles.effect;
    179. this.acceleration.y = dy * settings.particles.effect;
    180. this.age = 0;
    181. };
    182. Particle.prototype.update = function (deltaTime) {
    183. this.position.x += this.velocity.x * deltaTime;
    184. this.position.y += this.velocity.y * deltaTime;
    185. this.velocity.x += this.acceleration.x * deltaTime;
    186. this.velocity.y += this.acceleration.y * deltaTime;
    187. this.age += deltaTime;
    188. };
    189. Particle.prototype.draw = function (context, image) {
    190. function ease(t) {
    191. return --t * t * t + 1;
    192. }
    193. var size = image.width * ease(this.age / settings.particles.duration);
    194. context.globalAlpha = 1 - this.age / settings.particles.duration;
    195. context.drawImage(
    196. image,
    197. this.position.x - size / 2,
    198. this.position.y - size / 2,
    199. size,
    200. size
    201. );
    202. };
    203. return Particle;
    204. })();
    205. var ParticlePool = (function () {
    206. var particles,
    207. firstActive = 0,
    208. firstFree = 0,
    209. duration = settings.particles.duration;
    210. function ParticlePool(length) {
    211. particles = new Array(length);
    212. for (var i = 0; i < particles.length; i++)
    213. particles[i] = new Particle();
    214. }
    215. ParticlePool.prototype.add = function (x, y, dx, dy) {
    216. particles[firstFree].initialize(x, y, dx, dy);
    217. firstFree++;
    218. if (firstFree == particles.length) firstFree = 0;
    219. if (firstActive == firstFree) firstActive++;
    220. if (firstActive == particles.length) firstActive = 0;
    221. };
    222. ParticlePool.prototype.update = function (deltaTime) {
    223. var i;
    224. if (firstActive < firstFree) {
    225. for (i = firstActive; i < firstFree; i++)
    226. particles[i].update(deltaTime);
    227. }
    228. if (firstFree < firstActive) {
    229. for (i = firstActive; i < particles.length; i++)
    230. particles[i].update(deltaTime);
    231. for (i = 0; i < firstFree; i++) particles[i].update(deltaTime);
    232. }
    233. while (
    234. particles[firstActive].age >= duration &&
    235. firstActive != firstFree
    236. ) {
    237. firstActive++;
    238. if (firstActive == particles.length) firstActive = 0;
    239. }
    240. };
    241. ParticlePool.prototype.draw = function (context, image) {
    242. if (firstActive < firstFree) {
    243. for (i = firstActive; i < firstFree; i++)
    244. particles[i].draw(context, image);
    245. }
    246. if (firstFree < firstActive) {
    247. for (i = firstActive; i < particles.length; i++)
    248. particles[i].draw(context, image);
    249. for (i = 0; i < firstFree; i++) particles[i].draw(context, image);
    250. }
    251. };
    252. return ParticlePool;
    253. })();
    254. (function (canvas) {
    255. var context = canvas.getContext("2d"),
    256. particles = new ParticlePool(settings.particles.length),
    257. particleRate =
    258. settings.particles.length / settings.particles.duration,
    259. time;
    260. function pointOnHeart(t) {
    261. return new Point(
    262. 160 * Math.pow(Math.sin(t), 3),
    263. 130 * Math.cos(t) -
    264. 50 * Math.cos(2 * t) -
    265. 20 * Math.cos(3 * t) -
    266. 10 * Math.cos(4 * t) +
    267. 25
    268. );
    269. }
    270. var image = (function () {
    271. var canvas = document.createElement("canvas"),
    272. context = canvas.getContext("2d");
    273. canvas.width = settings.particles.size;
    274. canvas.height = settings.particles.size;
    275. function to(t) {
    276. var point = pointOnHeart(t);
    277. point.x =
    278. settings.particles.size / 2 +
    279. (point.x * settings.particles.size) / 350;
    280. point.y =
    281. settings.particles.size / 2 -
    282. (point.y * settings.particles.size) / 350;
    283. return point;
    284. }
    285. context.beginPath();
    286. var t = -Math.PI;
    287. var point = to(t);
    288. context.moveTo(point.x, point.y);
    289. while (t < Math.PI) {
    290. t += 0.01;
    291. point = to(t);
    292. context.lineTo(point.x, point.y);
    293. }
    294. context.closePath();
    295. context.fillStyle = "#ea80b0";
    296. context.fill();
    297. var image = new Image();
    298. image.src = canvas.toDataURL();
    299. return image;
    300. })();
    301. function render() {
    302. requestAnimationFrame(render);
    303. var newTime = new Date().getTime() / 1000,
    304. deltaTime = newTime - (time || newTime);
    305. time = newTime;
    306. context.clearRect(0, 0, canvas.width, canvas.height);
    307. var amount = particleRate * deltaTime;
    308. for (var i = 0; i < amount; i++) {
    309. var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
    310. var dir = pos.clone().length(settings.particles.velocity);
    311. particles.add(
    312. canvas.width / 2 + pos.x,
    313. canvas.height / 2 - pos.y,
    314. dir.x,
    315. -dir.y
    316. );
    317. }
    318. particles.update(deltaTime);
    319. particles.draw(context, image);
    320. }
    321. function onResize() {
    322. canvas.width = canvas.clientWidth;
    323. canvas.height = canvas.clientHeight;
    324. }
    325. window.onresize = onResize;
    326. setTimeout(function () {
    327. onResize();
    328. render();
    329. }, 10);
    330. })(document.getElementById("pinkboard"));
    331. </script>
    332. </body>
    333. </html>

    首先建一个txt文件

    打开将代码粘进去并保存

     

    找到    

    这里加你想要加的文字
     

    如果你想要修改文字颜色的话,将上面的blue修改为你想要的颜色

    然后保存退出,将文件名修改为表白.html,回车双击

    如果修改完文件名还是代码就看看文件>查看>文件拓展名选了没

     效果图

    彩蛋-红色炫酷爱心

    链接

    直达:💗

    下载:http://t.csdn.cn/80ICX

    效果图

     👍+✏️+⭐️是对博主最大的鼓励与支持!!!

  • 相关阅读:
    使用html+css实现一个静态页面(厦门旅游网站制作6个页面) 旅游网页设计制作 HTML5期末考核大作业,网站——美丽家乡。 学生旅行 游玩 主题住宿网页
    spark读取hive表字段,区分大小写问题
    [JMeter]Beanshell解析Json格式的接口响应数据
    RACV2022观点集锦 | 视觉基础模型
    视频 | 生信Linux - Linux下文件内容操作03
    VulnHub DC-6
    极致性能优化之道之消除伪共享
    让人头痛的事务问题到底要如何解决? (荣耀典藏版)
    ASCII_Util.java
    文件存储解决方案-云存储阿里 OSS
  • 原文地址:https://blog.csdn.net/m0_67388084/article/details/127728441