如何根据四个旋转和缩放的点绘制椭圆?
How can I draw an ellipse based on four rotated and scaled points?
我从一个完美的圆开始,然后我需要在不失去椭圆特征的情况下自由旋转和缩放它:
我想知道是否有一种方法可以描述仅基于余弦和正弦的扭曲椭圆。
我有这个代码:
float centerX = 100;
float centerY = 100;
float radiusX = 100;
float radiusY = 100;
float rotation = PI;
float res = 30;
for (int i = 0; i < res; i++) {
//find the point in space for the circle resolution
float angle = (float)i/res * TWO_PI;
float x = radiusX * cos(angle);
float y = -radiusY * sin(angle);
//rotate the point
float s = sin(rotation);
float c = cos(rotation);
// translate point to origin:
p->x -= centerX;
p->y -= centerY;
float xnew = p->x * c - p->y * s;
float ynew = p->x * s + p->y * c;
// translate point back
x = xnew + centerX;
y = ynew + centerY;
//apply X Y
}
这段代码只能表示同一个椭圆,忽略了旋转和缩放的关系:
我使用了几年前在这里问过的相同方法,但那个时候扭曲正方形很有用:
What's the correct way to draw a distorted plane in OpenGL?
代码如下:
//setup the four coordinates
float topX = 20;
float topY = 10;
float bottomX = 30;
float bottomY = 20;
float rightX = 30;
float rightY = 20;
float leftX = 30;
float leftY = 20;
//calculate the horizontal radius
float distHorX = rightX-leftX;
float distHorY = rightY-leftY;
float radiusX = sqrt(distHorX * distHorX + distHorY * distHorY)/2.f;
//calculate the vertical radius
float distVerX = topX-bottomX;
float distVerY = topY-bottomY;
radiusY = sqrt(distVerX * distVerX + distVerY * distVerY)/2.f;
float res = 30;
for (int i = 0; i < res; i++) {
float angle = (float)i/res * TWO_PI;
float x = radiusX * cos(angle);
float y = -radiusY * sin(angle);
//corvert the circle inside a square to a square inside a circle
//it is a magical number I have found to convert that proportion
x /= 0.705069124;
y /= 0.705069124;
//transform the points based on that four coordinates
float pctx = (x + radiusX) / (radiusX*2);
float pcty = (y + radiusY) / (radiusY*2);
float linePt0x = (1-pcty)* topX + pcty * leftX;
float linePt0y = (1-pcty)* topY + pcty * leftY;
float linePt1x = (1-pcty)* rightX + pcty * bottomX;
float linePt1y = (1-pcty)* rightY + pcty * bottomY;
float ptx = (1-pctx) * linePt0x + pctx * linePt1x;
float pty = (1-pctx) * linePt0y + pctx * linePt1y;
//apply X Y
x = ptx;
y = pty;
}
我从一个完美的圆开始,然后我需要在不失去椭圆特征的情况下自由旋转和缩放它:
我想知道是否有一种方法可以描述仅基于余弦和正弦的扭曲椭圆。
我有这个代码:
float centerX = 100;
float centerY = 100;
float radiusX = 100;
float radiusY = 100;
float rotation = PI;
float res = 30;
for (int i = 0; i < res; i++) {
//find the point in space for the circle resolution
float angle = (float)i/res * TWO_PI;
float x = radiusX * cos(angle);
float y = -radiusY * sin(angle);
//rotate the point
float s = sin(rotation);
float c = cos(rotation);
// translate point to origin:
p->x -= centerX;
p->y -= centerY;
float xnew = p->x * c - p->y * s;
float ynew = p->x * s + p->y * c;
// translate point back
x = xnew + centerX;
y = ynew + centerY;
//apply X Y
}
这段代码只能表示同一个椭圆,忽略了旋转和缩放的关系:
我使用了几年前在这里问过的相同方法,但那个时候扭曲正方形很有用: What's the correct way to draw a distorted plane in OpenGL?
代码如下:
//setup the four coordinates
float topX = 20;
float topY = 10;
float bottomX = 30;
float bottomY = 20;
float rightX = 30;
float rightY = 20;
float leftX = 30;
float leftY = 20;
//calculate the horizontal radius
float distHorX = rightX-leftX;
float distHorY = rightY-leftY;
float radiusX = sqrt(distHorX * distHorX + distHorY * distHorY)/2.f;
//calculate the vertical radius
float distVerX = topX-bottomX;
float distVerY = topY-bottomY;
radiusY = sqrt(distVerX * distVerX + distVerY * distVerY)/2.f;
float res = 30;
for (int i = 0; i < res; i++) {
float angle = (float)i/res * TWO_PI;
float x = radiusX * cos(angle);
float y = -radiusY * sin(angle);
//corvert the circle inside a square to a square inside a circle
//it is a magical number I have found to convert that proportion
x /= 0.705069124;
y /= 0.705069124;
//transform the points based on that four coordinates
float pctx = (x + radiusX) / (radiusX*2);
float pcty = (y + radiusY) / (radiusY*2);
float linePt0x = (1-pcty)* topX + pcty * leftX;
float linePt0y = (1-pcty)* topY + pcty * leftY;
float linePt1x = (1-pcty)* rightX + pcty * bottomX;
float linePt1y = (1-pcty)* rightY + pcty * bottomY;
float ptx = (1-pctx) * linePt0x + pctx * linePt1x;
float pty = (1-pctx) * linePt0y + pctx * linePt1y;
//apply X Y
x = ptx;
y = pty;
}