I'm trying to get on a professor's research project. He told me that at my level the best way to get involved is to help program this visual simulation thing for him. So I read through the first 11 chapters (Part 1 "the C# language") of Professional C# 3e by Simon Robinson et. I know I should finish the book, but I'm not a fast reader. I just wonder if anyone can tell me the specific topics I should read to get on this task, of which I will give the description below:

It's a form application which contains a button. The event handler upon click creates a 200*200-element (custom-developed GraphPane objects) color map, whose color distribution (ie element distribution) is at the present hard coded into the event handler. The color map can zoom in and out much like in autocad (I haven't read through the supporting classes yet, mostly because of my lack of knowledge). THE TASK IS TO ENABLE THE USER TO CUSTOMIZE THE COLORS ON THE DISPLAY.

I hope I've described enough for an expert to suggest specific reading.

I also have two additional questions:
1. My conception is that if I can identify the methods that enable the users to zoom in and out by pulling rectangular selection boxes, I may be able to build my "color custimization" method(s) on them. however I have no idea how to enable color selection. Should I make a color bar on the side with each color patch being a small button (is this even possible?) whose event handler turns the selected region into its color?

2. I simply don't understand the difference between two arrays whose indexing are marked [][] and [ , ] repectively. I probably overlooked a section in my reading because it seems to be an elementary concept. Here is an example code. My questions are written as comments

public void SetXY(double[] x, double[] y)
        {
            hBeta = y;
            hAlpha = new double[hBeta.Length][];
            for (int i = 0; i < hBeta.Length; i++)
                hAlpha[i] = x; //does this mean "for (0 to x.length) hAlpha[i][k]=x[k]"?
            values = new double[hBeta.Length][];
            for (int i = 0; i < hBeta.Length; i++)
                values[i] = new double[x.Length];
        }

and the SetXYZ:

public void SetXYZ(double[] x, double[] y, double[,] z)
        {
            if (x.Length != z.GetLength(0) || y.Length != z.GetLength(1)) 
            // does z.GetLength(0) mean the length of the array at index 0 of z[ , ]? if so, does this mean z[ , ]
            // is a 2d rectangular array , as opposed to a SQUARE array? does [][] indicate a SQUARE array?
            // is that the difference between [][] and [ , ]
                throw new Exception("x.Length * y.Length is not equal to z.GetLength(0)*z.GetLength(1)");

            SetXY(x, y);


            values = new double[hBeta.Length][]; 
            for (int i = 0; i < hBeta.Length; i++)
                values[i] = new double[x.Length];

            for (int i = 0; i < x.Length; i++)
                for (int j = 0; j < y.Length; j++)
                    values[j][i] = z[i, j];
        }

Recommended Answers

All 7 Replies

btw : your code will not compile...

How so though? I copied them from a working program.

>>2. I simply don't understand the difference between two arrays whose indexing are marked [][] and [ , ] repectively. I probably overlooked a section in my reading because it seems to be an elementary concept. Here is an example code. My questions are written as comments int[,] : Multi-dimensional array int[][] : Jagged array

commented: Concise answer! +6

int[,] : Multi-dimensional array int[][] : Jagged array

Thanks! I looked them up and understand the difference now. However from the examples I saw I couldn't figure out which way do the indices count... is it [colNum, rowNum] (just like "x, y") or the other way?

I meant, if hBeta is never defined somewhere else, it will not compile.
Array are usually by row and then by column.

Thank both of you and merry Christmas!! I still have a lot to learn before I can actually build this thing up but I'll mark it as solved for now. Afterall I think I'm in good company!

All right, I think finally I can ask this question in a more specific way.
Again, the main entry is a form application with a button, whose event handler defines three arrays, x, y and z, like the following:

for (int j = 0; j < y.Length; j++)
            {
                y[j] = j;//sets each element of y to its index
                for (int i = 0; i < x.Length; i++)
                {
                    x[i] = i+ i * i / 10.0 ;
                    z[j, i] = Math.Sin(2.3 * 2 * Math.PI * i / (x.Length - 1.0)) * (j - y.Length / 3) * (j - y.Length / 2);
                    //accounts for the color pattern that indeed look like parts of sinusoidal functions
                    //by determine the value on the one dimensional color bar based on the indeces i and j.
                   
                }
            }

