求一个彩色3D分形山的MATLAB或Java或vb源程序

求一个彩色3D分形山的MATLAB或Java或vb源程序,第1张

我这里有。要加分哦。兄弟。下面就是JAVA的源码:

// 3D painted fractal mountains, Evgeny Demidov 12 Oct 2001

import javaawt;

import javaawtevent;

import javautilStringTokenizer;

public class MountainC extends javaappletApplet

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 buffImage; Graphics 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 = IntegerparseInt(s);

xPol = new int[3]; yPol = new int[3];

buffImage = createImage(w, h); buffGraphics = buffImagegetGraphics();

col = new Color[4][256];

for (int i = 0; i < 256; i++){

col[0][i] = new Color(0, 0, i);

col[1][i] = new Color(0, (i220)/256, 0);

col[2][i] = new Color((i150)/256, (i150)/256, (i50)/256);

col[3][i] = new Color(i, i, i);}

s = getParameter("bgColor"); if (s != null){

StringTokenizer st = new StringTokenizer(s);

int red = IntegerparseInt(stnextToken());

int green = IntegerparseInt(stnextToken());

int blue = IntegerparseInt(stnextToken());

setBackground( new Color(red, green, blue));}

else setBackground(new Color(255,255,255));

addMouseListener(this);

addMouseMotionListener(this);

setup();

}

public double R(){ return rnd(Mathrandom()-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 = 0; i < n1; i++)

for (int j = 0; j < n1; j++){

vert[i][j][0] = dxi - w2; vert[i][j][2] = dxj - w2;

double mi = M[t++];

if (mi < 0) mi = 01Mathrandom();

vert[i][j][1] = wmi - w2/2;}

Norm = new double[n1][n1][2][3]; Norm1z = new double[n1][n1][2];

iCol = new int[n][n][2];

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++){

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+1Mathrandom() > 35) iCol[i][j][0] = 3;

else if (s+1Mathrandom() > 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+1Mathrandom() > 35) iCol[i][j][1] = 3;

else if (s+1Mathrandom() > 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 = Mathsqrt(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]) / 2555;

Norm[i][j][0][0] /= mod; Norm[i][j][0][1] /= mod; Norm[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 = Mathsqrt(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]) / 2555;

Norm[i][j][1][0] /= mod; Norm[i][j][1][1] /= mod; Norm[i][j][1][2] /= mod;}

rotate();

}

public void iterate(){

int nc=n, Max=n1n1, ncn1;

double Min=-1;

M = new double[Max];

for (int i=n+2; i < nn1-1; i++) M[i] = Min;

for (int i=2n1; i < nn1; i += n1) M[i] = M[i-1] = 0;

while ( (nc /= 2) >= 1){

ncn1 = ncn1;

for (int j=ncn1; j < Max; j += ncn1+ncn1){

for (int i= nc; i < n; i += 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 = egetX(); my0 = egetY();

if ( eisControlDown() ){

setup();

repaint();}

if ( eisAltDown() ){

if ( eisShiftDown() ){ n /= 2; if (n < 1) n = 1;}

else n = 2;

setup();

repaint();}

econsume();

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseDragged(MouseEvent e) {

int x1 = egetX(); int y1 = egetY();

if ( eisShiftDown() ) scale = Mathexp(-(y1 - my0)/(double)w);

else fiX += dfi(y1 - my0);

fiY += dfi(x1 - mx0); mx0 = x1; my0 = y1;

rotate();

repaint();

econsume();

}

public void rotate(){

double ct = Mathcos(fiX), cf = Mathcos(fiY),

st = Mathsin(fiX), sf = Mathsin(fiY),

m00 = scalecf, m02 = scalesf,

m10 = scalestsf, m11 = scalect, m12 = -scalestcf;

m20 = -ctsf; m21 = st; m22 = ctcf;

for (int i = 0; i < n1; i++)

for (int j = 0; j < n1; j++){

vert1[i][j][0] = m00vert[i][j][0] + m02vert[i][j][2];

vert1[i][j][1] = m10vert[i][j][0] + m11vert[i][j][1] + m12vert[i][j][2];}

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

for (int k = 0; k < 2; k++)

Norm1z[i][j][k] = m20Norm[i][j][k][0] + m21Norm[i][j][k][1] +

m22Norm[i][j][k][2];

painted = false;

}

public void mouseMoved(MouseEvent e) {}

public void paint(Graphics g) {

if ( !painted ){

buffGraphicsclearRect(0, 0, w, h);

int ib=0, ie=n, sti=1, jb=0, je=n, stj=1;

if (m20 < 0){ ib = n; ie = -1; sti = -1;}

if (m22 < 0){ jb = n; je = -1; stj = -1;}

for (int i = ib; i != ie; i += sti)

for (int j = jb; j != je; j += 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];

buffGraphicssetColor(col[iCol[i][j][0]][(int)(Norm1z[i][j][0])]);

buffGraphicsfillPolygon(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];

buffGraphicssetColor(col[iCol[i][j][1]][(int)(Norm1z[i][j][1])]);

buffGraphicsfillPolygon(xPol,yPol, 3);} }

painted = true;}

gdrawImage(buffImage, 0, 0, this);

showStatus( "n=" + n);

}

public void update(Graphics g){ paint(g); }

}

出错的原因应该是 你的函数名字起的太简单了,可能与别的gm函数冲突,引起调用错误,不过也可能是matlab抽风了,你重启试试。

我的解决办法是:把函数名字改为ggm 然后放到当前工作目录中,

函数内容的代码是:

function y=gg(x)

if x>2  

    y=x^2+2;

elseif 0<x<=2       

    y=3x;

elseif x<=0          

    y=x^3;

end

然后在命令行中输入

gg(6)

gg(2)

你看,运行正常!

结果如下:

你试试,如果有问题,欢迎继续讨论

以上就是关于求一个彩色3D分形山的MATLAB或Java或vb源程序全部的内容,包括:求一个彩色3D分形山的MATLAB或Java或vb源程序、用matlab求分段函数程序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9983172.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存