Author 
Topic: Degrees / Radians Converter (Read 853 times) 

Sapristi45
New Member
member is offline
Gender:
Posts: 12


Degrees / Radians Converter
« Thread started on: Mar 26^{th}, 2016, 5:10pm » 

Something I made for fun ;D (because maths is fun)
Code:[start]
cls
choice = 0
choice$ = ""
out = 0
angle = 0
pi = 3.14159265 ' No, π =/= 3.14159265, π = π ;)
print
print "Do you want to convert :"
print " 1) radians to degrees"
print " 2) degrees to radians"
print
input "> "; choice
if choice <> 1 and choice <> 2 then goto [start]
print
input "Input your angle here : "; angle
print
if choice = 1 then out = (angle * 180 / pi) mod 360
if choice = 2 then out = (pi * angle / 180) mod (pi * 2)
print
if choice = 1 then print angle; " rad equals ~"; out; "°."
if choice = 2 then print angle; "° angle equals ~"; out; " rad."
print
[error]
print "Do you want to convert another angle ? (Y/N) "
input "> "; choice$
select case
case choice$ = "Y"
goto [start]
case choice$ = "N"
end
case else
goto [error]
end select

« Last Edit: Mar 26^{th}, 2016, 5:13pm by Sapristi45 » 
Logged




bplus
Senior Member
member is offline
Gender:
Posts: 1290


Re: Degrees / Radians Converter
« Reply #1 on: Mar 26^{th}, 2016, 7:25pm » 

Well that is a new one! I'd never seen MOD involved with RAD calculation. Guess it's OK but seems like extra work:
Code:pi=3.14159265
for angle=0 to 5 step 1/60
print "angle degrees = ";angle;" is ";pi*angle/180;" radians and ";(pi * angle / 180) mod (pi * 2);" calculated with MOD (pi*2)."
next
Your code is very stylish but could go with upper$ with the Y/N test.

« Last Edit: Mar 26^{th}, 2016, 7:29pm by bplus » 
Logged

B+



Sapristi45
New Member
member is offline
Gender:
Posts: 12


Re: Degrees / Radians Converter
« Reply #2 on: Mar 26^{th}, 2016, 8:11pm » 

using MOD with radian angles seemed logic to me ^^ and I guess this Y/N code is better ;)
Code:
[error]
print "Do you want to convert another angle ? (Y/N) "
input "> "; choice$
select case
case UPPER$(choice$) = "Y"
goto [start]
case UPPER$(choice$) = "N"
end
case else
goto [error]
end select


Logged




bplus
Senior Member
member is offline
Gender:
Posts: 1290


Re: Degrees / Radians Converter
« Reply #3 on: Mar 27^{th}, 2016, 2:53pm » 

I have been wondering about how to teach people about Radians.
Seems to me, BASIC was born to help teach math.
Code:'Angle measure.txt for JB [B+=MGA] 20160327
global pi,d2r,r2d,d2f,f2d,r2f,f2r,angle,measure,fmt$,q
pi = 3.14159265
d2r=pi/180
r2d=180/pi
d2f=1/360
f2d=360
r2f=1/(2*pi)
f2r=2*pi
fmt$="###.##"
'====================== main code
call Intro
do
call Angle
loop until q
print:print " OK, goodbye"
'====================== procedures
sub Intro
print
print " *** Angle Conversion ***"
print
print " There are different ways to measure an angle. Most people learn Degrees early"
print " but get confused when Radian measures are discussed."
print
print " I think it might help bridge the gap to think about angles as fractions of a circle."
print
print " From a fraction of a circle, the conversion to Degrees or Radians is easier,"
print " because 1 circle = 360 Degrees AND 1 circle = 2 * pi Radians,"
print
print " So from a fraction of circle, one may pick which multiplier to apply,"
print " 360 for Degrees or 2*pi for Radians, both are measures for 1 circle."
print
print " Examples: (the ~ symbol is used here to mean 'approximately')"
print
print " 1/2 or .500 of a circle is 360*1/2 = 180 Degrees AND 1/2 * 2*pi = pi or ~3.1415 Radians"
print
print " 1/4 or .250 of a circle is 360*1/4 = 90 Degrees AND 1/4 * 2*pi = pi/2 or ~1.5708 Radians"
print
print " 1/8 or .125 of a circle is 360*1/8 = 45 Degrees AND 1/8 * 2*pi = pi/4 or ~0.7854 Radians"
print
print " 1/6 or ~.167 of a circle is 360*1/6 = 60 Degrees AND 1/6 * 2*pi = pi/3 or ~1.0472 Radians"
print
print " Use the following Converter to explore this relationship of different ways to measure an angle."
print
input " ... press enter to compare or convert angles using different measures or enter 1 to quit > ";q
end sub
sub Angle
if q then exit sub
cls
print " *** Angle Conversion ***"
print " Note: the ~ symbol is used here to mean 'approximately', assume all is approximate."
print
print " Press enter to exit Converter or enter an angle to see the different measures,"
input " you may use / to indicate a fraction or p for pi, eg 3/12 (of circle) or p/3 (radians) > ~";a$
if a$="" then q=1:exit sub
a$=lower$(a$)
p=instr(a$,"p")
If p then a$=mid$(a$,1,p1) + str$(pi) +mid$(a$,p+1)
slash=instr(a$,"/")
If slash then
n=val(mid$(a$,1,slash1)) : d=val(mid$(a$,slash+1))
angle=(n/d)
else
angle=val(a$)
end if
print " Enter a number for the type of measure your angle of ~";using(fmt$,angle)
print " 1) Radians"
print " 2) Degrees"
print " 3) Fraction of circle"
print " 4) exit Converter"
print
input "> "; measure
measure=int(measure)
print
if measure < 1 or measure >4 then print " Sorry, ";measure;" choice is not available."
if measure = 1 then print " Your angle of ~";using(fmt$,angle);" radians is ~";using(fmt$,r2d*angle);" degrees and is ~";using(fmt$,r2f*angle);" fraction of a circle."
if measure = 2 then print " Your angle of ~";using(fmt$,angle);" degrees is ~ ";using(fmt$,d2r*angle);" radians and is ~";using(fmt$,d2f*angle);" fraction of a circle."
if measure = 3 then print " Your angle of ~";using(fmt$,angle);" fraction of circle is ~";using(fmt$,f2r*angle);" radians and ~";using(fmt$,f2d*angle);" degrees."
if measure = 4 then q=1:exit sub
print
input "press enter to continue...";temp
end sub


Logged

B+



bplus
Senior Member
member is offline
Gender:
Posts: 1290


Re: Degrees / Radians Converter
« Reply #4 on: Mar 28^{th}, 2016, 12:20pm » 

Ah yes!
pi = 4*atn(1)
is a clever way to get pi constant that is more accurate than what you may want to type out. So JB doesn't print the half of it, interesting...
tan(45 degrees)=1 so the arc tan, the angle whose tan is 1, atn(1) = 45 degrees though JB will return it in radians.
4*45 degrees = 180 degrees and in radians 180 degrees = pi
So what is tan?
The short answer is sin/cos or opp/adj, some diagrams would be helpful to explain but
basically it is just a ratio of the two legs of a right triangle, the far one (opp leg) divided by the one attached to the angle in question (adj leg).
Oh, did I say sin and cos? What are those?
Just more ratios of parts of a right triangle. Besides the 2 legs attached to the right angle there is the longest diagonal side called the hypotenuse or hyp.
sin ratio is leg opp the angle in question divided by the hypotenuse, or opposite divided by hyp or opp/hyp.
cos ratio is leg attached to angle in question (the adjacent it's called) divided by the hypotenuse or adj/hyp.
There! now you know everything you need to know about Trigonometry except how to apply it in real world which takes practice. Drawing in BASIC is fun way for that.
So when using the sin(x) function, x is some angle measure and the function returns the sin ratio for that angle. This is used to calculate the other parts of the right triangle when only a leg and angle are known.
Since sin(x)=opp/hyp then opp=sin(x)*hyp or hyp=opp/sin(x) <==edit
BUT JB expects the angle measure to be in radians so if you like working in degrees you need to know how to convert to radians and that is the point of this thread.

« Last Edit: Mar 28^{th}, 2016, 1:33pm by bplus » 
Logged

B+



