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.