Subscribed unsubscribe Subscribe Subscribe

多角形の組み合わせとして多角形の図心を求める

図心の計算

[f:id:ti-nspire:20160609092354j:plain:W600]
nspired basic:

Define centroid2(verts)=
Func
:Local k,numverts,points,xg,yg,x,y,xc,yc,s,st
:numverts:=((dim(verts))/(2))
:points:=exact(augment(verts,{verts[1],verts[2]}))
:For k,1,((dim(points))/(2))
:x[k]:=points[2*k-1]:y[k]:=points[2*k]
:EndFor
:xc:=((seqn(x[n]+x[n+1],numverts))/(3)):yc:=((seqn(y[n]+y[n+1],numverts))/(3))
:s:=((seqn(x[n+1]*y[n]-y[n+1]*x[n],numverts))/(2))
:st:=sum(s):xg:=((sum(s*xc))/(st)):yg:=((sum(s*yc))/(st))
:Return {xg,yg,st}
:EndFunc
Define compcent(vertsmat)=
Func
:Local temp,x,y,area,i,st,xg,yg
:For i,1,rowDim(vertsmat)
:temp:=centroid2(delVoid(mat▶list(vertsmat[i])))
:x[i]:=temp[1]:y[i]:=temp[2]
:area[i]:=temp[3]
:EndFor
:st:=sum(area):xg:=((sum(x*area))/(st)):yg:=((sum(y*area))/(st))
:Return {xg,yg,st}
:EndFunc

1 行目が 1 個の多角形として計算した図心と総面積。2 行目が多角形の組み合わせとして計算した図心と総面積。
f:id:ti-nspire:20160609092529j:plain



nspired lua:

function centroid2(vertsList) --[[ xg, yg, area で返す --]]
   local numVerts = #vertsList/2
   local points = vertsList
   table.insert(points, vertsList[1]); table.insert(points, vertsList[2])
   local x, y = {}, {}
   for i = 1, #points/2 do
      x[i], y[i] = points[2*i-1], points[2*i]
   end
   local xc, yc, eachAreas = {}, {}, {}
   local totalArea, xgTemp, ygTemp = 0, 0, 0
   for i = 1, numVerts do
      xc[i], yc[i] = (x[i] + x[i+1])/3, (y[i] + y[i+1])/3
      eachAreas[i] = (x[i+1] * y[i] - y[i+1] * x[i])/2
      totalArea = totalArea + eachAreas[i]
      xgTemp, ygTemp = xgTemp + eachAreas[i] * xc[i], ygTemp + eachAreas[i] * yc[i]
   end
   return xgTemp/totalArea, ygTemp/totalArea, totalArea
end
function compCentroid(vertsTable) --[[ xg, yg, area で返す --]]
   local xg, yg, area = {}, {}, {}
   for i, v in ipairs(vertsTable) do
      xg[i], yg[i], area[i] = centroid2(v)
   end
   local tempXg, tempYg, totalArea = 0, 0, 0
   for i = 1, #xg do
      tempXg, tempYg = tempXg + xg[i] * area[i], tempYg + yg[i] * area[i]
      totalArea = totalArea + area[i]
   end
   return tempXg/totalArea, tempYg/totalArea, totalArea
end

-- 1 個の多角形として図心と面積とを求める。
print(centroid2({4,3,4.59,0.658,-0.591,-0.636,-1,1,-2.17,0.708,-3.17,4.71,-2,5,-1.18,1.71}))

-- 多角形の組み合わせとして全体の図心と面積とを求める。
print(compCentroid({{4,3,4.59,0.658,-0.591,-0.636,-1.18,1.71},{-2,5,-1,1,-2.17,0.708,-3.17,4.71}}))

f:id:ti-nspire:20160609092938j:plain:w600




検算(多角形の面積と重心(頂点データから計算) 中川雅央 - 滋賀大学):
f:id:ti-nspire:20160609093252j:plain

Remove all ads