So, I would start by breaking the polygon into s congruent isosceles triangles, each with base b, height r, side length l, and angle a (at base).
a = 90-180/s
(b/2)^2 + r^2 = l^2
sin(a) = r/l
p = b*s
1) If s is odd: h = r + l
2) If s is even, and you measure height between sides: h = r + r
3) If s is even, and you measure height between corners: h = l + l
r = l*sin(a)
l = r*csc(a)
1)
h = r * (1+csc(a)) = l * (1+sin(a))
(b/2)^2 = (h/(1+sin(a)))^2 - (h/(1+csc(a)))^2
(b/2)^2 = h^2 * (csc(a)-sin(a)) / (2+sin(a)+csc(a))
(b/2)^2 = h^2 * (cos^2(a)) / (sin^2(a) + 2sin(a) + 1)
b/2 = h * cos(a) / (sin(a)+1)
p = 2 * s * h * cos(90-180/s) / (sin(90-180/s)+1)
So your formula doesn't quite work for odd numbers of sides.
2)
r = h/2
l = h*csc(a)/2
(b/2)^2 = (h*csc(a)/2)^2 - (h/2)^2
(b/2)^2 = (h/2)^2 * (csc^2(a) - 1)
b/2 = h/2 * cot(a)
p = s * h * cot(90-180/s)
Your formula comes very close to working here, except it's cotangent, not tangent.
3)
l = h/2
r = h*sin(a)/2
(b/2)^2 = (h/2)^2 - (h*sin(a)/2)^2
(b/2)^2 = (h/2)^2 * (1 - sin^2(a))
b/2 = h/2 * cos(a)
p = s * h * cos(90-180/s)
Once again, very close to yours, but not quite the same...
Let's see check and see if mine works for squares/triangles:
1)
s = 3
If I'm right, I should get p = h*6/sqrt(3)
p = 2 * s * h * cos(90-180/s) / (sin(90-180/s)+1)
90-180/s = 30
cos30 = sqrt(3)/2
sin30 = 1/2
p = 2 * 3 * h * sqrt(3)/2 / (1/2 + 1) = 6 * h * sqrt(3)/2 * 2/3
sqrt(3)/2 * 2/3 = 1/sqrt(3)
p = 6 * h / sqrt(3)
So it works.
2)
s = 4
Should get p = h*4
p = s * h * cot(90-180/s)
90-180/s = 45
cot45 = 1
p = 4 * h * 1 = 4 * h
Also works.
3)
s = 4
Should get p = h*4/sqrt(2) = h*2*sqrt(2)
p = s * h * cos(90-180/s)
90-180/s = 45
cos45 = 1/sqrt(2)
p = 4 * h * 1/sqrt(2) = 4 * h / sqrt(2)
Also works.
Looks like I win
As for your other question, you can't just average the small and large diameters to get the average diameter; that would only work if the relationship between angle and diameter were linear (which it's not). But both the small and large diameters should converge on h, so that doesn't explain why you're getting strange answers. Try it with my formulas and see if it works (it shouldn't matter which one you use; all three should converge to the same value for very large values of s).