爱心代码html或c++调用opengl库两种实现(二维三维动态也可键盘交互)
最近打火机与公主裙电视剧追疯了!!!谁还没有李峋爱心代码!!快来领!!沉浸式追剧大学生今天午觉没睡怒干爱心代码现有三分资源如下:
效果:
1)、公主两个for循环二维C控制台输出爱心;
2)、原创C
最近打火机与公主裙电视剧追疯了!!!谁还没有李峋爱心代码!!快来领!!沉浸式追剧大学生今天午觉没睡怒干爱心代码现有三分资源如下:
效果:
1)、公主两个for循环二维C控制台输出爱心;
2)、原创C语言利用openGL库实现三维动态旋转粉爱心;
以上两者可以在一个代码文件中同时实现,所以写在了一起,全部代码包括注释实现如下所示:
注:前提是安装了OpenGL库!
#include stdlib.h
#include glut.h
#include <time.h>
#include<iostream>
#define STEP 1.0f
#pragma comment(lib,glut2.lib)
using namespace std;
//#define _USE_MATH_DEFIES //用到math里面的宏常量
#include <math.h>
GLfloat w = 840;
GLfloat h = 800;
double rotate_x = 0.0;//选转
double rotate_y = 0.0;
double rotate_z = 0.0;
// 对颜的宏定义
# define Hotpink 255.0/255.0, 105.0/255.0, 180.0/255.0
# define sunshine 255.0/255.0, 210.0/255.0, 166.0/255.0
#define BMP_Header_Length 54 //图像数据在内存块中的偏移量
// 旋转初始的角度
GLfloat angle = 0.0f;
// 设置旋转轴:两个三维的点确定的旋转轴
GLfloat axis[][] = {
0.0f, 0.5f, 0.5f,
1.0f, 0.5f, 0.5f
};
GLfloat tra[] = { -00, 0, -470 };
//材质参数
GLfloat matiral_specular[4] = { 0.00, 0.00, 0.00, 1.00 }; //材质参数:镜面反射,所得到的光的强度(颜)
GLfloat matiral_emission[4] = { 0.00, 0.00, 0.00, 1.00 }; //材质本身就微微的向外发射光线
ct GLfloat matiral_shininess = 11.00;
void setMetiral(GLfloat matiral_diffuse_ambient[4])
{
//设置颜
glMaterialfv(GL_FROT_AD_BACK, GL_AMBIET_AD_DIFFUSE, matiral_diffuse_ambient); //设置相同值更逼真
//设置参数
glMaterialfv(GL_FROT_AD_BACK, GL_SPECULAR, matiral_specular); //镜面反射,所得到的光的强度(颜)
glMaterialfv(GL_FROT_AD_BACK, GL_EMISSIO, matiral_emission); //材质本身就微微的向外发射光线,以至于眼睛感觉到它有这样的颜
glMaterialf(GL_FROT_AD_BACK, matiral_shininess, 0); //“镜面指数”(0~128) 该值越小,材质越粗糙,点光源发射的光线照射到上面,也可以产生较大的亮点。该值越大,表示材质越类似于镜面,光源照射到上面后,产生较小的亮点。
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); //增加M矩阵
glRotatef(rotate_x, 1.0, 0.0, 0.0);//绕x轴旋转
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glRotatef(rotate_z, 0.0, 0.0, 1.0);
glTranslatef(tra[0], tra[1], tra[2]);//平移
// 设置绕给定的轴旋转
glTranslatef(00, 0, 0);
glRotatef(angle, axis[1][1] - axis[0][1], axis[0][0] - axis[1][0], axis[1][2] - axis[0][2]);
glTranslatef(-00, 0, 0);
**************************************爱心*************
glColorf(Hotpink);
glTranslatef(00, 200, 0);//1
glutSolidSphere(70.0, 200, 200);//(半径/球面数)
glTranslatef(-00, -200, 0);
glColorf(Hotpink);
glTranslatef(540, 200, 0);//2
glutSolidSphere(10.0, 200, 200);//(半径/球面数)
glTranslatef(-540, -200, 0);
glColorf(Hotpink);
glTranslatef(60, 200, 0);//
glutSolidSphere(10.0, 200, 200);//(半径/球面数)
glTranslatef(-60, -200, 0);
glColorf(Hotpink);
glTranslatef(180, 260, 0);//4
glutSolidSphere(100.0, 200, 200);//(半径/球面数)
glTranslatef(-180, -260, 0);
glColorf(Hotpink);
glTranslatef(420, 260, 0);//5
glutSolidSphere(100.0, 200, 200);//(半径/球面数)
glTranslatef(-420, -260, 0);
glColorf(Hotpink);
glTranslatef(00, -180, 0);//6
glutSolidSphere(90.0, 200, 200);//(半径/球面数)
glTranslatef(-00, 180, 0);
glColorf(Hotpink);
glTranslatef(420, -28, 0);//7
glutSolidSphere(100.0, 200, 200);//(半径/球面数)
glTranslatef(-420, 28, 0);
glColorf(Hotpink);
glTranslatef(180, -28, 0);//8
glutSolidSphere(100.0, 200, 200);//(半径/球面数)
glTranslatef(-180, 28, 0);
glColorf(Hotpink);
glTranslatef(480, 90, 0);//9
glutSolidSphere(120.0, 200, 200);//(半径/球面数)
glTranslatef(-480, -90, 0);
glColorf(Hotpink);
glTranslatef(120, 90, 0);//10
glutSolidSphere(120.0, 200, 200);//(半径/球面数)
glTranslatef(-120, -90, 0);
glColorf(Hotpink);
glTranslatef(120, 270, 0);//11
glutSolidSphere(100.0, 200, 200);//(半径/球面数)
glTranslatef(-120, -270, 0);
glColorf(Hotpink);
glTranslatef(480, 270, 0);//12
glutSolidSphere(100.0, 200, 200);//(半径/球面数)
glTranslatef(-480, -270, 0);
glColorf(Hotpink);
glTranslatef(240, -100, 0);//1
glutSolidSphere(90.0, 200, 200);//(半径/球面数)
glTranslatef(-240, 100, 0);
glColorf(Hotpink);
glTranslatef(60, -100, 0);//14
glutSolidSphere(90.0, 200, 200);//(半径/球面数)
glTranslatef(-60, 100, 0);
glColorf(Hotpink);
glTranslatef(00, -50, 0);//15
glutSolidSphere(90.0, 200, 200);//(半径/球面数)
glTranslatef(-00, 50, 0);
glColorf(Hotpink);
glTranslatef(00, 50, 0);//16
glutSolidSphere(100.0, 200, 200);//(半径/球面数)
glTranslatef(-00, -50, 0);
glColorf(Hotpink);
glTranslatef(260, 100, 0);//17
glutSolidSphere(100.0, 200, 200);//(半径/球面数)
glTranslatef(-260, -100, 0);
glColorf(Hotpink);
glTranslatef(40, 100, 0);//18
glutSolidSphere(100.0, 200, 200);//(半径/球面数)
glTranslatef(-40, -100, 0);
//glColorf(Hotpink);
//glTranslatef(00, 40, -50);//19
//glutSolidSphere(200.0, 200, 200);//(半径/球面数)
//glTranslatef(-00, -40, 50);
//***************************************************
glFlush(); // 刷新
glutSwapBuffers();//双缓冲
}
// 窗口大小变化回调函数
void reshape(int w, int h) {
glViewport(0, 0, w, h);//定义视口大小
glMatrixMode(GL_PROJECTIO);//为投影变换指定当前矩阵
glLoadIdentity();//设置当前矩阵为单位矩阵
gluPerspective(90.0, (GLfloat)w / (GLfloat)h, 0.1, 100000.0);//设置透视投影矩阵,指定了要观察的视景体在世界坐标系中的具体大小
//(视角,横纵比,近处裁面,远处裁面)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// 动画所需的定时器回调函数
void timer_function(GLint value)
{
// 旋转角度增加
angle = STEP;
// 若角度大于60转完一圈则清零
if (angle > 60.0) angle -= 60.0;
glutPostRedisplay();
glutTimerFunc(, timer_function, value);
}
void init(void)
{
GLfloat sun_direction[] = { 700.0, 400.0, 100.0, 1.0 };
GLfloat sun_intensity[] = { sunshine, 1.0 };
GLfloat ambient_intensity[] = { 0.5, 0.5, 0.5, 1.0 };//周围环境的强度
glEnable(GL_LIGHTIG); // Set up ambient light.环境光
glLightModelfv(GL_LIGHT_MODEL_AMBIET, ambient_intensity);//全局光照
glEnable(GL_LIGHT0); // Set up sunlight.漫反射
glLightfv(GL_LIGHT0, GL_POSITIO, sun_direction);
glLightfv(GL_LIGHT0, GL_DIFFUSE, sun_intensity);
glEnable(GL_COLOR_MATERIAL); //如果在光照下希望模型的颜可以起作用,需要启动颜材料模式:
glColorMaterial(GL_FROT, GL_AMBIET_AD_DIFFUSE);//表示模型的正面接受环境光和散射光
glLineWidth(5);
glClearColor(0.0, 0.0, 0.0, 0.0); //定义背景颜--> black
//允许二维映射
glEnable(GL_MAP2_VERTEX_);
//二维映射:x、y方向U和V的参数[0, 1],且中间插值数量为各20个
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
//启用反走样
glEnable(GL_BLED);
glEnable(GL_LIE_SMOOTH);
glHint(GL_LIE_SMOOTH_HIT, GL_FASTEST); // (指定反走样线段的采样质量,选择速度最快选项)
glBlendFunc(GL_SRC_ALPHA, GL_OE_MIUS_SRC_ALPHA);
glEnable(GL_BLED); // 打开颜混合
glDisable(GL_DEPTH_TEST); // 关闭深度测试
glBlendFunc(GL_SRC_ALPHA, GL_OE); // 基于源像素α通道值的半透明混合函数,可以透过窗户看房内
glEnable(GL_DEPTH_TEST); //打开深度测试
glMatrixMode(GL_PROJECTIO); // 貌似和视角有关的定义 places the camera at (0,0,0) and faces it along(0,0,-1).
glOrtho(-w, w, -h, h, -w, w); // 创建一个正交平行的视景体。 一般用于物体不会因为离屏幕的远近而产生大小的变换的情况
}
void specialkeys(int key, int x, int y){ //键盘相应函数
//旋转
if (key == GLUT_KEY_RIGHT)
rotate_y -= 1;
else if (key == GLUT_KEY_LEFT)
rotate_y = 1;
else if (key == GLUT_KEY_DOW)
rotate_x -= 1;
else if (key == GLUT_KEY_UP)
rotate_x = 1;
glutPostRedisplay();
}
int main(int argc, char** ar) // main函数
{
float x, y, a;
for (y = 1.5; y > -1.5; y -= 0.1)
{
for (x = -1.5; x < 1.5; x = 0.05)
{
a = x * x y * y - 1;
putchar(a * a * a - x * x * y * y * y <= 0.0 ? * : );
}
putchar( \n );
}
glutInit(&argc, ar);
glutInitDisplayMode(GLUT_SIGLE | GLUT_RGB);
glutInitWindowSize(w, h);
glutInitWindowPosition(100, 100);
glutCreateWindow(The LOVE);
init();
glBlendFunc(GL_SRC_ALPHA, GL_OE_MIUS_SRC_ALPHA);//源颜的α作为源因子/1-α作为目标因子
glEnable(GL_DEPTH_TEST); //启动深度缓存
glutSpecialFunc(specialkeys); //调用键盘控制函数
glutDisplayFunc(display);
//glutReshapeFunc(reshape);
glutTimerFunc(500, timer_function, 1);
glutIdleFunc(display);//设置空闲时调用的函数
glutMainLoop();
//system(pause);
return 0;
}
实现效果:
)、李峋爱心htmlcssjs;
说明:本代码是参考其他博主,方便大家学习借鉴,全部代码也粘贴如下:
(自己新建一个文本文件,以下全部代码粘贴进去,然后将文本文件的格式重命名由.txt改成.html然后直接打开就可以啦)
另注:有一部分没有基础的小笨蛋们还会遇到这样的问题(因为我发给我的小伙伴们遇到了QAQ),大家新建文本文件后,一部分人的电脑没有改动前不显示文件格式,即不是名字.txt的形式,首先需要显示文件格式,才能改文件格式。
显示文件格式如下:
①打开我的电脑,在下图位置1点击三个点
②在位置2点击选项
③打开文件夹选项后,点击位置查看,之后滑动高级设置滚动栏,出位置4隐藏已知文件类型的扩展名,将它前面的勾取消即可。然后点击应用,点击确定就完成了!
显示出文件类型之后,将文件后缀.txt改成.htm(重命名改)就可以啦!
全部代码如下:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src=js/js></script>
</head>
<style>
* {
padding: 0;
margin: 0;
}
html,
body {
height: 100%;
padding: 0;
margin: 0;
background: #000;
}
.aa {
position: fixed;
left: 50%;
bottom: 10px;
color: #ccc;
}
.container {
width: 100%;
height: 100%;
}
canvas {
z-index: 99;
position: absolute;
width: 100%;
height: 100%;
}
</style>
<body>
<!-- 樱花 -->
<div id=jsi-cherry-container class=container>
<audio autoplay=autopaly>
<source src= type=audio/mp />
</audio>
<img class=img src=./12.png alt= />
<!-- 爱心 -->
<canvas id=pinkboard class=container> </canvas>
</div>
</body>
</html>
<script>
/*
* Settings
*/
var settings = {
particles: {
length: 500, // maximum amount of particles
duration: 2, // particle duration in sec
velocity: 100, // particle velocity in pixels/sec
effect: -0.75, // play with this for a nice effect
size: 0, // particle size in pixels
},
};
(function () {
var b = 0;
var c = [ms, moz, webkit, o];
for (var a = 0; a < c.length && !window.requestAnimationFrame; a) {
window.requestAnimationFrame = window[c[a] RequestAnimationFrame];
=
window[c[a] CancelAnimationFrame] ||
window[c[a] CancelRequestAnimationFrame];
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function (h, e) {
var d = new Date().getTime();
var f = (0, 16 - (d - b));
var g = window.setTimeout(function () {
h(d f);
}, f);
b = d f;
return g;
};
}
if (!) {
= function (d) {
clearTimeout(d);
};
}
})();
/*
* Point class
*/
var Point = (function () {
function Point(x, y) {
this.x = typeof x !== undefined ? x : 0;
this.y = typeof y !== undefined ? y : 0;
}
Point. = function () {
return new Point(this.x, this.y);
};
Point.prototype.length = function (length) {
if (typeof length == undefined)
return Math.sqrt(this.x * this.x this.y * this.y);
();
this.x *= length;
this.y *= length;
return this;
};
Point. = function () {
var length = this.length();
this.x /= length;
this.y /= length;
return this;
};
return Point;
})();
/*
* Particle class
*/
var Particle = (function () {
function Particle() {
this.position = new Point();
this.velocity = new Point();
this.acceleration = new Point();
this.age = 0;
}
Particle.prototype.initialize = function (x, y, dx, dy) {
this.position.x = x;
this.position.y = y;
this.velocity.x = dx;
this.velocity.y = dy;
this.acceleration.x = dx * settings.;
this.acceleration.y = dy * settings.;
this.age = 0;
};
Particle.prototype.update = function (deltaTime) {
this.position.x = this.velocity.x * deltaTime;
this.position.y = this.velocity.y * deltaTime;
this.velocity.x = this.acceleration.x * deltaTime;
this.velocity.y = this.acceleration.y * deltaTime;
this.age = deltaTime;
};
Particle.prototype.draw = function (context, image) {
function ease(t) {
return --t * t * t 1;
}
var size = image.width * ease(this.age / settings.particles.duration);
context.globalAlpha = 1 - this.age / settings.particles.duration;
context.drawImage(
image,
this.position.x - size / 2,
this.position.y - size / 2,
size,
size
);
};
return Particle;
})();
/*
* ParticlePool class
*/
var ParticlePool = (function () {
var particles,
firstActive = 0,
firstFree = 0,
duration = settings.particles.duration;
function ParticlePool(length) {
// create and populate particle pool
particles = new Array(length);
for (var i = 0; i < particles.length; i)
particles[i] = new Particle();
}
ParticlePool.prototype.add = function (x, y, dx, dy) {
particles[firstFree].initialize(x, y, dx, dy);
// handle circular queue
firstFree;
if (firstFree == particles.length) firstFree = 0;
if (firstActive == firstFree) firstActive;
if (firstActive == particles.length) firstActive = 0;
};
ParticlePool.prototype.update = function (deltaTime) {
var i;
// update active particles
if (firstActive < firstFree) {
for (i = firstActive; i < firstFree; i)
particles[i].update(deltaTime);
}
if (firstFree < firstActive) {
for (i = firstActive; i < particles.length; i)
particles[i].update(deltaTime);
for (i = 0; i < firstFree; i) particles[i].update(deltaTime);
}
// remove inactive particles
while (
particles[firstActive].age >= duration &&
firstActive != firstFree
) {
firstActive;
if (firstActive == particles.length) firstActive = 0;
}
};
ParticlePool.prototype.draw = function (context, image) {
// draw active particles
if (firstActive < firstFree) {
for (i = firstActive; i < firstFree; i)
particles[i].draw(context, image);
}
if (firstFree < firstActive) {
for (i = firstActive; i < particles.length; i)
particles[i].draw(context, image);
for (i = 0; i < firstFree; i) particles[i].draw(context, image);
}
};
return ParticlePool;
})();
/*
* Putting it all together
*/
(function (canvas) {
var context = canvas.getContext(2d),
particles = new ParticlePool(settings.particles.length),
particleRate =
settings.particles.length / settings.particles.duration, // particles/sec
time;
// get point on heart with -PI <= t <= PI
function pointOnHeart(t) {
return new Point(
160 * Math.pow(Math.sin(t), ),
10 * (t) -
50 * (2 * t) -
20 * ( * t) -
10 * (4 * t)
25
);
}
// creating the particle image using a dummy canvas
var image = (function () {
var canvas = (canvas),
context = canvas.getContext(2d);
canvas.width = settings.particles.size;
canvas.height = settings.particles.size;
// helper function to create the path
function to(t) {
var point = pointOnHeart(t);
point.x =
settings.particles.size / 2
(point.x * settings.particles.size) / 50;
point.y =
settings.particles.size / 2 -
(point.y * settings.particles.size) / 50;
return point;
}
// create the path
context.beginPath();
var t = -Math.PI;
var point = to(t);
(point.x, point.y);
while (t < Math.PI) {
t = 0.01; // baby steps!
point = to(t);
context.lineTo(point.x, point.y);
}
();
// create the fill
context.fillStyle = #ea80b0;
context.fill();
// create the image
var image = new Image();
image.src = ();
return image;
})();
// render that thing!
function render() {
// next animation frame
requestAnimationFrame(render);
// update time
var newTime = new Date().getTime() / 1000,
deltaTime = newTime - (time || newTime);
time = newTime;
// clear canvas
(0, 0, canvas.width, canvas.height);
// create new particles
var amount = particleRate * deltaTime;
for (var i = 0; i < amount; i) {
var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
var dir = ().length(settings.particles.velocity);
particles.add(
canvas.width / 2 pos.x,
canvas.height / 2 - pos.y,
dir.x,
-dir.y
);
}
// update and draw particles
particles.update(deltaTime);
particles.draw(context, image);
}
// handle (re-)sizing of the canvas
function onResize() {
canvas.width = ;
canvas.height = ;
}
= onResize;
// delay rendering bootstrap
setTimeout(function () {
onResize();
render();
}, 10);
})(document.getElementById(pinkboard));
</script>
<script>
var REDERER = {
IIT_CHERRY_BLOSSOM_COUT: 0,
MAX_ADDIG_ITERVAL: 10,
init: function () {
this.setParameters();
this.rectructMethods();
();
this.render();
if (
navigator.userAgent.match(
/(phone|pod|iPhone|iPod|ios|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserG|WebOS|Symbian|Windows Phone)/i
)
) {
// var box = document.querySelectorAll(.box)[0];
// cole.log(box, 移动端);
// box. = 65%;
}
},
setParameters: function () {
this.$container = $(#jsi-cherry-container);
this.width = this.$container.width();
this.height = this.$container.height();
= $(<canvas />)
.attr({ width: this.width, height: this.height })
.appendTo(this.$container)
.get(0)
var rate = this.FOCUS_POSITIO / (this.z this.FOCUS_POSITIO),
x = this.renderer.width / 2 this.x * rate,
y = this.renderer.height / 2 - this.y * rate;
return { rate: rate, x: x, y: y };
},
re
}
} else {
this.phi = Math.PI / (axis.y == ? 200 : 500);
this.phi %= Math.PI;
}
if (this.y <= -this.renderer.height * this.SURFACE_RATE) {
this.x = 2;
this.y = -this.renderer.height * this.SURFACE_RATE;
} else {
this.x = this.vx;
this.y = this.vy;
}
return (
this.z > -this.FOCUS_POSITIO &&
this.z < this.FAR_LIMIT &&
this.x < this.renderer.width * 1.5
);
},
};
$(function () {
REDERER.init();
});
</script>
实现效果如下:
自己兴趣所做,有问题可以评论提出虚心请教!感谢喜欢!
加字比如:
有想加特定字的再和我联系啦啦啦啦
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2023-06-23 08:10:58
推荐阅读
留言与评论(共有 13 条评论) |
本站网友 九头鸟餐厅 | 23分钟前 发表 |
0);//17 glutSolidSphere(100.0 | |
本站网友 代理商管理 | 15分钟前 发表 |
0.00 | |
本站网友 唐家湾租房 | 6分钟前 发表 |
0.0 | |
本站网友 产后减肚子 | 4分钟前 发表 |
GL_OE_MIUS_SRC_ALPHA); glEnable(GL_BLED); // 打开颜混合 glDisable(GL_DEPTH_TEST); // 关闭深度测试 glBlendFunc(GL_SRC_ALPHA | |
本站网友 zhongnan | 19分钟前 发表 |
point.y); while (t < Math.PI) { t = 0.01; // baby steps! point = to(t); context.lineTo(point.x | |
本站网友 背阔肌 | 8分钟前 发表 |
90 | |
本站网友 成龙影院 | 28分钟前 发表 |
int x | |
本站网友 车险理赔难 | 17分钟前 发表 |
100.0 | |
本站网友 四妙丸 | 2分钟前 发表 |
180.0/255.0 # define sunshine 255.0/255.0 | |
本站网友 百日破 | 3分钟前 发表 |
-200 | |
本站网友 硝镪水 | 5分钟前 发表 |
0);//8 glutSolidSphere(100.0 | |
本站网友 农业板块龙头股 | 4分钟前 发表 |
1.0 |