8.4.2.2_利用最小平移向量使两个物体粘在一起

8.4.2.2_利用最小平移向量使两个物体粘在一起

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>利用最小平移向量使两个物体粘在一起</title>
        <style>
            body{
                background: #fff;
            }
            #canvas{
                background: #eee;
            }
        </style>
    </head>
    <body>
        <canvas id="canvas" width="800" height="600"></canvas>
    </body>
        <!-- 精灵对象 -->
    <script>
        var Sprite = function(name,painter,behaviors){
    
    
            if(name !== undefined){ this.name = name; }
            if(painter !== undefined){ this.painter = painter; }

            this.top = 0;
            this.left = 0;
            this.width = 10;
            this.height = 10;
            this.velocityX = 0;
            this.velocityY = 0;
            this.visible = true;
            this.animating = false;
            this.behaviors = behaviors || [];

        }

        Sprite.prototype = {
            paint:function(context){
    
    
                if(this.painter !== undefined && this.visible){
                    this.painter.paint(this,context);
                }
            },
            update:function(context,time){
    
    
                for(var i=0;i<this.behaviors.length;i++){
                    this.behaviors[i].execute(this,context,time);
                }
            }
        }
    </script>

    <!-- 图像绘制器 -->
    <script>
        var ImagePainter = function (imageUrl){
    
    
            var self = this;
            this.image = new Image();
            this.loaded = false;
            this.image.src = imageUrl;

            this.image.addEventListener('load',function(){
    
    
                self.loaded = true;
            },false);

        }
        ImagePainter.prototype ={
            paint:function(sprite,context){
    
    
                //因为图像绘制器专门负责反复绘制精灵对象所用的图像,所以它并不负责图像的加载
                //这里不能像之间一样不负责加载了,之间是用在requestAnimationFrame中,代码会按帧持续执行,图片早晚都会加载出来,
                //所以这块代码块一定会执行。但是在现在这个程序中,在初始化中只走一次,所以很有可能图片还没有加载完成this.image.complete为false

                    context.drawImage(this.image,sprite.left,sprite.top,sprite.width,sprite.height);

            }
        }
    </script>

    <!-- Vector对象 向量对象 --> 
    <script>
        //坐标系中的向量,以0,0出发,到点x,y,为一个向量,向量长度方向相同为同一个向量
        var Vector = function(x,y){
    
    
            this.x = x;
            this.y = y;
        };

        Vector.prototype = {
            //得到向量的长度
            getMagnitude:function(){
    
      
                return Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2));
            },

            //两向量相加得到的新向量
            add:function(anotherVector){
    
    
                var v = new Vector();
                v.x = this.x + anotherVector.x;
                v.y = this.y + anotherVector.y;
                return v;
            },

            //两向量相减,得到边缘法向量 OA-OB = BA;
            subtract:function(anotherVector){
    
    
                var v = new Vector();
                v.x = this.x - anotherVector.x;
                v.y = this.y - anotherVector.y;
                return v;
            },

            //两向量的点积,一个向量在别一处向量上的投影,得到的不是一个向量,是投影的长度
            dotProduct:function(anotherVector){
    
    
                return this.x *anotherVector.x+ this.y*anotherVector.y;
            },

            //得到多边形的边缘向量,即多边形,相邻两点的向量
            edge:function(anotherVector){
    
    
                return this.subtract(anotherVector);
            },

            //得到垂直于边缘向量的边缘法向量,即投影轴向量
            perpendicular:function(){
    
    
                var v = new Vector();
                v.x = this.y;
                v.y = 0-this.x;
                return v;
            },

            //得去某向量的单位向量,即方向相同,长度为1的向量,单位向量主要是用来指示方向的
            normalize:function(){
    
    
                var v = new Vector(0,0);
                var m = this.getMagnitude();
                if(m!=0){ //避免向量为0
                    v.x = this.x/m;
                    v.y = this.y/m;
                }
                return v;
            },

            //得去边缘法向量的单位向量,即投影轴向量的单位方向,表示投影轴的方向
            perpendicularNormal:function(){
    
    
                var p = this.perpendicular();
                return p.normalize();
            }
        }
    </script>

    <!-- Projection 投影对象 -->
    <script>
        var Projection = function(min,max){
    
    
            this.min = min;
            this.max = max;
        };

        Projection.prototype = {

            //检测两个多边形在同一个投影轴上的投影是否有重合,重叠返回true
            overlaps:function(projection){
    
    
                return this.max > projection.min && projection.max >this.min;
            },
            overlap : function(projection){
    
    
                var minP = 10000;
                if(this.max > projection.min && projection.max >this.min){ //两投影在当前投影轴上发生了重叠
                    var pro1 = Math.abs(this.max - projection.min);
                    var pro2 = Math.abs(projection.max - this.min);
                    var pro3 = Math.abs(this.max - this.min );
                    var pro4 = Math.abs(projection.max - projection.min );


                    return Math.min(pro1,pro2,pro3,pro4);
                }else{
                    return 0;
     
### 中职学校网络安全理论课程大纲和教学内容 #### 2025年中职学校网络安全理论课程概述 随着信息技术的发展网络安全已成为信息化社会的重要组成部分。为了适应这一需求,中职学校的网络安全理论课程旨在培养学生具备基本的网络安全意识和技术能力,使学生能够在未来的职业生涯中应对各种网络威胁。 #### 教学目标 该课程的目标是让学生理解网络安全的基本概念、原理和技术手段,掌握常见的安全防护措施,并能应用这些知识解决实际问题。具体来说,学生应达到以下几点: - 掌握计算机网络基础架构及其工作原理; - 理解信息安全管理体系框架及其实现方法; - 学习密码学基础知识以及加密算法的应用场景; - 能够识别常见攻击方式并采取有效防御策略; #### 主要章节安排 ##### 第一章 计算机网络与互联网协议 介绍计算机网络的基础结构和服务模型,重点讲解TCP/IP五层体系结构中的各层次功能特点,特别是传输控制协议(TCP)和用户数据报协议(UDP)[^1]。 ##### 第二章 信息系统安全保障概论 探讨信息系统的脆弱性和风险评估机制,阐述如何通过物理隔离、访问控制等措施来保障系统安全性。 ##### 第三章 密码学入门 讲述对称密钥体制和非对称密钥体制的区别与发展历程,分析公钥基础设施(PKI)的工作流程及其重要性。 ##### 第四章 防火墙技术与入侵检测系统(IDS) 解释防火墙的作用原理及其分类形式(包过滤型、代理服务器型),讨论IDS的功能特性及部署建议。 ##### 第五章 Web应用程序安全 针对Web环境下的特殊挑战展开论述,如SQL注入漏洞利用、跨站脚本(XSS)攻击防范等内容。 ##### 实践环节设置 除了上述理论部分外,在每学期还设有专门实践课时用于模拟真实环境中可能遇到的安全事件处理过程,增强学生的动手操作能力和应急响应水平。 ```python # Python代码示例:简单的MD5哈希函数实现 import hashlib def md5_hash(text): hasher = hashlib.md5() hasher.update(text.encode('utf-8')) return hasher.hexdigest() print(md5_hash("example")) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值