java椭圆写字距离_从点到椭圆弧的最短距离算法

该博客介绍了如何计算从一个点到椭圆弧的最短距离,提供了一个C++示例代码,并通过递归提高计算精度。适用于任何椭圆弧,不仅限于轴对齐的椭圆。
摘要由CSDN通过智能技术生成

我通常使用省略号:

sample the arc by N points

90 度块使用 N>=8 所以你不要错过任何东西

find the closest point

sample arc around that point by N points

覆盖前一点到下一点的区域

recursively loop to #2

每次迭代/递归都会提高准确性 . 如果达到所需的精度范围(采样区域足够小)或可变精度限制(以避免 FPU 的下溢),则停止 .

Ab3lU.png

[Notes]

这适用于任何椭圆弧而不仅仅是轴对齐 .

[Edit1] C++ example

double x0,y0,rx,ry,a0,a1; // elliptic arc center,semi-axises,start/end angles CW

void ellarc_closest_point(double &x_out,double &y_out,double x_in,double y_in)

{

int e,i;

double ll,l,aa,a,da,x,y,b0,b1;

while (a0>=a1) a0-=pi2; // just make sure a0

b0=a0; b1=a1; da=(b1-b0)/25.0; // 25 sample points in first iteration

ll=-1; aa=a0; // no best solution yet

for (i=0;i<3;i++) // recursions more means more accurate result

{

// sample arc a= with step da

for (e=1,a=b0;e;a+=da)

{

if (a>=b1) { a=b1; e=0; }

// elliptic arc sampled point

x=x0+rx*cos(a);

y=y0-ry*sin(a); // mine y axis is in reverse order therefore -

// distance^2 to x_in,y_in

x-=x_in; x*=x;

y-=y_in; y*=y; l=x+y;

// remember best solution

if ((ll<0.0)||(ll>l)) { aa=a; ll=l; }

}

// use just area near found solution aa

b0=aa-da; if (b0

b1=aa+da; if (b1>a1) b1=a1;

// 10 points per area stop if too small area already

da=0.1*(b1-b0); if (da<1e-6) break;

}

x_out=x0+rx*cos(aa);

y_out=y0-ry*sin(aa); // mine y axis is in reverse order therefore -

}

和视觉输出:

5b216524fbfbaa8ea67e6e25b66051a6.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值