// 3D painted fractal mountains, Evgeny Demidov 12 Oct 2001
import java.awt.*
import java.awt.event.*
import java.util.StringTokenizer
public class MountainC extends java.applet.Applet
implements MouseListener, MouseMotionListener {
int n = 16, n1, h,w,h2,w2, mx0,my0, xPol[],yPol[], iCol[][][]
double rnd, fiX = .2, fiY = .3, dfi = .01, scale = .8, m20,m21,m22
double vert[][][], vert1[][][], Norm[][][][], Norm1z[][][], M[]
Image buffImageGraphics buffGraphics
Color col[][]
boolean painted
public void init(){
w = getSize().width h = getSize().height w2 = w/2 h2 = h/2
String s=getParameter("N") if (s != null) n = Integer.parseInt(s)
xPol = new int[3] yPol = new int[3]
buffImage = createImage(w, h) buffGraphics = buffImage.getGraphics()
col = new Color[4][256]
for (int i = 0i <256i++){
col[0][i] = new Color(0, 0, i)
col[1][i] = new Color(0, (i*220)/256, 0)
col[2][i] = new Color((i*150)/256, (i*150)/256, (i*50)/256)
col[3][i] = new Color(i, i, i)}
s = getParameter("bgColor")if (s != null){
StringTokenizer st = new StringTokenizer(s)
int red = Integer.parseInt(st.nextToken())
int green = Integer.parseInt(st.nextToken())
int blue = Integer.parseInt(st.nextToken())
setBackground( new Color(red, green, blue))}
else setBackground(new Color(255,255,255))
addMouseListener(this)
addMouseMotionListener(this)
setup()
}
public double R(){ return rnd*(Math.random()-.5)}
public void setup(){
rnd = 1
n1 = n+1
iterate()
vert = new double[n1][n1][3] vert1 = new double[n1][n1][2]
double dx = w/(double)n
int t = 0
for (int i = 0i <n1i++)
for (int j = 0j <n1j++){
vert[i][j][0] = dx*i - w2 vert[i][j][2] = dx*j - w2
double mi = M[t++]
if (mi <0) mi = .01*Math.random()
vert[i][j][1] = w*mi - w2/2}
Norm = new double[n1][n1][2][3] Norm1z = new double[n1][n1][2]
iCol = new int[n][n][2]
for (int i = 0i <ni++)
for (int j = 0j <nj++){
double s =
((vert[i][j][1] + vert[i+1][j][1] + vert[i+1][j+1][1])/3 + w2/2)/w
if (s <.01) iCol[i][j][0] = 0
else if (s+.1*Math.random() >.35) iCol[i][j][0] = 3
else if (s+.1*Math.random() >.15) iCol[i][j][0] = 2
else iCol[i][j][0] = 1
s = ((vert[i][j][1] + vert[i][j+1][1] + vert[i+1][j+1][1])/3 + w2/2)/w
if (s <.01) iCol[i][j][1] = 0
else if (s+.1*Math.random() >.35) iCol[i][j][1] = 3
else if (s+.1*Math.random() >.15) iCol[i][j][1] = 2
else iCol[i][j][1] = 1
Norm[i][j][0][0] = vert[i][j][1] - vert[i+1][j][1]
Norm[i][j][0][1] = dx
Norm[i][j][0][2] = vert[i+1][j][1] - vert[i+1][j+1][1]
double mod = Math.sqrt(Norm[i][j][0][0]*Norm[i][j][0][0] + Norm[i][j][0][1]*
Norm[i][j][0][1] + Norm[i][j][0][2]*Norm[i][j][0][2]) / 255.5
Norm[i][j][0][0] /= modNorm[i][j][0][1] /= modNorm[i][j][0][2] /= mod
Norm[i][j][1][0] = vert[i][j+1][1] - vert[i+1][j+1][1]
Norm[i][j][1][1] = dx
Norm[i][j][1][2] = vert[i][j][1] - vert[i][j+1][1]
mod = Math.sqrt(Norm[i][j][1][0]*Norm[i][j][1][0] + Norm[i][j][1][1]*
Norm[i][j][1][1] + Norm[i][j][1][2]*Norm[i][j][1][2]) / 255.5
Norm[i][j][1][0] /= modNorm[i][j][1][1] /= modNorm[i][j][1][2] /= mod}
rotate()
}
public void iterate(){
int nc=n, Max=n1*n1, ncn1
double Min=-1
M = new double[Max]
for (int i=n+2i <n*n1-1i++) M[i] = Min
for (int i=2*n1i <n*n1i += n1) M[i] = M[i-1] = 0
while ( (nc /= 2) >= 1){
ncn1 = nc*n1
for (int j=ncn1j <Maxj += ncn1+ncn1){
for (int i= nci <ni += nc+nc){
if (M[i+j]==Min)
M[i+j] = (M[i+j+nc-ncn1] + M[i+j-nc+ncn1])/2.+R()
if (M[i+j+nc]==Min)
M[i+j+nc] = (M[i+j+nc+ncn1] + M[i+j+nc-ncn1])/2.+R()
if (M[i+j+ncn1]==Min)
M[i+j+ncn1] = (M[i+j-nc+ncn1] + M[i+j+nc+ncn1])/2.+R()}}
rnd /= 2.}
}
public void destroy() {
removeMouseListener(this)
removeMouseMotionListener(this)
}
public void mouseClicked(MouseEvent e){} // event handling
public void mousePressed(MouseEvent e) {
mx0 = e.getX() my0 = e.getY()
if ( e.isControlDown() ){
setup()
repaint()}
if ( e.isAltDown() ){
if ( e.isShiftDown() ){ n /= 2 if (n <1) n = 1}
else n *= 2
setup()
repaint()}
e.consume()
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
int x1 = e.getX() int y1 = e.getY()
if ( e.isShiftDown() ) scale *= Math.exp(-(y1 - my0)/(double)w)
else fiX += dfi*(y1 - my0)
fiY += dfi*(x1 - mx0) mx0 = x1 my0 = y1
rotate()
repaint()
e.consume()
}
public void rotate(){
double ct = Math.cos(fiX), cf = Math.cos(fiY),
st = Math.sin(fiX), sf = Math.sin(fiY),
m00 = scale*cf,m02 = scale*sf,
m10 = scale*st*sf, m11 = scale*ct, m12 = -scale*st*cf
m20 = -ct*sfm21 = stm22 = ct*cf
for (int i = 0i <n1i++)
for (int j = 0j <n1j++){
vert1[i][j][0] = m00*vert[i][j][0] + m02*vert[i][j][2]
vert1[i][j][1] = m10*vert[i][j][0] + m11*vert[i][j][1] + m12*vert[i][j][2]}
for (int i = 0i <ni++)
for (int j = 0j <nj++)
for (int k = 0k <2k++)
Norm1z[i][j][k] = m20*Norm[i][j][k][0] + m21*Norm[i][j][k][1] +
m22*Norm[i][j][k][2]
painted = false
}
public void mouseMoved(MouseEvent e) {}
public void paint(Graphics g) {
if ( !painted ){
buffGraphics.clearRect(0, 0, w, h)
int ib=0, ie=n, sti=1, jb=0, je=n, stj=1
if (m20 <0){ ib = nie = -1sti = -1}
if (m22 <0){ jb = nje = -1stj = -1}
for (int i = ibi != iei += sti)
for (int j = jbj != jej += stj){
if (Norm1z[i][j][0] >0){
xPol[0] = w2 + (int)vert1[i][j][0]
xPol[1] = w2 + (int)vert1[i+1][j][0]
xPol[2] = w2 + (int)vert1[i+1][j+1][0]
yPol[0] = h2 - (int)vert1[i][j][1]
yPol[1] = h2 - (int)vert1[i+1][j][1]
yPol[2] = h2 - (int)vert1[i+1][j+1][1]
buffGraphics.setColor(col[iCol[i][j][0]][(int)(Norm1z[i][j][0])])
buffGraphics.fillPolygon(xPol,yPol, 3)}
if (Norm1z[i][j][1] >0){
xPol[0] = w2 + (int)vert1[i][j][0]
xPol[1] = w2 + (int)vert1[i][j+1][0]
xPol[2] = w2 + (int)vert1[i+1][j+1][0]
yPol[0] = h2 - (int)vert1[i][j][1]
yPol[1] = h2 - (int)vert1[i][j+1][1]
yPol[2] = h2 - (int)vert1[i+1][j+1][1]
buffGraphics.setColor(col[iCol[i][j][1]][(int)(Norm1z[i][j][1])])
buffGraphics.fillPolygon(xPol,yPol, 3)} }
painted = true}
g.drawImage(buffImage, 0, 0, this)
showStatus( "n=" + n)
}
public void update(Graphics g){ paint(g)}
}
注意查收!此光盘收录的是《分形算法与程序设计VC版》一书中所讲解的程序设计的源代码及部分供参考的效果图。具体内容和使用方法如下:
文件夹<第2章VC>包括:
<2_01>:内含Cantor三分集源代码。双击Debug下的canto.exe文件,程序运行;双击canto.dsw文件,进入编辑环境。
<2_02>:内含Koch曲线源代码。双击Debug下的Koch.exe文件,程序运行;双击Koch.dsw文件,进入编辑环境。
<2_03>:内含Koch雪花源代码。双击Debug下的snow.exe文件,程序运行;双击snow.dsw文件,进入编辑环境。
<2_04>:内含Arboresent肺源代码。双击Debug下的Arboresent.exe文件,程序运行;双击Arboresent.dsw文件,进入编辑环境。
……
请采纳答案,支持我一下。
你这个是tc的文件,我用的是vc运行不了。关于文件 *** 作,你把你的数据复制一下,然后在你的源程序文件夹下新建一个txt的文件把数据保存在里面。比如它的名字是123.txt,你就记住它,然后当你程序运行时,要求输入文件名,你就输入123.txt回车,就行了。不同的数据之间用空格或换行符隔开。
那个flag是状态标致符,就是当flag=1时完成一个动作,当flag=0时,完成另一个动作。这样可以用来实现状态条件的判断,即要满足什么样的条件才执行相应的 *** 作。因为我没用过tc,所以画图部分看不懂。你仔细分析一下嘛
if(flag==0)
{
xscale=418/(XMax-XMin)
yscale=Min(315/(YMax-YMin),xscale/1.38)
if(yscale<xscale/1.38)
xscale =1.38*yscale
xoffset=320-(XMax+XMin)*xscale/2
yoffset=175-(YMax+YMin)*yscale/2
flag=1//将这个 *** 作执行完后,把flag置1,即条件不满足了
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)