hello,
i'm making a random graph generator
the coordonates are loaded from a file
i've managed to draw the points but can't manage to draw the lines between these points
here's the code

private void button2_Click(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
            gr = pictureBox1.CreateGraphics();
            SolidBrush redBrush = new SolidBrush(Color.Red);
            int p1h = pictureBox1.Size.Height;
            int p1w = pictureBox1.Size.Width;
        
            gr.DrawLine(Pens.Black, 0, p1h / 2, p1h, p1w / 2);
            gr.DrawLine(Pens.Black, p1h / 2, 0, p1h / 2, p1w);
            gr.DrawLine(Pens.Black, p1h / 2, 0, p1h / 2 - 5, 10);
            gr.DrawLine(Pens.Black, p1h / 2, 0, p1h / 2 + 5, 10);
            gr.DrawLine(Pens.Black, p1w, p1h / 2, p1h - 10, p1w / 2 + 5);
            gr.DrawLine(Pens.Black, p1w, p1h / 2, p1h - 10, p1w / 2 - 5);

            
            string filePath = path + @"\coordonate.txt";
            string line;
            
            char[] separatori = new char[] { ' ', '\n' };
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            while ((line = sr.ReadLine()) != null)
            {
                
                for (int i1 = 0; i1 < line.Length-1; i1++)
                {

                    coord[i1] = line.Split(separatori);
                    for (int i = 0; i < coord[i1].Length; i++)
                    {
                        float x1 = (float)Convert.ToDouble(coord[i1][0]);
                        float y1 = (float)Convert.ToDouble(coord[i1][1]);
                        gr.FillEllipse(redBrush, x1 * p1h, y1 * p1h, 5, 5);
                        gr.DrawLine(Pens.Red, ...............);
                    }
                }
            }
            sr.Close();
        }

the file looks like this (all numbers are random)

-0.97 -0.58
1.00 0.21
0.97 0.59
-0.46 -0.20
-0.17 -0.10
0.59 0.78
0.16 0.43
-0.12 0.01
0.17 0.28
-0.04 -0.53
-0.51 -0.82
0.18 0.49
-0.69 0.61
0.68 -0.32
0.97 -0.30
0.79 -0.44
-0.60 0.16
0.15 -0.81
0.80 -0.60

when i run this code on this line

coord[i1] = line.Split(separatori);

i get : Object reference not set to an instance of an object.
thanks

Edited 5 Years Ago by darkseid: n/a

now i get the same error on

float x1 = (float)Convert.ToDouble(coord[i1][0]);
string[][] coord = new string[1000][];

Edited 5 Years Ago by darkseid: n/a

You defined a 2D array of strings, but one side is empty(has no smemory allocated, hence: ERROR.
Why not use an array of PointF structures? Look it up on MSDN.

thanks for the help . it works
the problem i have now is that i have negative numbers and some point arent drawn inside of the picturebox.
how could i "move" the 0,0 of the picturebox from the corner to the center of the picturebox?

thank you but i found a easier way
value * picturebox.heigth/2 + picturebox.heigth/2
and, to my suprise, it works :D

now .. the last problem .. how do i sort the values from the file? cause it doesnt look at all like a graph . more like a star or something :p
i'm thinking to sort by their sum (x+y) but how

Drop the doubles in an array or better yet, a List and call the Sort method of the Array or List.

This article has been dead for over six months. Start a new discussion instead.