X and y are the calibration of the corresponding coordinates, and z[j,i] specifies the index that will be used to define the color at j,i.The event handler then calls two methods on a ColorBarControl (custom class) object cp:

cp.SetXYZ(x, y, z);
            cp.AxisChange();

These two methods will together build a dialog that contains a nearly square color pane as specified by x, y, and z (the color patterns do have the shape of sinusoidal functions), and a color bar that contains the current spectrum of colof on the right of this square.SetXYZ sets x, y, and z to hAlpha[][], hBeta[], and values[,] (hAlpha in this case is just repeated x arrays put into one). Both parts are created in a similar fashion inside “AxisChange”. Here is the one tha created the color pane:

private Bitmap MakeBMPObjectDiscrete(RectangleF rec)//produces the actual color pattern (the square one on the left).
        {
            int dimXBMP = 1000;
            int dimYBMP = 1000;//dimension of the bmp 
            double curX, curY;
            Bitmap bmp = new Bitmap(dimXBMP, dimYBMP);//”canvas”
            Graphics g = Graphics.FromImage(bmp); //pallet
            Brush brushTransparent = new SolidBrush(Color.Transparent);
            int ny = hBeta.Length;
            for (int j = 0; j < ny; j++)
            {
                
                //the calculation of tmpX, tmpY, sqWidth1 etc are omitted. They simply provide parameters for FillRectange to draw the patch of color at j,i, as dictated by values[j][i]
                        if (double.IsNaN(values[j][i]))
                        {
                            g.FillRectangle(brushTransparent,
                                tmpX - sqWidth1,
                                tmpY - Math.Abs(sqHeight2),
                                sqWidth1 + Math.Abs(sqWidth2),
                                Math.Abs(sqHeight1 + sqHeight2));
                            continue;
                        }

                        //linear scale
                        double cc = (colMax - colMin) / (colorMap.brushes.Length - 1.0); 
                        //the fraction of the current color range to the biggest allowed?
                        int index = (int)((values[j][i] - colMin) / cc);
                        //scale the difference between the color at j,i and colMin to the whole range of the colorMap?

                        //log scale
                        if (colorBarPane.Y2Axis.Scale.Type == AxisType.Log)
                            index = (int)((colorMap.brushes.Length - 1) / Math.Log(colMax / colMin) * Math.Log(values[j][i] / colMin));

                        if (index < 0)
                            index = 0;
                        if (index >= colorMap.brushes.Length)
                            index = colorMap.brushes.Length - 1;
                        Brush brush = colorMap.brushes[index]; //brush set to an index produced by values[j][i]


                        g.FillRectangle(brush,
                            tmpX - sqWidth1,
                            tmpY - Math.Abs(sqHeight2),
                            sqWidth1 + Math.Abs(sqWidth2),
                            Math.Abs(sqHeight1 + sqHeight2)); //(brushing)
                    }
                }
            }

            //bmp.Save("c:\\a.png");
            return bmp;
        }

However I still haven't discovered anything that even accounts for the rectangular zooming box, leaving alone trying to use the code for the selection before customizing a patch of color. However there is a “protected override void OnLoad(EventArgs e)” that have some very suspect code.

this.KeyDown += delegate(object sender1, KeyEventArgs e1)
            {
                if (e1.KeyCode == Keys.Escape)
                    if (MasterPane != null)
                    {
                        PointF mouse = new PointF(MousePosition.X, MousePosition.Y);
                        ZoomOut(pane);
                    }
            }; //this one is definitely the “zoom out” action

I still need to locate the code for the zoom-in action. Alas, the original code came with nearly 0 commenting, so I have no idea even where to look...
Nontheless my question is: after I zoom in to a certain level, how can I modify the values [,] items that CORRESPOND to what’s DISPLAYED on the color pane—to turn them into the same number—and then reflect the change through the graph?
Please let me know if any additional info is needed!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.