① 求一個用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]);
}