欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

matrix.cpp

A*算法 A*算法 A*算法 A*算法A*算法A*算法
CPP
第 1 页 / 共 2 页
字号:
{
    Rotate(-degrees,0,0);
    return true;
}

// counter clockwise rotate around a point
//
//  cos(r) -sin(r)    x(1-cos(r))+y(sin(r)
//  sin(r)  cos(r)    y(1-cos(r))-x(sin(r)
//    0      0        1
wxTransformMatrix&  wxTransformMatrix::Rotate(const double &degrees, const double &x, const double &y)
{
    double angle = degrees * pi / 180.0;
    double c = cos(angle);
    double s = sin(angle);
    double r00,r10,r20,r01,r11,r21;

    if (m_isIdentity)
    {
        double tx  = x*(1-c)+y*s;
        double ty  = y*(1-c)-x*s;
        r00 = c ;
        r10 = -s;
        r20 = tx;
        r01 = s;
        r11 = c;
        r21 = ty;
    }
    else if (x!=0 || y!=0)
    {
        double tx  = x*(1-c)+y*s;
        double ty  = y*(1-c)-x*s;
        r00 = c * m_matrix[0][0] - s * m_matrix[0][1] + tx * m_matrix[0][2];
        r10 = c * m_matrix[1][0] - s * m_matrix[1][1] + tx * m_matrix[1][2];
        r20 = c * m_matrix[2][0] - s * m_matrix[2][1] + tx;// * m_matrix[2][2];
        r01 = c * m_matrix[0][1] + s * m_matrix[0][0] + ty * m_matrix[0][2];
        r11 = c * m_matrix[1][1] + s * m_matrix[1][0] + ty * m_matrix[1][2];
        r21 = c * m_matrix[2][1] + s * m_matrix[2][0] + ty;// * m_matrix[2][2];
    }
    else
    {
        r00 = c * m_matrix[0][0] - s * m_matrix[0][1];
        r10 = c * m_matrix[1][0] - s * m_matrix[1][1];
        r20 = c * m_matrix[2][0] - s * m_matrix[2][1];
        r01 = c * m_matrix[0][1] + s * m_matrix[0][0];
        r11 = c * m_matrix[1][1] + s * m_matrix[1][0];
        r21 = c * m_matrix[2][1] + s * m_matrix[2][0];
    }

    m_matrix[0][0] = r00;
    m_matrix[1][0] = r10;
    m_matrix[2][0] = r20;
    m_matrix[0][1] = r01;
    m_matrix[1][1] = r11;
    m_matrix[2][1] = r21;

/* or like this
    wxTransformMatrix rotate;
    rotate.m_matrix[2][0] = tx;
    rotate.m_matrix[2][1] = ty;

    rotate.m_matrix[0][0] = c;
    rotate.m_matrix[0][1] = s;

    rotate.m_matrix[1][0] = -s;
    rotate.m_matrix[1][1] = c;

   rotate.m_isIdentity=false;
   *this = rotate * (*this);
*/
    m_isIdentity = IsIdentity1();

    return *this;
}

// Transform a point from logical to device coordinates
bool wxTransformMatrix::TransformPoint(double x, double y, double& tx, double& ty) const
{
    if (IsIdentity())
    {
        tx = x; ty = y; return true;
    }

    tx = x * m_matrix[0][0] + y * m_matrix[1][0] + m_matrix[2][0];
    ty = x * m_matrix[0][1] + y * m_matrix[1][1] + m_matrix[2][1];

    return true;
}

// Transform a point from device to logical coordinates.

// Example of use:
//   wxTransformMatrix mat = dc.GetTransformation();
//   mat.Invert();
//   mat.InverseTransformPoint(x, y, x1, y1);
// OR (shorthand:)
//   dc.LogicalToDevice(x, y, x1, y1);
// The latter is slightly less efficient if we're doing several
// conversions, since the matrix is inverted several times.
bool wxTransformMatrix::InverseTransformPoint(double x, double y, double& tx, double& ty) const
{
    if (IsIdentity())
    {
        tx = x; ty = y; return true;
    }

    double z = (1.0 - m_matrix[0][2] * x - m_matrix[1][2] * y) / m_matrix[2][2];
    if (z == 0.0)
    {
//      z = 0.0000001;
        return false;
    }
    tx = x * m_matrix[0][0] + y * m_matrix[1][0] + z * m_matrix[2][0];
    ty = x * m_matrix[0][1] + y * m_matrix[1][1] + z * m_matrix[2][1];
    return true;
}

wxTransformMatrix& wxTransformMatrix::operator*=(const double& t)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            m_matrix[i][j]*= t;
    m_isIdentity = IsIdentity1();
    return *this;
}

wxTransformMatrix& wxTransformMatrix::operator/=(const double& t)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            m_matrix[i][j]/= t;
    m_isIdentity = IsIdentity1();
    return *this;
}

wxTransformMatrix& wxTransformMatrix::operator+=(const wxTransformMatrix& mat)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            m_matrix[i][j] += mat.m_matrix[i][j];
    m_isIdentity = IsIdentity1();
    return *this;
}

wxTransformMatrix& wxTransformMatrix::operator-=(const wxTransformMatrix& mat)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            m_matrix[i][j] -= mat.m_matrix[i][j];
    m_isIdentity = IsIdentity1();
    return *this;
}

wxTransformMatrix& wxTransformMatrix::operator*=(const wxTransformMatrix& mat)
{

    if (mat.m_isIdentity)
        return *this;
    if (m_isIdentity)
    {
        *this = mat;
        return *this;
    }
    else
    {
        wxTransformMatrix  result;
        for (int i = 0; i < 3; i++)
        {
           for (int j = 0; j < 3; j++)
           {
               double sum = 0;
               for (int k = 0; k < 3; k++)
                   sum += m_matrix[k][i] * mat.m_matrix[j][k];
               result.m_matrix[j][i] = sum;
           }
        }
        *this = result;
    }

    m_isIdentity = IsIdentity1();
    return *this;
}


// constant operators
wxTransformMatrix  wxTransformMatrix::operator*(const double& t) const
{
    wxTransformMatrix result = *this;
    result *= t;
    result.m_isIdentity = result.IsIdentity1();
    return result;
}

wxTransformMatrix  wxTransformMatrix::operator/(const double& t) const
{
    wxTransformMatrix result = *this;
//    wxASSERT(t!=0);
    result /= t;
    result.m_isIdentity = result.IsIdentity1();
    return result;
}

wxTransformMatrix  wxTransformMatrix::operator+(const wxTransformMatrix& m) const
{
    wxTransformMatrix result = *this;
    result += m;
    result.m_isIdentity = result.IsIdentity1();
    return result;
}

wxTransformMatrix  wxTransformMatrix::operator-(const wxTransformMatrix& m) const
{
    wxTransformMatrix result = *this;
    result -= m;
    result.m_isIdentity = result.IsIdentity1();
    return result;
}


wxTransformMatrix  wxTransformMatrix::operator*(const wxTransformMatrix& m) const
{
    wxTransformMatrix result = *this;
    result *= m;
    result.m_isIdentity = result.IsIdentity1();
    return result;
}


wxTransformMatrix  wxTransformMatrix::operator-() const
{
    wxTransformMatrix result = *this;
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            result.m_matrix[i][j] = -(this->m_matrix[i][j]);
    result.m_isIdentity = result.IsIdentity1();
    return result;
}

static double CheckInt(double getal)
{
    // check if the number is very close to an integer
    if ( (ceil(getal) - getal) < 0.0001)
        return ceil(getal);

    else if ( (getal - floor(getal)) < 0.0001)
        return floor(getal);

    return getal;

}

double wxTransformMatrix::Get_scaleX()
{
    double scale_factor;
    double rot_angle = CheckInt(atan2(m_matrix[1][0],m_matrix[0][0])*180/pi);
    if (rot_angle != 90 && rot_angle != -90)
        scale_factor = m_matrix[0][0]/cos((rot_angle/180)*pi);
    else
        scale_factor = m_matrix[0][0]/sin((rot_angle/180)*pi);  // er kan nl. niet door 0 gedeeld worden !

    scale_factor = CheckInt(scale_factor);
    if (scale_factor < 0)
        scale_factor = -scale_factor;

    return scale_factor;
}

double wxTransformMatrix::Get_scaleY()
{
    double scale_factor;
    double rot_angle = CheckInt(atan2(m_matrix[1][0],m_matrix[0][0])*180/pi);
    if (rot_angle != 90 && rot_angle != -90)
       scale_factor = m_matrix[1][1]/cos((rot_angle/180)*pi);
    else
       scale_factor = m_matrix[1][1]/sin((rot_angle/180)*pi);   // er kan nl. niet door 0 gedeeld worden !

    scale_factor = CheckInt(scale_factor);
    if (scale_factor < 0)

        scale_factor = -scale_factor;

    return scale_factor;

}

double wxTransformMatrix::GetRotation()
{
    double temp1 = GetValue(0,0);   // for angle calculation
    double temp2 = GetValue(0,1);   //

    // Rotation
    double rot_angle = atan2(temp2,temp1)*180/pi;

    rot_angle = CheckInt(rot_angle);
    return rot_angle;
}

void wxTransformMatrix::SetRotation(double rotation)
{
    double x=GetValue(2,0);
    double y=GetValue(2,1);
    Rotate(-GetRotation(), x, y);
    Rotate(rotation, x, y);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -