c# - 如何在给定角度的情况下找到椭圆上的点

标签 c#

我有一个中心点在原点(0,0)的椭圆

double dHalfwidthEllipse = 10; 
double dHalfheightEllipse = 20;
double dAngle = 30;//Its in degree
PointF ptfPoint = new PointF();//To be found
PointF ptfOrigin = new PointF(0, 0);//Origin

点相对于原点的角度 = 30 度; 如何使用 C# 在给定上述值的情况下得到该点?

最佳答案

参见 http://www.mathopenref.com/coordparamellipse.html

中心点在原点,半宽a半高b的椭圆的参数方程为

x(t) = a cos t,
y(t) = b sin t

如果你只是想画一个椭圆,给定

double dHalfwidthEllipse = 10;       // a
double dHalfheightEllipse = 20;      // b
PointF ptfOrigin = new PointF(0, 0); // Origin

你只需要

PointF ptfPoint = 
    new PointF(ptfOrigin.X + dHalfwidthEllipse * Math.Cos(t * Math.Pi/180.0), 
               ptfOrigin.Y + dHalfheightEllipse * Math.Sin(t * Math.Pi/180.0) );

t 在 -180 到 180 度之间变化。

但是,正如@Sebastian 指出的那样,如果您希望计算与通过中心且角度为 theta 的线的精确交点,它会变得有点复杂,因为我们需要找到对应于 theta 的 t:

y(t)/x(t) = tan θ

b sin t / (a cos t) = tan θ

b/a tan t = tan θ

t= arctan(a tan θ / b) + n * π

如果我们添加

double dAngle = 30;                  // theta, between -90 and 90 degrees

我们可以计算 t 和 ptfPoint:

double t = Math.Atan( dHalfwidthEllipse * Math.Tan( dAngle * Math.Pi/180.0 ) 
                                                    / dHalfheightEllipse);
PointF ptfPoint = 
    new PointF(ptfOrigin.X + dHalfwidthEllipse * Math.Cos(t), 
               ptfOrigin.Y + dHalfheightEllipse * Math.Sin(t) );

这适用于正 x 轴周围的区域。 对于 90 到 180 度之间的 theta,添加 π:

double t = Math.Atan( dHalfwidthEllipse * Math.Tan( dAngle * Math.Pi/180.0 ) 
                                                    / dHalfheightEllipse) + Math.Pi;

对于 -180 到 -90 度之间的 theta,减去 π:

double t = Math.Atan( dHalfwidthEllipse * Math.Tan( dAngle * Math.Pi/180.0 ) 
                                                    / dHalfheightEllipse) - Math.Pi;

当您接近 y 轴时,x(t) 接近零并且上述计算除以零,但您可以使用相反的方法:

x(t)/y(t) = tan (90 - θ)

a cos t / (b sin t) = tan (90 - θ)

a/b tan t = tan (90 - θ)

t = arctan ( b tan (90 - θ) / a ) + n * π

关于c# - 如何在给定角度的情况下找到椭圆上的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17762077/

相关文章:

c# - ZeroMQ 非阻塞非队列推送

c# - 在一种方法中构造多个查询的最佳方法 c# asp.net

c# - Postgres analog for c# SQL Server´s stored procedure CLR

c# - 从 MsSQL 传输到 MySQL 错误

c# - 事件到命令行为不执行命令

c# - 如何从 PropertyInfo 获取属性的值?

c# 循环traceListener不滚动文件

c# - 使用反射的模糊异常

c# - 多处理中的 Path.GetTempFileName

c# - 如何在 C# 中打开和检测 exe 的关闭