如何知道两个矩形是否彼此接触(C#,UWP应用) [英] How to know if two rectangle touch each others(C#, UWP app)

查看:171
本文介绍了如何知道两个矩形是否彼此接触(C#,UWP应用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到有关interset/intersetWith的信息,
但我看不到这种方法..
即时搜索了两个小时,但我什么都没找到.
我尝试过的事情:

//我有矩形对象
矩形a = new Rectangle();
矩形b =新的Rectangle();

////然后我尝试使用相交"方法检查"a"是否碰到"b",并且没有看到他..

im read about interset /intersetWith ,
but i dont see this method ..
im search for two hours but i dont find anything..

What I have tried:

// i have rectangle object
Rectangle a = new Rectangle();
Rectangle b = new Rectangle();

// then i try to check if "a" touch "b" using Intersect method and i dont see him ..

推荐答案

Google搜索出现了许多有用的信息链接: UWP游戏开发-碰撞检测-YouTube [ Rect‐Helper类 [^ ]和Intersect(Rect, Rect)方法:
Google Search turned up a number of useful links: uwp 2d collision detection - Google Search[^]

UPDATE 1: Thanks for the downvote. Obviously, whoever gave a poor vote did not take the time to click on the link. Tonnes of examples, of which there was no need for me to copy and paste here, including videos like this one showing you how:


* UWP Game Dev - Collision Detection - YouTube[^]

UPDATE 2: UWP actually has a Rect​Helper Class[^] with an Intersect(Rect, Rect) method:
报价:

将两个相交的指定Rect值的区域作为新Rect返回. C#和Microsoft Visual Basic代码应改为使用相交".

Returns the areas of two specified Rect values that intersect, as a new Rect. C# and Microsoft Visual Basic code should use Intersect instead.


在小学毕业时,每个人都可以分辨出两个矩形是否相互接触而不绘制它们.
拿一张纸和一支铅笔.用你的大脑来手工解决问题.由于哈利·波特(Harry Potter)没有给您解决方案,所以没有魔力,解决方案也没跳到您的脸上.
您通过遵循一个过程解决了该问题,该过程就是您的算法.您需要以机械方式(如计算机)写下步骤.该程序将遵循这些步骤.

我们不做您的家庭作业.
家庭作业并非旨在测试您乞求他人完成工作的技能,而是可以让您思考并帮助您的老师检查您对所修课程的理解以及在应用这些课程时遇到的问题.
您的任何失败都会帮助您的老师发现您的弱点并采取补救措施.
您的任何失败都会帮助您学习什么有效,什么无效,这被称为试错"学习.
因此,请尝试一下,重新阅读您的课程并开始工作.如果您遇到特定问题,请显示代码并解释这个确切的问题,我们可能会提供帮助.

作为程序员,您的工作是创建解决特定问题的算法,您不能依靠别人永远为您完成任务,因此有时您必须学习如何.而且越早越好.
当您只是寻求解决方案时,这就像试图通过培训其他人来学习驾驶汽车.
创建算法基本上是在寻找数学并进行必要的调整以适合您的实际问题.

所谓发展"是指:系统地利用科学和技术知识来满足特定的目标或要求." BusinessDictionary.com [
[更新]
At the end of primary school, every one can tell if 2 rectangles touch each others without drawing them.
Take a sheet of paper and a pencil. Use your brain and solve the problem by hand. Since Harry Potter didn''t gave you the solution, there is no magic, and the solution didn''t jump to your face.
You solved the problem by following a procedure, that procedure is your algorithm. You need to write down the steps in a mechanical manner (computer like). The program will follow those steps.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don''t, it is called ''trial and error'' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can''t rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That''s not the same thing as "have a quick google and give up if I can''t find exactly the right code".

[Update]
报价:

im搜索了两个小时,但我什么都没找到..

im search for two hours but i dont find anything..


有时,构建自己的解决方案要花很多时间,而谷歌搜索要花几个小时.
当给您2个矩形时,使用相同逻辑的变体来查找它们是分开的,接触的,昆虫的还是重叠的都是问题.
只要看一下坐标,就可以得到2个矩形A =(1,1,2,3)和B =(5,5,6,7),您的大脑告诉您它们是分开的.
考虑一下您刚才使用的逻辑,您的程序将使用相同的逻辑.


Sometimes, it get just faster to build your own solution rather than Googling for hours.
When you are given 2 rectangles, finding if they are separated, touching, insect or overlapping are all problems using variations of the same logic.
Get 2 rectangles A=(1,1,2,3) and B=(5,5,6,7), just by looking at coordinates, your brain tell you that they are separated.
Think about the logic you just used, your program will use the same logic.



这篇关于如何知道两个矩形是否彼此接触(C#,UWP应用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