2016-03-03 23 views
0

我今天拿起Processing,編寫了一個程序來生成雙縫干涉圖案。在稍微調整值之後,它可以工作,但生成的模式比其他一些程序中可能的模式更模糊。下面是截圖:在處理中平滑逐像素繪圖

interference pattern

正如你所看到的,條紋並不在邊緣光滑如我相信是可能的。我期望他們看起來像thisthis

這是我的代碼:

// All quantities in mm 

float slit_separation = 0.005; 
float screen_dist = 50; 
float wavelength = 5e-4f; 

PVector slit1, slit2; 

float scale = 1e+1f; 

void setup() { 
    size(500, 500); 
    colorMode(HSB, 360, 100, 1); 
    noLoop(); 
    background(255); 

    slit_separation *= scale; 
    screen_dist *= scale; 
    wavelength *= scale; 

    slit1 = new PVector(-slit_separation/2, 0, -screen_dist); 
    slit2 = new PVector(slit_separation/2, 0, -screen_dist); 
} 

void draw() { 
    translate(width/2, height/2); 
    for (float x = -width/2; x < width/2; x++) { 
    for (float y = -height/2; y < height/2; y++) { 
     PVector pos = new PVector(x, y, 0); 
     float path_diff = abs(PVector.sub(slit1, pos).mag() - PVector.sub(slit2, pos).mag()); 
     float parameter = map(path_diff % wavelength, 0, wavelength, 0, 2 * PI); 
     stroke(100, 100, pow(cos(parameter), 2)); 
     point(x, y); 
    } 
    } 
} 

我的代碼在數學上是正確的,所以我想知道如果有什麼問題,我在做轉化的物理值的像素在屏幕上。

回答

0

我不完全確定你在問什麼,你期望它看起來像什麼?是否有可能將其縮小到一個行爲不合適的行,而不是嵌套for循環?

但是隻是猜測你在說什麼:請記住,Processing默認啓用了抗鋸齒功能。要禁用它,你必須調用noSmooth()函數。你可以把它在你的setup()功能:

void setup() { 
    size(500, 500); 
    noSmooth(); 
    //rest of your code 

如果你比較他們來說,這是很明顯並排側:

如果這不是你在說些什麼,請發佈MCVE只有一行或兩行,而不是嵌套for循環。包含你期望的內容和你得到的內容的模型也是有幫助的。祝你好運!

+0

謝謝你嘗試回答。我期望在明亮條紋和黑暗條紋之間的過渡更加平滑,就像抖動的漸變,這將是預期的結果。不幸的是,這個問題不能縮小到特定的代碼行,但如果我猜測,我會把注意力集中在最後三行上。讓我用預期的結果更新我的問題。 – duci9y

+0

@ duci9y當我說「線」時,我不是指「代碼行」 - 我的意思是單一的線條。在你的情況下,也許是一個單一的點? –

+0

我不知道繪製一條線還是一個點會使問題變得明顯,因爲只有繪製更大的區域時纔會出現問題。我相信我的代碼是一個MCVE,嵌套for循環不會降低可讀性/可重複性,並且是程序運行所必需的。 – duci9y