/// <summary> /// Vytvoří fyzický model tělesa jako objekt z daných vertexů /// </summary> /// <param name="Vertices">Vertexy tělesa</param> /// <param name="InitPosition">Počáteční poloha tělesa</param> /// <param name="COG">Těžiště tělesa</param> public Geometry(PointF[] Vertices,PointF InitPosition, PointF? COG) { if (Vertices == null || Vertices.Length < 3) throw new ArgumentException(); surf = vol = angle = 0; scale = 1.0f; desc = AnalyzeVertexGroup(Vertices); geom = new PointF[Vertices.Length]; Vertices.CopyTo(geom, 0); if (COG.HasValue) center = (Vector)COG; else center = (Vector)desc.Centroid; Nail = (Vector)InitPosition; Position = (Vector)InitPosition; }
/// <summary> /// Analyzuje pole vertexů jako polygon a zjistí jeho geometrický střed (centroid), šířku a výšku /// </summary> /// <param name="Vertices">Pole vertexů</param> /// <returns>Výsledek analýzy polygonu</returns> public static GeometryDescriptor AnalyzeVertexGroup(PointF[] Vertices) { if (Vertices.Length <= 1) throw new ArgumentException(); GeometryDescriptor Description = new GeometryDescriptor(Vertices); Description.FrontalArea = PolygonArea(Vertices); for (int i = 0,j = 0; i < Vertices.Length; i++) { j = (i + 1) % Vertices.Length; Description.Centroid.X += (Vertices[i].X + Vertices[j].X) * (Vertices[i].X * Vertices[j].Y - Vertices[i].Y * Vertices[j].X); Description.Centroid.Y += (Vertices[i].Y + Vertices[j].Y) * (Vertices[i].X * Vertices[j].Y - Vertices[i].Y * Vertices[j].X); } Description.ConvexHull = ConvexHull.GetConvexHull(Vertices); Description.Centroid.X /= (float)(Description.FrontalArea * 6); Description.Centroid.Y /= (float)(Description.FrontalArea * 6); using (System.Drawing.Drawing2D.GraphicsPath p = new System.Drawing.Drawing2D.GraphicsPath()) { p.AddPolygon(Vertices); RectangleF r = p.GetBounds(); Description.Width = r.Width; Description.Height = r.Height; } return Description; }
private void panel1_MouseClick(object sender, MouseEventArgs e) { if (COG != null) return; if (e.Button == System.Windows.Forms.MouseButtons.Left) { foreach (PointF p in pts) { if (p.X == e.X && p.Y == e.Y) return; } pts.Add(new PointF(e.X, e.Y)); } else if (e.Button == System.Windows.Forms.MouseButtons.Right) { COG = e.Location; try { Desc = Geometry.AnalyzeVertexGroup(pts.ToArray()); label_cCOG.Text = String.Format("Centroid: [{0:f1};{1:f1}]", Desc.Centroid.X, Desc.Centroid.Y); label_COG.Text = String.Format("Určené těžiště: [{0};{1}]", COG.Value.X, COG.Value.Y); } catch (ArgumentException) { MessageBox.Show("Neplatný počet vertexů!"); } } text_objName.Enabled = COG != null; button_selTexture.Enabled = button_SetCOG.Enabled = button_selColor.Enabled = COG != null; drawPanel.Invalidate(); }