2013-06-01 46 views
0

如問題所述,我想繪製一條從X,Y位置開始的線,例如鼠標方向上的10個像素...函數I已經繪製了一條線2點之間,但我想不出如何使用鼠標方向在鼠標方向上繪製一條具有恆定長度的線條

這裏恆定lenght要做的只是功能:

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); 
     } 
    } 
} 

我也有獲取鼠標的X和Y位置的函數在屏幕上(getmouseX(),getmouseY())

回答

1
  1. 的Direct3D具有D3DXVECTOR2D3DXVECTOR3D3DXCOLOR和類似結構。你應該可以使用它們。或使用typedef D3DXVECTOR2 Vec2;或類似的東西。那些結構具有數學功能,所以使用它們是有意義的。哦,他們在浮游物上運作。
  2. 一次繪製一個像素是一個糟糕的主題 - 速度很慢。您也可以使用IDirect3DDevice9->DrawPrimitive(D3DPT_LINELIST..)
  3. 輕鬆畫出整條生產線的一個呼叫即使你不想使用D3DX*結構,你應該使用strctures存儲的顏色和座標:

例子:

struct Coord{ 
    int x, y; 
}; 

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

關於你的問題。

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); 

這樣endPosdesiredLength比startPos不會進一步。除非startPos == endPos

P.S.如果你真的想自己畫線,你可能想研究bresenham line drawing algorithm

0

ratio =(開始位置和mous之間的長度È位置)/ 10

X = STARTX +(mouseX-STARTX)/比 Y = startY +(mouseY的-startY)/比率

我認爲它是這樣的

+0

Aaand你會得到零分。 – SigTerm

相關問題