0
// Program to implement 2D transformations on a rectangle.
// (1) Translation (2) Rotation (3) Scaling.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void main()
{
   clrscr();
   int tx,ty,c,t,i,j,k,sx,sy,x1,x2,y1,y2;
   float tx1;
   int gd = DETECT, gm;
   initgraph(&gd, &gm,"C:\\TC\\BGI");
   x1 = y2 = 100;
   x2 = y1 = 150;
   rectangle(x1,y1,x2,y2);

   cout<<"Enter any one transformation:\n1:Translation \n2:Rotation \n3:Scaling  ";
   cout<<"\nSelected: ";
   cin>>c;
switch(c)
{
    case 1:
        cout<<"Enter tx & ty :";
        cin>>tx>>ty;
        rectangle(x1+tx, y1+ty, x2+tx, y2+ty);
        break;

    case 2 :
        int xx1, yy1, xx2, yy2, xx3, yy3, xx4, yy4;
        int ax1, ay1, ax2, ay2, ax3, ay3, ax4, ay4;
        int refx, refy;
        cout<<"Enter an angle for rotation: ";
        cin>>tx1;
        tx1=tx1*(3.14/180);

        refx = refy = 100;

        xx1 = yy1 = yy2 = xx4 = 100;
        xx2 = xx3 = yy3 = yy4 = 150;

        ax1 = refy +(xx1-refx)* cos(tx1)-(yy1-refy)*sin(tx1);
        ay1 = refy +(xx1-refx)* sin(tx1)+(yy1-refy)*cos(tx1);

        ax2 = refy +(xx2-refx)* cos(tx1)-(yy2-refy)*sin(tx1);
        ay2 = refy +(xx2-refx)* sin(tx1)+(yy2-refy)*cos(tx1);

        ax3 = refy +(xx3-refx)* cos(tx1)-(yy3-refy)*sin(tx1);
        ay3 = refy +(xx3-refx)* sin(tx1)+(yy3-refy)*cos(tx1);

        ax4 = refy +(xx4-refx)* cos(tx1)-(yy4-refy)*sin(tx1);
        ay4 = refy +(xx4-refx)* sin(tx1)+(yy4-refy)*cos(tx1);

        line(ax1,ay1, ax2, ay2);
        line(ax2,ay2, ax3, ay3);
        line(ax3,ay3, ax4, ay4);
        line(ax4,ay4, ax1, ay1);
        break;

    case 3 :
       cout<<"Enter sx & sy: ";
       cin>>sx>>sy;
       rectangle(x1*sx, y1*sy, x2*sx, y2*sy);
       break;

    default :
        cout<<"Error! Enter a valid choice";
   }
   getch();
   closegraph();
}

使用 C++ 实现的 BGI graphics.h 库。我知道它很古老,但我需要它用于学校项目。显然在 IDE 中不适合我,我已经安装了所有库。它只是不显示任何输出,只显示黑屏。实现 2D 变换的程序 (i) 缩放 (ii) 旋转

4

1 回答 1

0

很难猜出这个问题的答案,因为您使用的 1980 年代技术已经过时多年(甚至比最早的标准早了十多年),但是如果 Turbo C++ 有,flush那么您将需要它。在用户提交输入之前,您的控制台输出可能会被缓冲。

于 2020-07-10T12:01:11.160 回答