/NerdSharp.Net_Studio/NerdSharp_UberNet/Science/Comp_Sci/Shapes/ThreeDShapes/ThreeDPolygon.cs
C# | 195 lines | 146 code | 21 blank | 28 comment | 8 complexity | ad9988908d08820b7bcc3701b6e63af3 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Media.Media3D;
- using Geom = NerdSharp.Net_Studio.NerdSharp_UberNet.Science.Math.Utils.Geometry;
-
- namespace NerdSharp.Net_Studio.NerdSharp_UberNet.Science.Comp_Sci.Shapes.ThreeDShapes
- {
- public class ThreeDPolygon
- {
- [Flags]
- public enum PolygonNames
- {
- triangle = 3, square, pentagon, hexagon, octagon,
- nonagon, decagon, unidecagon, dodecagon, tridecagon, circle = 100
- }
-
- #region instance vars
- private List<Triangle> triangles = null;
-
- internal List<Triangle> Triangles
- {
- get { return triangles; }
- set { triangles = value; }
- }
-
- private List<Triangle> outterTriangles = null;
-
- private int sides = 3;
-
- public int Sides
- {
- get { return sides; }
- set
- {
- if (value >= 3)
- {
- sides = value;
- }
- }
- }
-
- private Brush brush = null;
-
- public Brush Brush
- {
- get { return brush; }
- set { brush = value; }
- }
-
- private double sideLength = 1d;
-
- public double SideLength
- {
- get { return sideLength; }
- set
- {
- if (value > 0.0d)
- {
- sideLength = value;
- }
- }
- }
-
- private string name = "";
-
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
-
- private int externalTriangles = 0;
- private double interiorAngle = 0.0d;
- private Model3DGroup mod3DGrp = null;
- private Point3D startPoint;
- private int remainingTriangles;
-
- #endregion
-
-
- public ThreeDPolygon(int sides, Brush brush, double sideLength,
- string name, Model3DGroup mod3DGrp, Point3D startPoint)
- {
- //nothing less than a triangle
- if (sides < 3)
- {
- throw new ArgumentException("Three or more shall be the number of sides of a polygon.");
- }
- //asigning instance variables
- this.startPoint = startPoint;
- this.SideLength = sideLength;
- this.Sides = sides;
- this.brush = brush;
- //total triangles always equals 2 less than the count of its sides
- triangles = new List<Triangle>(sides - 2);
- /*the number of external triangles to be rendered on first pass of the for loop
- is the sides integer divided by two*/
- externalTriangles = sides / 2;
- /* Calculates the remaining triangles that make up the exterior
- * shape. This allows iteration to render the polygon in a
- * regular manner.
- * */
- remainingTriangles = triangles.Count - externalTriangles;
- //list of the outter triangles for reference for conneting to other shapes
- outterTriangles = new List<Triangle>(externalTriangles);
- this.interiorAngle = Geom.InterriorAngle(Sides);
- this.mod3DGrp = mod3DGrp;
- //builds the polygon using the 3D library via triangles
- Add3DShape();
- }
-
- #region _3DShape Members
-
- /// <summary>
- /// Adds the shape to a Model3DGroup
- /// </summary>
- public void Add3DShape()
- {
- //local vars for 3D modeling
- MeshGeometry3D meshGeom3D = new MeshGeometry3D();
- Point3DCollection pointCol = new Point3DCollection(sides);
- //temp variable used for makking all the triangles
- Triangle einExternTriAng = null;
- //adds to lists for later references
- int iterations = 0;
- //produces the external triangles
- try
- {
- Point3D pt1 = new Point3D(startPoint.X, startPoint.Y, startPoint.Z);
- Point3D pt2 = new Point3D();
- Point3D pt3 = new Point3D();
- do
- {
- if (iterations == 0)
- {
- this.outterTriangles.Add(einExternTriAng);
- }
- for (int i = 0; i < externalTriangles; i++)
- {
- Point3DCollection extrnTrigAngPoints = new Point3DCollection();
- //calculates and adds points need for each outer triangle for the polygon
- extrnTrigAngPoints.Add(pt1);
- extrnTrigAngPoints.Add(pt2);
- extrnTrigAngPoints.Add(pt3);
-
- Int32Collection extrnTriAngVerticies = new Int32Collection();
- einExternTriAng = new Triangle
- (extrnTrigAngPoints, extrnTriAngVerticies, this.brush, meshGeom3D);
- }
- //helps keep track of do while loop iterations for keeping track of external triangles
- iterations++;
- } while (remainingTriangles >= 3);
- //iterates internal triangles until only one triangle is left to be made
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
-
- /// <summary>
- /// This adds a control of either type UserControl or (add later)
- /// to the face of the polygon. This is a hack around to make making
- /// polyhedra with 2D controls much easier.
- /// </summary>
- /// <param name="control">The control to add to the polygon</param>
- /// <returns></returns>
- public void AddControlsToShape(object control)
- {
- VisualBrush visBrsh = new VisualBrush();
- if (control is UserControl)
- {
- visBrsh.Visual = (UserControl)control;
- }
- else
- {
- throw new ArgumentException("Cannot take this type for a user control");
- }
- this.brush = visBrsh;
- }
-
- #endregion
- }
- }