request help on enumeration/numbering algorithm

R

Rich P

I have a bunch of dots that I draw on a form and connect them with
lines. I have 3 basic groups of dots within a larger group (of which I
have hundreds of the larger groups - meaning I will have nearly 1000 of
the individual dot groups). I need to color code these dots with 3
separate colors. So starting with a counter at 1 -- the 1 dots group
will be blue (for example) and 2 dots group will be yellow, the 3 dots
group will be purple -- this is for the first big group. In the 2nd big
group we have the 4 dots group (blue), the 5 dots group (yellow), the 6
dots group (purple). Then the next big group will have 7 dots group
(blue), 8 dots group (yellow), 9 dots group (purple)...

so the numbering would be 1,4,7,10, ... blue dots
2,5,8,11,... yellow dots
3,6,9,12,... purple dots.

if (x=1) dots.color = color.blue;
if (x=2) dots.color = color.yellow;
if (x=3) dots.color = color.purple;
if (x=4) dots.color = color.blue;
if (x=5) dots.color = color.yellow;
if (x=6) dots.color = color.purple;
...

In a loop I need to do something like this:
for (int x=1;x<999;x++)
{
...
if (x%something = 0) dots.color = color.blue;
if (x%somethingelse = 0) dots.color = color.yellow;
if (x%3=0) dots.color = color.purple;
...
}

I am tempted to do something convoluted like creating arrays of numbers
so that if I am on a particular number in an array - the dots are that
respective color

int[] a1 = new int[]{1,4,7,10,...};
int[] a2 = new int[]{2,5,8,11,...};
int[] a3 = new int[]{3,6,9,12,...};

the problem with this is that I will have hundreds of numbers in each
array. Surely there is a simpler way to perform this enumeration. Any
ideas/suggestions appreciated.

Rich
 
P

Peter Duniho

Rich said:
[...]
the problem with this is that I will have hundreds of numbers in each
array. Surely there is a simpler way to perform this enumeration. Any
ideas/suggestions appreciated.

I may not understand the question. But assuming I do:

All you need to do is subtract 1 from the dot count, % 3, and then use
the result to map to a color:

// I assume this loop is just a placeholder for something
// that actually looks at dot groups and gets the count of
// dots in a group, where "x" is the count
for (int x = 1; x < 999; x++)
{
switch ((x - 1) % 3)
{
case 0:
dots.color = Color.Blue;
break;
case 1:
dots.color = Color.Yellow;
break;
case 2:
dots.color = Color.Purple;
break;
}
}

Pete
 
R

Rich P

Thank you for your reply. This is what I was looking for.
Interestingly, however, I did break down in the meantime and tried the
array thing

