2013-04-22 75 views
0

在我目前的程序中,我希望能夠繪製「DNA形狀」。我寫了一個「DrawPixel(x,y,r,g,b)」函數,它可以在屏幕上繪製一個像素。從這一點開始,我實現了Bresenham line algorithm來畫一條線:「DrawLine(x1,y1,x2,y2,r,g,b)」。Directx C++ - 繪製DNA形狀

現在我意識到使用DNA形狀的圖像將是一個非常糟糕的選擇(在多個方面)。所以我試着製作一個繪製DNA形狀的函數(因爲我找不到算法)。這是目前基於圓圈繪製算法(Midpoint Circle Algorithm):

void D3DGHandler::DrawDNAShape(int x1, int y1, int length, int curves, int dir 
           int off, int r, int g, int b){ 
    int x2Pos = sin(dir)*length+x1; 
    int y2Pos = cos(dir)*length+y1; 


    for (int i = 0; i < curves; i++) { 
      int xIncrease = (x2Pos/curves) * i; 
      int yIncrease = (y2Pos/curves) * i; 

      int rSquared = off * off; 
      int xPivot = (int)(off * 0.707107 + 0.5f); 
      for (int x = 0; x <= xPivot; x++) { 
       int y = (int)(sqrt((float)(rSquared - x*x)) + 0.5f); 
       DrawPixel(x1+x+xIncrease,y1+y+yIncrease,r,g,b); 
       DrawPixel(x1-x+xIncrease,y1+y+yIncrease,r,g,b); 
       DrawPixel(x1+x+xIncrease,y1-y+yIncrease,r,g,b); 
       DrawPixel(x1-x+xIncrease,y1-y+yIncrease,r,g,b); 
       DrawPixel(x1+y+xIncrease,y1+x+yIncrease,r,g,b); 
       DrawPixel(x1-y+xIncrease,y1+x+yIncrease,r,g,b); 
       DrawPixel(x1+y+xIncrease,y1-x+yIncrease,r,g,b); 
       DrawPixel(x1-y+xIncrease,y1-x+yIncrease,r,g,b); 
      } 
    } 
} 

此實現目前讓我一些全新的功能,我不是在尋找。

線沿線的:

DrawDNAShape()

我會很高興聽到你能不能給我任何信息!

更新

預期結果:

Expected Result

但後來方式更加線等。

+0

你可以舉一個你想畫的東西的例子嗎? DNA的雙螺旋結構是3D形狀,因此在2D中有很多方法可以繪製。 – 2013-04-22 20:07:31

+0

@DavidBrown @DavidBrown對不起現在更新:) – 2013-04-22 20:14:44

回答

2

你可以繪製兩個正弦曲線圖,其中一些偏移量會給你所需的形狀。

例如, In R

x=(1:100)/10.0 
plot(sin(x),x) 
points(sin(x+2.5),x) 
+0

但是如果偏移量有點高不會繪製罪例如(對於x)意味着會有差距,因爲每個x上可能有多個像素? – 2013-04-22 21:18:45

+1

我不明白你在說什麼。有兩條線,所以有兩組正弦圖。請參閱:http://www.slideshare.net/dvidby0/the-dna-double-helix – ElKamina 2013-04-22 21:24:38

+0

感謝您的鏈接,將幫助我:)謝謝 – 2013-04-22 21:27:37