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

标签 java ellipse

我目前正在制作一个屏幕保护程序,我希望我的椭圆慢慢地变成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);
}
}

从矩形转换为椭圆形比较困难,因为中间的形状既不是矩形也不是椭圆形。可能有一些更一般的物体,可以是矩形、椭圆形或介于两者之间的物体,但我想不出一个。

所以,简单的方法已经出来了,但还有更难的方法。假设我将单位圆分成N block ,并在椭圆Ei和矩形Ri上写点。现在,随着变换的发生,点 Ei 移动到点 Ri 中。实现此目的的一个简单方法是使用线性组合。

Ti = (1-v) * Ei + v * Ri

因此,为了进行转换,我们将 v 从 0 慢慢增加到 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;
}
}

关于java - 在Java中如何将椭圆形转换为矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34126452/

相关文章:

java - 从 Firebase 加载数据仅在第一次返回 null

java - 将棋盘坐标显示为 ASCII 棋盘

java - 基于主题/ token 和 token 组的完整FCM Java/Spring集成

java - 如何使 IntelliJ 自动完成不在 Javadoc 中插入完整路径

java - 在 Java 中导入类

java - 移动已绘制的椭圆

.net - 在椭圆圆周上找到一个点,该点在具有中心点,高度和宽度的矩形内?

matlab - 如何确定纬度和经度是否在椭圆内

image-processing - fitEllipse 在 OpenCV 中如何工作?

math - 给定椭圆度的测量值,画一个椭圆