Aja*_*sja 27
另一种选择是将所有内容都放入2D旋转椭圆的等式中,并查看结果是否小于1.
因此,如果以下不等式为真,则在椭圆内部有一个点
其中(xp,yp)是点坐标,(x0,y0)是椭圆的中心.
我实施了一个小型Mathematica程序,证明这确实有效:
这是在行动:
以下是代码:
ellipse[x_, y_, a_, b_, \[Alpha]_, x0_: 0, y0_: 0] :=
(((x - x0)*Cos[\[Alpha]] + (y - y0)*Sin[\[Alpha]])/a)^2
+ (((x - x0)*Sin[\[Alpha]] - (y - y0)*Cos[\[Alpha]])/b)^2;
Manipulate[
RegionPlot[
ellipse[x, y, a, b, \[Alpha] \[Degree], Sequence @@ pos] < 1, {x, -5, 5}, {y, -5, 5},
PlotStyle -> If[ellipse[Sequence @@ p, a, b, \[Alpha] \[Degree], Sequence @@ pos] <= 1, Orange, LightBlue],
PlotPoints -> 25]
, {{a, 2}, 1, 5, Appearance -> "Labeled"}
, {{b, 4}, 2, 5, Appearance -> "Labeled"}
, {\[Alpha], 0, 180, Appearance -> "Labeled"}
, {{p, {3, 1}}, Automatic, ControlType -> Locator}
, {{pos, {0, 0}}, Automatic, ControlType -> Locator}]
Run Code Online (Sandbox Code Playgroud)
Rao*_*oul 10
您只需将数据输入上述公式即可.这是我在Ajasja的建议上做的python实现:
def pointInEllipse(x,y,xp,yp,d,D,angle):
#tests if a point[xp,yp] is within
#boundaries defined by the ellipse
#of center[x,y], diameter d D, and tilted at angle
cosa=math.cos(angle)
sina=math.sin(angle)
dd=d/2*d/2
DD=D/2*D/2
a =math.pow(cosa*(xp-x)+sina*(yp-y),2)
b =math.pow(sina*(xp-x)-cosa*(yp-y),2)
ellipse=(a/dd)+(b/DD)
if ellipse <= 1:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
为了处理椭圆,我更喜欢将它们转换为另一个坐标系,其中椭圆是以原点为中心的单位圆.
如果您将椭圆视为单位圆(半径1),按(a,b)缩放,按phi旋转并由(x,y)变换,则生活变得更加容易.如果您有转换矩阵,则可以使用它来执行更容易的包含查询.如果将点变换为椭圆为单位圆的坐标系,则您所要做的就是点对点圆测试,这是一项微不足道的测试.如果"变换"是一个矩阵,如上所述将单位圆变换为椭圆,那么
transformedPoint = transform.Invert().Transform(point);
pointInEllipse = transformedPoint.DistanceTo(0,0) < 1.0;
Run Code Online (Sandbox Code Playgroud)