Views
2 years ago

DNDJ 3-6 June.indd - sys-con.com's archive of magazines - SYS ...

DNDJ 3-6 June.indd - sys-con.com's archive of magazines - SYS ...

}}//constructor override the base constructor//to seed the generatorpublic Normal(float mean, float variance) : base(unchecked((int)DateTime.Now.Ticks)){//// TODO: Add constructor logic here//tmean = mean;tvariance = variance;}//add a function to create normal distributionpublic float NextNormal(){float fac, rsq, v1, v2;do{v1 = (float)(2 * this.NextDouble()-1);v2 = (float)(2 * this.NextDouble()-1);rsq = v1 * v1 + v2 * v2;} while(rsq >= 1 || rsq == 0.0f);fac = (float)Math.Sqrt(-2.0 * Math.Log(rsq)/rsq);return tmean + v1 * fac * tvariance;}}}Listing 2using System;using System.Drawing.Drawing2D;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace fractalprog{public class Landscape //class for generating fractallandscapes{//defines how big the resulting bitmap will be//suitable values are of the form 2^n, this isprivate const int numboxes = 512;//alpha value with corresponding propertyprivate float alpha;public float alphaValue{get{ return alpha;}set{ if (value > 0 && value < 1) alpha = value; }}//2D sqaure array for holding landscape heightsprivate float [,] pts;//Vector to specify lighting positionprivate float [] L;//holds the maxiumum height in a particular landscapeprivate float maxh;//random number generator from Normal distributionprivate Normal n;//constructorpublic Landscape(){//create the landscape arraypts = new float[numboxes + 1,numboxes + 1];//create a random number generatorn = new Normal(0,1);//create lighting vector and assign reasonable valuesL = new float[4];L[1] = 0f;L[2] = 0.5f;L[3] = 1;//set initial alpha value as the inverse of the goldenratioalpha = 1/1.614f;}public Bitmap Generate(int level){//create initial squaremaxh = 0;int maxlevel = (int)(Math.Log(numboxes)/Math.Log(2));//varaince must be set proportional to the sidelengthn.variance = (float)Math.Pow(2 * numboxes * numboxes,alpha);//if level == 1 then we are starting from scratch//otherwise we are doing a zoom and only want to enhancethe detailsif(level == 1){pts[0,0] = n.NextNormal();pts[numboxes,0] = n.NextNormal();pts[numboxes,numboxes] = n.NextNormal();pts[0,numboxes] = n.NextNormal();}//construction of landscapewhile(level < maxlevel + 1){GenerateMidPoints(level);GenerateSidePoints(level);level++;}//render the bitmapint i, j;float val, factor, tmp1, tmp2, tmp3;//help with the shadingtmp3 = (float) Math.Sqrt(L[1]*L[1]+L[2]*L[2]+L[3]*L[3]);Color col;Bitmap b = new Bitmap(numboxes, numboxes);for(i = 0; i < numboxes; i++)for(j = 0; j < numboxes; j++){//value is average of 4 surounding point heightsval = 1/4f * (pts[i,j] + pts[i+1,j] + pts[i,j+1] +pts[i+1,j+1]);if(val < 0)//waterb.SetPixel(i,j,Color.DodgerBlue);else{//shade the pixel according to its slope//first pick a color based on heightif(val >2* maxh/3)col = Color.Yellow;else if (val < 2 * maxh /3 && val > maxh /3)col = Color.GreenYellow;elsecol = Color.Green;//shading factortmp1 = pts[i,j+1] + pts[i,j] - pts[i+1,j] -dotnetdevelopersjournal.com • June 200513

DNDJ 6-3 web.indd - sys-con.com's archive of magazines - SYS ...
DNDJ 6-3WEB.indd - sys-con.com's archive of magazines - SYS ...
DNDJ 3-11 Nov.indd - sys-con.com's archive of magazines - SYS ...
DNDJ 4-9.indd - sys-con.com's archive of magazines - SYS-CON ...
JDJ 10-10.indd - sys-con.com's archive of magazines - SYS-CON ...
DNDJ 1-12 Dec.(NEWCONF - sys-con.com's archive of magazines ...
DNDJ 6-4 web.indd - sys-con.com's archive of magazines - SYS ...
DNDJ 3-7 July.indd - sys-con.com's archive of magazines - SYS ...
Figure 1 - sys-con.com's archive of magazines - SYS-CON Media
JDJ 10-3.indd - sys-con.com's archive of magazines - SYS-CON Media
DNDJ 2-2.qxd - sys-con.com's archive of magazines - SYS-CON ...
Download - sys-con.com's archive of magazines - SYS-CON Media
Download - sys-con.com's archive of magazines - SYS-CON Media
jdjedge conference & expo - sys-con.com's archive of magazines ...
JDJ 10-6.indd - sys-con.com's archive of magazines - SYS-CON Media
issue 6 - sys-con.com's archive of magazines - SYS-CON Media
SCOTT McNEALY - sys-con.com's archive of magazines - SYS-CON ...
Conclusion - sys-con.com's archive of magazines
DNDJ February 3-2.indd - sys-con.com's archive of magazines ...
WEB SERVICES EDGE 2002 - sys-con.com's archive of magazines ...
Web Designer'sJournal Developer's - sys-con.com's archive of ...
issue 11 - sys-con.com's archive of magazines - SYS-CON Media
WSJ 4-2 February... - sys-con.com's archive of magazines - SYS ...
DNDJ 4-8.indd - sys-con.com's archive of magazines - SYS-CON ...
JDJ 11-1 Jan.indd - sys-con.com's archive of magazines - SYS-CON ...
LinuxWorld.com - sys-con.com's archive of magazines - SYS-CON ...
JAVA Vol 3 Issue4 - sys-con.com's archive of magazines
JDJ 9-11.indd - sys-con.com's archive of magazines - SYS-CON Media
Java - sys-con.com's archive of magazines
Wireless - sys-con.com's archive of magazines - SYS-CON Media