毕达哥拉斯定理检查一个细胞是否在半径内 [英] Pythagorean theorem to check whether a cell is inside the radius

查看:81
本文介绍了毕达哥拉斯定理检查一个细胞是否在半径内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有尺寸矩阵和给定单元格 半径



所以让我们来看看这个例子:

https://i.imgur.com /51hTJpv.png [矩阵]

矩阵有 5行 6列。给予细胞(2,3) 半径2 。它有影响,它会破坏特定半径内的所有物品(撞击细胞带阴影黑色其他细胞半径为阴影灰色)。我发现我可以使用毕达哥拉斯定理来检查一个单元是否在半径范围内:

I have the dimensions of a matrix and a given cell with radius.

So let's take a look at this example:
https://i.imgur.com/51hTJpv.png[Matrix]
The matrix has 5 rows and 6 columns. We are given the cell (2, 3) with radius 2. It has an impact, and it destroys all of the items in a certain radius (the impact cell is shaded black and the other cells within the radius are shaded grey). I found that I could use the Pythagorean Theorem to check whether a cell is inside the radius:

if (Math.Pow(targetRow - row, 2) + Math.Pow(targetColumn - col, 2) <= radius * radius)
{
    matrix[row, col] = 1; 
}



我不明白为什么会有效,如果你能向我解释,我将非常感激。我试图调试,但我仍然没有得到它。



我尝试过:




I don't understand why it works, and I would be very grateful if you could explain it to me. I tried to debug, but I still don't get it.

What I have tried:

if (Math.Pow(targetRow - row, 2) + Math.Pow(targetColumn - col, 2) <= radius * radius)
{
    matrix[row, col] = 1; 
}

推荐答案

这是基本数学。

targetRow - 行是从目标到圆心的水平距离,半径 radius targetColumn - col 是垂直距离。根据定义, radius 永远不会短于这两者的绝对值中的最长值,因此它始终是斜边。由于任何数字的平方总是为零或正数,另外两边的平方总是正的,毕达哥拉斯告诉我们根(x 2 + y 2 )是从中心到你的点的距离。如果它小于半径,则您的点在圆圈内。
This is basic maths.
targetRow - row is the horizontal distance from the target to the centre of your circle with radius radius, and targetColumn - col is the vertical distance. The radius by definition never shorter than the longest of the absolute values of those two, so it is always the hypotenuse. Since the square of any number is always zero or positive, your squares of the other two sides are always positive, and Pythagoras tells us that root(x2 + y2) is the distance from the centre to your point. If that is smaller than the radius, your point is inside the circle.


这篇关于毕达哥拉斯定理检查一个细胞是否在半径内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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