Length of an arc between two angles: Difference between revisions
(Reworded task description based on consensus opinion of what's required here and converted to draft task.) |
(Added Go) |
||
Line 9: | Line 9: | ||
Illustrate the use of your method by calculating the length of the major arc of a circle of radius 10 units, between angles of 10 and 120 degrees. |
Illustrate the use of your method by calculating the length of the major arc of a circle of radius 10 units, between angles of 10 and 120 degrees. |
||
=={{header|Go}}== |
|||
{{trans|Julia}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"math" |
|||
) |
|||
func arcLength(radius, angle1, angle2 float64) float64 { |
|||
return (360 - math.Abs(angle2-angle1)) * math.Pi * radius / 180 |
|||
} |
|||
func main() { |
|||
fmt.Println(arcLength(10, 10, 120)) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
43.63323129985823 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
Revision as of 09:59, 16 March 2020
- Task
Write a method (function, procedure etc.) in your language which calculates the length of the major arc of a circle of given radius between two angles.
In this diagram the major arc is colored green.
Illustrate the use of your method by calculating the length of the major arc of a circle of radius 10 units, between angles of 10 and 120 degrees.
Go
<lang go>package main
import (
"fmt" "math"
)
func arcLength(radius, angle1, angle2 float64) float64 {
return (360 - math.Abs(angle2-angle1)) * math.Pi * radius / 180
}
func main() {
fmt.Println(arcLength(10, 10, 120))
}</lang>
- Output:
43.63323129985823
Julia
The task seems to be to find the distance along the circumference of the circle which is NOT swept out between the two angles. <lang julia> arclength(r, angle1, angle2) = (360 - abs(angle2 - angle1)) * π/180 * r @show arclength(10, 10, 120) # --> arclength(10, 10, 120) = 43.63323129985823 </lang>
Phix
<lang Phix>function arclength(atom r, angle1, angle2)
return (360 - abs(angle2 - angle1)) * PI/180 * r
end function ?arclength(10, 10, 120) -- 43.6332313</lang>
Raku
<lang perl6>sub arc ( \r, \a1, \a2 ) { r × (τ - abs(a2 - a1)) } sub postfix:<°> (\d) { d × τ / 360 }
say arc(10, 10°, 120°), ' engineering units';</lang>
- Output:
43.63323129985824 engineering units
REXX
<lang rexx>/*REXX program calculates the length of an arc between two angles (stated in degrees).*/ parse arg radius angle1 angle2 . /*obtain optional arguments from the CL*/ if radius== | radius=="," then radius= 10 /*Not specified? Then use the default.*/ if angle1== | angle1=="," then angle1= 10 /* " " " " " " */ if angle2== | angle2=="," then angle2= 120 /* " " " " " " */
say ' circle radius = ' radius say ' angle 1 = ' angle1"º" say ' angle 2 = ' angle2"º" say say ' arc length = ' arcLength(radius, angle1, angle2) exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ arcLength: procedure; parse arg r,a1,a2; return (360-abs(a1-a2)) * pi()/180 * r /*──────────────────────────────────────────────────────────────────────────────────────*/ pi: pi= 3.1415926535897932384626433832795; return pi /*use 32 digs (overkill).*/</lang>
- output when using the default inputs:
circle radius = 10 angle 1 = 10º angle 2 = 120º arc length = 43.6332313