① 求一个用python实现的基于deboor-cox的B样条算法
#!/usr/sbin/env python
# -*- coding:utf-8 -*-
import math
# ItemCF算法
def ItemSimilarity(train):
C = dict()
N = dict()
for u,items in train.items():
for i in items.keys():
N[i] += 1
for j in items.keys():
if i == j:
continue
C[i][j] += 1
W = dict()
for i,related_items in C.items():
for j,cij in related_items.items():
W[i][j] = cij / math.sqrt( N[i] * N[j])
return W
# ItemCF-IUF算法
def ItemSimilarity_v2(train):
C = dict()
N = dict()
for u,items in train.items():
for i in items.keys():
N[i] += 1
for j in items.keys():
if i == j:
continue
C[i][j] += 1 / math.log(1+len(items)*1.0)
W = dict()
for i,related_items in C.items():
for j,cij in related_items.items():
W[i][j] = cij / math.sqrt( N[i] * N[j])
return W
def Recommend(train,user_id,W,K):
rank = dict()
ru = train[user_id]
for i,pi in ru.items():
for j,wj in sorted(W[i].items,key=itemgetter(1),reverse=True)[0:K]:
if j in ru:
continue
rank[j] += pi*wj
return rank
② 计算机图形学问题
1、直线的生成和2种算法:DDA算法:代码如下
void
dda(Graphics
g,int
x1,int
x2,int
y1,int
y2)
{int
k;
float
x,y,dx,dy;
k=Math.abs(x2-x1);
if(Math.abs(y2-y1)>k)
k=Math.abs(y2-y1);
dx=(float)(x2-x1)/k;
dy=(float)(y2-y1)/k;
x=(float)x1;
y=(float)y1;
for(int
i=0;i<k;i++)
drawLine((int)x+.5f,((int)y+.5f,((int)x+.5f.((int)y+.5f);
x=x+dx;
y=y+dy;
}
}
Breseham算法;
2、填充的定义和2种算法分别是
扫描线种子填充算法和递归算法
3、图像学的定义与运用
定义是研究如何在计算机环境下生成,处理和现实图形的一门学科。具体应用很广泛:像有些公司在制造汽车,飞机等时,会先画出制图,一般都会在计算机上绘制出来;等等吧
③ 计算机图形学程序
void CDeBoorView::OnDraw(CDC* pDC)
{
int deg=8;float coeffx[9]={200,50,100,150,200,250,300,350,200};
float coeffy[9]={200,100,50,50,100,50,50,100,200};
float knot[18]={0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2};
CDeBoorDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int L,n1=9,n=100,i,j;
L=n1-deg;
float u;
float ps[100][2];
for(i=0;i<n1;i++){
if(i==0)pDC->MoveTo(coeffx[i],coeffy[i]);
pDC->亏戚LineTo(coeffx[i],coeffy[i]);
}
for(i=deg;i<衫空谨或基L+deg+1;i++){
if(knot[i+1]>knot[i])
for(j=0;j<n;j++){
u=knot[i]+j*(knot[i+1]-knot[i])/n;
ps[j][0]=deboor(deg,coeffx,knot,u,i,pDC);
ps[j][1]=deboor(deg,coeffy,knot,u,i,pDC);
CPen newpen(PS_SOLID,3,RGB(255,0,0));
CPen *old1=pDC->SelectObject(&newpen);
if(j==0)
pDC->MoveTo(ps[j][0],ps[j][1]);
pDC->LineTo(ps[j][0],ps[j][1]);
}
}
}
float CDeBoorView::deboor(int deg,float coeff[],float knot[],float u,int i,CDC*pDC){
int k,j;
float t1,t2;
float coeffa[30];
for(j=i-deg;j<=i;j++)
coeffa[j]=coeff[j];
for(k=1;k<=deg;k++)
for(j=i;j>=i-deg+k;j--)
{
t1=(knot[j]-u)/(knot[j]-knot[j+deg-k+1]);
t2=1.0-t1;
coeffa[j]=t2*coeffa[j-1]+t1*coeffa[j];
}
return(coeffa[i]);
}