We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › weather, topographic, gradient map
Page Index Toggle Pages: 1
weather, topographic, gradient map (Read 1926 times)
weather, topographic, gradient map
Jan 16th, 2009, 9:47pm
 
how might one create a map which could describe gradient conditions between loci of intensity? say I specify points representing highs and lows, and I wanted to see the space in between those points as a gradient, how might I approach this?

I am thinking in terms of natural features, like landscapes or climatic qualities like temperature or humidity...I would want to describe the shift either in terms of colour or contour lines. Typically these maps would have multiple centres and wouldnt be a simple case of a linear blend between two points.

I have no idea how to even start with this thing. Any ideas?
thanks everyone!
Re: weather, topographic, gradient map
Reply #1 - Jan 22nd, 2009, 1:29am
 
I'm assuming that you've got a grid of values and you'd like to compute gradients. have I got the wrong end of the stick?

you can use finite difference techniques to compute the gradient at each grid point: http://en.wikipedia.org/wiki/Finite_difference. that page is very abstract (and maths heavy), so i'll try and give you an applied example. using the forward difference technique to compute the x & y gradients at a point (i,j):

float dx = (grid[j][i+1] - grid[j][i]) / cell_width;
float dy = (grid[j+1][i] - grid[j][i]) / cell_height;

you can think of (dx,dy) as the gradient vector for that point. so you can calculate the magnitude of the gradient (how much the function is changing at that point) using:
float gradient_size = mag(dx, dy);

once you have the gradient values you can try all kinds of things. colour coding my gradient magnitude, or mapping gradient angle to hue and magnitude to saturation, etc.

hope this helps.
Re: weather, topographic, gradient map
Reply #2 - Jan 22nd, 2009, 3:46am
 
thanks, yes that does help though alot of it is going over my head. but what it did do is remind me of my experiments with parametric models where I am measuring the difference (distance) between each point on a grid and a selected attractor point. Then those values are used to drive diferent things including heights of lines etc...so that was a very helpful clue...now I just have to figure out how you are proposing I do it in processing. thanks!

float dx = (grid[j][i+1] - grid[j][i]) / cell_width;
float dy = (grid[j+1][i] - grid[j][i]) / cell_height;

it's this part that I'm not quite getting - grid[i] and [j] make me think of a 2d array of points...I am not sure what the equations are measuring - is the forward technique about measuring between each cell and its neighbour?

I don't have my grid, I just have the points which are the high and low values...
Re: weather, topographic, gradient map
Reply #3 - Jan 22nd, 2009, 4:48am
 
you wouldn't happen to know of any processing work that demonstrates your example, do you?
Re: weather, topographic, gradient map
Reply #4 - Jan 22nd, 2009, 6:59pm
 
ah okay, I assumed you had a grid of values (say from an image), but it sounds like you have a scattered set of points: (10, 9) = 11, (54, 123) = -3, .... and you want to compute values (and gradients) at other points. this is the same problem as the attractor one you described - you have a set of attractors (the scattered set in my example) and at each point on a grid you want to calculate a value, which in your case was distance to attractor. is this right?

to answer your other questions: yeah I assumed you had a grid of values and that you wanted to evaluate the gradient at each grid point. for brevity I represented the 2D grid as nested arrays.

the forward difference just calculates the difference between the value at one location and the value at the next location in the 'forward' direction, and divides by the step size (which in this case is the width of a grid cell). most of the time that width will be 1, but when you are working with physical simulations it will often be a different value.

as we have a 2d grid we will have a 2d gradient. The 2d gradient is represented by a 2 component vector (dx, dy) in my example. we have to calculate each component of the vector separately, and we use the forward differencing technique to achieve this. for the x-axis (dx) the forward direction is one step in the positive direction in the x-axis, so that's [j][i+1]. similarly for the y-axis (dy) it's [j+1][i].

i know there's a lot in my response, but i've tried to explain it as best i can. it can take a while to get your head around the concepts.
Re: weather, topographic, gradient map
Reply #5 - Jan 22nd, 2009, 7:02pm
 
here is some code I wrote a while back to explore the gradient field of the Perlin noise field. in essence does the same thing that I outlined in my posts, but here i use the central difference technique rather than forward differences (no reason) and I evaluate the noise function, rather than read values from a grid.

this is my first attempt at posting code so I hope this comes out.

// Sketch to explore Perlin noise.
// Mouse X coordinate sets z coordinate in noise lookups.
// Mode 1: show Perlin noise value
// Mode 2: show gradient of noise
// Mode can be set via 1 & 2 keys.
int mode = 1;

void setup() {
 size(160, 120);
 noiseDetail(4);
}

void draw() {
 float noiseScale = 0.015f;
 for (int i = 0; i < width; ++i) {
   for (int j = 0; j < height; ++j) {
     float x = i * noiseScale;
     float y = j * noiseScale;
     float z = mouseX * noiseScale;
     
     if (mode == 1) {
       stroke(noise(x, y, z)*255);
     } else {
       // use central difference to compute first derivative
       float dx = noise(x + noiseScale/2, y, z) - noise(x - noiseScale/2, y, z);
       float dy = noise(x, y + noiseScale/2, z) - noise(x, y - noiseScale/2, z);
       // in theory the range of dx & dy is [-1,1], but because noise
       // is smoothly varying the range is much smaller so we need to scale values
       // up.
       float r = constrain((dx*100 + 1) * 0.5, 0, 1);
       float g = constrain((dy*100 + 1) * 0.5, 0, 1);
       stroke(r * 255, g * 255, 0);
     }
     
     point(i, j);
   }
 }

 if (keyPressed) {
   if (key == '1') mode = 1;
   else if (key == '2') mode = 2;
 }
}
Re: weather, topographic, gradient map
Reply #6 - Mar 9th, 2009, 5:01am
 
...back at this after having to put it aside...

in your example, lollipop, you are using the gradient vectors to modify the colour of the point drawn on each cycle. How might I draw regions, as in polyline boundaries that enclose similar values as you might have in a weather map with isolines? Has anyone ever seen this done?
Re: weather, topographic, gradient map
Reply #7 - Mar 9th, 2009, 1:45pm
 
I had an Idea for how to do this, but it might not be what you need, regardless what I put together is here:
http://angeloyazar.phpwebhosting.com/processing/region_borders/

The ideas that are important for this method are clamping and edge finding in that space, which could be some thing like this:

1. clamp values in the field to the bands you want.
2. find edges
 For each cell:
 a. count neighbors that have the same value as the current cell.
 b. if that number is bigger than 5, current cell is not an edge.
3. draw edges.
Re: weather, topographic, gradient map
Reply #8 - Mar 9th, 2009, 4:56pm
 
wow. yes that is extremely useful. I was hoping to actually have lines or curves making up each boundary - but I suppose I could simply trace the points. thanks alot for putting that together
Re: weather, topographic, gradient map
Reply #9 - Mar 9th, 2009, 10:35pm
 
You could also set the cell_size to 1, but the way the example is coded it's already kinda slow. But I'm sure that can be changed.

I'm glad it was helpful though, good luck with your project!
Re: weather, topographic, gradient map
Reply #10 - Mar 10th, 2009, 3:36am
 
Here's a version that runs a little more reliably, but it assumes static sources.

http://angeloyazar.phpwebhosting.com/processing/region_borders_b/
Re: weather, topographic, gradient map
Reply #11 - Mar 14th, 2009, 7:24pm
 
I think this is a good addition to the discussion. I'd be excited to see someone implement it. I haven't tried yet.

CONRC - A Countouring Subroutine
Page Index Toggle Pages: 1