Search code examples
c#arrays.netemgucvdrawrectangle

Numbering coordinates and drawstring from left to right


I'm trying to number blobs using the rectangle COG of objects. But the rectangles are in an elliptical path, hence I'm not getting it in a proper sequence.

Font annotationFont = new Font("Verdana", 12, FontStyle.Bold);
Pen annotationPen = new Pen(Color.FromName("White"), 2.5f);

Graphics g = imageBoxMain.CreateGraphics();
for (int i = 0; i < totalrectcount; i++)
{
    Rectangle rect = new Rectangle(arrayX[i] /* blobid[i].name.Length * 6)*/, imageBoxMain.Image.Height - arrayminY[i]- 6, 100, 20);

    g.DrawString(Convert.ToString(i + 1), annotationFont, annotationPen.Brush, new System.Drawing.Point(rect.X, rect.Y));
}

This is what im getting :

enter image description here

I want the rectangles to be labelled from left to right.


Solution

  • So you have two arrays, one for your X and one for your Y?

    //far left is number 1, far right is 2, middle is 3
    var arrayX = new[] { 100, 300, 200 };
    var arrayY = new[] { 100, 95, 130 };
    

    That's gonna be a pain in the ass; convert them to a single array of Point first, then sort them, then draw them:

    var points = new Point[arrayX.Length];
    for(int x = 0; x<points.Length; x++){
      points[x] = new Point(arrayX[x], arrayY[x]);
    }
    
    foreach(Point r in points.OrderBy(p=>p.X)){
      Rectangle rect = new Rectangle(r.X /* blobid[i].name.Length * 6)*/, imageBoxMain.Image.Height - r.Y - 6, 100, 20);
      g.DrawString(Convert.ToString(i + 1), annotationFont, annotationPen.Brush, new System.Drawing.Point(rect.X, rect.Y));
    }