Log in

View Full Version : C# - alternating brush colors


Jean Poutine
December 10th, 2008, 05:37 AM
I've got a little problem...I've got something to do where a user has to select 2 colors, a form, enter a number and it draws squares that fill up the shape while alternating colors.

I've got that all right, my problem is that it alternates them TOO well.

So far I've got this code for the brush colours :

switch (cboCouleur1.SelectedIndex)
{
case 0 :
d1 = rouge;
break;

case 1 :
d1 = bleu;
break;

case 2 :
d1 = jaune;
break;

case 3 :
default :
d1 = orange;
break;
}

switch (cboCouleur2.SelectedIndex)
{
case 0:
d2 = rouge;
break;

case 1:
d2 = bleu;
break;

case 2:
d2 = jaune;
break;

case 3:
default:
d2 = orange;
break;
}

This works. Now the code for a square, which works perfectly :

if (rdoCarre.Checked)
for (cy = 0; cy != nombre; cy++) //calculates the number of squares following y axis
{
x = 0;
for (cx = 0; cx != nombre; cx++) //does the same for x axis, fills 'em up then...
{
if (brosse) //that's a boolean
utilise = d1;
else
utilise = d2;
wtf.FillRectangle(utilise, x, y, 10, 10);
x = x + 15;
brosse = !brosse;
}
y += 15; //changes lines once the loop has been executes n times
}

My problem comes with the inverted triangle...

if (rdoTriangleInverse.Checked)
for (cy = nombre; cy != 0; cy = cy -1) //same as square
{
x = 0;
for (cx = 0; cx != nombre; cx++) //same as square
{
if (brosse)
utilise = d1;
else
utilise = d2;
wtf.FillRectangle(utilise, x, y, 10, 10);
x = x + 15;
brosse = !brosse;
}
y += 15; //switches line...
nombre = nombre - 1; //substract from the number to get that super triangle sensation!
}

Except on the example program it's supposed to be :

(1)(2)(1)(2)(1)
(2)(1)(2)(1)
(1)(2)(1)
(2)(1)
(1)

And on mine it does :

(1)(2)(1)(2)(1)
(2)(1)(2)(1)
(2)(1)(2)
(1)(2)
(1)

I was wondering if there was something I'm doing wrong. The square works perfectly...the triangles, not.

Note that I must turn this in tomorrow morning so I just want to know for amusement's sake. I got farther into this than most in my class would be able to alone, anyway.

Thanks!

Kiros
December 10th, 2008, 10:19 AM
From what I can tell, this is caused purely by the alteration logic.

You want this:
(1)(2)(1)(2)(1)
(2)(1)(2)(1)
(1)(2)(1)
(2)(1)
(1)

But you get this:
(1)(2)(1)(2)(1)
(2)(1)(2)(1)
(2)(1)(2)
(1)(2)
(1)

Because of brosse = !brosse; So with this logic, if the last output was (1) then the next will be (2) and vice versa.

I've only taken a glance at your code because I'm still tired from the other day, but I believe you can solve it quite easily. Instantiate a new boolean before the for loop as bool bStartOnAlt = false; (for starting on the alternate color). Within the first for loop, use brosse = !bStartOnAlt; and then after the second for loop (but before the first one closes use bStartOnAlt = !bStartOnAlt;

If that doesn't work then I may have overlooked something. Let me know.

By the way, Jason, are you French?

Jean Poutine
December 10th, 2008, 02:55 PM
That's actually what my prog teacher told me, too. Thanks for the help!

And yeah, I'm like, something like a tenth generation French Canadian lol (my ancestors were at Port Royal before coming to settle in Québec). French is my mother tongue, that explains the combo box names :}

Kiros
December 10th, 2008, 07:39 PM
Ah, well if you speak French as well as you speak English, than I'm impressed.

Good to hear that the programming error was simple to fix.

Jean Poutine
December 11th, 2008, 10:17 PM
Well given that French is my first language, my English is worse than my French ;}

I still have some problems with English vocabulary, though, but thanks for the compliment!

iJack
December 11th, 2008, 11:40 PM
Well given that French is my first language, my English is worse than my French ;}

I still have some problems with English vocabulary, though, but thanks for the compliment!
You seem pretty good at it.

Jean Poutine
December 12th, 2008, 12:58 AM
English has over 700,000 words, and I do not know even 1/20 of that I'd bet. French has around 100,000.

What I mean is, I won't consider myself good with the language until I have at least around 100k English words in my repertory. I speak with fairly simple words, sure I'm able to be understood because I have a good grasp of English grammar, but an habit of the French language carried over in English, which is the temptation of using synonyms rather than the same word (aside from common words such as articles and prepositions, one rarely encounters the same French word twice in a certain period), and it sucks not knowing synonyms off my head sometimes (like for the word "word" :P).

I also would love to start writing and studying English poetry, but I don't feel my vocabulary is powerful enough to give a "poetic" feel to a text in the classical sense. I'm a very technical and classical writer, and I'm hung on old, obsolete rules. i need a better grasp of the vocab' if I'm going to do that in another language!

Kiros
December 13th, 2008, 05:08 PM
Hmmm.... A synonym for the word, "word."

*ponders*

The only thing I could think of is the word, "phrase," which can mean a single word, however it usually means a group of words which may or may not make a complete sentence. Eh, the English language has many words but only for wordy words :P