IDL建立泰森多邊形

ucsb發表於2018-03-06

結果圖:

附加原始碼:

PRO testVoronoi



  idx = 0

  ; 建立離散點

  CASE idx OF

    ; 隨機離散點

    0: BEGIN

      N = 36

      X = RANDOMN(seed, N)

      Y = RANDOMN(seed, N)

    END

    ; 規則離散點

    1: BEGIN

      X = REBIN(INDGEN(6)+10, 36,1)

      Y = REFORM(REBIN(INDGEN(6)+10, 6,6),36,1)

      N = N_ELEMENTS(X)

    END

    ; 較規則離散點

    2: BEGIN

      X = REBIN(INDGEN(6)+10, 36,1)+ RANDOMU(seed, 36,1)

      Y = REFORM(REBIN(INDGEN(6)+10, 6,6),36,1)+ RANDOMU(seed, 36,1)

      N = N_ELEMENTS(X)

    END

    ELSE:

  ENDCASE



  ; 構建Delaunay三角網

  TRIANGULATE, X, Y, tr, CONN=C



  ; 繪製離散點

  Points = PLOT(x, y,                    $

    LINESTYLE=6,                         $

    SYMBOL='o', SYM_COLOR='black',          $

    SYM_SIZE=0.5,                        $

    /SYM_FILLED, SYM_FILL_COLOR='black', $

    AXIS_STYLE=4,                        $

    MARGIN=[0,0,0,0],                    $

    WINDOW_TITLE='泰森多邊形')



  FOR I=0, N-1 DO BEGIN



    ; 獲取第i個泰森多邊形:

    VORONOI, X, Y, I, C, Xp, Yp



    ; 快速視覺化繪製

    ; 建立Polygon函式中的CONNECTIVITY關鍵字,指定連通性

    n=N_ELEMENTS(Xp)

    con = [n,INDGEN(n)]

    ; 繪製泰森多邊形

    poly = POLYGON(Xp,Yp,/data,CONNECTIVITY=con, /current,  $

      FILL_BACKGROUND=1, FILL_COLOR=I*7+5, RGB_TABLE=25,    $

      COLOR='white')



  ENDFOR



  ; 繪製三角網,如果不想繪製,把這幾行註釋即可

  n_Tr = (SIZE(tr, /DIMENSIONS))[1]

  FOR i = 0,n_Tr-1 DO BEGIN

    tri = POLYGON(X[tr[*,i]], Y[tr[*,i]], CONNECTIVITY=[3,0,1,2],    $

      /current, color = 'black', /data, FILL_BACKGROUND=0,       $

      LINESTYLE=4)

  ENDFOR



  ; 將離散點置頂顯示

  Points.ORDER, /BRING_TO_FRONT



END

 

相關文章