Click here to Skip to main content
15,881,204 members
Articles / Desktop Programming / WPF

PolyStar - WPF Polygons and Stars

Rate me:
Please Sign up or sign in to vote.
4.88/5 (32 votes)
27 Sep 2008CPOL4 min read 82.1K   2K   44  
Control and tool for the creation of various stars and polygons.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using PointTransformations;
using System.Diagnostics;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;

namespace PolygonImageLib
{
    public enum EShapeType
    {
        Polygon,
        Star

    }
    public class PolyMatrixBuilder 
    {

      private  int mSides ;
      private  double mRadius;
      private  double mInnerRadius;
      private EShapeType mShapeType;
      bool mIsAngularlyOffset;
      private double mAngularOffset ;
      private bool mIsBeveled;
      private double mBevelOffset;

      public bool IsBeveled
      {
          get { return mIsBeveled; }
          set
          {
              mIsBeveled = value;
          }

      }
      public int Sides
      {
          get { return mSides; }
          set
          {
              mSides = value;
          }
      }


      public double Radius
      {
          get { return mRadius; }
          set
          {
              mRadius = value;
          }

      }

      public double BevelOffset
      {
          get { return mBevelOffset; }
          set
          {
              mBevelOffset = value;
          }

      }

      public bool IsAngularlyOffset
      {
          get { return mIsAngularlyOffset; }
          set { mIsAngularlyOffset = value;
          }

      }

      public double AngularOffset
      {
          get { return mAngularOffset; }
          set
          {
              mAngularOffset = value;
          }

      }


      public EShapeType ShapeType
      {
          get { return mShapeType; }
          set
          {
              mShapeType = value;
          }

      }
        public double InnerRadius
        {
            get { return mInnerRadius; }
            set
            {
                mInnerRadius = value;

            }


        }

        public Matrix GeneratePolygon()
        {


            if (mShapeType == EShapeType.Polygon)
            {
                Matrix mat = new Matrix(2);
                for (int i = 0; i < mSides; i++)
                {
                    double x = mRadius * Math.Cos((2 * Math.PI / mSides) * i);
                    double y = mRadius * Math.Sin((2 * Math.PI / mSides) * i);
                    mat.AddRow(new Double[] { x, y });
                }


                if (mIsBeveled)
                {
                    Matrix bevelmat = (Matrix)mat.Clone();
                    PointTransformations.RotationalTransformation rot = new RotationalTransformation() { Matrix = bevelmat };
                    rot.Matrix = bevelmat;
                    rot.RotateDegreesAroundOrigin(mBevelOffset);
                    mat.Interleave(bevelmat, false);
                    rot.Matrix = mat;
                    rot.RotateDegreesAroundOrigin(-mBevelOffset / 2);
                }


                return mat;
            }
            else
            {
                
               
                Matrix outerMat = new Matrix(2);
                for (int i = 0; i < mSides; i++)
                {
                    double x = mRadius * Math.Cos((2 * Math.PI / mSides) * i);
                    double y = mRadius * Math.Sin((2 * Math.PI / mSides) * i);
                    outerMat.AddRow(new Double[] { x, y });
                }

                Matrix innerMat = (Matrix)outerMat.Clone();
                innerMat.Multiply(mInnerRadius / mRadius); // Will work because centered about origin
                PointTransformations.RotationalTransformation rot = new RotationalTransformation() { Matrix = innerMat };
                rot.RotateRadiansAroundOrigin(Math.PI / mSides); //Circle is 2PI radians 
                if (mIsAngularlyOffset)
                {
                    rot.RotateDegreesAroundOrigin(mAngularOffset);
                }
                outerMat.Interleave(innerMat, false);

                if (mIsBeveled )
                {
                    Matrix bevelmat = (Matrix)outerMat.Clone();
                    rot.Matrix = bevelmat;
                    rot.RotateDegreesAroundOrigin(mBevelOffset);
                    outerMat.Interleave(bevelmat, false);
                    rot.Matrix = outerMat;
                    rot.RotateDegreesAroundOrigin(-mBevelOffset / 2);//Remove appearance of rotation 
                }

                return outerMat;

            }
        }
 


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Written software for what seems like forever. I'm currenly infatuated with WPF. Hopefully my affections are returned.

Comments and Discussions