I and a friend are currently stuck with a math predicament in which we cannot solve. We are trying to recreate a parliamentary diagram using lua, using the source code on this website

^ This website has the source code which is in Python, we converted it into Lua.

We use parts/color to represent delegates of each party. We currently have a few features down but the parts are not coming into place as we'd hope.

Instead of it looking like this: Bad Image

We want it to look like this: Good Image

If any of you could of be of any help and tell us what we've done wrong in our code, that'd be great.

We are almost certain it's something to do with the math.

[LUA CODE]

local function UpdateParliamentaryDiagram(countryid)
	if #viewportframe:GetChildren() == 1 then
		for _,x in pairs(viewportframe:GetChildren()) do
			x:Destroy()
		end
	end
	local checkpartiesexist = false
	local CountrySelected = findcountry(countryid)
	if #c_database[CountrySelected].Parties:GetChildren() > 0 then
		checkpartiesexist = true
	end
	-- fetch party information for country selected
	if checkpartiesexist == true then
		local PartyList = {} --create blank party list
		for _,x in pairs(c_database[CountrySelected].Parties:GetChildren()) do --add parties to list
			local temprow = {}
			table.insert(temprow,x.Name) --name of party
			table.insert(temprow,x.Value) --amount of representatives from political party
			table.insert(temprow,x.Color.Value) --party colour
			table.insert(PartyList,temprow) --add party to total party list
		end
		local sumdelegates = 0 -- total amount of seats
		for _,x in pairs(PartyList) do --get total delegates
			sumdelegates = sumdelegates + x[2]
		end
		--totals is the total amount of delegates per row
		local totals = {3, 15, 33, 61, 95, 138, 189, 247, 313, 388, 469, 559, 657, 762, 876, 997, 1126, 1263, 1408, 1560, 1722, 1889, 2066, 2250, 2442, 2641, 2850, 3064, 3289, 3519, 3759, 4005, 4261, 4522, 4794, 5071, 5358, 5652, 5953, 6263, 6581, 6906, 7239, 7581, 7929, 8287, 8650, 9024, 9404, 9793, 10187, 10594, 11003, 11425, 11850, 12288, 12729, 13183, 13638, 14109, 14580, 15066, 15553, 16055, 16557, 17075, 17592, 18126, 18660, 19208, 19758, 20323, 20888, 21468, 22050, 22645, 23243, 23853, 24467, 25094, 25723, 26364, 27011, 27667, 28329, 29001, 29679, 30367, 31061}
		local spotcounter = 0
		local diagramlines = 0
		local rows = 0
		--figure out how many rows are needed
		for _,x in pairs(totals) do
			if x >= sumdelegates then
				rows = rows + 1
			end
		end
		--calculate radius
		local radius = 0.3/rows 
		--calculate positioning
		local poslist = {}
		for x=1,rows do
			--Each row can contain pi/(2asin(2/(3n+4i-2))) spots, where n is the number of rows and i is the number of the current row.
			local J = tonumber((tonumber(sumdelegates)/totals[rows])*math.pi)/(2*math.asin((2.0/((((3.0*rows)+4.0)*x)-2.0)))))
			--The radius of the ith row in an N-row diagram (Ri) is (3*N+4*i-2)/(4*N)
			local R = ((((3.0*rows)+4.0)*x)-2.0)/(4.0*rows)
			if J == 1 then
				table.insert(poslist,{(math.pi/2.0),(1.75*R),R})
			else
				for j=0,J do
					--The angle to a spot is n.(pi-2sin(r/Ri))/(Ni-1)+sin(r/Ri) where Ni is the number in the arc
					--x=R.cos(theta) + 1.75
					--y=R.sin(theta)
					local angle = tonumber(j) * ((math.pi-2.0)*(math.sin(radius/R)))/(tonumber(J)-1.0)+(math.sin(radius/R))
					table.insert(poslist,{angle,R*math.cos(angle)+1.75,R*math.sin(angle)})
				end
			end
		end
		local J = sumdelegates - #poslist
		local R = (7.0*rows-2.0)/(4.0*rows)
		if J == 1 then
			table.insert(poslist,{math.pi/2.0,1.75*R,R})
		else
			for j=0,J do
				local angle = tonumber(j) * (math.pi-2.0*math.sin(radius/R))/(tonumber(J)-1.0)+math.sin(radius/R)
				table.insert(poslist,{angle,R*math.cos(angle)+1.75,R*math.sin(angle)})
			end
		end
		--table.sort(poslist,function(a, b) return a[2] > b[2] end)
		local counter = 1 --how many spots are drawn
		local ExportFolder = Instance.new("Folder") --new diagram
		ExportFolder.Name = "ParliamentaryDiagram"
		for i,x in pairs(PartyList) do
			local NewFolder = Instance.new("Folder") --create folder for each individual political party
			NewFolder.Name = x[1]
			local CircleColor = x[3]
			for u=counter,counter+x[2]-1 do
				local NewPart = Instance.new("Part") --each part represents each representative in political party
				NewPart.Anchored = true
				NewPart.Parent = NewFolder
				NewPart.Color = CircleColor
				--print(counter .. " " .. #poslist)
				NewPart.CFrame = CFrame.new(10.0*(1.75-poslist[counter][3])+5.0,0,poslist[counter][2]*10.0+5.0) --CFrame.new(100.0*(1.75-poslist[counter][3])+5.0,0,poslist[counter][2]*100.0+5.0) --CFrame.new(poslist[u][2]*100.0+5.0,0,100.0*(1.75-poslist[u][3])+5.0)
				NewPart.Size = Vector3.new(radius*100.0,0.1,radius*100.0)
				NewPart.Orientation = Vector3.new(0, 0, 0)
				counter = counter +1
			end
			NewFolder.Parent = ExportFolder
		end
		ExportFolder.Parent = viewportframe
	end
end