연산자 Mod를 재발견

For문 등에서 배열을 계산하다보면, 인덱스의 범위를 넘어가는 경우가 생긴다. 이때 Mod[footnote]c/c++에서는 %연산자를 이용한다.[/footnote]를 사용하면 편리하다.

(출처 : http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/)

'다각형면적구하기[vbnet]
Type tPoint
  X As Double
  Y As Double
End Type

Function PolygonArea(N As Integer, Points() As tPoint) As Double
  Dim i As Integer, j As Integer
  Dim area As Double
  area = 0
  For i = 0 To N - 1
    j = (i + 1) Mod N
    area = area + Points(i).X * Points(j).Y - Points(j).X * Points(i).Y
  Next i
  PolygonArea = area / 2
End Function

Print Friendly, PDF & Email
%d bloggers like this: