圆上任意角度的点的坐标
如上图,给定圆心(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.
{px=Cx+Rcos(θ)py=Cy+Rsin(θ)
注意如果以上竖直坐标系向下,此时变为图像的坐标系,即
结果变成:
{
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.
{px=Cx+Rcos(θ)py=Cy−Rsin(θ)
仅仅是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.
{a2x2+b2y2xy==1tan(θ)
将上面的第二式带入第一式,可以求解得到:
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)}
x2=b2+a2tan2(θ)a2b2
这时我们考虑
θ
\theta
θ的范围,可以得到:
(一).
0
≤
θ
<
p
i
/
2
0\leq\theta<pi/2
0≤θ<pi/2 或者
3
∗
p
i
2
<
θ
≤
2
∗
p
i
\frac{3*pi}{2}<\theta \leq 2*pi
23∗pi<θ≤2∗pi
{
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=b2+a2tan2(θ)aby=b2+a2tan2(θ)abtan(θ)
(二).
p
i
/
2
<
θ
<
3
∗
p
i
2
pi/2<\theta<\frac{3*pi}{2}
pi/2<θ<23∗pi
{
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=−b2+a2tan2(θ)aby=−b2+a2tan2(θ)abtan(θ)
(三).
θ
=
p
i
/
2
\theta = pi/2
θ=pi/2
{
x
=
0
y
=
b
\left\{\begin{matrix} x=0 \\ y=b \end{matrix}\right.
{x=0y=b
(四).
θ
=
3
∗
p
i
2
\theta = \frac{3*pi}{2}
θ=23∗pi
{
x
=
0
y
=
−
b
\left\{\begin{matrix} x=0 \\ y=-b \end{matrix}\right.
{x=0y=−b
再考虑一般情况下的椭圆,如下:
如上 θ ∈ [ 0 , 2 ∗ p i ] \theta\in[0,2*pi] θ∈[0,2∗pi]是相对于主轴的角度, α ∈ [ − p i , p i ] \alpha\in[-pi,pi] α∈[−pi,pi], 不过我们一般仅仅考虑 α ∈ [ 0 , p i ] \alpha\in[0,pi] α∈[0,pi],求 θ \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})
(xy)=R((XY)−(XcYc))
其中
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=(cos(−α)sin(−α)−sin(−α)cos(−α))=(cos(α)−sin(α)sin(α)cos(α))
因此
(
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}
(XY)=R−1(xy)+(XcYc)
其中
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=RT=(cos(α)sin(α)−sin(α)cos(α)) (★)
这样将前面
(
x
y
)
\begin{pmatrix} x\\ y \end{pmatrix}
(xy) 的四个结果应用过来,即得到倾斜的椭圆上的对应角度的点的坐标。
扩展:
- 如果以上的角度是相对于水平轴的角度,则对应的椭圆上的点的坐标如何求呢?
答: 其实很简单,只需要:
θ ′ = { θ − α + 2 ∗ p i , θ < α θ − α , θ ≥ α \theta'=\left\{\begin{matrix} \theta-\alpha+2*pi, & \theta<\alpha \\ \theta-\alpha, & \theta \geq \alpha \end{matrix}\right. θ′={θ−α+2∗pi,θ−α,θ<αθ≥α
将 θ ′ \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://blog.csdn.net/he_zhidan/article/details/81347426
根据“圆心角”弧度(函数会将圆心角转成离心角),获取斜椭圆上的点坐标。ptCenter,椭圆中心;a椭圆长轴长度;b,椭圆短轴长度;radian,点于椭圆长轴夹角的弧度;dChangZhouAngle,长轴弧度
CDoublePoint2d GetPointOnEllipse(const CDoublePoint2d& ptCenter, double a, double b, double radian, double dChangZhouAngle)
{
const double dLiXin = atan2(asin(radian), bcos(radian));//离心角
double x = a*cos(dLiXin)cos(dChangZhouAngle) - bsin(dLiXin)sin(dChangZhouAngle) + ptCenter.x;
double y = acos(dLiXin)sin(dChangZhouAngle) + bsin(dLiXin)*cos(dChangZhouAngle) + ptCenter.y;
return CDoublePoint2d(x, y);
}
问题:
一,为什么不用atan?
答:atan的范围是[-π/2,π/2],atan2的范围是(-π,π]。前者只能表示两个象限,后者可以表示4个。
二,为什么不atan2(a*tan(radian),b)?
答:这样写,和用atan的效果一样。