以下のプログラムでは、三角形を反コック方向に45度回転させようとしています。ポリゴンはある角度に回転し、別の場所に移動しました。これを修正する方法を教えてください。
#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;
}
出力:
編集1:
出力では、赤い三角形(つまり回転後の三角形)が元の三角形よりも大きく見えます。
新しいコード
#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;
}
出力:
編集2:
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;
}
出力: