0

I have a problem with the function atan2.

Actually, I'm trying to connect the lines to look symmetric.

Currently it looks like this:

pic1 pic2

The calculation:

    CRect rectCurrent = pCurrent->GetRect();
    CRect rectNext = pNext>GetRect();

    CPoint ptLineStart = rectCurrent.CenterPoint();
    CPoint ptLineEnd = rectNext.CenterPoint();

    FLOAT fRadian = ((atan2(ptLineEnd.y - ptLineStart.y, ptLineEnd.x - ptLineStart.x)));
    {
        FLOAT fScaleX = sqrt(pow(ptLineStart.x - ptLineEnd.x, 2) + pow(ptLineStart.y - ptLineEnd.y, 2));
        RenderTextureRotate(ptLineStart, pTexture, 255, fScaleX / pTexture->GetSize().cx, 1.0f, fRadian);
    }

The function RenderTextureRotate:

void RenderTextureRotate(_In_ CPoint pt, _Inout_ CMTexture* pTexture, _In_ CONST DWORD dwBlendFactor, _In_ FLOAT fScaleX, _In_ FLOAT fScaleY, _In_ FLOAT fRadian)
{
    if (!pTexture)
        return;

    // scalling
    fScaleX = (pTexture->GetSize().cx * fScaleX);
    fScaleY = (pTexture->GetSize().cy * fScaleY);
    // set the texture FLOAT rect
    S_fRect rct = { pt.x, pt.y, pt.x + fScaleX, pt.y + fScaleY };

    D3DXVECTOR2 v1, v2, v3, v4;
    D3DXMATRIX  mRot;

    // set up 2d vectors for rotation
    v1.x = 0;   
    v1.y = 0;
    v2.x = fScaleX;
    v2.y = 0;
    v3.x = 0;   
    v3.y = fScaleY;
    v4.x = fScaleX;
    v4.y = fScaleY;

    // rotation in radians
    D3DXMatrixRotationZ(&mRot, fRadian);
    D3DXVec2TransformCoord(&v1, &v1, &mRot);
    D3DXVec2TransformCoord(&v2, &v2, &mRot);
    D3DXVec2TransformCoord(&v3, &v3, &mRot);
    D3DXVec2TransformCoord(&v4, &v4, &mRot);

    // set texture vertex
    sTEXTUREVERTEX vertex[4];
    SetTextureVertex((&vertex[0]), rct.fLeft + v1.x, rct.fTop + v1.y, pTexture->GetCoordinates().fuLT, pTexture->GetCoordinates().fvLT);
    SetTextureVertex((&vertex[1]), rct.fLeft + v2.x, rct.fTop + v2.y, pTexture->GetCoordinates().fuRT, pTexture->GetCoordinates().fvRT);
    SetTextureVertex((&vertex[2]), rct.fLeft + v3.x, rct.fTop + v3.y, pTexture->GetCoordinates().fuLB, pTexture->GetCoordinates().fvLB);
    SetTextureVertex((&vertex[3]), rct.fLeft + v4.x, rct.fTop + v4.y, pTexture->GetCoordinates().fuRB, pTexture->GetCoordinates().fvRB);

    // prepare device for texture rendering just small things like blendfactor.
    PREPARE_DEVICE_TEXTURRENDER(m_pD3DDevice);

    // set texture
    m_pD3DDevice->SetTexture(0, pTexture->GetDXTexture());
    // set FVF
    m_pD3DDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1);
    // draw
    m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vertex, sizeof(sTEXTUREVERTEX));
    // reset texture.
    m_pD3DDevice->SetTexture(0, NULL);
}

S_fRect struct:

struct S_fRect
{
    FLOAT fLeft;
    FLOAT fTop;
    FLOAT fRight;
    FLOAT fBottom;
};

What am I doing wrong?

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
Cucy
  • 1
  • 1
  • You should rotate the rectangles about some fix point on the line. Not about the top left corner. At least, offset the y-coordinates of `v1` thru `v4` (before transformation) by `-fScaleY / 2.0f`. – Nico Schertler Dec 15 '15 at 12:02
  • okay thank you, it works now. – Cucy Dec 15 '15 at 13:35

0 Answers0