2015-12-07 46 views

回答

1

有一些形状很容易相互转换。例如,正方形是具有相等边长的矩形,圆是具有相同轴的椭圆。所以很容易将一个正方形转换成一个矩形,因为您可以使用一些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); 
} 
} 

从矩形到椭圆形转变为更硬的,因为在它们之间的形状既不是矩形也不是椭圆。也许有一些更一般的对象,可以是矩形,椭圆或其他东西,但我想不出来。

所以,简单的方法是,但有一个更难的方法来做到这一点。假设我将单位圆划分成N个部分并在椭圆Ei和矩形Ri上写入点。现在,随着转变发生,点Ei移动到点Ri。一个简单的方法是使用线性组合。

TI =(1-V)* EI + V *日

那么做,我们慢慢地从0增加v到1的转变,我们的点Ti的画线(或更好,但插值)。

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; 
} 
} 
+0

非常好 - 你能添加一些代码片段吗? –

+0

刚刚添加。我还没有测试过它。 –

+0

哇 - 我希望我可以添加更多的加号 - 非常有价值的答案! –