求看看去我这个代码哪里写错了?要怎么修改啊?

调用ScaleTransform函数了,但绘制不出图形,这个代码要怎么修改啊?是哪里写错了?

#include <graphics.h>
#include<math.h>
/*void MoveTransform(POINT p[20],int m,float MoveX,float MoveY)
{
    int i;
    for(i=0;i<m;i++)
    {    p[i].x=p[i].x+MoveX;p[i].y=p[i].y+MoveY;}
}*/
/*void RotateTransform(POINT p[20],int m,POINT Cen,float theta)
{
    int i;double a,b,c,d;
    POINT p1[20];
    double alfa=theta*3.1415926/180;
    a=cos(alfa);b=sin(alfa);
    for(i=0;i<m;i++)
    {
    p1[i].x=p[i].x*a-p[i].y*b+Cen.x*(1-a)+Cen.y*b;
    p1[i].y=p[i].x*b+p[i].y*a-Cen.x*b+Cen.y*(1-a);
    }
    for(i=0;i<m;i++)
    {   p[i].x=p1[i].x;p[i].y=p1[i].y;}
}*/
void ScaleTransform(POINT p[20],int m,float ScaleX,float ScaleY)
{
    int i;
    for(i=0;i<m;i++)
    {
    p[i].x=p[i].x*ScaleX;
    p[i].y=p[i].y*ScaleY;
    }
}
int main()
{
    int i,n=5;
    int ch;
    POINT p[20],p1,p2;
    p[0].x=200;p[0].y=50;
    p[1].x=300;p[1].y=50;
    p[2].x=300;p[2].y=100;
    p[3].x=200;p[3].y=100;
    p[4].x=200;p[4].y=50;
initgraph(800,600);
setbkcolor(WHITE);
setcolor(BLACK);
    moveto(p[0].x,p[0].y);
    for(i=1;i<n;i++) lineto(p[i].x,p[i].y);
    do{
        ch=getch();
        /*if(ch=='z')  MoveTransform(p,n,-50,0);
        if(ch=='y') MoveTransform(p,n,50,0);
        if(ch=='s')    MoveTransform(p,n,0,-50);
        if(ch=='x')  MoveTransform(p,n,0,50);*/
        if(ch=='z')  ScaleTransform(p,n,-0.5,1);
        if(ch=='y') ScaleTransform(p,n,0.5,1);
        if(ch=='s')    ScaleTransform(p,n,1,-0.5);
        if(ch=='x')  ScaleTransform(p,n,1,0.5);
        cleardevice();
         moveto(p[0].x,p[0].y);
           for(i=1;i<n;i++)    lineto(p[i].x,p[i].y);
    }while(1);
    getch();
    closegraph();
}

你的代码在调用 ScaleTransform 函数时,你把参数传递给了该函数,但函数结束后并没有进行重绘操作,导致屏幕画面未能更新。所以在每次执行完变换操作后,需要调用 cleardevice() 函数清空屏幕,再调用 moveto()lineto() 函数重新绘制图形。
修改后的代码参考:



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

void ScaleTransform(POINT p[20], int m, float ScaleX, float ScaleY)
{
    int i;
    for (i = 0; i < m; i++) {
        p[i].x = p[i].x * ScaleX;
        p[i].y = p[i].y * ScaleY;
    }
}

int main()
{
    int i, n = 5;
    int ch;
    POINT p[20];
    p[0].x = 200; p[0].y = 50;
    p[1].x = 300; p[1].y = 50;
    p[2].x = 300; p[2].y = 100;
    p[3].x = 200; p[3].y = 100;
    p[4].x = 200; p[4].y = 50;

    initgraph(800, 600);
    setbkcolor(WHITE);
    setcolor(BLACK);

    // 绘制起始图形
    moveto(p[0].x, p[0].y);
    for (i = 1; i < n; i++) {
        lineto(p[i].x, p[i].y);
    }

    do {
        ch = getch();
        if (ch == 'z') {
            ScaleTransform(p, n, -0.5, 1); // x 方向缩小一半
        }
        if (ch == 'y') {
            ScaleTransform(p, n, 0.5, 1); // x 方向放大一倍
        }
        if (ch == 's') {
            ScaleTransform(p, n, 1, -0.5); // y 方向缩小一半
        }
        if (ch == 'x') {
            ScaleTransform(p, n, 1, 0.5); // y 方向放大一倍
        }
        cleardevice(); // 清空屏幕

        // 重绘变换后的图形
        moveto(p[0].x, p[0].y);
        for (i = 1; i < n; i++) {
            lineto(p[i].x, p[i].y);
        }
    } while (1);

    getch();
    closegraph();

    return 0;
}

每次执行完变换操作后都调用了 cleardevice() 函数清空屏幕,以防止屏幕画面没有被重绘的情况发生。同时,在重新绘制变换后的图形时,也进行了重绘操作。