string[] a1 = new string[]{",a,d,g,j,m...,";
string[] a2 = new string[]{",b,e,h,k,n,...,"};
string[] a3 = new string[]{",c,f,i,l,o,...,"};

if (a1.IndexOf(s1) > -1) dots.color = color.blue;
if (a2.IndexOf(s1) > -1) dots.color = color.yellow;
if (a3.IndexOf(s1) > -1) dots.color = color.purple;

and noticed a significant increase in performance over the % method.
There are several calculations occuring for each dot. But the % method
requires less coding. In the end, the end user will dictate which
method I will use.

Thanks again for the help.

Rich
 
P

Peter Duniho

Rich said:
Thank you for your reply. This is what I was looking for.
Interestingly, however, I did break down in the meantime and tried the
array thing

string[] a1 = new string[]{",a,d,g,j,m...,";
string[] a2 = new string[]{",b,e,h,k,n,...,"};
string[] a3 = new string[]{",c,f,i,l,o,...,"};

if (a1.IndexOf(s1) > -1) dots.color = color.blue;
if (a2.IndexOf(s1) > -1) dots.color = color.yellow;
if (a3.IndexOf(s1) > -1) dots.color = color.purple;

and noticed a significant increase in performance over the % method.

All due respect, that statement makes no sense whatsoever.

First, scanning all but the shortest arrays is on average going to be
much slower than doing a simple % operation.

Second, the code example you posted, which I quoted above, bears
practically no resemblance whatsoever to the original problem and as
literally given could not be implemented using the % operator at all
(that operator being for integer arithmetic, and your example being
entirely string-based).
There are several calculations occuring for each dot. But the % method
requires less coding. In the end, the end user will dictate which
method I will use.

If you allow the end user to make that decision, you should make sure
you yourself really understand the implications of the different
possibilities.

And at the very least, if you are going to scan a string for specific
characters, don't waste space in the string with characters that
accomplish nothing. They will just slow the scan down.

Pete
 
F

Family Tree Mike

I have a bunch of dots that I draw on a form and connect them with
lines. I have 3 basic groups of dots within a larger group (of which I
have hundreds of the larger groups - meaning I will have nearly 1000 of
the individual dot groups). I need to color code these dots with 3
separate colors. So starting with a counter at 1 -- the 1 dots group
will be blue (for example) and 2 dots group will be yellow, the 3 dots
group will be purple -- this is for the first big group. In the 2nd big
group we have the 4 dots group (blue), the 5 dots group (yellow), the 6
dots group (purple). Then the next big group will have 7 dots group
(blue), 8 dots group (yellow), 9 dots group (purple)...

so the numbering would be 1,4,7,10, ... blue dots
2,5,8,11,... yellow dots
3,6,9,12,... purple dots.

if (x=1) dots.color = color.blue;
if (x=2) dots.color = color.yellow;
if (x=3) dots.color = color.purple;
if (x=4) dots.color = color.blue;
if (x=5) dots.color = color.yellow;
if (x=6) dots.color = color.purple;
..

In a loop I need to do something like this:
for (int x=1;x<999;x++)
{
..
if (x%something = 0) dots.color = color.blue;
if (x%somethingelse = 0) dots.color = color.yellow;
if (x%3=0) dots.color = color.purple;
..
}

I am tempted to do something convoluted like creating arrays of numbers
so that if I am on a particular number in an array - the dots are that
respective color

int[] a1 = new int[]{1,4,7,10,...};
int[] a2 = new int[]{2,5,8,11,...};
int[] a3 = new int[]{3,6,9,12,...};

the problem with this is that I will have hundreds of numbers in each
array. Surely there is a simpler way to perform this enumeration. Any
ideas/suggestions appreciated.

Rich

It sounds like a dot belongs to a "dotCollection" object, and maybe the
color should belong to the dotCollection class.
 
O

ozbear

I have a bunch of dots that I draw on a form and connect them with
lines. I have 3 basic groups of dots within a larger group (of which I
have hundreds of the larger groups - meaning I will have nearly 1000 of
the individual dot groups). I need to color code these dots with 3
separate colors. So starting with a counter at 1 -- the 1 dots group
will be blue (for example) and 2 dots group will be yellow, the 3 dots
group will be purple -- this is for the first big group. In the 2nd big
group we have the 4 dots group (blue), the 5 dots group (yellow), the 6
dots group (purple). Then the next big group will have 7 dots group
(blue), 8 dots group (yellow), 9 dots group (purple)...

so the numbering would be 1,4,7,10, ... blue dots
2,5,8,11,... yellow dots
3,6,9,12,... purple dots.

if (x=1) dots.color = color.blue;
if (x=2) dots.color = color.yellow;
if (x=3) dots.color = color.purple;
if (x=4) dots.color = color.blue;
if (x=5) dots.color = color.yellow;
if (x=6) dots.color = color.purple;
..

In a loop I need to do something like this:
for (int x=1;x<999;x++)
{
..
if (x%something = 0) dots.color = color.blue;
if (x%somethingelse = 0) dots.color = color.yellow;
if (x%3=0) dots.color = color.purple;
..
}

I am tempted to do something convoluted like creating arrays of numbers
so that if I am on a particular number in an array - the dots are that
respective color

int[] a1 = new int[]{1,4,7,10,...};
int[] a2 = new int[]{2,5,8,11,...};
int[] a3 = new int[]{3,6,9,12,...};

the problem with this is that I will have hundreds of numbers in each
array. Surely there is a simpler way to perform this enumeration. Any
ideas/suggestions appreciated.

Rich

Your problem is similar to performing a conversion of a one
dimensional array to a two dimensional array, if you view
the two dimensional array as a series of rows, each containing
three columns which represent blue, yellow, and purple.
Given a one dimensional index of x, we are not interested in
the row that the corresponding two dimension cell resides in,
but we do care about the column.
There are many ways of coding this, just be certain what the
first value of x really is, i.e., is it really one-based or
does x actually start at zero?
Assuming it really does start at 1 you could write a method:

Color XToColor(int x)
{
switch ((x - 1) % 3)
{
case 0: return Color.Blue;
case 1: return Color.Yellow;
default: return Color.Purple;
}
}

If your class included a static array of colors elsewhere in the
class definition, it gets slightly simpler:

static Color[] arr = new Color[] { Color.Blue, Color.Yellow,
Color.Purple };

Color XToColor(int x)
{
return arr[(x - 1) % 3];
}

Oz
 
R

Rich P

Thank you all for your replies. I think I started tripping out on this
project a little bit. As can be the case (most of the time) there are
several ways to tackle something, and in this case the underlying
architecture of the project may lend itself more effectively to one
method over another. Too many pieces to describe here. I just happen
to get caught up on the % operator - mentally - and just couldn't go
forward until I figured out the way to use % even though it ended up not
being the most ideal fix to the portion of the project I am working on.

Peter answered my question on how to use % in a loop for a condition
similar to the one I am currently working on. Thanks Pete. Now I have
a little bit better understanding of ways to use %.

I just happen to stumble on a different technique (the array thing) that
appears to work a little faster for the given architechture of my
current project - a tad more typing - but I don't think the end users
care how much I have to type to get their project going :).

Rich
 

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

Top