fill % of an array

G

Gaz

Hi;

Can someone point me in the direction of some code that allows me to
fill a certain percentage of a 2d array with a colour. I tried using
math.random but it dosen't create clean lines due to its random nature.
 
S

Stefan Ram

Gaz said:
Can someone point me in the direction of some code that allows
me to fill a certain percentage of a 2d array with a colour. I
tried using math.random but it dosen't create clean lines due
to its random nature.

If the task is to fill a certain percentage, this does not
require clean lines - so you have completed the task given
(that is: if the percentage filled is correct).
 
G

Gaz

Stefan said:
If the task is to fill a certain percentage, this does not
require clean lines - so you have completed the task given
(that is: if the percentage filled is correct).

ye but i want clean lines
 
E

Eric Sosman

Gaz said:
ye but i want clean lines

Stefan is trying to tell you that you have not specified
the problem sufficiently. What do you mean by "clean line?"
Must the filled area be contiguous, or can you use multiple
discontiguous regions? What other constraints are there on
the shape(s) of the filled area(s)? Is your "2d array" square,
rectangular, or ragged?

static void fill(Color[][] array, Color hue, double pct) {
if (pct < 0.0 || pct > 100.0)
throw new IllegalArgumentException("Loser!");

/* How many array elements are there? */
int total = 0;
for (int i = 0; i < array.length; ++i)
total += array.length;

/* How many of them should be filled? */
int count = (int)Math.round(total * pct / 100.0);

/* Fill that many. */
for (int i = 0; count > 0; ++i) {
for (int j = 0; j < array.length; ++j) {
array[j] = hue;
if (--count <= 0)
break;
}
}
}

The above meets all the requirements you've stated (using
my own private definition of "clean lines," since you haven't
explained your own). If it's not satisfactory, DON'T just say
"The filled area isn't in the middle" or "I wanted an oval."
Instead, describe ALL your requirements, taking care to define
fuzzy terms like "clean lines" or "non-Cubist shapes."

The first step in solving a problem is to state it clearly.
 
G

Gaz

Eric said:
Stefan Ram wrote:




ye but i want clean lines


Stefan is trying to tell you that you have not specified
the problem sufficiently. What do you mean by "clean line?"
Must the filled area be contiguous, or can you use multiple
discontiguous regions? What other constraints are there on
the shape(s) of the filled area(s)? Is your "2d array" square,
rectangular, or ragged?

static void fill(Color[][] array, Color hue, double pct) {
if (pct < 0.0 || pct > 100.0)
throw new IllegalArgumentException("Loser!");

/* How many array elements are there? */
int total = 0;
for (int i = 0; i < array.length; ++i)
total += array.length;

/* How many of them should be filled? */
int count = (int)Math.round(total * pct / 100.0);

/* Fill that many. */
for (int i = 0; count > 0; ++i) {
for (int j = 0; j < array.length; ++j) {
array[j] = hue;
if (--count <= 0)
break;
}
}
}

The above meets all the requirements you've stated (using
my own private definition of "clean lines," since you haven't
explained your own). If it's not satisfactory, DON'T just say
"The filled area isn't in the middle" or "I wanted an oval."
Instead, describe ALL your requirements, taking care to define
fuzzy terms like "clean lines" or "non-Cubist shapes."

The first step in solving a problem is to state it clearly.

Sorry I should have been more precise, its suppsoed to resemble the
simulation of the seetling of a pint of beer so I am using a 2d to fill
a glass painted on the screen. I am using math.random with textures with
two for loops to loop through each part of the array giving it a random
colour e.g. if > 0.8 its white or <0.8 black. I have then given each
colour a weight and use a method that if the particle on top is heavier
than the one below they swap. I have white set to be heavier than black
so when the loop is finished all the whites are on top like a head on
the pint and all the black is to the bottom however due to the random
nature of math.random there is not an equal number of whites along the
top so the head appears jagged.


Now after that long explaination is over what I want to do is when the
two for loops are finished. I want another for loop (or something else)
to make everything in the array above a certain height to be white, so
the bottom 95% of the array is black and the top 5% is white with a nice
smooth line between them.
 
O

Owen Jacobson

Sorry I should have been more precise, its suppsoed to resemble the
simulation of the seetling of a pint of beer so I am using a 2d to fill a
glass painted on the screen. I am using math.random with textures with two
for loops to loop through each part of the array giving it a random colour
e.g. if > 0.8 its white or <0.8 black. I have then given each colour a
weight and use a method that if the particle on top is heavier than the
one below they swap. I have white set to be heavier than black so when the
loop is finished all the whites are on top like a head on the pint and all
the black is to the bottom however due to the random nature of math.random
there is not an equal number of whites along the top so the head appears
jagged.

Using your 0.8 (80%) example value:

1. Fill the whole column with black.
2. Fill the top (100-80)% of the with white.

At this point each column appears how it will appear after your settling
algorithm completes.

3. Shuffle the column. There was a thread about this recently.

If you choose a sane shuffling algorithm, the column will be randomly
filled with exactly 80% black, 20% white pixels, as opposed to the
"approximately" 80% black, 80% white you get with randomly choosing each
pixel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top