求圆和椭圆上任意角度的点的坐标

圆上任意角度的点的坐标

在这里插入图片描述

如上图,给定圆心(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=CyRsin(θ)
仅仅是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 ≤ θ &lt; p i / 2 0\leq\theta&lt;pi/2 0θ<pi/2 或者 3 ∗ p i 2 &lt; θ ≤ 2 ∗ p i \frac{3*pi}{2}&lt;\theta \leq 2*pi 23pi<θ2pi
{ 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 &lt; θ &lt; 3 ∗ p i 2 pi/2&lt;\theta&lt;\frac{3*pi}{2} pi/2<θ<23pi
{ 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} θ=23pi
{ 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,2pi]是相对于主轴的角度, α ∈ [ − 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) &amp;-sin(-\alpha) \\ sin(-\alpha) &amp; cos(-\alpha) \end{pmatrix}=\begin{pmatrix} cos(\alpha) &amp;sin(\alpha) \\ -sin(\alpha) &amp; 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)=R1(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) &amp;-sin(\alpha) \\ sin(\alpha) &amp; cos(\alpha) \end{pmatrix}~~~~~~~~~~~~~~~~~~~~~~~~~~~(\bigstar) R1=RT=(cos(α)sin(α)sin(α)cos(α))                           ()
这样将前面 ( x y ) \begin{pmatrix} x\\ y \end{pmatrix} (xy) 的四个结果应用过来,即得到倾斜的椭圆上的对应角度的点的坐标。

扩展:

  1. 如果以上的角度是相对于水平轴的角度,则对应的椭圆上的点的坐标如何求呢?
    答: 其实很简单,只需要:
    θ ′ = { θ − α + 2 ∗ p i , θ &lt; α θ − α , θ ≥ α \theta&#x27;=\left\{\begin{matrix} \theta-\alpha+2*pi, &amp; \theta&lt;\alpha \\ \theta-\alpha, &amp; \theta \geq \alpha \end{matrix}\right. θ={θα+2pi,θα,θ<αθα
    θ ′ \theta&#x27; θ替换以上标准椭圆下的 θ \theta θ即可。
  2. 如果竖直坐标轴为竖直向下的,即为图像坐标系下的椭圆,那么如何求对应的角度?
    答:
    需要做两方面的改变即可:
    { α → − α y → − y \left\{\begin{matrix} \alpha\rightarrow -\alpha\\ y\rightarrow -y \end{matrix}\right. {ααyy
    第一行改变 ★ \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 = a
cos(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的效果一样。

参考文献

  1. https://math.stackexchange.com/questions/22064/calculating-a-point-that-lies-on-an-ellipse-given-an-angle (主要参考这个)
  2. https://stackoverflow.com/questions/17762077/how-to-find-the-point-on-ellipse-given-the-angle (这里有一个从极坐标变换角度新的推导)
  3. https://blog.csdn.net/xiamentingtao/article/details/54934467
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值