矩阵缩放和投影

缩放在目前代码只有3D的,2D自己想想(╯▽╰)吧

投影矩阵公式: 3D

wps65B.tmp

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
    class Matrix3x3 
    {
        public double m11, m12, m13;
        public double m21, m22, m23;
        public double m31, m32, m33;


        #region 缩放
        public static Matrix3x3 setUpScale(float Kx, float Ky, float Kz)
        {
            Matrix3x3 m = new Matrix3x3();
            m.m11 = Kx; m.m12 = 0; m.m13 = 0;
            m.m11 = 0; m.m12 = Ky; m.m13 = 0;
            m.m11 = 0; m.m12 = 0; m.m13 = Kz;
            return m;
        }

        public static Matrix3x3 setUpScale(Vector3 v)
        {
            Matrix3x3 m = new Matrix3x3();
            m.m11 = v.x; m.m12 = 0; m.m13 = 0;
            m.m11 = 0; m.m12 = v.y; m.m13 = 0;
            m.m11 = 0; m.m12 = 0; m.m13 = v.z;
            return m;
        }
        #endregion

        #region 正交投影

        /// <summary>
        /// 投影到xy,xz,yz其中一个平面
        /// </summary>
        /// <param name="axis">1=xy,2=xz,3=yz</param>
        public void setUpProject(int axis) 
        {
            switch (axis)
            {
                case 1:
                    //xy平面
                       m11 = 1; m12 = 0; m13 =0;
                    m21 = 0; m22 = 1; m23 =0;
                    m31 = 0; m32 = 0; m33 =0;
                    break;
                case 2:
                    //xz平面
                        m11 = 1; m12 = 0; m13 =0;
                    m21 = 0; m22 = 0; m23 =0;
                    m31 = 0; m32 = 0; m33 =1;
                    break;
                case 3:
                    //yz平面
                       m11 = 0; m12 = 0; m13 =0;
                    m21 = 0; m22 = 1; m23 =0;
                    m31 = 0; m32 = 0; m33 =1;
                    break;
            }
        }

        //投影到任意一个平面
        public void setUpProject(Vector3 v) 
        {
            m11 = 1 - v.x * v.x;
            m22 = 1 - v.y * v.y;
            m33 = 1 - v.z * v.z;

            m12 = m21 = -v.x * v.y;
            m13 = m31 = -v.x * v.z;
            m23 = m32 = -v.y * v.z;
        }
        #endregion
    }

    class Vector3 
    {
        public double x, y, z;

        public Vector3(double x, double y, double z) 
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2}", x, y, z);
        }
    }
                                                                
}
posted @ 2015-04-26 22:10  盘子脸  阅读(600)  评论(0编辑  收藏  举报