圆上任意角度的点的坐标
如上图,给定圆心(Cx,Cy),半径为R, 求
θ
\theta
θ 对应的点的坐标? 此处
θ
\theta
θ 是相对于水平轴的角度。
显然我们可以使用极坐标转换来求:
{
p
x
=
C
x
+
R
c
o
s
(
θ
)
p
y
=
C
y
+
R
s
i
n
(
θ
)
\left\{\begin{matrix} px= Cx+Rcos(\theta) \\ py= Cy+Rsin(\theta) \end{matrix}\right.
{ p x = C x + R c o s ( θ ) p y = C y + R s i n ( θ ) 注意如果以上竖直坐标系向下,此时变为图像的坐标系,即
结果变成:
{
p
x
=
C
x
+
R
c
o
s
(
θ
)
p
y
=
C
y
−
R
s
i
n
(
θ
)
\left\{\begin{matrix} px= Cx+Rcos(\theta) \\ py= Cy-Rsin(\theta) \end{matrix}\right.
{ p x = C x + R c o s ( θ ) p y = C y − R s i n ( θ ) 仅仅是y变了。
椭圆上任意角度的点的坐标
首先我们先来考虑标准椭圆上任意角度的点的坐标,再进行推广。
已知主轴a(即椭圆箭头所指方向对应的轴),次轴b,求解与水平轴相交
θ
\theta
θ 对应的点的坐标?
{
x
2
a
2
+
y
2
b
2
=
1
y
x
=
tan
(
θ
)
\left\{\begin{matrix} \frac{x^2}{a^2}+\frac{y^2}{b^2}&=&1 \\ \frac{y}{x}& =& \tan(\theta) \end{matrix}\right.
{ a 2 x 2 + b 2 y 2 x y = = 1 tan ( θ ) 将上面的第二式带入第一式,可以求解得到:
x
2
=
a
2
b
2
b
2
+
a
2
tan
2
(
θ
)
x^2=\frac{a^2b^2}{b^2+a^2\tan^2(\theta)}
x 2 = b 2 + a 2 tan 2 ( θ ) a 2 b 2 这时我们考虑
θ
\theta
θ 的范围,可以得到: (一).
0
≤
θ
<
p
i
/
2
0\leq\theta<pi/2
0 ≤ θ < p i / 2 或者
3
∗
p
i
2
<
θ
≤
2
∗
p
i
\frac{3*pi}{2}<\theta \leq 2*pi
2 3 ∗ p i < θ ≤ 2 ∗ p i
{
x
=
a
b
b
2
+
a
2
tan
2
(
θ
)
y
=
a
b
tan
(
θ
)
b
2
+
a
2
tan
2
(
θ
)
\left\{\begin{matrix} x=\frac{ab}{\sqrt{b^2+a^2\tan^2(\theta)}} \\ y=\frac{ab\tan(\theta)}{\sqrt{b^2+a^2\tan^2(\theta)}} \end{matrix}\right.
⎩ ⎨ ⎧ x = b 2 + a 2 tan 2 ( θ )
a b y = b 2 + a 2 tan 2 ( θ )
a b tan ( θ ) (二).
p
i
/
2
<
θ
<
3
∗
p
i
2
pi/2<\theta<\frac{3*pi}{2}
p i / 2 < θ < 2 3 ∗ p i
{
x
=
−
a
b
b
2
+
a
2
tan
2
(
θ
)
y
=
−
a
b
tan
(
θ
)
b
2
+
a
2
tan
2
(
θ
)
\left\{\begin{matrix} x=-\frac{ab}{\sqrt{b^2+a^2\tan^2(\theta)}} \\ y=-\frac{ab\tan(\theta)}{\sqrt{b^2+a^2\tan^2(\theta)}} \end{matrix}\right.
⎩ ⎨ ⎧ x = − b 2 + a 2 tan 2 ( θ )
a b y = − b 2 + a 2 tan 2 ( θ )
a b tan ( θ ) (三).
θ
=
p
i
/
2
\theta = pi/2
θ = p i / 2
{
x
=
0
y
=
b
\left\{\begin{matrix} x=0 \\ y=b \end{matrix}\right.
{ x = 0 y = b (四).
θ
=
3
∗
p
i
2
\theta = \frac{3*pi}{2}
θ = 2 3 ∗ p i
{
x
=
0
y
=
−
b
\left\{\begin{matrix} x=0 \\ y=-b \end{matrix}\right.
{ x = 0 y = − b 再考虑一般情况下的椭圆,如下:
如上
θ
∈
[
0
,
2
∗
p
i
]
\theta\in[0,2*pi]
θ ∈ [ 0 , 2 ∗ p i ] 是相对于主轴的角度,
α
∈
[
−
p
i
,
p
i
]
\alpha\in[-pi,pi]
α ∈ [ − p i , p i ] , 不过我们一般仅仅考虑
α
∈
[
0
,
p
i
]
\alpha\in[0,pi]
α ∈ [ 0 , p i ] ,求
θ
\theta
θ 对应的点的坐标?
基本思路就是先转换成标准椭圆,再应用标准椭圆下的结果。
显然我们需要将以上椭圆的中心移到原点,再绕原点旋转
−
α
-\alpha
− α ,即顺时针旋转
α
\alpha
α ,即:
(
x
y
)
=
R
(
(
X
Y
)
−
(
X
c
Y
c
)
)
\begin{pmatrix} x\\ y \end{pmatrix}=R(\begin{pmatrix} X\\ Y \end{pmatrix}-\begin{pmatrix} X_c\\Y_c \end{pmatrix})
( x y ) = R ( ( X Y ) − ( X c Y c ) ) 其中
R
=
(
c
o
s
(
−
α
)
−
s
i
n
(
−
α
)
s
i
n
(
−
α
)
c
o
s
(
−
α
)
)
=
(
c
o
s
(
α
)
s
i
n
(
α
)
−
s
i
n
(
α
)
c
o
s
(
α
)
)
R=\begin{pmatrix} cos(-\alpha) &-sin(-\alpha) \\ sin(-\alpha) & cos(-\alpha) \end{pmatrix}=\begin{pmatrix} cos(\alpha) &sin(\alpha) \\ -sin(\alpha) & cos(\alpha) \end{pmatrix}
R = ( c o s ( − α ) s i n ( − α ) − s i n ( − α ) c o s ( − α ) ) = ( c o s ( α ) − s i n ( α ) s i n ( α ) c o s ( α ) ) 因此
(
X
Y
)
=
R
−
1
(
x
y
)
+
(
X
c
Y
c
)
\begin{pmatrix} X\\ Y \end{pmatrix} =R^{-1}\begin{pmatrix} x\\ y \end{pmatrix}+\begin{pmatrix} X_c\\Y_c \end{pmatrix}
( X Y ) = R − 1 ( x y ) + ( X c Y c ) 其中
R
−
1
=
R
T
=
(
c
o
s
(
α
)
−
s
i
n
(
α
)
s
i
n
(
α
)
c
o
s
(
α
)
)
(
★
)
R^{-1}=R^{T}=\begin{pmatrix} cos(\alpha) &-sin(\alpha) \\ sin(\alpha) & cos(\alpha) \end{pmatrix}~~~~~~~~~~~~~~~~~~~~~~~~~~~(\bigstar)
R − 1 = R T = ( c o s ( α ) s i n ( α ) − s i n ( α ) c o s ( α ) ) ( ★ ) 这样将前面
(
x
y
)
\begin{pmatrix} x\\ y \end{pmatrix}
( x y ) 的四个结果应用过来,即得到倾斜的椭圆上的对应角度的点的坐标。
扩展:
如果以上的角度是相对于水平轴的角度,则对应的椭圆上的点的坐标如何求呢? 答: 其实很简单,只需要:
θ
′
=
{
θ
−
α
+
2
∗
p
i
,
θ
<
α
θ
−
α
,
θ
≥
α
\theta'=\left\{\begin{matrix} \theta-\alpha+2*pi, & \theta<\alpha \\ \theta-\alpha, & \theta \geq \alpha \end{matrix}\right.
θ ′ = { θ − α + 2 ∗ p i , θ − α , θ < α θ ≥ α 将
θ
′
\theta'
θ ′ 替换以上标准椭圆下的
θ
\theta
θ 即可。
如果竖直坐标轴为竖直向下的,即为图像坐标系下的椭圆,那么如何求对应的角度? 答: 需要做两方面的改变即可:
{
α
→
−
α
y
→
−
y
\left\{\begin{matrix} \alpha\rightarrow -\alpha\\ y\rightarrow -y \end{matrix}\right.
{ α → − α y → − y 第一行改变
★
\bigstar
★ 出的
α
\alpha
α . 第二行改变标准椭圆下的y值的符号。
matlab代码
demo.m
################################
center_x=282;
center_y=263;
phi=pi/6;
R1=141;
R2=62;
%DrawEllipse([center_x,center_y],R1,R2,phi);
axis equal;
hold on;
set(gca,'ydir','reverse')
for angle=linspace(0,3*pi/2,360)
[ px,py ] = get_points_ellipse(center_x, center_y,phi,R1,R2, angle );
plot(px,py,'ro');
hold on;
end
axis([0,500,0,500])
function [ px,py ] = get_points_ellipse(center_x, center_y,phi,R1,R2, angle )
% 求椭圆某个角度上的点的坐标。
%https://math.stackexchange.com/questions/22064/calculating-a-point-that-lies-on-an-ellipse-given-an-angle
%https://blog.csdn.net/xiamentingtao/article/details/54934467
%https://stackoverflow.com/questions/17762077/how-to-find-the-point-on-ellipse-given-the-angle
%另一种解法 从极坐标的角度出发。
% 椭圆参数 圆心 (center_x.center_y) 角度 phi(-pi,pi),实际上仅考虑(0,pi) 沿着角度的主轴半径R1,另一主轴对应的半径R2
% 求相对于水平轴x轴上angle(基于0-2pi之间)上对应的点坐标。
% 注意求解的坐标系为:水平向右为x轴,竖直向上为y轴。 如果竖直向下为y轴,则下面的y应该变换成-y, phi变成-phi
%先将斜的椭圆转正,并平移到原点。方法就是平移到中心,且旋转-phi角度,再应用标准椭圆求取点。
if(angle<phi)
theta = angle-phi+2*pi; %想对于主轴的角度
else
theta = angle-phi;
end
% theta为相对于主轴的角度
alpha = -phi; %与竖直的轴有关,如果是竖直向上,则变为正phi
% 先计算标准椭圆 x^2/R1^2+y^2/R2^2=1 与 直线 y/x=tan(angle)的交点。
assert(angle>=0 && angle<=2*pi);
if(theta==pi/2)
x=0;
y=R2;
elseif(theta==pi*3/2)
x=0;
y=-R2;
elseif(theta>pi/2 && theta<pi*3/2)
x=-R1*R2/sqrt(R2^2+R1^2*tan(theta)^2);
y=-R1*R2*tan(theta)/sqrt(R2^2+R1^2*tan(theta)^2);
else
x=R1*R2/sqrt(R2^2+R1^2*tan(theta)^2);
y=R1*R2*tan(theta)/sqrt(R2^2+R1^2*tan(theta)^2);
end
y=-y; %与竖直的轴有关,如果是竖直向上,则变为正y
% 将以上结果转换回去
px= cos(alpha)*x-sin(alpha)*y+center_x;
py=sin(alpha)*x+cos(alpha)*y+center_y;
end
function DrawEllipse(C,a,b,alpha)
% DRAWELLIPSE plots an ellipse
% DrawEllipse(C,a,b,alpha) plots ellipse with center C, semiaxis a
% and b and angle alpha between a and the x-axis
s=sin(alpha); c=cos(alpha);
Q =[c -s; s c]; theta=[0:0.02:2*pi];
u=diag(C)*ones(2,length(theta)) + Q*[a*cos(theta); b*sin(theta)];
plot(u(1,:),u(2,:));
hold on;
plot(C(1),C(2),'+');
显示结果,起点对应着角度为0。
参考文献
https://math.stackexchange.com/questions/22064/calculating-a-point-that-lies-on-an-ellipse-given-an-angle (主要参考这个)
https://stackoverflow.com/questions/17762077/how-to-find-the-point-on-ellipse-given-the-angle (这里有一个从极坐标变换角度新的推导)
https://blog.csdn.net/xiamentingtao/article/details/54934467