반응형
다각형의 중립축에서의 폭 계산
1. 중립축 x와 다각형의 교차점을 찾는다.
2. 교차점의 x좌표값이 작은 순서대로 오름차순으로 정렬한다.
3. 폭 B를 계산한다.
B = 짝수번째 x좌표값의 합 - 홀수번째 x좌표값의 합
드디어 교차되는 폭을 계산하는 코드를 완성했다.
물론 인터넷에서 도움을 받았다.
Public Function calc_width(x1 As Double, y1 As Double, x2 As Double, y2 As Double, Polygon As Variant) As Variant
Dim x As Long, m As Double, b As Double, Poly As Variant
Dim LB1 As Long, LB2 As Long, UB1 As Long, UB2 As Long, NumSidesCrossed As Long
Dim dblCrossX As Double
Dim dblCrossY As Double
Dim dblTestx1 As Double
Dim dblTesty1 As Double
Dim dblTestx2 As Double
Dim dblTesty2 As Double
Dim arrCrossX() As Variant
Dim z As Integer
Dim vaArr_Sort As Variant
Dim oddx As Double
Dim evenx As Double
Poly = Polygon
For x = LBound(Poly) To UBound(Poly) - 1
dblTestx1 = Poly(x, 1)
dblTesty1 = Poly(x, 2)
dblTestx2 = Poly(x + 1, 1)
dblTesty2 = Poly(x + 1, 2)
If m_CalculateIntersection(x1, y1, x2, y2, dblTestx1, dblTesty1, dblTestx2, dblTesty2, dblCrossX, dblCrossY) Then
z = z + 1
ReDim Preserve arrCrossX(1 To z)
arrCrossX(z) = dblCrossX
End If
Next
vaArr_Sort = SortArray(arrCrossX, xlAscending, True)
For x = 1 To UBound(vaArr_Sort)
If CBool(x Mod 2) Then
oddx = oddx + vaArr_Sort(x)
Else
evenx = evenx + vaArr_Sort(x)
End If
Next
calc_width = evenx - oddx
End Function
m_CalculateIntersection, SortArray는 인터넷에서 구한 함수이다.
반응형
최근댓글