计算机图形学作业
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
y(down)=d(j,2)*Bbase(temp-4,3,u,uu(kk))+d(j+1,2)*Bbase(temp-3,3,u,uu(kk))+d(j+2,2)*Bbase(temp-2,3,u,uu(kk))+d(j+3,2)*Bbase(temp-1,3,u,uu(kk));
end
end
plot(x,y,'g');
for(i=0;i<3;i++) for (j=0;j=3;j++) m[i] [j] = (I == j);
}
Void matrix3x3PreMultiply (Matrix3x3 a,Matrix3x3b)
{
Intr,c;
Matrix3x3tmp;
for(r=0;r<3;r++)
for(c=0;c<3;c++)
kn=find(u>knot,1,'first');
newu=u;
newu(kn)=knot;
newu(kn+1:length(u)+1)=u(kn:length(u));
inknot=DeBoor(d,u,knot,kn-1,3);
plot(inknot(1),inknot(2),'m.');
dd=d;
P’ =R(a).P
; ; ;
程序实现三角形逆时针90度旋转。
流程图
绕
intRorArbitry2P(floatx1,floaty1,floatz1,floatx2,floaty2,floatz2,floatalpha,floatR[4])
{floatsa,ca,D,D1,D2,D3;
floata1,b1,c1,a2,b2,c2,a3,b3,c3,d1,d2,d3;
R[2][1]=-sa;R[2][2]=ca;
unit_matrix4X4(Rt);
Rt[0][0]=a1;Rt[0][1]=b1;Rt[0][2]=c1;
Rt[1][0]=a2;Rt[1][1]=b2;Rt[1][2]=c2;
Rt[2][0]=a3;Rt[2][1]=b3;Rt[2][2]=c3;
pFillArea(3,pts);
matrix3x3SetIdentity (theMatrix);
scale2 (0.5,0.5,refPt);
rotate2 (90.0,refPt);
pFillArea(3,pts);
sleep(10);
closeGraphics(windowID);
}
利用二维变换的齐次坐标系思想实现旋转变换
dd(n+3,:)=0;
dd(kn:n+3,:)=dd(kn-1:n+2,:);
fori=kn-k:(kn-1)
dd(i,:)=DeBoor(d,u,knot,i,1);
end
plot(dd(:,1),dd(:,2),'b--');
x=0;y=0;down=0;
for j=1:n
uu=(newu(j+3)):0.005:newu(j+4);
fori=3:n
for j=0:2
A(i,i+j-1)=Bbase(i+j-1,3,u,u(2+i));
end
end
e=0;
fori=1:numy
e(n+2,i)=0;
end
fori=1:n
e(i+1,:)=X(i,:);
end
d=inv(A)*e;
plot(X(:,1),X(:,2),'r.');
else
alpha=(t-u(i))/(u(i+k)-u(i));
end
if u(i+k+1)-u(i+1)==0
beta=0;
else
beta=(u(i+k+1)-t)/(u(i+k+1)-u(i+1));
end
tmp[r] [c] =a[r] [0]*b[0] [c]+a[r] [1]*b[1] [c]+a[r] [2]*b[2] [c];
for(r=0;r<3;r++)
for(c=0;c<3;c++)
b[r] [c] =tmp[r] [c];
}
voidscale2 (floatax,rloatsy,wcPt2refpt)
[n,numy]=size(X);k=3;
u(k+1)=0;
fori=1:(n-1)
u(k+i+1)=u(k+i)+abs(X(i+1,1)-X(i,1));
end
temp=u(n+k);
fori=(k):(n+k) %(k+1):(n-1+k)
u(i)=u(i)/temp;
end
fori=1:(k+1)
y(down)=dd(j,2)*Bbase(temp-4,3,newu,uu(kk))+dd(j+1,2)*Bbase(temp-3,3,newu,uu(kk))+dd(j+2,2)*Bbase(temp-2,3,newu,uu(kk))+dd(j+3,2)*Bbase(temp-1,3,newu,uu(kk));
{
wcPt2pts[3] = { 50.0,50.0,150.0,50.0,100.0,150.0 };
wcPt2refPt= {100.0, 100.0};
longwindowID=openGraphics(*argv, 200, 350);
setBackground(WHITE);
setColor(BLUE);
forkk=1:length(uu)
temp=j+4;
down=down+1;
x(down)=dd(j,1)*Bbase(temp-4,3,newu,uu(kk))+dd(j+1,1)*Bbase(temp-3,3,newu,uu(kk))+dd(j+2,1)*Bbase(temp-2,3,newu,uu(kk))+dd(j+3,1)*Bbase(temp-1,3,newu,uu(kk));
floatdx,dy,dz;
floatLt[4][4],Rm[4][4];
if(dpp(x1,y1,x2,y2)<Eps) {
rotz(x1,y1,z1,alpha,R);
return0;;}
else{
dx=x2-x1;dy=y2-y1;dz=z2-z1;Fra Baidu bibliotek
D=sqrt(dx*dx+dy*dy+dz*dz);
Lt[2][0]=c1; Lt[2][1]=c2; Lt[2][2]=c3;
Lt[3][0]=d1; Lt[3][1]=d2; Lt[3][2]=c3;
sa=alpha*DEGREE;
ca=cos(sa);sa=sin(sa);
unit_matrix4X4(R);
R[1][1]=ca; R[1][2]=sa;
u(i)=0;
u(n-1+k+i)=1;
end
A=zeros(n+2);%A(1,1)=1;A(1,2)=-2;A(1,3)=1;A(n+2,n)=1;A(n+2,n+1)=-2;A(n+2,n+2)=1;
A(1,1)=1;A(1,2)=-1;A(n+2,n+1)=-1;A(n+2,n+2)=1;
A(2,2)=1;A(n+1,n+1)=1;
}
voidrotate2 (float a, wcPt2refpt)
{
Matrix3x3 m;
matrix3x3SetIdentity (m);
a =pToRadians(a);
m[0] [0] =cosf(a);
m[0] [1] = -sinf(a);
m[0] [2] =refPt.x*(1-cosf(a))+refPt.y*sinf(a);
{
Matrix3x3 m;
Matrix3x3SetIdentity(m);
m[0] [0] =sx;
m[0] [2] = (1 –sx) *refpt.x;
m[1] [1] =sy;
m[1] [2] = (1 –sy) *refpt.y;
matrix3x3PreMultiply (m,theMatrix);
D1=-(a1*d1+a2*d2+a3*d3);
D2=-(b1*d1+b2*d2+b3*d3);
D3=-(c1*d1+c2*d2+c3*d3);
unit_matrix4X4(Lt);
Lt[0][0]=a1; Lt[0][1]=a2; Lt[0][2]=a3;
Lt[1][0]=b1; Lt[1][1]=b2; Lt[1][2]=b3;
end
end
plot(x,y,'r-.');
functionresult =Bbase(i,k,u,t)
ifk==0
if (u(i)<=t && t<u(i+1));
result=1;
return;
else
result=0;
return;
end
end
if u(i+k)-u(i)==0
alpha=0;
Rt[3][0]=D1;Rt[3][1]=D2;Rt[3][2]=D3;
mult_4X4(Lt,R,Rm);
mult_4X4(Rm,Rt,R);
return(1);
}}
绕Z轴旋转变化矩阵 =
该算法实现了由空间2点定义的任意轴 角的变化矩阵R。当所给两点呈 时,算法返回绕平行于Z轴的轴旋转变换矩阵
X=load('data.txt');
a1=dx/D; b1=dy/D; c1=dz/D;
D=sqrt(a1*a1+b1*b1);
a2=-b1/D;b2=a1/D; c2=0;
a2=-b2*c1;b3=a2*c1;c3=D;
d1=-(a1*x1+b1*y1+c1*z1);
d2=-(a2*x1+b2*y1+c2*z1);
d3=-(a3*x1+b3*y1+c3*z1);
fprintf('请输入一个%d-%d之间的',min(X(:,1)),max(X(:,1)) );
Knot=input('一个补充节点:');
if Knot<min(X(:,1)) || Knot>max(X(:,1))
fprintf('你的输入有误,超过了范围.\n');
return;
end
knot=(Knot-min(X(:,1)))/(max(X(:,1))-min(X(:,1)));
m[1] [0] =sinf(a);
m[1] [1] =cosf(a);
m[1] [2] =refPt.y*(1-cosf(a))+refPt.x*sinf(a);
matrix3x3PreMultiply (m,theMatrix);
}
Void main (intargc, char **argv)
plot(d(:,1),d(:,2),'b');
x=0;y=0;down=0;
for j=1:(n-1)
uu=(u(j+3)):0.005:u(j+4);
forkk=1:length(uu)
temp=j+4;
down=down+1;
x(down)=d(j,1)*Bbase(temp-4,3,u,uu(kk))+d(j+1,1)*Bbase(temp-3,3,u,uu(kk))+d(j+2,1)*Bbase(temp-2,3,u,uu(kk))+d(j+3,1)*Bbase(temp-1,3,u,uu(kk));
Transformation
二维旋转变换
程序
#include<math.h>
#include “graghics.h”
typedeffloat Matrix3x3[3] [3];
Matrix3x3theMatrix;
Voidmatrix3x3SetIdentity(Matrix3x3 m)
{
intI,j;
end
end
plot(x,y,'g');
for(i=0;i<3;i++) for (j=0;j=3;j++) m[i] [j] = (I == j);
}
Void matrix3x3PreMultiply (Matrix3x3 a,Matrix3x3b)
{
Intr,c;
Matrix3x3tmp;
for(r=0;r<3;r++)
for(c=0;c<3;c++)
kn=find(u>knot,1,'first');
newu=u;
newu(kn)=knot;
newu(kn+1:length(u)+1)=u(kn:length(u));
inknot=DeBoor(d,u,knot,kn-1,3);
plot(inknot(1),inknot(2),'m.');
dd=d;
P’ =R(a).P
; ; ;
程序实现三角形逆时针90度旋转。
流程图
绕
intRorArbitry2P(floatx1,floaty1,floatz1,floatx2,floaty2,floatz2,floatalpha,floatR[4])
{floatsa,ca,D,D1,D2,D3;
floata1,b1,c1,a2,b2,c2,a3,b3,c3,d1,d2,d3;
R[2][1]=-sa;R[2][2]=ca;
unit_matrix4X4(Rt);
Rt[0][0]=a1;Rt[0][1]=b1;Rt[0][2]=c1;
Rt[1][0]=a2;Rt[1][1]=b2;Rt[1][2]=c2;
Rt[2][0]=a3;Rt[2][1]=b3;Rt[2][2]=c3;
pFillArea(3,pts);
matrix3x3SetIdentity (theMatrix);
scale2 (0.5,0.5,refPt);
rotate2 (90.0,refPt);
pFillArea(3,pts);
sleep(10);
closeGraphics(windowID);
}
利用二维变换的齐次坐标系思想实现旋转变换
dd(n+3,:)=0;
dd(kn:n+3,:)=dd(kn-1:n+2,:);
fori=kn-k:(kn-1)
dd(i,:)=DeBoor(d,u,knot,i,1);
end
plot(dd(:,1),dd(:,2),'b--');
x=0;y=0;down=0;
for j=1:n
uu=(newu(j+3)):0.005:newu(j+4);
fori=3:n
for j=0:2
A(i,i+j-1)=Bbase(i+j-1,3,u,u(2+i));
end
end
e=0;
fori=1:numy
e(n+2,i)=0;
end
fori=1:n
e(i+1,:)=X(i,:);
end
d=inv(A)*e;
plot(X(:,1),X(:,2),'r.');
else
alpha=(t-u(i))/(u(i+k)-u(i));
end
if u(i+k+1)-u(i+1)==0
beta=0;
else
beta=(u(i+k+1)-t)/(u(i+k+1)-u(i+1));
end
tmp[r] [c] =a[r] [0]*b[0] [c]+a[r] [1]*b[1] [c]+a[r] [2]*b[2] [c];
for(r=0;r<3;r++)
for(c=0;c<3;c++)
b[r] [c] =tmp[r] [c];
}
voidscale2 (floatax,rloatsy,wcPt2refpt)
[n,numy]=size(X);k=3;
u(k+1)=0;
fori=1:(n-1)
u(k+i+1)=u(k+i)+abs(X(i+1,1)-X(i,1));
end
temp=u(n+k);
fori=(k):(n+k) %(k+1):(n-1+k)
u(i)=u(i)/temp;
end
fori=1:(k+1)
y(down)=dd(j,2)*Bbase(temp-4,3,newu,uu(kk))+dd(j+1,2)*Bbase(temp-3,3,newu,uu(kk))+dd(j+2,2)*Bbase(temp-2,3,newu,uu(kk))+dd(j+3,2)*Bbase(temp-1,3,newu,uu(kk));
{
wcPt2pts[3] = { 50.0,50.0,150.0,50.0,100.0,150.0 };
wcPt2refPt= {100.0, 100.0};
longwindowID=openGraphics(*argv, 200, 350);
setBackground(WHITE);
setColor(BLUE);
forkk=1:length(uu)
temp=j+4;
down=down+1;
x(down)=dd(j,1)*Bbase(temp-4,3,newu,uu(kk))+dd(j+1,1)*Bbase(temp-3,3,newu,uu(kk))+dd(j+2,1)*Bbase(temp-2,3,newu,uu(kk))+dd(j+3,1)*Bbase(temp-1,3,newu,uu(kk));
floatdx,dy,dz;
floatLt[4][4],Rm[4][4];
if(dpp(x1,y1,x2,y2)<Eps) {
rotz(x1,y1,z1,alpha,R);
return0;;}
else{
dx=x2-x1;dy=y2-y1;dz=z2-z1;Fra Baidu bibliotek
D=sqrt(dx*dx+dy*dy+dz*dz);
Lt[2][0]=c1; Lt[2][1]=c2; Lt[2][2]=c3;
Lt[3][0]=d1; Lt[3][1]=d2; Lt[3][2]=c3;
sa=alpha*DEGREE;
ca=cos(sa);sa=sin(sa);
unit_matrix4X4(R);
R[1][1]=ca; R[1][2]=sa;
u(i)=0;
u(n-1+k+i)=1;
end
A=zeros(n+2);%A(1,1)=1;A(1,2)=-2;A(1,3)=1;A(n+2,n)=1;A(n+2,n+1)=-2;A(n+2,n+2)=1;
A(1,1)=1;A(1,2)=-1;A(n+2,n+1)=-1;A(n+2,n+2)=1;
A(2,2)=1;A(n+1,n+1)=1;
}
voidrotate2 (float a, wcPt2refpt)
{
Matrix3x3 m;
matrix3x3SetIdentity (m);
a =pToRadians(a);
m[0] [0] =cosf(a);
m[0] [1] = -sinf(a);
m[0] [2] =refPt.x*(1-cosf(a))+refPt.y*sinf(a);
{
Matrix3x3 m;
Matrix3x3SetIdentity(m);
m[0] [0] =sx;
m[0] [2] = (1 –sx) *refpt.x;
m[1] [1] =sy;
m[1] [2] = (1 –sy) *refpt.y;
matrix3x3PreMultiply (m,theMatrix);
D1=-(a1*d1+a2*d2+a3*d3);
D2=-(b1*d1+b2*d2+b3*d3);
D3=-(c1*d1+c2*d2+c3*d3);
unit_matrix4X4(Lt);
Lt[0][0]=a1; Lt[0][1]=a2; Lt[0][2]=a3;
Lt[1][0]=b1; Lt[1][1]=b2; Lt[1][2]=b3;
end
end
plot(x,y,'r-.');
functionresult =Bbase(i,k,u,t)
ifk==0
if (u(i)<=t && t<u(i+1));
result=1;
return;
else
result=0;
return;
end
end
if u(i+k)-u(i)==0
alpha=0;
Rt[3][0]=D1;Rt[3][1]=D2;Rt[3][2]=D3;
mult_4X4(Lt,R,Rm);
mult_4X4(Rm,Rt,R);
return(1);
}}
绕Z轴旋转变化矩阵 =
该算法实现了由空间2点定义的任意轴 角的变化矩阵R。当所给两点呈 时,算法返回绕平行于Z轴的轴旋转变换矩阵
X=load('data.txt');
a1=dx/D; b1=dy/D; c1=dz/D;
D=sqrt(a1*a1+b1*b1);
a2=-b1/D;b2=a1/D; c2=0;
a2=-b2*c1;b3=a2*c1;c3=D;
d1=-(a1*x1+b1*y1+c1*z1);
d2=-(a2*x1+b2*y1+c2*z1);
d3=-(a3*x1+b3*y1+c3*z1);
fprintf('请输入一个%d-%d之间的',min(X(:,1)),max(X(:,1)) );
Knot=input('一个补充节点:');
if Knot<min(X(:,1)) || Knot>max(X(:,1))
fprintf('你的输入有误,超过了范围.\n');
return;
end
knot=(Knot-min(X(:,1)))/(max(X(:,1))-min(X(:,1)));
m[1] [0] =sinf(a);
m[1] [1] =cosf(a);
m[1] [2] =refPt.y*(1-cosf(a))+refPt.x*sinf(a);
matrix3x3PreMultiply (m,theMatrix);
}
Void main (intargc, char **argv)
plot(d(:,1),d(:,2),'b');
x=0;y=0;down=0;
for j=1:(n-1)
uu=(u(j+3)):0.005:u(j+4);
forkk=1:length(uu)
temp=j+4;
down=down+1;
x(down)=d(j,1)*Bbase(temp-4,3,u,uu(kk))+d(j+1,1)*Bbase(temp-3,3,u,uu(kk))+d(j+2,1)*Bbase(temp-2,3,u,uu(kk))+d(j+3,1)*Bbase(temp-1,3,u,uu(kk));
Transformation
二维旋转变换
程序
#include<math.h>
#include “graghics.h”
typedeffloat Matrix3x3[3] [3];
Matrix3x3theMatrix;
Voidmatrix3x3SetIdentity(Matrix3x3 m)
{
intI,j;