笔记18 面试题:吸附

图片说明

在这里插入图片描述

代码:移动物体、垂直地面、吸附到Cube中心

public class CubeTest : MonoBehaviour {
    //来一个布尔值
    private bool isMove;

	void Start () {
			
	}
	
	void Update () {
		//如果移动
        if(isMove)
        {
            //射线
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //拿到射线的结构体
            RaycastHit hit;
            //进行射线检测。如果没有指定层,会看到物体逐渐靠近屏幕。
            //使用了层,那么必须写上前面那个表示距离的参数(500f),以避免层失效。
            bool res = Physics.Raycast(ray, out hit, 500f, 1<<8);
            if (res)
            {
                //为真,则位置等于点击的位置。
                transform.position = hit.point;
                //如果出现问题,可将法线画出
                //Debug.DrawRay(hit.point, hit.normal, Color.red);

                //因为Player是垂直于平面的,所以让它看向碰撞点的法线(一个向量)
                transform.rotation = Quaternion.LookRotation(hit.normal);
                /*看向一个向量,就是说其Z轴(因为Z轴表示前方)和向量的方向保持一致。
                所以,使用此方法时,要保证Player的Z轴(本地坐标)是朝上的
                区别于transform.LookAt(Vector3.zero);是看向某一点,例如相机看向汽车。
                另一个方法:
                碰撞点向上的方向等于碰撞点法线的方向。好处:不需要修改细长方块的本地旋转。
                transform.up = hit.normal;
                */
            }

            //判断附近有没有大方块。范围:中心点transform.position,半径1米的圆
            Collider[] colliders = Physics.OverlapSphere(transform.position, 1f);
            //遍历碰撞体
            foreach(Collider collider in colliders)
            {
                //做判断:如果碰撞器的标签是Taget(之前将方块的标签改为了Taget)
                if (collider.tag == "Target")
                {
                    //便将我的位置等于方块的中心点的位置
                    transform.position = collider.transform.position;
                }
            }
        }
	}

    private void OnMouseDown()
    {
        //isMove表示是否能移动。在这里设置为true
        isMove = true;
    }

    private void OnMouseUp()
    {
        isMove = false;
    }
} 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述: 给定一篇手写笔记的图片,要求用代码实现将其还原成文字。 示例: 输入: ![handwriting](https://cdn.luogu.com.cn/upload/image_hosting/jz2h2y1f.png) 输出: ``` Hello, world! This is a handwriting recognition problem. Can you solve it? Let's see. 1. First, let's import some libraries. - numpy: a popular library for numerical computing - PIL: Python Imaging Library, for image processing - tensorflow: a popular library for machine learning 2. Next, let's load the image and preprocess it. - Convert it to grayscale - Resize it to a fixed size - Invert the colors 3. Now we can use a pre-trained OCR model to recognize the text. - We'll use Tesseract, an open-source OCR engine. - It works best on clean, high-quality images, so we'll need to do some post-processing. 4. Finally, let's print the recognized text and see how well we did. - We should be able to get pretty good accuracy if the image is clear and the text is legible. ``` 解题思路: 1. 首先需要加载一些常用的 Python 库,如 numpy、PIL 和 tensorflow。 2. 接着需要读取手写笔记的图片,并对其进行预处理。具体来说,需要将其转换为灰度图像、调整大小并反转颜色。 3. 接下来,可以使用一个预训练的 OCR 模型来识别文本。这里使用的是 Tesseract,一个开源的 OCR 引擎。需要注意的是,Tesseract 在处理高质量、清晰的图像时表现最佳,因此在处理后需要进行一些后处理。 4. 最后,将识别出的文本打印出来,以便进行检查。 代码实现: ```python import numpy as np from PIL import Image import pytesseract # Step 1: Load the image and preprocess it img = Image.open('handwriting.png').convert('L') img = img.resize((int(img.size[0] * 2), int(img.size[1] * 2))) img = ImageOps.invert(img) # Step 2: Use Tesseract to recognize the text text = pytesseract.image_to_string(img) # Step 3: Post-process the recognized text text = text.replace('\n\n', '\n') text = text.strip() # Step 4: Print the recognized text print(text) ``` 其中,`pytesseract` 库需要提前安装。在 Ubuntu 系统下,可以使用以下命令进行安装: ``` sudo apt install tesseract-ocr sudo apt install libtesseract-dev pip install pytesseract ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值