first 3 moments of inertia for 2D particle physics, image analysis or a case where you need information on an object that that you haven't explicitly constructed.
Code:
;first 3 moments of inertia 2D
;
; 1st moment, center of a mass
; where
; sumx + x
; sum y + y
;
; cx = sumx / area
; cy = sumy / area
;
; 2nd moment, inertia of mass around axis
; where
; sumXX + (X*X)
; sumYY + (Y*Y)
; sumXY + (X*Y)
;
; IX = SumXX - (Area * (cx * cx))
; IY = SumYY - (Area * (cy * cy))
; IXY = Sumxy - (Area * (cx * cy))
;
; 3rd moments, ra,rb, rc or shape parameter
; *note the shape parameter is invariant to both scale and rotation for a given identical mass
; and can be used to identify shapes in some cases.
; ta= Sqr((2.0 * ix * iY) - (4.0 *( ixy * ixy))/2.0)
; Ra = (ix + iy) + ta
; Rb = (ix + iy) - ta
; Shape = (Ra / Rb)
;
; orientation of mass
;
; where
; mmx = Xmax - Xmin
; mmy = Ymax - Ymin
;
; If ix = iy
; orient = 45
; ElseIf mmx >= mmy
; orient = (0.5 * (ATan(2 * (Ixy / (Iy - ix))))) / (#PI / 180)
; Else
; orient = (0.5 * (ATan(2 * Ixy / (ix - Iy)))) / (#PI / 180)
; EndIf
;example below
Structure moments
;moments
;1st
cx.f
cy.f
;2nd
ix.f
iy.f
ixy.f
;3rd
ra.f
rb.f
shape.f
;orientation
orient.f
;elipse
majoraxis.f
minoraxis.f
;bounds
Xmin.i
Xmax.i
Ymin.i
Ymax.i
EndStructure
Procedure GetMoments(img,x1,x2,y1,y2,color,*mom.moments)
Protected x,y,px,area.f,sumX.f,sumY.f,sumXX.f,sumYY.f,sumXY.f
Protected ta.f,mmx.f,mmy.f,cmx.f,cmy.f,orient.f
*mom\Xmin = 99999
*mom\Ymin = 99999
If StartDrawing(ImageOutput(img))
For x = x1 To x2
For y = y1 To y2
px = Point(x,y)
If px = color
sumX + x
sumY + y
Area + 1
sumXX + (x*x)
sumYY + (y*y)
sumXY + (x*y)
If x < *mom\Xmin
*mom\Xmin = x
EndIf
If x > *mom\XMax
*mom\Xmax = x
EndIf
If y < *mom\Ymin
*mom\Ymin = y
EndIf
If y > *mom\Ymax
*mom\Ymax = y
EndIf
EndIf
Next
Next
StopDrawing()
;1st moments cx = centerX cy=centerY
*mom\cx = sumx / Area
*mom\cy = sumy / Area
;2nd moments Ix Iy Ixy
*mom\ix = SumXX - (Area * (cx * cx))
*mom\iY = SumYY - (Area * (cy * cy))
*mom\ixY = Sumxy - (Area * (cx * cy))
;3rd moment shape parameter invarent to scale and rotation
ta= Sqr((2.0 * *mom\ix * *mom\iY) - (4.0 *(*mom\ixy * *mom\ixy)) * 0.5)
*mom\Ra = (*mom\ix + *mom\iy) + ta
*mom\Rb = (*mom\ix + *mom\iy) - ta
*mom\Shape = (*mom\Ra / *mom\Rb)
mmx = (*mom\Xmax - *mom\Xmin)
mmy = (*mom\Ymax - *mom\Ymin)
cmx = (*mom\Xmax + *mom\Xmin) * 0.5
cmy = (*mom\Ymax + *mom\Ymin) * 0.5
dmx = *mom\cx - cmx
dmy = *mom\cy - cmy
;orientation
If *mom\ix = *mom\iy
orient = 45
ElseIf mmx >= mmy
orient = (0.5 * (ATan(2 * (*mom\Ixy / (*mom\iy - *mom\ix))))) / (#PI / 180)
*mom\majoraxis = (mmx / Cos(Abs(orient) / (180 / #PI))) * 0.5
*mom\minoraxis = (Area) / (#PI * *mom\majoraxis)
Else
orient = (0.5 * (ATan(2 * *mom\Ixy / (*mom\ix - *mom\Iy)))) / (#PI / 180)
*mom\majoraxis = (mmy / Cos(Abs(orient) / (180 / #PI))) * 0.5
*mom\minoraxis = (Area) / (#PI * *mom\majoraxis)
EndIf
If mmx = mmy And Abs(*mom\ix- *mom\iy) < 1000
orient = 0
EndIf
;messy fudge to output 0 to 360 degrees
If *mom\ixy > 0
If *mom\iy > *mom\ix
If dmx > dmy
orient = (360 + orient) / (180/#PI)
Else
orient = (180 + orient) / (180/#PI)
EndIf
Else
If dmx > dmy
orient = (90 - orient) / (180/#PI)
Else
orient = (270 - orient) / (180/#PI)
EndIf
EndIf
Else
If *mom\iy > *mom\ix
If dmy > dmx
orient = (180 + orient) / (180/#PI)
Else
orient = orient / (180/#PI)
EndIf
Else
If dmx > dmy
orient = (90 - orient) / (180/#PI)
Else
orient = (270 - orient) / (180/#PI)
EndIf
EndIf
EndIf
If orient < 0
orient = 0
EndIf
*mom\orient = orient
EndIf
EndProcedure