Ejemplo n.º 1
0
        internal Affine(AffinePlan[] creationPlans)
        {
            //-----------------------
            //start with identity matrix

            sx  = 1;
            shy = 0;
            shx = 0;
            sy  = 1;
            tx  = 0;
            ty  = 0;
            //-----------------------
            int j = creationPlans.Length;

            for (int i = 0; i < j; ++i)
            {
                AffinePlan plan = creationPlans[i];
                switch (plan.cmd)
                {
                case AffineMatrixCommand.None:
                    break;

                case AffineMatrixCommand.Rotate:
                {
                    double angleRad = plan.x;
                    double ca       = Math.Cos(angleRad);
                    double sa       = Math.Sin(angleRad);
                    double t0       = sx * ca - shy * sa;
                    double t2       = shx * ca - sy * sa;
                    double t4       = tx * ca - ty * sa;
                    shy = sx * sa + shy * ca;
                    sy  = shx * sa + sy * ca;
                    ty  = tx * sa + ty * ca;
                    sx  = t0;
                    shx = t2;
                    tx  = t4;
                }
                break;

                case AffineMatrixCommand.Scale:
                {
                    double mm0 = plan.x;
                    double mm3 = plan.y;
                    sx  *= mm0;
                    shx *= mm0;
                    tx  *= mm0;
                    shy *= mm3;
                    sy  *= mm3;
                    ty  *= mm3;
                }
                break;

                case AffineMatrixCommand.Translate:
                {
                    tx += plan.x;
                    ty += plan.y;
                }
                break;

                case AffineMatrixCommand.Skew:
                {
                    double m_sx  = 1;
                    double m_sy  = 1;
                    double m_shx = Math.Tan(plan.x);
                    double m_shy = Math.Tan(plan.y);
                    double t0    = sx * m_sx + shy * m_shx;
                    double t2    = shx * m_sx + sy * m_shx;
                    double t4    = tx * m_sx + ty * m_shx + 0;     //0=m.tx
                    shy = sx * m_shy + shy * m_sy;
                    sy  = shx * m_shy + sy * m_sy;
                    ty  = tx * m_shy + ty * m_sy + 0;       //0= m.ty;
                    sx  = t0;
                    shx = t2;
                    tx  = t4;
                }
                break;

                case AffineMatrixCommand.Invert:
                {
                    double d  = CalculateDeterminantReciprocal();
                    double t0 = sy * d;
                    sy  = sx * d;
                    shy = -shy * d;
                    shx = -shx * d;
                    double t4 = -tx * t0 - ty * shx;
                    ty = -tx * shy - ty * sy;
                    sx = t0;
                    tx = t4;
                }
                break;

                default:
                {
                    throw new NotSupportedException();
                }
                }
            }
        }
Ejemplo n.º 2
0
        private Affine(Affine copyFrom, AffinePlan creationPlan)
        {
            //-----------------------
            sx  = copyFrom.sx;
            shy = copyFrom.shy;
            shx = copyFrom.shx;
            sy  = copyFrom.sy;
            tx  = copyFrom.tx;
            ty  = copyFrom.ty;
            //-----------------------
            switch (creationPlan.cmd)
            {
            default:
            {
                throw new NotSupportedException();
            }

            case AffineMatrixCommand.None:
                break;

            case AffineMatrixCommand.Rotate:
            {
                double angleRad = creationPlan.x;
                double ca       = Math.Cos(angleRad);
                double sa       = Math.Sin(angleRad);
                double t0       = sx * ca - shy * sa;
                double t2       = shx * ca - sy * sa;
                double t4       = tx * ca - ty * sa;
                shy = sx * sa + shy * ca;
                sy  = shx * sa + sy * ca;
                ty  = tx * sa + ty * ca;
                sx  = t0;
                shx = t2;
                tx  = t4;
            }
            break;

            case AffineMatrixCommand.Scale:
            {
                double mm0 = creationPlan.x;
                double mm3 = creationPlan.y;
                sx  *= mm0;
                shx *= mm0;
                tx  *= mm0;
                shy *= mm3;
                sy  *= mm3;
                ty  *= mm3;
            }
            break;

            case AffineMatrixCommand.Skew:
            {
                double m_sx  = 1;
                double m_sy  = 1;
                double m_shx = Math.Tan(creationPlan.x);
                double m_shy = Math.Tan(creationPlan.y);
                double t0    = sx * m_sx + shy * m_shx;
                double t2    = shx * m_sx + sy * m_shx;
                double t4    = tx * m_sx + ty * m_shx + 0;     //0=m.tx
                shy = sx * m_shy + shy * m_sy;
                sy  = shx * m_shy + sy * m_sy;
                ty  = tx * m_shy + ty * m_sy + 0;       //0= m.ty;
                sx  = t0;
                shx = t2;
                tx  = t4;
                //return new Affine(1.0, Math.Tan(y), Math.Tan(x), 1.0, 0.0, 0.0);
            }
            break;

            case AffineMatrixCommand.Translate:
            {
                tx += creationPlan.x;
                ty += creationPlan.y;
            }
            break;

            case AffineMatrixCommand.Invert:
            {
                double d  = CalculateDeterminantReciprocal();
                double t0 = sy * d;
                sy  = sx * d;
                shy = -shy * d;
                shx = -shx * d;
                double t4 = -tx * t0 - ty * shx;
                ty = -tx * shy - ty * sy;
                sx = t0;
                tx = t4;
            }
            break;
            }
        }