|
Author |
Topic: rounding precision (and noise) (Read 276 times) |
|
Sjeiti
|
rounding precision (and noise)
« on: Jul 21st, 2004, 8:43pm » |
|
In stead of using noise to generate a continuous field I wanted to use it to calculate points: for example the position of trees. So I thought I'd sample the noise field in a grid. Loose all the gridpoints that are below a threshold of say .5. Then replace the remaining gridpoints by multiplying their noisevalue by a large number and use this as an angle: rotating the gridpoint around it's original position (with the size of the grid as a radius). Maybe this is clearer: Code:int iScl, iX3d, iY3d, iAng; float fSc1, fXtr, fYtr, fMrg, fNsc; int[][] lLsp; void setup() { fXtr = fYtr = 2000; // starting position in noise iScl = 4; // the scale of the grid fMrg = 0.55; // threshold fNsc = .1; // scale of the noise iAng = 60; // the large number size(400,400); background(255); stroke(0); strokeWeight(iScl/2); } void loop() { background(255); if (!keyPressed) { translate(width/2, height/2, 0); rotateY(-(mouseX-width/2)*.005); rotateX((mouseY-height/2)*.005); iX3d = -width/2; iY3d = -height/2; } else { iX3d = 0; iY3d = 0; } fXtr += (mouseX-width/2)*.007; fYtr += (mouseY-height/2)*.007; for (int i=0; i<width/iScl; i++) { for (int j=0; j<height/iScl; j++) { float fNoi = noise(i*fNsc + int(fXtr)*fNsc, j*fNsc + int(fYtr)*fNsc); if (fNoi>fMrg) { float fRad = fNoi*iAng; float fXps = i*iScl + iScl/2 + sin(fRad)*iScl - (fXtr-int(fXtr))*iScl + iX3d; float fYps = j*iScl + iScl/2 + cos(fRad)*iScl - (fYtr-int(fYtr))*iScl + iY3d; int iPps = j*(width/iScl)+i; point(fXps,fYps); } } } } |
| The problem I get is that some points seem to pop over and under the threshold value, it looks like a rounding precision problem (probably when doing: int(fXtr)*fNsc). I've tried several other ways of rounding but without result. A colleague of mine told me doubles are more precise than floats... maybe that's a solution? Then again, since perlin noise is 'pseudo' infinite, I got some doubts on wether there is a solution. I hope there is because it would be a bit HHGTTG to have a forest where some trees keep disappearing and reappearing. oh yeah.. almost forgot.. I'm not really sure of what I'm doing is correct because when you turn iAng way up (as in 10000) the position of the points is suddenly variable (I think per grid). ...Sjeiti
|
« Last Edit: Jul 21st, 2004, 8:53pm by Sjeiti » |
|
http://www.sjeiti.com/
|
|
|
narain
|
Re: rounding precision (and noise)
« Reply #1 on: Jul 22nd, 2004, 6:47am » |
|
Whoa. Holy Hungarian notation, Batman! The problem seems to go away if you replace i*fNsc + int(fXtr)*fNsc with (i + int(fXtr))*fNsc), even with iAng = 1000. It probably is a float precision thing.
|
|
|
|
narain
|
Re: rounding precision (and noise)
« Reply #2 on: Jul 22nd, 2004, 8:10am » |
|
Might I add that using noise() for a forest is a pretty cool idea... It looks good, it's patchy enough and the rotation hides the grid perfectly. Have you tried experimenting with noiseDetail()?
|
|
|
|
Sjeiti
|
Re: rounding precision (and noise)
« Reply #3 on: Jul 22nd, 2004, 10:11am » |
|
hey... weird solution but it works... thanks! haven't tried noiseDetail yet... will do now... Is noiseDetail the only thing that let's you control noise? Because evertime I restart, the noise is different. Isn't there a randomseed I can set? Noise can be used for just about everything that needs to look natural. Put a third dimension to the above code and you've got a starfield... or a mucky water field. Btw... these are cool links about noise: http://mrl.nyu.edu/~perlin/ http://www.noisemachine.com/talk1/ http://freespace.virgin.net/hugo.elias/models/m_perlin.htm ...Sjeiti
|
« Last Edit: Jul 22nd, 2004, 10:13am by Sjeiti » |
|
http://www.sjeiti.com/
|
|
|
narain
|
Re: rounding precision (and noise)
« Reply #4 on: Jul 22nd, 2004, 3:51pm » |
|
Yep... Apparently with floats, you should do addition/subtraction before multiplication to minimize rounding error. But that's only when precision is critical... like planning a shuttle launch, or passing off black dots as trees As for seeding, I haven't seen any such function... You'll have to ask fry or REAS about that. Maybe even post it in the Suggestions forum.
|
|
|
|
|