在Java中如何将椭圆转换为矩形?

在Java中如何将椭圆转换为矩形?,java,ellipse,Java,Ellipse,我目前正在制作一个屏幕保护程序,我希望我的椭圆在java中慢慢转换成一个矩形。最简单的方法是什么?有些形状很容易相互转换。例如,正方形是边长相等的矩形,圆形是轴相等的椭圆。因此,很容易将正方形转换为矩形,因为您可以使用一些drawrectangle函数,并在整个过程中调整参数。从圆到椭圆同上 squaretorect(double width,double height) { //Transform a square width * width to a rectangle width * he

我目前正在制作一个屏幕保护程序,我希望我的椭圆在java中慢慢转换成一个矩形。最简单的方法是什么?

有些形状很容易相互转换。例如,正方形是边长相等的矩形,圆形是轴相等的椭圆。因此,很容易将正方形转换为矩形,因为您可以使用一些drawrectangle函数,并在整个过程中调整参数。从圆到椭圆同上

squaretorect(double width,double height)
{
//Transform a square width * width to a rectangle width * height
int n = 100;//Number of intermediate points
int i;
double currentheight;
for(i=0;i<n;i++)
{
currentheight = width + (height-width) * i/(n-1);
drawrectangle(width,currentheight);
}
}
squaretorect(双宽双高)
{
//将正方形宽度*宽度转换为矩形宽度*高度
int n=100;//中间点的数量
int i;
双电流高度;

对于(i=0;i)您需要显示您尝试添加的内容。但是我还没有测试它。哇-我希望我可以添加更多的优点-非常有价值的答案!
ellipsetorectangle(double a, double b, double w, double h)
{
 //(x/a)^2+(y/b)^2 = 1
 //Polar r = 1/sqrt(cos(phi)^2/a^2 + sin(phi)^2/b^2)

int N = 1000;
int i;
double phi; double r;
double phirect = atan(w/h);//Helps determine which of the 4 line segments we are on
ArrayList<Point> Ei;
ArrayList<Point> Ri;
for(i=0;i<N;i++)
{
//Construct ellipse
phi = 2PI * (double)i/N;
r = 1/sqrt(cos(phi)^2/a^2 + sin(phi)^2/b^2);
Ei.add(new Point(r * cos(phi),r * sin(phi));

//Construct Rectangle (It's hard)
if(phi > 2Pi - phirect || phi < phirect)
{Ri.add(new Point(w/2,w/2 * tan(phi)));}
else if(phi > phirect)
{Ri.add(new Point(h/2 * tan(phi),h/2));}
else if(phi > PI-phirect)
{Ri.add(new Point(-w/2,-w/2 * tan(phi)));}
else if(phi > PI+phirect)
{Ri.add(new Point(-h/2,-h/2 * tan(phi)));}
}

}

Arraylist<Point> Ti;
int transitionpoints = 100;
double v;
int j;
for(j=0;j<transitionpoints;j++)
{
//This outer loop represents one instance of the object. You should probably clear the picture here. This probably belongs in a separate function but it would take awhile to write it that way.
for(i=0;i<N;i++)    
{    
v = (double)1 * j/(N-1);
Ti = new Point(v * Ri.get(i).getx + (1-v) * Ei.get(i).getx,
     v * Ri.get(i).gety + (1-v) * Ei.get(i).gety);
if(i != 0)
drawline(Ti,Tiold);
Tiold = Ti;
}
}