简体   繁体   English

C-图形,无法旋转多边形

[英]C - Graphics, Unable to rotate a polygon

In the below program I am trying to rotate the triangle by 45 degree anti-cockwise. 在下面的程序中,我试图将三角形逆时针旋转45度。 The polygon did rotate to some angle and translated to some other place. 多边形确实旋转了一定角度,然后平移到其他地方。 Please let me know how this can be fixed. 请让我知道如何解决此问题。

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

int main(void)
{
 int poly[8], rpoly[8],angle;
 int gd = DETECT, gm;
 initgraph(&gd,&gm,"C:\\TC\\BGI");

 printf("\n Enter co-ordinates for Triangle :  \n\n");

 printf("Vertex 1 (x,y)  : ");
 scanf("%d %d", &poly[0], &poly[1]);
 printf("Vertex 2 (x,y)  : ");
 scanf("%d %d", &poly[2], &poly[3]);
 printf("Vertex 3 (x,y)  : ");
 scanf("%d %d", &poly[4], &poly[5]);
 poly[6] = poly[0];
 poly[7] = poly[1];

 setcolor(2);
 drawpoly(4,poly);

 angle = 45;
 rpoly[0] = poly[0]*cos(angle) - poly[1]*sin(angle);
 rpoly[1] = poly[0]*sin(angle) + poly[1]*cos(angle);

 rpoly[2] = poly[2]*cos(angle) - poly[3]*sin(angle);
 rpoly[3] = poly[2]*sin(angle) + poly[3]*cos(angle);

 rpoly[4] = poly[4]*cos(angle) - poly[5]*sin(angle);
 rpoly[5] = poly[4]*sin(angle) + poly[5]*cos(angle);

 rpoly[6] = rpoly[0];
 rpoly[7] = rpoly[1];

 setcolor(4);
 drawpoly(4,rpoly);

 getch();
 closegraph();
 return 0;
}

Output: 输出: 在此处输入图片说明

EDIT 1: 编辑1:

In the Output the Red Triangle (ie Triangle after Rotation) looks bigger than the original one. 在输出中,红色三角形(即旋转后的三角形)看起来比原始三角形大。

New Code 新密码

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

int main(void)
{
 int poly[8], rpoly[8], angle, centroid_x, centroid_y,i;
 int gd = DETECT, gm;
 initgraph(&gd,&gm,"C:\\TC\\BGI");

 printf("\n Enter co-ordinates for Triangle :  \n\n");

 printf("Vertex 1 (x,y)  : ");
 scanf("%d %d", &poly[0], &poly[1]);
 printf("Vertex 2 (x,y)  : ");
 scanf("%d %d", &poly[2], &poly[3]);
 printf("Vertex 3 (x,y)  : ");
 scanf("%d %d", &poly[4], &poly[5]);
 poly[6] = poly[0];
 poly[7] = poly[1];

 setcolor(2);
 drawpoly(4,poly);

 angle = 45 * 2 * 3.14 / 360;
 centroid_x =  (poly[0] + poly[2] + poly[4]) / 3;
 centroid_y =  (poly[1] + poly[3] + poly[5]) / 3;

 rpoly[0] = (poly[0] - centroid_x) * cos(angle)  -  (poly[1] - centroid_y) * sin(angle)  +  centroid_x;
 rpoly[1] = (poly[0] - centroid_x) * sin(angle)  -  (poly[1] - centroid_y) * cos(angle)  +  centroid_y;

 rpoly[2] = (poly[2] - centroid_x) * cos(angle)  -  (poly[3] - centroid_y) * sin(angle)  +  centroid_x;
 rpoly[3] = (poly[2] - centroid_x) * sin(angle)  -  (poly[3] - centroid_y) * cos(angle)  +  centroid_y;

 rpoly[4] = (poly[4] - centroid_x) * cos(angle)  -  (poly[5] - centroid_y) * sin(angle)  +  centroid_x;
 rpoly[5] = (poly[4] - centroid_x) * sin(angle)  -  (poly[5] - centroid_y) * cos(angle)  +  centroid_y;

 rpoly[6] = rpoly[0];
 rpoly[7] = rpoly[1];

 setcolor(4);
 drawpoly(4,rpoly);

 for(i=0;i<7;i++)
 {printf("\n %d", &rpoly[i]);}

 getch();
 closegraph();
 return 0;
}

