【从头写CAD】 转换矩阵类

/*转换矩阵Matrix
 * 编制人: $ource
 * 修改版次:0版完成版
 * 本版次创建时间: 2024年8月16日
 * 最后修改时间: 无
 * 待完善问题:错误捕获未核实对错,double转string是否加参数
 */

 mod Number;
    // 变换矩阵 TransformMatrix2D
    //  sc rx dx       Xx  Yx  Dx
    //  ry sy dy       Xy  Yy  Dy
    //  0  0  1        0   0   1
    #[derive(Debug,PartialEq,PartialOrd,Clone,Copy)]
    struct Matrix2D{
        Xx:Number, Yx:Number, Zx:Number, Dx:Number, 
        Xy:Number, Yy:Number, Zy:Number, Dy:Number, 
        Xz:Number, Yz:Number, Zz:Number, Dz:Number
    }
    
    impl Matrix2D{
        //构造函数:()//单位矩阵,(Rectangle,Point[]),(RectangleF,PointF[]),(sx,ry,rx,sy,dx,dy)//取自单词delta Scale shear,(origin,xAxis,yAxis)
        fn new(dx:Number,dy:Number,sx:Number, ry:Number, rx:Number, sy:Number)->Matrix2D{
            Matrix2D{
                Xx:sx,Xy:ry,
                Yx:rx,Yy:sy,
                Dx:dx,Dy:dy
            }
        }

        fn from(origin:Vector2D, xAxis:Vector2D , yAxis:Vector2D)->Matrix2D{
            Matrix2D{
            Xx:xAxis.x, Xy:xAxis.y,
            Yx:yAxis.x, Yy:yAxis.y,
            Dx:origin.x, Dy:origin.y
            }
        }

        //属性
        fn origin()->Vector2D{
            Vector2D(Dx,Dy)
        }

        fn set_origin(v:Vector2D){
            Dx = v.X();
            Dy = v.Y();
        }

        fn x_axis()->Vector2D{
            Vector2D(Xx,Xy)
        }

        fn set_x_axis(v:Vector2D){
            Xx = v.x.m;
            Xy = v.y.m;
        }

        fn y_axis()->Vector2D{
            Vector2D(Yx, Yy)
        }

        fn set_y_axis(v:Vector2D){
            Yx = v.X().m;
            Yy = v.Y().m;
        }
        fn ReversibleMatrix()->Option<Matrix2D>{//返回逆矩阵Invert
            let temp:f64 = Xx * Yy - Xy * Yx;
            if (temp == 0){
                Option::None;
            }else{
                Some(sMatrix2D(
                    Yy / temp                 ,   -Xy / temp,
                    -Yx / temp                ,    Xx / temp,
                    (Dy * Yx - Dx * Yy) / temp,   (Dx * Xy - Dy * Xx)/ temp
                ))
            }
        }
        fn is_reversible()->bool{//是否为可逆矩阵
            return Xx * Yy - Xy * Yx != 0.0;
        }
    }

    impl Mul for Matrix2D{
        fn mul(&self, tm: Self) -> Matrix2D {
            Matrix2D{
                Xx * tm.Xx + Xy * tm.Yx        ,    Xx * tm.Xy + Xy * tm.Yy,
                Yx * tm.Xx + Yy * tm.Yx        ,    Yx * tm.Xy + Yy * tm.Yy,
                Dx * tm.Xx + Dy * tm.Yx + tm.Dx,    Dx * tm.Xy + Dy * tm.Yy + tm.Dy
            }
        }
    }
    
    // impl Div for Matrix2D{
    //     fn div(&self, other: f64) -> Matrix2D {
    //         Matrix2D{*self * other.ReversibleMatrix()}
    //     }
    // }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值