fill % of an array

Discussion in 'Java' started by Gaz, Apr 22, 2005.

  1. Gaz

    Gaz Guest

    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.
     
    Gaz, Apr 22, 2005
    #1
    1. Advertising

  2. Gaz

    Stefan Ram Guest

    Gaz <> writes:
    >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).
     
    Stefan Ram, Apr 22, 2005
    #2
    1. Advertising

  3. Gaz

    Gaz Guest

    Stefan Ram wrote:
    > Gaz <> writes:
    >
    >>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).


    ye but i want clean lines
     
    Gaz, Apr 22, 2005
    #3
  4. Gaz

    Eric Sosman Guest

    Gaz wrote:
    > Stefan Ram wrote:
    >
    >>Gaz <> writes:
    >>
    >>
    >>>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).

    >
    >
    > 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.

    --
     
    Eric Sosman, Apr 22, 2005
    #4
  5. "Gaz" <> schreef in bericht
    news:j4dae.52059$...
    > Stefan Ram wrote:
    >> Gaz <> writes:
    >>
    >>>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).

    >
    > ye but i want clean lines


    So create them!
     
    Boudewijn Dijkstra, Apr 22, 2005
    #5
  6. Gaz

    Gaz Guest

    Eric Sosman wrote:
    >
    > Gaz wrote:
    >
    >>Stefan Ram wrote:
    >>
    >>
    >>>Gaz <> writes:
    >>>
    >>>
    >>>
    >>>>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).

    >>
    >>
    >>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.
     
    Gaz, Apr 22, 2005
    #6
  7. On Fri, 22 Apr 2005 23:05:53 +0100, Gaz wrote:

    > 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.
     
    Owen Jacobson, Apr 23, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fabio Papa
    Replies:
    1
    Views:
    521
    Jim Blizzard [MSFT]
    Sep 24, 2003
  2. Replies:
    14
    Views:
    864
  3. fill in array

    , Jan 4, 2008, in forum: C Programming
    Replies:
    1
    Views:
    324
    user923005
    Jan 4, 2008
  4. Gennaro Prota
    Replies:
    1
    Views:
    281
  5. Thomas Jollans
    Replies:
    5
    Views:
    1,742
    Thomas Jollans
    Jun 14, 2010
Loading...

Share This Page