OutPut: 输出: 在此处输入图片说明

EDIT 2: 编辑2:

Code after correcting the Y - terms: 纠正Y-项后的代码:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

int main(void)
{
 int poly[8], rpoly[8], angle, centroid_x, centroid_y,i;
 int gd = DETECT, gm;
 initgraph(&gd,&gm,"C:\\TC\\BGI");

 printf("\n Enter co-ordinates for Triangle :  \n\n");

 printf("Vertex 1 (x,y)  : ");
 scanf("%d %d", &poly[0], &poly[1]);
 printf("Vertex 2 (x,y)  : ");
 scanf("%d %d", &poly[2], &poly[3]);
 printf("Vertex 3 (x,y)  : ");
 scanf("%d %d", &poly[4], &poly[5]);
 poly[6] = poly[0];
 poly[7] = poly[1];

 setcolor(2);
 drawpoly(4,poly);

 angle = 45 * 2 * 3.14 / 360;
 centroid_x =  (poly[0] + poly[2] + poly[4]) / 3;
 centroid_y =  (poly[1] + poly[3] + poly[5]) / 3;

 rpoly[0] = (poly[0] - centroid_x) * cos(angle)  -  (poly[1] - centroid_y) * sin(angle)  +  centroid_x;
 rpoly[1] = (poly[0] - centroid_x) * sin(angle)  +  (poly[1] - centroid_y) * cos(angle)  +  centroid_y;

 rpoly[2] = (poly[2] - centroid_x) * cos(angle)  -  (poly[3] - centroid_y) * sin(angle)  +  centroid_x;
 rpoly[3] = (poly[2] - centroid_x) * sin(angle)  +  (poly[3] - centroid_y) * cos(angle)  +  centroid_y;

 rpoly[4] = (poly[4] - centroid_x) * cos(angle)  -  (poly[5] - centroid_y) * sin(angle)  +  centroid_x;
 rpoly[5] = (poly[4] - centroid_x) * sin(angle)  +  (poly[5] - centroid_y) * cos(angle)  +  centroid_y;

 rpoly[6] = rpoly[0];
 rpoly[7] = rpoly[1];

 setcolor(4);
 drawpoly(4,rpoly);

// for(i=0;i<8;i++)
// {printf("\n %d", &rpoly[i]);}

 getch();
 closegraph();
 return 0;
}

Output: 输出:

在此处输入图片说明

Two problems: sin and cos take arguments in radians , not degrees; 两个问题: sincos弧度而不是度数争论; and you probably want to rotate your polygon about its centre, not about (0, 0). 并且您可能想绕多边形的中心而不是(0,0)旋转多边形。

To fix the first problem, change: 要解决第一个问题,请更改:

angle = 45;

to: 至:

angle = 45.0 * 2.0 * M_PI / 360.0;

To fix the second problem you need to first calculate the centroid of your polygon, then do your translations of the coordinates of the vertices relative to the centroid. 要解决第二个问题,您需要首先计算多边形的质心,然后对顶点相对于质心的坐标进行平移。

 rpoly[0] = (poly[0] - centroid_x) * cos(angle)  -  (poly[1] - centroid_y) * sin(angle)  +  centroid_x;
 rpoly[1] = (poly[0] - centroid_x) * sin(angle)  +  (poly[1] - centroid_y) * cos(angle)  +  centroid_y;

rpoly[1] is calculated after rpoly[0] has been changed. rpoly [0]更改后,将计算rpoly [1]。

try this; 尝试这个;

float newX = .......;
rpoly[0] = newX;
float newY = .......;
rpoly[1] = newY;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM