From: Jan Simon on 12 Jul 2010 18:17 Dear Enrico, > center = round(radius); > cvs = zeros(2*center,2*center); > cvs(center+1,center+1)=10^(color/10); > for az=0:1/(2*radius):2*pi > for r=1:center > x=round(center+sin(az)*r); > y=round(center+cos(az)*r); > cvs(y+1,x+1)=10^(color/10*rand()*(center-r-1)/(center-1)); > end > end Move as much calculations as possible out of the loop: "center - 1", "sin(az)", "cos(az)", "10^", "/(center - 1)" etc can be moved outside the loop over r. X and y can be calculated as a vector outside the r loop also. On the other hand, avoiding the repeated overwriting of the elements of cvs would be more efficient. Jan
From: Enrico on 12 Jul 2010 21:32 "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i1g490$h0h$1(a)fred.mathworks.com>... > > Move as much calculations as possible out of the loop: > "center - 1", "sin(az)", "cos(az)", "10^", "/(center - 1)" etc can be moved outside the loop over r. X and y can be calculated as a vector outside the r loop also. > On the other hand, avoiding the repeated overwriting of the elements of cvs would be more efficient. > > Jan Good point about moving some stuff outside. But yes, I'd really like to look for a way to avoid overwriting elements. Any suggestions on how I might do that? I can think of lots of faster ways to fill it in, but none that would let me do the center weighted fill. I tried drawing alot of concentric circles, but I would miss spots.
From: ImageAnalyst on 12 Jul 2010 22:13 On Jul 12, 9:32 pm, "Enrico " <e...(a)rcsnetwork.com> wrote: [snip] > But yes, I'd really like to look for a way to avoid overwriting elements. Any suggestions on how I might do that? I can think of lots of faster ways to fill it in, but none that would let me do the center weighted fill. I tried drawing alot of concentric circles, but I would miss spots. ---------------------------------------------------------------- If you scan by x and y, you won't miss spots since you'll be hitting every pixel. But there's probably some clever way to do it with meshgrid() (that I don't have time to figure out for you right now), like in this example from the help: [X,Y] = meshgrid(-2:.2:2, -2:.2:2); Z = X .* exp(-X.^2 - Y.^2); surf(X,Y,Z)
From: Enrico on 12 Jul 2010 22:42 ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <40f44131-4667-4572-a392-f25187d85d28(a)j8g2000yqd.googlegroups.com>... > On Jul 12, 9:32 pm, "Enrico " <e...(a)rcsnetwork.com> wrote: > [snip] > > But yes, I'd really like to look for a way to avoid overwriting elements. Any suggestions on how I might do that? I can think of lots of faster ways to fill it in, but none that would let me do the center weighted fill. I tried drawing alot of concentric circles, but I would miss spots. > ---------------------------------------------------------------- > If you scan by x and y, you won't miss spots since you'll be hitting > every pixel. But there's probably some clever way to do it with > meshgrid() (that I don't have time to figure out for you right now), > like in this example from the help: > > [X,Y] = meshgrid(-2:.2:2, -2:.2:2); > Z = X .* exp(-X.^2 - Y.^2); > surf(X,Y,Z) Yeah, scanning horizontally is how I would normally fill a circle and that would be really fast. I'm doing it this way because I need to weight the center with higher random numbers than at the edges. I'll look into mesh grid, seems promising.
From: Cris Luengo on 13 Jul 2010 05:21
"Enrico " <en(a)rcsnetwork.com> wrote in message <i1gjps$6ht$1(a)fred.mathworks.com>... > ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <40f44131-4667-4572-a392-f25187d85d28(a)j8g2000yqd.googlegroups.com>... > > On Jul 12, 9:32 pm, "Enrico " <e...(a)rcsnetwork.com> wrote: > > [snip] > > > But yes, I'd really like to look for a way to avoid overwriting elements. Any suggestions on how I might do that? I can think of lots of faster ways to fill it in, but none that would let me do the center weighted fill. I tried drawing alot of concentric circles, but I would miss spots. > > ---------------------------------------------------------------- > > If you scan by x and y, you won't miss spots since you'll be hitting > > every pixel. But there's probably some clever way to do it with > > meshgrid() (that I don't have time to figure out for you right now), > > like in this example from the help: > > > > [X,Y] = meshgrid(-2:.2:2, -2:.2:2); > > Z = X .* exp(-X.^2 - Y.^2); > > surf(X,Y,Z) > > > Yeah, scanning horizontally is how I would normally fill a circle and that would be really fast. I'm doing it this way because I need to weight the center with higher random numbers than at the edges. I'll look into mesh grid, seems promising. color = 1; radius = 100; center = round(radius); [x,y] = meshgrid(0:2*center,0:2*center); r = sqrt((x-center).^2+(y-center).^2); cvs = zeros(size(r)); mask = r<=center; noise = rand(sum(mask(:)),1); cvs(mask) = 10.^(color./10.*noise.*(center-r(mask)-1)./(center-1)); cvs(center+1,center+1) = 10^(color/10); % is this necessary? Another way to speed up your double loop is to do them the other way around. Instead of for az=0:1/(2*radius):2*pi for r=1:center do for r=1:center for az=0:1/(2*r):2*pi That way the inner loop can scale with the outer loop. You still need to decrease the angular step size a little, though. Your original solution does leave out a few spots, more visible for larger radius. Cheers, Cris. |