Arjuna Aircraft Ident.: F-MBSD | Voici un exemple de ce que ça donne avec ma méthode, et donc des objets dont le centre de gravité n'est pas vraiment évident si on se contente des bounds de l'objet.
Mes objets mesurés :
Le résultat :
Le source (C#) :
Code :
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Gravite
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Cursor.Current = Cursors.WaitCursor;
- this.SuspendLayout();
- // Chargement des 3 images d'exemple
- // Puis recherche du centre de gravite
- // Et affichage
- for (int i = 0; i < 3; i++)
- {
- Bitmap myBmp = new Bitmap(string.Format(@"c:\object{0}.PNG", i.ToString()));
- long cumX = 0, cumY = 0, nbPix = 0;
- int moyX = 0, moyY = 0;
- for (int x = 0; x < myBmp.Width; x++)
- {
- for (int y = 0; y < myBmp.Height; y++)
- {
- if (myBmp.GetPixel(x, y).R < 127 || myBmp.GetPixel(x, y).G < 127 || myBmp.GetPixel(x, y).B < 127)
- {
- cumX += x;
- cumY += y;
- nbPix++;
- }
- }
- }
- if (nbPix != 0)
- {
- moyX = (int)(cumX / nbPix);
- moyY = (int)(cumY / nbPix);
- Graphics g = Graphics.FromImage(myBmp);
- g.DrawLine(Pens.Red, moyX, moyY - 5, moyX, moyY + 5);
- g.DrawLine(Pens.Red, moyX - 5, moyY, moyX + 5, moyY);
- }
- PictureBox tmpPictureBox = new PictureBox();
- tmpPictureBox.Image = myBmp;
- tmpPictureBox.Size = tmpPictureBox.Image.Size;
- this.Controls.Add(tmpPictureBox);
- tmpPictureBox.Location = new Point((tmpPictureBox.Image.Size.Width + 4) * i + 4, 4);
- }
- this.ResumeLayout();
- Cursor.Current = Cursors.Default;
- }
- }
- }
|
|