Carlos Martinez
Carlos Martinez

Reputation: 4510

Draw a line with a constant length by mouse direction

As question says, I want to draw a line starting at X,Y position to, for example, 10 pixels on mouse direction... The function I already have draws a line between 2 points, but I can't figure how to do it with a constant lenght on mouse direction

Here is the function:

    void D3DGraphics::DrawLine( int x1,int y1,int x2,int y2,int r,int g,int blu )
{
    int dx = x2 - x1;
    int dy = y2 - y1;

    if( dy == 0 && dx == 0 )
    {
        PutPixel( x1,y1,r,g,blu );
    }
    else if( abs( dy ) > abs( dx ) )
    {
        if( dy < 0 )
        {
            int temp = x1;
            x1 = x2;
            x2 = temp;
            temp = y1;
            y1 = y2;
            y2 = temp;
        }
        float m = (float)dx / (float)dy;
        float b = x1 - m*y1;
        for( int y = y1; y <= y2; y = y + 1 )
        {
            int x = (int)(m*y + b + 0.5f);
            PutPixel( x,y,r,g,blu );
        }
    }
    else
    {
        if( dx < 0 )
        {
            int temp = x1;
            x1 = x2;
            x2 = temp;
            temp = y1;
            y1 = y2;
            y2 = temp;
        }
        float m = (float)dy / (float)dx;
        float b = y1 - m*x1;
        for( int x = x1; x <= x2; x = x + 1 )
        {
            int y = (int)(m*x + b + 0.5f);
            PutPixel( x,y,r,g,blu );
        }
    }
}

I also have a function that gets the mouse X and Y position on the screen (getmouseX(), getmouseY())

Upvotes: 0

Views: 327

Answers (2)

SigTerm
SigTerm

Reputation: 26419

  1. Direct3D has D3DXVECTOR2, D3DXVECTOR3, D3DXCOLOR and similar structures. You should probably use them. Or use typedef D3DXVECTOR2 Vec2; or something like that. Those structures come with mathematical functions, so using them makes sense. Oh, and they operate on floats.
  2. Drawing one pixel at a time is a bad theme - it is slow. Also you can easily draw entire line in one call using IDirect3DDevice9->DrawPrimitive(D3DPT_LINELIST..)
  3. Even if you don't want to use D3DX* structures, you should probably use strctures to store colors and coordinates:

Example:

struct Coord{
    int x, y;
};

struct Color{
    unsigned char a, b, g, r;
};

regarding your question.

typedef D3DXVECTOR2 Vec2;

....

Vec2 startPos = ...;
Vec2 endPos = getMousePos();
const float desiredLength = ...;//whatever you need here.
Vec2 diff = endPos - startPos; //should work. If it doesn't, use
                               //D3DXVec2Subtract(&diff, &endPos, &startPoss);
float currentLength = D3DXVec2Length(&diff);
if (currentLength != 0)
    D3DXVec2Scale(&diff, &diff, desiredLength/currentLength);// diff *= desiredLength/currentLength
else
    diff = Vec2(0.0f, 0.0f);
endPos = startPos + diff; //if it doesn't work, use D3DXVec2Add(&endPos, &startPos, &diff);

This way endPos will not be further than desiredLength than startPos. Unless startPos == endPos

P.S. If you REALLY want to draw the line yourself, you may want to research bresenham line drawing algorithm.

Upvotes: 1

andryr
andryr

Reputation: 684

ratio = (length betweeen start position and mouse position)/10

x = startX+(mouseX-startX)/ratio y = startY+(mouseY-startY)/ratio

I think its something like this

Upvotes: 0

Related Questions