當前位置:編程學習大全網 - 編程語言 - 利用C語言實現二維圖形的變換

利用C語言實現二維圖形的變換

妳先看看吧,思路大概就是這樣,不懂的問我。

#include<stdio.h>

#include<math.h>

#include<iostream>

using namespace std;

struct point

{

float x;

float y;

};

void translation(point*pt, float xp,float yp,int num)//num代表點的個數

{

for(int i=0;i<num;i++)

{

(pt+i)->x+=xp;

(pt+i)->y+=yp;

}

}

void scale(point *pt,float xs,float ys,int num)

{

for(int i=0;i<num;i++)

{

(pt+i)->x*=xs;

(pt+i)->y*=ys;

}

}

void rotation(point *pt,float angle,int num)

{

int a[2][2];

angle=angle/180*3.141592657;

a[0][0]=cos(angle);

a[0][1]=-sin(angle);

a[1][0]=sin(angle);

a[1][1]=cos(angle);

point* temp;

for(int i=0;i<num;i++)

{

temp->x=(pt+i)->x;

temp->y=(pt+i)->y;

(pt+i)->x=temp->x*a[0][0]+a[0][1]*temp->y;

(pt+i)->y*=temp->x*a[1][0]+a[1][1]*temp->y;

}

}

int main()

{

int i=0,N,mode,angle,xp,yp,xk,yk,num;

cout<<"please input the number of point "<<endl;

scanf("%d",&N);

num=N;

point pt[10];

while(N--)

{

printf("please input points(x,y):\n");

scanf("%f%f",&pt[i].x,&pt[i].y);

i++;

}

printf("please input motions\n");

printf("0 stand for translation:\n");

printf("1 stand for scale:\n");

printf("2 stand for rotation:\n");

scanf("%d",&mode);

switch(mode)

{

case 0:

printf("please input the translation in x and y direction respectivly:\n");

cin>>xp>>yp;

translation(pt, xp,yp,num);

break;

case 1:

printf("please input the scale in x and y direction respectivly:\n");

scanf("%f%f",&xk,&yk);

scale(pt, xk,yk,num);

break;

case 2:

printf("please input the angle:\n");

scanf("%f",&angle);

rotation(pt, angle,num);

break;

}

printf("after translatiton or scale or rotation:\n");

for(int i=0;i<num;i++)

printf("%f %f\n",pt[i].x,pt[i].y);

}

  • 上一篇:《終結者》系列|AiFilm
  • 下一篇:c語言為什麽不能用goto寫循環
  • copyright 2024編程學習大全網