Click here to Skip to main content
15,888,579 members
Articles / Desktop Programming / WPF

XAML polygon transformation tool

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
26 Aug 2008CPOL3 min read 34.2K   594   18  
A tool to automate flipping, shifting, rotating, and scaling polygons and polylines.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;

namespace PolyTransform
{
    public class FlipTransformation : INotifyPropertyChanged, PolyTransform.ITransformation
    {
        private bool mFlipX;
        private bool mFlipY;
        private double mXAxis;
        private double mYAxis;

        private bool mIsXAxisCentered = true;
        private bool mIsYAxisCentered = true;

        private Matrix mMatrix;
        public Matrix Matrix
        {

            get { return mMatrix; }
            set { mMatrix = value; }
        }


        public bool IsXAxisCentered
        {
            get { return mIsXAxisCentered; }
            set
            {
                mIsXAxisCentered = value;
                NotifyPropertyChanged("IsXAxisCentered");
            }

        }


        public bool IsYAxisCentered
        {
            get { return mIsYAxisCentered; }
            set
            {
                mIsYAxisCentered = value;
                NotifyPropertyChanged("IsYAxisCentered");
            }

        }


        public double XAxis
        {
            get { return mXAxis; }
            set
            {
                mXAxis = value;
                NotifyPropertyChanged("XAxis");
            }

        }


        public double YAxis
        {
            get { return mYAxis; }
            set
            {
                mYAxis = value;
                NotifyPropertyChanged("YAxis");
            }

        }

        public bool FlipX
        {
            get { return mFlipX; }
            set
            {
                mFlipX = value;
                NotifyPropertyChanged("FlipX");
            }

        }

        public bool FlipY
        {
            get { return mFlipY; }
            set
            {
                mFlipY = value;
                NotifyPropertyChanged("FlipY");
            }

        }



        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }


        public void FlipColumn(int col, double axis)
        {
            mMatrix.Subtract(axis, col);
            mMatrix.Multiply(-1, col);
            mMatrix.Add(axis, col);

        }

        #endregion


        #region ITransformation Members

        public void Transform()
        {
            double axis = 0;
            if (mFlipX)
            {
                if (mIsXAxisCentered)// Is the x Axis centered
                {
                    double min = mMatrix.Min(1);
                    double max = mMatrix.Max(1);
                    axis = (min + max) / 2;
                }
                else
                {
                    axis = mXAxis;
                }
                FlipColumn(1, axis);

            }
            if (mFlipY)
            {
                if (mIsYAxisCentered) // Is the Y axis centered
                {
                    double min = mMatrix.Min(0);
                    double max = mMatrix.Max(0);
                    axis = (min + max) / 2;
                }
                else
                {
                    axis = mYAxis;
                }
                FlipColumn(0, axis);
            }
        }

        #endregion
    }
}

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