Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

drawing lines on a grid with skips after every 2 and 3 lines

Posted on 2011-03-12
Medium Priority
290 Views
I'm writing a quick program to print out a grid. I have the code to draw the horiz lines:

float horizline = 0;
float vertline = 0;
RectangleF rect = g.Clip.GetBounds(g);
SizeF size = rect.Size;
float myy = size.Height;
float myx = rect.Right;

for(horizline = 1;horizline < 20;horizline += 0.1F)
g.DrawLine(new Pen(Brushes.Black, 0.01f), 0, horizline, myx, horizline);

Then I want to draw the vert lines with an extra space at every 4th and 7th line then repeat.
Its going to be used as notation for piano so the lines will represent the black keys and the spaces between the lines will represent the white keys. The following will draw a line at every interval:

for (vertline = 1; vertline < 20; vertline += 0.2F)
g.DrawLine(new Pen(Brushes.Blue, 0.01f), vertline, 0, vertline, myy);

I'm thinking an enum of 7 booleans where the 1st 3 are true, the 4th false, the next 2 are true and the last false. Then have a loop inside the above for loop which checks the enum. Any ideas?

And no, this isn't homework.
0
Question by:zliminator
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 37

Accepted Solution

TommySzalapski earned 2000 total points
ID: 35116603
You have
for (vertline = 1; vertline < 20; vertline += 0.2F)

So vertline*5 will give you integers ranging from 5 - 99
If 5 is the first, then 8 and 11 are the 4th and 7th 8%7 == 1 and 11%7 == 4
So you could do
``````for (vertline = 1; vertline < 20; vertline += 0.2F)
{
int line = vertline*5
if(line%7 == 1 || line%7 == 4)
// Do nothing
else
{
//Draw line
}
}
``````
0

LVL 37

Expert Comment

ID: 35116620
If you have never used the % operator, then this is a good time to learn it. It is called the modulo operator (or mod for short).
8%7 is read as "eight mod seven" and it basically equals the remainder of a division problem.

So
0%3 == 0
1%3 == 1
2%3 == 2
3%3 == 0
4%3 == 1
5%3 == 2
6%3 == 0
7%3 == 1
8%3 == 2
etc.
So every 4th and 7th line will be found and skipped by the code suggested above.
I haven't tested it, so you might need to change a number or two, but this is the standard way of doing it since the operator is very fast (especially compared to looping through bools)
0

Author Closing Comment

ID: 35116777
Of course. I shoulda gave it some more thought. I suspected there might be a more elegant solution.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month7 days, 19 hours left to enroll