2011-04-11 56 views
0

我试过下面的代码,如果矩形的宽度与它的高度相同,它会绘制一个很好的圆的近似值;但它并没有画出一个很大的椭圆形,“角落”非常尖锐。有什么建议么?如何使用贝塞尔曲线近似椭圆以填充给定的矩形?

float width = rect.width(); 
float height = rect.height(); 
float centerX = rect.width()/2; 
float centerY = rect.height()/2; 

float diameter = Math.min(width, height); 
float length = (float) (0.5522847498 * diameter/2); 


path.moveTo(0, centerY); 
path.cubicTo(0, centerY - length, 0, centerX - length, 0, centerX, 0); 
path.cubicTo(centerX + length, 0, width, centerY - length, height, centerY); 
path.cubicTo(width, centerY + length, centerX + length, height, centerX, height); 
path.cubicTo(centerX - length, height, 0, centerY + length, 0, centerY); 

回答

1

您应该根据其轴位于哪个轴上来缩放length,以便从每个圆弧终点到相邻控制点的距离(不是固定的)是您在此点平行移动的轴的固定分数。

+0

谢谢,这工作完美。 – ab11 2011-04-12 17:08:22

0

如果它是一个真正的矩形,用直角,那么它应该很容易。椭圆的长轴和短轴等于矩形边的长度,而椭圆的中心位于矩形对角线的交点处。

我不知道如何表达它,因为贝塞尔曲线离开了我的头顶,但椭圆的经典方程应该很容易写,只要您先转换到适当的坐标系(例如沿矩形/椭圆的长轴的x轴,沿矩形/椭圆的短轴的y轴)。