简体   繁体   English

Math.Sqrt中的Math.Pow-我不理解此代码

[英]Math.Pow inside Math.Sqrt - I don't understand this code

I'm using this code I took from somewhere, for learning purposes. 我正在使用从某处获取的这段代码来进行学习。 I'm trying to break it down and understand how it does what it does, if you could help me grasp it better. 如果您可以帮助我更好地理解它,我将尝试对其进行分解,并了解它是如何工作的。

This function returns the distance between mouse and the respective element. 此函数返回鼠标与相应元素之间的距离。

function calculateDistance(elem, mouseX, mouseY) {
    return Math.floor(Math.sqrt(Math.pow(mouseX - (elem.offset().left+(elem.width()/2)), 2) + Math.pow(mouseY - (elem.offset().top+(elem.height()/2)), 2)));
}

It returns a positive number, calculating only the pixels inbetween mouse and element. 它返回一个正数,仅计算鼠标和元素之间的像素。

Stripping it down and leaving only the Math.floor. 剥离下来,只剩下Math.floor。 I don't know why without the math.sqrt(math.pow...) I get negative values depending on mouse position relaative to the element -> left(-x), right(x), above(-y), below(y) the element 我不知道为什么没有math.sqrt(math.pow ...)我会得到负值,具体取决于鼠标相对于元素的位置-> left(-x),right(x),above(-y),在元素下方

and also get a different center for the element. 并获得元素的不同中心。

function calculateDistance(elem, mouseX, mouseY) {
    return Math.floor(mouseX - (elem.offset().left+(elem.width()/2)) + mouseY - (elem.offset().top+(elem.height()/2)));
}

I know what Math.pow and sqrt do on their own. 我知道Math.pow和sqrt会自己做什么。 I'm not seeing how they're finding the center of the element as I thought that elem.offset().left+(elem.width()/2) was doing just that horizontally meanwhile elem.offset().top+(elem.height()/2) did it vertically. 我没有看到他们如何找到元素的中心,因为我认为elem.offset().left+(elem.width()/2)只是在水平方向执行,而elem.offset().top+(elem.height()/2)垂直进行。

Its the pythagorean theorem, a²+b²=c² where you have a (x) and b (y) and search for c (distance) 毕达哥拉斯定理a²+b²=c²,您有一个(x)和b(y)并搜索c(距离)

https://en.wikipedia.org/wiki/Pythagorean_theorem https://zh.wikipedia.org/wiki/毕达哥拉斯理论

很棒的绘画技巧

So 1 所以1

is elem.width()/2 

2 is 2

elem.height()/2 

by adding 通过增加

elem.offset().left and elem.offset().top 

you get the center of the element. 您将获得元素的中心。

You get 3 by 你得到3

mouseX - (elem.offset().left+(elem.width()/2)

And you get 4 by 你得到4

mouseY - (elem.offset().top+(elem.height()/2)

Finally in order to find the distance between the mouse pointer and the element you have to use good old Pythagorean theorem a²+b²=c². 最后,为了找到鼠标指针和元素之间的距离,您必须使用旧的毕达哥拉斯定理a²+b²=c²。 In order to find the square of sides 3 and 4 you use Math.pow(). 为了找到侧面34的平方,请使用Math.pow()。 About your question why is it returning a negative integer when you remove it is that the square of a number is always positive regardless if the you square a positive or a negative number. 关于您的问题,为什么删除时它返回一个负整数是数字的平方始终为正,无论您对一个正数还是一个负数求平方。 For example in this case the result for side 3 (mouseX - (elem.offset().left+(elem.width()/2)) will be negative since the mouse is to the left of the element. 例如,在这种情况下,第3面的结果(mouseX-(elem.offset()。left +(elem.width()/ 2))将为负,因为鼠标位于元素的左侧。

This will give you the length if the distance squared, so that's why you need to use Math.sqrt to get from c² just c. 如果距离为平方,这将为您提供长度,因此这就是为什么您需要使用Math.sqrt从c²中获得c的原因。

Finally Math.floor is used just to round down to the nearest integer. 最后,Math.floor仅用于舍入到最接近的整数。

I know what Math.pow and sqrt do on their own 我知道Math.pow和sqrt自己做什么

And here they are implementing Pythagoras' theorem 他们在这里实现毕达哥拉斯定理

I'm not seeing how they're finding the center of the element as I thought that elem.offset().left+(elem.width()/2) was doing just that horizontally meanwhile elem.offset().top+(elem.height()/2) did it vertically 我没有看到他们如何找到元素的中心,因为我认为elem.offset()。left +(elem.width()/ 2)只是在水平方向执行,而elem.offset()。top +(elem .height()/ 2)垂直完成

That's exactly what the developer is doing here. 这正是开发人员在这里所做的。

mouseX - (elem.offset().left+(elem.width()/2)) corresponds to 'a' in the linked diagram, and elem.offset().top+(elem.height()/2) corresponds to b mouseX - (elem.offset().left+(elem.width()/2))对应于链接图中的'a',而elem.offset().top+(elem.height()/2)对应于b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM