# Regular polygon area • ### Question

• Hello
Sorry for the code in Spanish
I suspect a mathematical problem
Calculate the area of ​​a regular polygon
Use as long side length always: 100
If 5-sided entry area gives me a positive value
If I use 7 sides also gives me positive area
Now with 6 sides and length 100  the area gives me negative

_____________________________________________________________________________________________________________

' para cualquier polígono, el área es
' (nl^2)/(4 tan(360/2n) )
' donde

TextWindow.WriteLine("el dividendo es :  " + dividendo)
angulo = 360/ (2* n)
TextWindow.WriteLine( "el angulo es:  " + angulo)
tangente = Math.Tan(angulo)
TextWindow.WriteLine(" la tang en radianes es: " + tangente)
divisor = 4 * tangente
TextWindow.WriteLine( "el divisor es:  " + divisor)
Area = dividendo / divisor
TextWindow.WriteLine( " el area es: " + Area)

__________________________________________________________________________________________________________

thank you very much

carlosfmur - Buenos Aires

Saturday, April 28, 2012 10:35 PM

• CORRECTION!!!

I've checked Math.Tan() w/ other languages, even WolfRamAlpha.com and tangent of rads can indeed return negative values!

When choosing 6 sides, variable angulo becomes = 30, and Math.Tan(angulo) = -6.40533119664627578489607550566795862628892033671599... So, nothing wrong to see here apparently.  @_@
Sunday, April 29, 2012 12:33 AM
• ArcTan returns an angle between -pi/2 and pi/2 by definition.  If you want the equivalent obtuse angle (> pi/2 or 90deg) just add pi to any value that is less than 0.  This is also a solution to the ArcTan, since Tan(x) = Tan(x+pi).

Sunday, April 29, 2012 8:20 AM

### All replies

• Holá carlosfmur!

Although my trigonometry and geometry knowledge is almost nil, I've played w/ some numbers to find out the range the negative result was happening.

Seems like somewhere inside the following 29.8451302 & 31.4159266 radian range, Math.Tan() is putting a minus sign on the result! Nevertheless, the result is correct apart from the minus sign!!!

I advice to use Math.Abs( Math.Tan() ) when using that radian range for the tangent. [WRONG advice ;-P]

Anywayz, here's a code of concept:

`' Tangent Negative Bug! {Not a bug anymore!!!}TAB = Text.GetCharacter(9)LF  = Text.GetCharacter(10)ang    = 30angMin = 29.8451302angMax = 31.4159266Adjust =   .0000001tan       = Math.Tan(ang)tanMin    = Math.Tan(angMin)tanMinAdj = Math.Tan(angMin + Adjust)tanMax    = Math.Tan(angMax)tanMaxAdj = Math.Tan(angMax - Adjust)TextWindow.WriteLine("RadianAng : " + ang)TextWindow.WriteLine("Tan : " + tan + LF)TextWindow.WriteLine("RadianAngMin : " + angMin)TextWindow.WriteLine("TanMin : " + tanMin)TextWindow.WriteLine(TAB + tanMinAdj + LF)TextWindow.WriteLine("RadianAngMax : " + angMax)TextWindow.WriteLine("TanMan : " + tanMax)TextWindow.WriteLine(TAB + tanMaxAdj + LF)`
Saturday, April 28, 2012 11:49 PM
• CORRECTION!!!

I've checked Math.Tan() w/ other languages, even WolfRamAlpha.com and tangent of rads can indeed return negative values!

When choosing 6 sides, variable angulo becomes = 30, and Math.Tan(angulo) = -6.40533119664627578489607550566795862628892033671599... So, nothing wrong to see here apparently.  @_@
Sunday, April 29, 2012 12:33 AM
• Oh thank you very much GoToLoop
The Pentagon do well  as septagon
And the hexagon gives a negative value (I used degrees, too)
I´ll check with the calculator and will keep working
I avoid the formula that uses the apothem of the polygon in order to simplify the code
I'll give you my feedback as soon as possible.

Thank you very much again

carlosfmur - Buenos Aires

Sunday, April 29, 2012 6:11 AM
• ArcTan returns an angle between -pi/2 and pi/2 by definition.  If you want the equivalent obtuse angle (> pi/2 or 90deg) just add pi to any value that is less than 0.  This is also a solution to the ArcTan, since Tan(x) = Tan(x+pi).

Sunday, April 29, 2012 8:20 AM
• Many thanks, litdev

I ´ll try with this issue

carlosfmur - Buenos Aires

Sunday, April 29, 2012 2:54 PM
• Hi !

This does not look very prolix
Using 100 as long side length, always
I tried 3, 4, 5 and 6 sides
apparently it works

Regards,

-------------------------------------------------------------------------------------------------------------------------------

' para cualquier polígono, el área es
' (nl^2)/(4 tan(360/2n) )
' donde
' This is also a solution to the ArcTan, since Tan(x) = Tan(x+pi).

TextWindow.WriteLine("el dividendo es :  " + dividendo)

angulo = (360/ (2* n) + Math.Pi)
TextWindow.WriteLine( "el angulo es:  " + angulo)
tangente = Math.Tan(angulo)
TextWindow.WriteLine(" la tang en radianes es: " + tangente)
divisor = 4 * tangente
TextWindow.WriteLine( "el divisor es:  " + divisor)
If divisor < 1 Then
divisor = divisor * (-1)
Else
EndIf
Area = dividendo / divisor
TextWindow.WriteLine( " el area es: " + Area)

-----------------------------------------------------------------------------------------------------------------------------

carlosfmur - Buenos Aires

Wednesday, May 2, 2012 12:08 AM
• Sorry

If divisor < 0 Then

If divisor < 1Then

Many thanks

carlosfmur - Buenos Aires

Wednesday, May 2, 2012 1:40 PM
• I have another idea to calculate regular polygon area. Nonki Takahashi

Thursday, May 3, 2012 11:23 AM
• Hello Nonki
This is my code to calculate the perimeter and area of a regular polygon

QCD342

Qould You send me yours?
thanks a lot

carlosfmur - Buenos Aires

Thursday, May 3, 2012 12:16 PM
• 