api618 发表于 2007-1-15 20:12

[求助]用C语言实现的二维图形的变换

<P>效果为一三角形绕一点旋转一周,下面程序应如何修改,请高手指点<BR>#include"graphics.h"<BR>#include"math.h"<BR>#define PAI 3.14159265<BR>void fdw(float m[][3])<BR>{unsigned count;<BR>for(count=0;count&lt;3;count++)<BR>{m[count][0]=0;m[count][1]=0;<BR>m[count][2]=0;m[count][count]=1;<BR>}}<BR>void mm2(float ma[][3],float mb[][3],float mc[][3])<BR>{int i,j,k;<BR>for(i=0;i&lt;3;i++)<BR>for(j=0;j&lt;3;j++)<BR>{mc[i][j]=0;<BR>for(k=0;k&lt;3;k++);<BR>mc[i][j]=mc[i][j]+ma[i][k]*mb[k][j];}}</P>
<P>pallel(float x,float y,float p[3][3])<BR>{fdw(p);p[2][0]=x;p[2][1]=y;}</P>
<P>rotate(float c1,float p[3][3])<BR>{c1=PAI/180*c1;fdw(p);<BR>p[0][0]=cos(c1);p[0][1]=sin(c1);<BR>p[1][0]=-sin(c1);p[1][1]=cos(c1);}</P>
<P>affinex(float x,float y,float t[3][3])<BR>{float xx; xx=x*t[0][0]*y*t[1][0]*t[2][0];<BR>return xx;}</P>
<P>scx(float xi)<BR>{float x;<BR>x=xi+getmaxx()/2;<BR>return(x);<BR>}</P>
<P>scy(float yi)<BR>{float y;<BR>y=yi+getmaxy()/2;<BR>return(y);}</P>
<P>main()<BR>{int i,c=13;<BR>float x=120,y=-50;<BR>float x1[]={120,200,250,120};<BR>float y1[]={70,70,30,70};<BR>float x2[4];<BR>float y2[4];<BR>float t1[3][3],t2[3][3],t3[3][3],m1[3][3],m2[3][3];<BR>float c1=30;<BR>int gd=DETECT,gm;<BR>initgraph(&amp;gd,&amp;gm,"");<BR>getch();<BR>setcolor(RED);<BR>line(getmaxx()/2,0,getmaxx()/2,getmaxy());<BR>getch();<BR>line(0,getmaxy()/2,getmaxx()/2,getmaxx());<BR>getch();<BR>putpixel(scx(x),scy(y),GREEN);<BR>setcolor(YELLOW);<BR>circle(scx(x),scy(y),3);<BR>getch();<BR>while (c1&lt;=360)<BR>{setcolor(c);<BR>pallel(-x,-y,t1);<BR>rotate(c1,t2);<BR>pallel(x,y,t3);<BR>mm2(t1,t2,m1);<BR>mm2(m1,t3,m2);<BR>for(i=0;i&lt;=3;i++)<BR>{x2[i]=affinex(x1[i],y1[i],m2);<BR>y2[i]=affinex(x1[i],y1[i],m2);<BR>}<BR>}}</P>

leadfuture 发表于 2007-1-25 20:28

程序写的规范一点看起来比较方便.还有把注释写上

leadfuture 发表于 2007-1-25 21:16

<P>我写的一个实现这个功能的东西:<BR>#include "Conio.h"<BR>#define closegr closegraph<BR>#include"graphics.h"<BR>#include"math.h"<BR>#define PI 3.14159265<BR>void initgr(void);<BR>void rotate(int x,int y,float r,float width,float angle);<BR>void initgr(void) /* BGI初始化 */<BR>{<BR>  int gd =VGA, gm = 0; /* 和gd = VGA,gm = VGAHI是同样效果 */<BR>  registerbgidriver(EGAVGA_driver);/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */<BR>  initgraph(&amp;gd, &amp;gm, "");<BR>}<BR>void rotate(int x,int y,float r,float width,float angle)<BR>{<BR> float co,si;<BR> float midl=width/(sqrt(3));<BR> float x1,y1,x2,y2,x3,y3;<BR> co=cos(angle);<BR> si=sin(angle);<BR> x1=x+(r-midl)*co;<BR> y1=y-(r-midl)*si;<BR> x2=x1+width*cos(angle-30*PI/180);<BR> y2=y1-width*sin(angle-30*PI/180);<BR> x3=x1+width*cos(angle+30*PI/180);<BR> y3=y1-width*sin(angle+30*PI/180);<BR> line(x1,y1,x2,y2);<BR> line(x2,y2,x3,y3);<BR> line(x3,y3,x1,y1);<BR> line(x,y,x+r*co,y-r*si);</P>
<P>}<BR>void main()<BR>{<BR> int page=0;<BR> float angle=0;<BR> initgr();<BR> setcolor(4);<BR> while(!kbhit())<BR> {<BR>  if(angle&lt;=2*PI)<BR>  angle+=0.001;<BR>  else<BR>  angle=0;<BR>  page=1-page;<BR>  setactivepage(page);<BR>  cleardevice();<BR>  rotate(getmaxx()/2,getmaxy()/2,100,20,angle);<BR>  setvisualpage(page);<BR>  delay(200);<BR> }</P>
<P>}</P>

页: [1]

编程论坛