在Mathematica中笛卡尔坐标系中计算距离 [英] Compute distance in Cartesian Coordinate System in Mathematica

查看:202
本文介绍了在Mathematica中笛卡尔坐标系中计算距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

分析屏幕上的眼动,我将原点设置为它的左下角
(此时很难修改)。

Analyzing Eye-movements on a screen, I set my origin to the bottom left corner of it (Hard to modify at that point).

尝试计算某些点与屏幕中心之间的距离,我使用下面显示的简单公式。
问题是在条件语句中使用它会很丑。

Trying to compute distance between some points and the center of the screen I use the simple formula displayed below. Problem is that using this in conditional statement, it gets ugly.

Sqrt[
(
(fixationX - centerX)^2 + (fixationY - centerY)^2
)
]

是否可以自定义范数以计算点之间的距离,而不是点与原点之间的距离?

Is there a way to customize Norm to compute distance between points and not between a point and the origin ?

或者在我的情况下,设置原点处于当前坐标系的中心?

Or in my case, set the origin to be at the "center" of the current coordinate system ?

推荐答案

Simon方法的微小变化是使用默认值而不是全局变量( $ Center )。

A slight variation of Simon's method is to use a default value in the function, rather than a global variable ($Center).

假设您的默认来源是(5, 5),然后:

Suppose your default origin is (5, 5), then:

myNorm[pos:{_, _}, center_:{5, 5}] := EuclideanDistance[pos, center]

请注意使用 _:{5,5 } 定义默认值。

Notice the use of _:{5, 5} to define the default value.

现在您可以执行以下操作:

Now you can do:

myNorm[{5, 7}]

(* Out[]= 2 *)

或通过以下方式临时使用其他中心:

Or temporarily use a different the center with:

myNorm[{5, 7}, {8, 8}]

(* Out[]= Sqrt[10] *)

对于此简单功能,可以在第二种情况下使用 EuclideanDistance ,但是我希望您可以看到此方法的实用程序是 myNorm的定义。 更复杂。

For this simple function, you could use EuclideanDistance in the second case instead, but I hope you can see the utility of this method were the definition of myNorm more complex.

此方法的缺点是您不能轻易更改默认中心。

The downside to this method is that you cannot easily change the default center.

另一种确实可以轻松更改默认中心但更冗长的变化是使用 Options

Another variation that does allow one to easily change the default center, but is more verbose, is to use Options:

Options[myNorm2] = {Center -> {5, 5}};

myNorm2[pos : {_, _}, OptionsPattern[]] := 
 EuclideanDistance[pos, OptionValue[Center]]

语法是:

myNorm2[{5, 7}]

myNorm2[{5, 7}, Center -> {8, 8}]



   2



   Sqrt[10]

更改默认中心:

SetOptions[myNorm2, Center -> {8, 8}];

myNorm2[{5, 7}]



   Sqrt[10]

这篇关于在Mathematica中笛卡尔坐标系中计算距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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