4
\$\begingroup\$

I have simple code, that draw graph of my histogram. But code looks ugly, do you have idea, how can I make it looks better ?

            for (int i = 0; i < hueCount - 2; ++i)
            {
                if( i % 6 == 0)
                g.DrawLine(borderPen,
                   margin+ (width - 2 * margin) * ((float)i / (float)hueCount), 
                   height - margin,
                   margin+ (width - 2 * margin) * ((float)i / (float)hueCount),
                   height - margin - 5);

                g.DrawLine(redPen,
                    margin + ((float)i / (float)hueCount) * (width - 2 * margin),
                    (-(float)reds[i] / (float)maxRed) * (height - 2 * margin) - margin + height,
                    margin + ((float)(i + 1) / (float)hueCount) * (width - 2 * margin),
                    (-(float)reds[i + 1] / (float)maxRed) * (height - 2 * margin) - margin + height);

                g.DrawLine(greenPen,
                    margin + ((float)i / (float)hueCount) * (width - 2 * margin),
                    (-(float)greens[i] / (float)maxGreen) * (height - 2 * margin) - margin + height,
                    margin + ((float)(i + 1) / (float)hueCount) * (width - 2 * margin),
                    (-(float)greens[i + 1] / (float)maxGreen) * (height - 2 * margin) - margin + height);

                g.DrawLine(bluePen,
                    margin + ((float)i / (float)hueCount) * (width - 2 * margin),
                    (-(float)blues[i] / (float)maxBlue) * (height - 2 * margin) - margin + height,
                    margin + ((float)(i + 1) / (float)hueCount) * (width - 2 * margin),
                    (-(float)blues[i + 1] / (float)maxBlue) * (height - 2 * margin) - margin + height);
            }
\$\endgroup\$
4
\$\begingroup\$

Here's what I came up with using a helper class:

            Draw draw = new Draw(g, borderPen, hueCount, margin, width, height);

            for (int i = 0; i < hueCount - 2; ++i)
            {
                draw.DrawBorder(i);
                draw.DrawLine(redPen, i, reds, maxRed);
                draw.DrawLine(greenPen, i, greens, maxGreen);
                draw.DrawLine(bluePen, i, blues, maxBlue);
            }

...

internal sealed class Draw
{
    private readonly Graphics g;

    private readonly Pen borderPen;

    private readonly int hueCount;

    private readonly int margin;

    private readonly int doubleMargin;

    private readonly int width;

    private readonly int height;

    internal Draw(Graphics g, Pen borderPen, int hueCount, int margin, int width, int height)
    {
        this.g = g;
        this.borderPen = borderPen;
        this.hueCount = hueCount;
        this.margin = margin;
        this.doubleMargin = 2 * this.margin;
        this.width = width;
        this.height = height;
    }

    internal void DrawBorder(int i)
    {
        if (i % 6 == 0)
        {
            float x = margin + (width - doubleMargin) * (float)i / hueCount;
            float y = height - margin;

            g.DrawLine(borderPen, x, y, x, y - 5);
        }
    }

    internal void DrawLine(Pen pen, int i, int[] colors, int maxColor)
    {
        float wm = width - doubleMargin;
        float hm = height - doubleMargin;
        float mh = margin - height;

        g.DrawLine(
            pen,
            margin + ((float)i / hueCount) * wm,
            (-(float)colors[i] / maxColor) * hm - mh,
            margin + ((float)(i + 1) / hueCount) * wm,
            (-(float)colors[i + 1] / maxColor) * hm - mh);
    }
}
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

I had to fake a lot of your values but it seems like your DrawLine's is the same except for the color so just create a method to do the real draw line and pass in the colors etc.

        for (int i = 0; i < hueCount - 2; ++i)
        {
            if (i % 6 == 0)
                g.DrawLine(borderPen,
                   margin + (width - 2 * margin) * ((float)i / (float)hueCount),
                   height - margin,
                   margin + (width - 2 * margin) * ((float)i / (float)hueCount),
                   height - margin - 5);

            DrawLine(reds, width, height, maxRed, hueCount, redPen, i, margin);
            DrawLine(greens, width, height, maxGreen, hueCount, redPen, i, margin);
            DrawLine(blues, width, height, maxBlue, hueCount, redPen, i, margin);
        } 

    private static void DrawLine(Array colors, int width, int height, int max, 
                                 int hueCount, Pen redPen, Graphics g, int i,
                                 int margin)
    {
        g.DrawLine(redPen,
                   margin + ((float) i/(float) hueCount)*(width - 2*margin),
                   (-(float) colors[i]/(float) max)*(height - 2*margin) - margin + height,
                   margin + ((float) (i + 1)/(float) hueCount)*(width - 2*margin),
                   (-(float) colors[i + 1]/(float) max)*(height - 2*margin) - margin + height);
    }
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.