Solved

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

Posted on 2011-03-12
3
281 Views
Last Modified: 2012-05-11
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
Comment
Question by:zliminator
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 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
  }
}

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
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

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now