Length of an arc between two angles: Difference between revisions

From Rosetta Code
Content added Content deleted
m (syntax highlighting fixup automation)
Line 12: Line 12:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F arc_length(r, angleA, angleB)
<syntaxhighlight lang="11l">F arc_length(r, angleA, angleB)
R (360.0 - abs(angleB - angleA)) * math:pi * r / 180.0
R (360.0 - abs(angleB - angleA)) * math:pi * r / 180.0


print(arc_length(10, 10, 120))</lang>
print(arc_length(10, 10, 120))</syntaxhighlight>


{{out}}
{{out}}
Line 25: Line 25:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
{{libheader|Action! Real Math}}
<lang Action!>INCLUDE "H6:REALMATH.ACT"
<syntaxhighlight lang="action!">INCLUDE "H6:REALMATH.ACT"


PROC ArcLength(REAL POINTER r,a1,a2,len)
PROC ArcLength(REAL POINTER r,a1,a2,len)
Line 50: Line 50:
ArcLength(r,a1,a2,len)
ArcLength(r,a1,a2,len)
PrintR(len)
PrintR(len)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Length_of_an_arc_between_two_angles.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Length_of_an_arc_between_two_angles.png Screenshot from Atari 8-bit computer]
Line 58: Line 58:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_Io;
<syntaxhighlight lang="ada">with Ada.Text_Io;
with Ada.Numerics;
with Ada.Numerics;


Line 89: Line 89:
Distance_Io.Put (Arc_Length, Exp => 0, Aft => 4);
Distance_Io.Put (Arc_Length, Exp => 0, Aft => 4);
New_Line;
New_Line;
end Calculate_Arc_Length;</lang>
end Calculate_Arc_Length;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 97: Line 97:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
Follows the Fortran interpretation of the task and finds the length of the major arc.
Follows the Fortran interpretation of the task and finds the length of the major arc.
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
% returns the length of the arc between the angles a and b on a circle of radius r %
% returns the length of the arc between the angles a and b on a circle of radius r %
% the angles should be specified in degrees %
% the angles should be specified in degrees %
Line 110: Line 110:
% task test case %
% task test case %
write( r_w := 10, r_d := 4, r_format := "A", majorArcLength( 10, 120, 10 ) )
write( r_w := 10, r_d := 4, r_format := "A", majorArcLength( 10, 120, 10 ) )
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 118: Line 118:
=={{header|APL}}==
=={{header|APL}}==
{{works with|Dyalog APL}}
{{works with|Dyalog APL}}
<lang APL>arc ← (○÷180)×⊣×360-(|(-/⊢))</lang>
<syntaxhighlight lang="apl">arc ← (○÷180)×⊣×360-(|(-/⊢))</syntaxhighlight>
{{out}}
{{out}}
<pre> 10 arc 10 120
<pre> 10 arc 10 120
Line 124: Line 124:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>MsgBox % result := arcLength(10, 10, 120)
<syntaxhighlight lang="autohotkey">MsgBox % result := arcLength(10, 10, 120)
return
return


arcLength(radius, angle1, angle2){
arcLength(radius, angle1, angle2){
return (360 - Abs(angle2-angle1)) * (π := 3.141592653589793) * radius / 180
return (360 - Abs(angle2-angle1)) * (π := 3.141592653589793) * radius / 180
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>43.633231</pre>
<pre>43.633231</pre>


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LENGTH_OF_AN_ARC_BETWEEN_TWO_ANGLES.AWK
# syntax: GAWK -f LENGTH_OF_AN_ARC_BETWEEN_TWO_ANGLES.AWK
# converted from PHIX
# converted from PHIX
Line 145: Line 145:
}
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
function abs(x) { if (x >= 0) { return x } else { return -x } }
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 152: Line 152:


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang BASIC>10 DATA 10, 10, 120
<syntaxhighlight lang="basic">10 DATA 10, 10, 120
20 READ R, A1, A2
20 READ R, A1, A2
30 GOSUB 100
30 GOSUB 100
Line 159: Line 159:
100 REM Calculate length of arc of radius R, angles A1 and A2
100 REM Calculate length of arc of radius R, angles A1 and A2
110 A = ATN(1)*R*(360-ABS(A1-A2))/45
110 A = ATN(1)*R*(360-ABS(A1-A2))/45
120 RETURN</lang>
120 RETURN</syntaxhighlight>
{{out}}
{{out}}
<pre> 43.6332</pre>
<pre> 43.6332</pre>
Line 165: Line 165:
=={{header|C}}==
=={{header|C}}==
{{Trans|AWK}}
{{Trans|AWK}}
<syntaxhighlight lang="c">
<lang c>
#define PI 3.14159265358979323846
#define PI 3.14159265358979323846
#define ABS(x) (x<0?-x:x)
#define ABS(x) (x<0?-x:x)
Line 177: Line 177:
printf("%.7f\n",arc_length(10, 10, 120));
printf("%.7f\n",arc_length(10, 10, 120));
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 185: Line 185:
=={{header|C++}}==
=={{header|C++}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>


#define _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
Line 198: Line 198:
std::cout << "arc length: " << al << '\n';
std::cout << "arc length: " << al << '\n';
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>arc length: 43.6332</pre>
<pre>arc length: 43.6332</pre>
Line 204: Line 204:
=={{header|D}}==
=={{header|D}}==
{{trans|C++}}
{{trans|C++}}
<lang d>import std.math;
<syntaxhighlight lang="d">import std.math;
import std.stdio;
import std.stdio;


Line 213: Line 213:
void main() {
void main() {
writeln("arc length: ", arcLength(10.0, 10.0, 120.0));
writeln("arc length: ", arcLength(10.0, 10.0, 120.0));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>arc length: 43.6332</pre>
<pre>arc length: 43.6332</pre>
Line 219: Line 219:
=={{header|Delphi}}==
=={{header|Delphi}}==
{{Trans|AWK}}
{{Trans|AWK}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Length_of_an_arc;
program Length_of_an_arc;


Line 237: Line 237:
Readln;
Readln;
end.
end.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 244: Line 244:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: kernel math math.constants math.trig prettyprint ;
<syntaxhighlight lang="factor">USING: kernel math math.constants math.trig prettyprint ;


: arc-length ( radius angle angle -- x )
: arc-length ( radius angle angle -- x )
- abs deg>rad 2pi swap - * ;
- abs deg>rad 2pi swap - * ;


10 10 120 arc-length .</lang>
10 10 120 arc-length .</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 256: Line 256:


=={{header|FOCAL}}==
=={{header|FOCAL}}==
<lang FOCAL>01.10 S A1=10 ;C SET PARAMETERS
<syntaxhighlight lang="focal">01.10 S A1=10 ;C SET PARAMETERS
01.20 S A2=120
01.20 S A2=120
01.30 S R=10
01.30 S R=10
Line 264: Line 264:


02.01 C CALCULATE LENGTH OF ARC OF RADIUS R, ANGLES A1 AND A2
02.01 C CALCULATE LENGTH OF ARC OF RADIUS R, ANGLES A1 AND A2
02.10 S A=(360 - FABS(A2-A1)) * (3.14159 / 180) * R</lang>
02.10 S A=(360 - FABS(A2-A1)) * (3.14159 / 180) * R</syntaxhighlight>
{{out}}
{{out}}
<pre>= 43.6332</pre>
<pre>= 43.6332</pre>
Line 270: Line 270:
=={{header|Fortran}}==
=={{header|Fortran}}==
The Fortran subroutine contains the MAX(DIF, 360. - DIF) operation. Other solutions presented here correspond to different interpretations of the problem. This subroutine computes the length of the major arc, which is not necessarily equal to distance traveling counter-clockwise.
The Fortran subroutine contains the MAX(DIF, 360. - DIF) operation. Other solutions presented here correspond to different interpretations of the problem. This subroutine computes the length of the major arc, which is not necessarily equal to distance traveling counter-clockwise.
<lang fortran>*-----------------------------------------------------------------------
<syntaxhighlight lang="fortran">*-----------------------------------------------------------------------
* given: polar coordinates of two points on a circle of known radius
* given: polar coordinates of two points on a circle of known radius
* find: length of the major arc between these points
* find: length of the major arc between these points
Line 317: Line 317:
END
END


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre> first angle: 120.000000 second angle: 10.0000000 radius: 10.0000000 Length of major arc: 43.6332321
<pre> first angle: 120.000000 second angle: 10.0000000 radius: 10.0000000 Length of major arc: 43.6332321
Line 326: Line 326:
=={{Header|FreeBASIC}}==
=={{Header|FreeBASIC}}==


<lang freebasic>
<syntaxhighlight lang="freebasic">
#define DEG 0.017453292519943295769236907684886127134
#define DEG 0.017453292519943295769236907684886127134


Line 334: Line 334:
print arclength(10, 10, 120)
print arclength(10, 10, 120)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 342: Line 342:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Julia}}
{{trans|Julia}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 355: Line 355:
func main() {
func main() {
fmt.Println(arcLength(10, 10, 120))
fmt.Println(arcLength(10, 10, 120))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 364: Line 364:
=={{header|Haskell}}==
=={{header|Haskell}}==
{{Trans|Julia}}
{{Trans|Julia}}
<lang Haskell>arcLength radius angle1 angle2 = (360.0 - (abs $ angle1 - angle2)) * pi * radius / 180.0
<syntaxhighlight lang="haskell">arcLength radius angle1 angle2 = (360.0 - (abs $ angle1 - angle2)) * pi * radius / 180.0


main = putStrLn $ "arcLength 10.0 10.0 120.0 = " ++ show (arcLength 10.0 10.0 120.0)</lang>
main = putStrLn $ "arcLength 10.0 10.0 120.0 = " ++ show (arcLength 10.0 10.0 120.0)</syntaxhighlight>
{{out}}
{{out}}
<pre>arcLength 10.0 10.0 120.0 = 43.63323129985823</pre>
<pre>arcLength 10.0 10.0 120.0 = 43.63323129985823</pre>


=={{header|Java}}==
=={{header|Java}}==
<lang java>public static double arcLength(double r, double a1, double a2){
<syntaxhighlight lang="java">public static double arcLength(double r, double a1, double a2){
return (360.0 - Math.abs(a2-a1))*Math.PI/180.0 * r;
return (360.0 - Math.abs(a2-a1))*Math.PI/180.0 * r;
}</lang>
}</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
{{Trans|AWK}}
{{Trans|AWK}}
<syntaxhighlight lang="javascript">
<lang JavaScript>
function arc_length(radius, angle1, angle2) {
function arc_length(radius, angle1, angle2) {
return (360 - Math.abs(angle2 - angle1)) * Math.PI / 180 * radius;
return (360 - Math.abs(angle2 - angle1)) * Math.PI / 180 * radius;
Line 383: Line 383:


console.log(arc_length(10, 10, 120).toFixed(7));
console.log(arc_length(10, 10, 120).toFixed(7));
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 398: Line 398:


In case you're wondering why `length` appears below where you might expect `abs`, rest assured that jq's `length` applied to a number yields its absolute value.
In case you're wondering why `length` appears below where you might expect `abs`, rest assured that jq's `length` applied to a number yields its absolute value.
<lang jq># Output is in the same units as radius; angles are in degrees.
<syntaxhighlight lang="jq"># Output is in the same units as radius; angles are in degrees.
def arclength(radius; angle1; angle2):
def arclength(radius; angle1; angle2):
def pi: 1 | atan * 4;
def pi: 1 | atan * 4;
Line 404: Line 404:


# The task:
# The task:
arclength(10; 10; 120)</lang>
arclength(10; 10; 120)</syntaxhighlight>
# {{out}}
# {{out}}
<pre>
<pre>
Line 412: Line 412:
=={{header|Julia}}==
=={{header|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.
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>
<syntaxhighlight lang="julia">
arclength(r, angle1, angle2) = (360 - abs(angle2 - angle1)) * π/180 * r
arclength(r, angle1, angle2) = (360 - abs(angle2 - angle1)) * π/180 * r
@show arclength(10, 10, 120) # --> arclength(10, 10, 120) = 43.63323129985823
@show arclength(10, 10, 120) # --> arclength(10, 10, 120) = 43.63323129985823
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Go}}
{{trans|Go}}
<lang scala>import kotlin.math.PI
<syntaxhighlight lang="scala">import kotlin.math.PI
import kotlin.math.abs
import kotlin.math.abs


Line 429: Line 429:
val al = arcLength(10.0, 10.0, 120.0)
val al = arcLength(10.0, 10.0, 120.0)
println("arc length: $al")
println("arc length: $al")
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>arc length: 43.63323129985823</pre>
<pre>arc length: 43.63323129985823</pre>
Line 435: Line 435:
=={{header|Lua}}==
=={{header|Lua}}==
{{trans|D}}
{{trans|D}}
<lang lua>function arcLength(radius, angle1, angle2)
<syntaxhighlight lang="lua">function arcLength(radius, angle1, angle2)
return (360.0 - math.abs(angle2 - angle1)) * math.pi * radius / 180.0
return (360.0 - math.abs(angle2 - angle1)) * math.pi * radius / 180.0
end
end
Line 443: Line 443:
end
end


main()</lang>
main()</syntaxhighlight>
{{out}}
{{out}}
<pre>arc length: 43.633231299858</pre>
<pre>arc length: 43.633231299858</pre>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[MajorArcLength]
<syntaxhighlight lang="mathematica">ClearAll[MajorArcLength]
MajorArcLength[r_, {a1_, a2_}] := Module[{d},
MajorArcLength[r_, {a1_, a2_}] := Module[{d},
d = Mod[Abs[a1 - a2], 360];
d = Mod[Abs[a1 - a2], 360];
Line 454: Line 454:
d Degree r
d Degree r
]
]
MajorArcLength[10, {10, 120}] // N</lang>
MajorArcLength[10, {10, 120}] // N</syntaxhighlight>
{{out}}
{{out}}
<pre>43.6332</pre>
<pre>43.6332</pre>


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import math, strformat
<syntaxhighlight lang="nim">import math, strformat


const TwoPi = 2 * Pi
const TwoPi = 2 * Pi
Line 469: Line 469:
result = r * (if d >= Pi: d else: TwoPi - d)
result = r * (if d >= Pi: d else: TwoPi - d)


echo &"Arc length: {arcLength(10, degToRad(10.0), degToRad(120.0)):.5f}"</lang>
echo &"Arc length: {arcLength(10, degToRad(10.0), degToRad(120.0)):.5f}"</syntaxhighlight>


{{out}}
{{out}}
Line 476: Line 476:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use utf8;
use utf8;
Line 500: Line 500:
[-90, 180, 10/π],
[-90, 180, 10/π],
[-90, 0, 10/π],
[-90, 0, 10/π],
[ 90, 0, 10/π];</lang>
[ 90, 0, 10/π];</syntaxhighlight>
{{out}}
{{out}}
<pre>Arc length: 43.63323 Parameters: (2.0943951, 0.1745329, 10.0000000)
<pre>Arc length: 43.63323 Parameters: (2.0943951, 0.1745329, 10.0000000)
Line 511: Line 511:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Julia}}
{{trans|Julia}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">arclength</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">angle1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">angle2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">arclength</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">angle1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">angle2</span><span style="color: #0000FF;">)</span>
Line 517: Line 517:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">arclength</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">120</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 43.6332313</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">arclength</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">120</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 43.6332313</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|Python}}==
=={{header|Python}}==
<lang Python>import math
<syntaxhighlight lang="python">import math


def arc_length(r, angleA, angleB):
def arc_length(r, angleA, angleB):
Line 526: Line 526:




</syntaxhighlight>
</lang>
<pre>
<pre>
radius = 10
radius = 10
Line 556: Line 556:
degrees to radians and a postfix ᵍ to convert gradians to radians.
degrees to radians and a postfix ᵍ to convert gradians to radians.


<lang perl6>sub arc ( Real \a1, Real \a2, :r(:$radius) = 1 ) {
<syntaxhighlight lang="raku" line>sub arc ( Real \a1, Real \a2, :r(:$radius) = 1 ) {
( ([-] (a2, a1).map((* + τ) % τ)) + τ ) % τ × $radius
( ([-] (a2, a1).map((* + τ) % τ)) + τ ) % τ × $radius
}
}
Line 575: Line 575:
\(175ᵍ, -45ᵍ, :r(10/π)) { # test gradian parameters
\(175ᵍ, -45ᵍ, :r(10/π)) { # test gradian parameters
printf "Arc length: %8s Parameters: %s\n", arc(|$_).round(.000001), $_.raku
printf "Arc length: %8s Parameters: %s\n", arc(|$_).round(.000001), $_.raku
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Task example: from 120° counter-clockwise to 10° with 10 unit radius
<pre>Task example: from 120° counter-clockwise to 10° with 10 unit radius
Line 593: Line 593:


This REXX version handles angles (in degrees) that may be &nbsp; <big> &gt; </big> &nbsp; 360º.
This REXX version handles angles (in degrees) that may be &nbsp; <big> &gt; </big> &nbsp; 360º.
<lang rexx>/*REXX program calculates the length of an arc between two angles (stated in degrees).*/
<syntaxhighlight 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*/
parse arg radius angle1 angle2 . /*obtain optional arguments from the CL*/
if radius=='' | radius=="," then radius= 10 /*Not specified? Then use the default.*/
if radius=='' | radius=="," then radius= 10 /*Not specified? Then use the default.*/
Line 608: Line 608:
arcLength: procedure; parse arg r,a1,a2; #=360; return (#-abs(a1//#-a2//#)) * pi()/180 * r
arcLength: procedure; parse arg r,a1,a2; #=360; return (#-abs(a1//#-a2//#)) * pi()/180 * r
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
pi: pi= 3.1415926535897932384626433832795; return pi /*use 32 digs (overkill).*/</lang>
pi: pi= 3.1415926535897932384626433832795; return pi /*use 32 digs (overkill).*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 619: Line 619:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
decimals(7)
decimals(7)
pi = 3.14159265
pi = 3.14159265
Line 629: Line 629:
x = (360 - fabs(angle2-angle1)) * pi / 180 * radius
x = (360 - fabs(angle2-angle1)) * pi / 180 * radius
return x
return x
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 638: Line 638:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|C}}
{{trans|C}}
<lang ruby>def arc_length(radius, angle1, angle2)
<syntaxhighlight lang="ruby">def arc_length(radius, angle1, angle2)
return (360.0 - (angle2 - angle1).abs) * Math::PI / 180.0 * radius
return (360.0 - (angle2 - angle1).abs) * Math::PI / 180.0 * radius
end
end


print "%.7f\n" % [arc_length(10, 10, 120)]</lang>
print "%.7f\n" % [arc_length(10, 10, 120)]</syntaxhighlight>
{{out}}
{{out}}
<pre>43.6332313</pre>
<pre>43.6332313</pre>
Line 648: Line 648:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|go}}
{{trans|go}}
<lang vlang>import math
<syntaxhighlight lang="vlang">import math


fn arc_length(radius f64, angle1 f64, angle2 f64) f64 {
fn arc_length(radius f64, angle1 f64, angle2 f64) f64 {
Line 655: Line 655:
fn main() {
fn main() {
println(arc_length(10, 10, 120))
println(arc_length(10, 10, 120))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>43.633231299858</pre>
<pre>43.633231299858</pre>
Line 661: Line 661:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Julia}}
{{trans|Julia}}
<lang ecmascript>var arcLength = Fn.new { |r, angle1, angle2| (360 - (angle2 - angle1).abs) * Num.pi / 180 * r }
<syntaxhighlight lang="ecmascript">var arcLength = Fn.new { |r, angle1, angle2| (360 - (angle2 - angle1).abs) * Num.pi / 180 * r }


System.print(arcLength.call(10, 10, 120))</lang>
System.print(arcLength.call(10, 10, 120))</syntaxhighlight>


{{out}}
{{out}}
Line 671: Line 671:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>def Pi = 3.14159265358979323846;
<syntaxhighlight lang="xpl0">def Pi = 3.14159265358979323846;


func real ArcLen(Radius, Angle1, Angle2); \Length of major arc of circle
func real ArcLen(Radius, Angle1, Angle2); \Length of major arc of circle
Line 682: Line 682:


RlOut(0, ArcLen(10., 10., 120.));
RlOut(0, ArcLen(10., 10., 120.));
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 691: Line 691:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|Julia}}
{{trans|Julia}}
<lang zkl>fcn arcLength(radius, angle1, angle2){
<syntaxhighlight lang="zkl">fcn arcLength(radius, angle1, angle2){
(360.0 - (angle2 - angle1).abs()).toRad()*radius
(360.0 - (angle2 - angle1).abs()).toRad()*radius
}
}
println(arcLength(10,10,120));</lang>
println(arcLength(10,10,120));</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>

Revision as of 17:46, 27 August 2022

Length of an arc between two angles is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
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   (note: this website leaves cookies).

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.

11l

Translation of: Python
F arc_length(r, angleA, angleB)
   R (360.0 - abs(angleB - angleA)) * math:pi * r / 180.0

print(arc_length(10, 10, 120))
Output:
43.6332

Action!

INCLUDE "H6:REALMATH.ACT"

PROC ArcLength(REAL POINTER r,a1,a2,len)
  REAL tmp1,tmp2,r180,r360,pi

  IntToReal(360,r360)
  IntToReal(180,r180)
  ValR("3.14159265",pi)
  RealAbsDiff(a1,a2,tmp1)     ;tmp1=abs(a1-a2)
  RealSub(r360,tmp1,tmp2) ;tmp2=360-abs(a1-a2)
  RealMult(tmp2,pi,tmp1)  ;tmp1=(360-abs(a1-a2))*pi
  RealMult(tmp1,r,tmp2)   ;tmp2=(360-abs(a1-a2))*pi*r
  RealDiv(tmp2,r180,len)  ;len=(360-abs(a1-a2))*pi*r/180
RETURN

PROC Main()
  REAL r,a1,a2,len

  Put(125) PutE() ;clear screen
  Print("Length of arc: ")
  IntToReal(10,r)
  IntToReal(10,a1)
  IntToReal(120,a2)
  ArcLength(r,a1,a2,len)
  PrintR(len)
RETURN
Output:

Screenshot from Atari 8-bit computer

Length of arc: 43.63323122

Ada

with Ada.Text_Io;
with Ada.Numerics;

procedure Calculate_Arc_Length is
   use Ada.Text_Io;

   type Angle_Type is new Float range 0.0 .. 360.0;       -- In degrees
   type Distance   is new Float range 0.0 .. Float'Last;  -- In units

   function Major_Arc_Length (Angle_1, Angle_2 : Angle_Type;
                              Radius           : Distance)
                             return Distance
   is
      Pi            : constant := Ada.Numerics.Pi;
      Circumference : constant Distance   := 2.0 * Pi * Radius;
      Major_Angle   : constant Angle_Type := 360.0 - abs (Angle_2 - Angle_1);
      Arc_Length    : constant Distance   :=
        Distance (Major_Angle) / 360.0 * Circumference;
   begin
      return Arc_Length;
   end Major_Arc_Length;

   package Distance_Io is new Ada.Text_Io.Float_Io (Distance);

   Arc_Length : constant Distance := Major_Arc_Length (Angle_1 =>  10.0,
                                                       Angle_2 => 120.0,
                                                       Radius  =>  10.0);
begin
   Put ("Arc length : ");
   Distance_Io.Put (Arc_Length, Exp => 0, Aft => 4);
   New_Line;
end Calculate_Arc_Length;
Output:
Arc length : 43.6332

ALGOL W

Follows the Fortran interpretation of the task and finds the length of the major arc.

begin
    % returns the length of the arc between the angles a and b on a circle of radius r %
    % the angles should  be specified in degrees                                       %
    real procedure majorArcLength( real value a, b, r ) ;
    begin
        real angle;
        angle := abs( a - b );
        while angle > 360 do angle := angle - 360;
        if angle < 180 then angle := 360 - angle;
        ( r * angle * PI ) / 180
    end majorArcLength ;
    % task test case                                                                   %
    write( r_w := 10, r_d := 4, r_format := "A", majorArcLength( 10, 120, 10 ) )
end.
Output:
   43.6332

APL

Works with: Dyalog APL
arc  (○÷180)×⊣×360-(|(-/))
Output:
      10 arc 10 120
43.6332313

AutoHotkey

MsgBox % result := arcLength(10, 10, 120)
return

arcLength(radius, angle1, angle2){
    return (360 - Abs(angle2-angle1)) * (π := 3.141592653589793) * radius / 180
}
Output:
43.633231

AWK

# syntax: GAWK -f LENGTH_OF_AN_ARC_BETWEEN_TWO_ANGLES.AWK
# converted from PHIX
BEGIN {
    printf("%.7f\n",arc_length(10,10,120))
    exit(0)
}
function arc_length(radius,angle1,angle2) {
    return (360 - abs(angle2-angle1)) * 3.14159265 / 180 * radius
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
Output:
43.6332313

BASIC

10 DATA 10, 10, 120
20 READ R, A1, A2
30 GOSUB 100
40 PRINT A
50 END
100 REM Calculate length of arc of radius R, angles A1 and A2
110 A = ATN(1)*R*(360-ABS(A1-A2))/45
120 RETURN
Output:
 43.6332

C

Translation of: AWK
#define PI 3.14159265358979323846
#define ABS(x)  (x<0?-x:x)

double arc_length(double radius, double angle1, double angle2) {
    return (360 - ABS(angle2 - angle1)) * PI / 180 * radius;
}

void main()
{
    printf("%.7f\n",arc_length(10, 10, 120));
}
Output:
43.6332313

C++

Translation of: Kotlin
#include <iostream>

#define _USE_MATH_DEFINES
#include <math.h>

double arcLength(double radius, double angle1, double angle2) {
    return (360.0 - abs(angle2 - angle1)) * M_PI * radius / 180.0;
}

int main() {
    auto al = arcLength(10.0, 10.0, 120.0);
    std::cout << "arc length: " << al << '\n';
    return 0;
}
Output:
arc length: 43.6332

D

Translation of: C++
import std.math;
import std.stdio;

double arcLength(double radius, double angle1, double angle2) {
    return (360.0 - abs(angle2 - angle1)) * PI * radius / 180.0;
}

void main() {
    writeln("arc length: ", arcLength(10.0, 10.0, 120.0));
}
Output:
arc length: 43.6332

Delphi

Translation of: AWK
program Length_of_an_arc;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils;

function arc_length(radius, angle1, angle2: Double): Double;
begin
  Result := (360 - abs(angle2 - angle1)) * PI / 180 * radius;
end;

begin
  Writeln(Format('%.7f', [arc_length(10, 10, 120)]));
  Readln;
end.
Output:
43.6332313

Factor

USING: kernel math math.constants math.trig prettyprint ;

: arc-length ( radius angle angle -- x )
    - abs deg>rad 2pi swap - * ;

10 10 120 arc-length .
Output:
43.63323129985824

FOCAL

01.10 S A1=10        ;C SET PARAMETERS
01.20 S A2=120
01.30 S R=10
01.40 D 2            ;C CALL SUBROUTINE 2
01.50 T %6.4,A,!     ;C DISPLAY RESULT
01.60 Q

02.01 C CALCULATE LENGTH OF ARC OF RADIUS R, ANGLES A1 AND A2
02.10 S A=(360 - FABS(A2-A1)) * (3.14159 / 180) * R
Output:
= 43.6332

Fortran

The Fortran subroutine contains the MAX(DIF, 360. - DIF) operation. Other solutions presented here correspond to different interpretations of the problem. This subroutine computes the length of the major arc, which is not necessarily equal to distance traveling counter-clockwise.

*-----------------------------------------------------------------------
* given:  polar coordinates of two points on a circle of known radius
* find:  length of the major arc between these points
*
*___Name_____Type___I/O___Description___________________________________
*   RAD      Real   In    Radius of circle, any unit of measure
*   ANG1     Real   In    Angle of first point, degrees
*   ANG2     Real   In    Angle of second point, degrees
*   MAJARC   Real   Out   Length of major arc, same units as RAD
*-----------------------------------------------------------------------
      FUNCTION MAJARC (RAD, ANG1, ANG2)
       IMPLICIT NONE
       REAL RAD, ANG1, ANG2, MAJARC

       REAL FACT                          ! degrees to radians
       PARAMETER (FACT = 3.1415926536 / 180.)
       REAL DIF

*       Begin
       MAJARC = 0.
       IF (RAD .LE. 0.) RETURN
       DIF = MOD(ABS(ANG1 - ANG2), 360.)   ! cyclic difference
       DIF = MAX(DIF, 360. - DIF)          ! choose the longer path
       MAJARC = RAD * DIF * FACT           ! L = r theta
       RETURN
      END  ! of majarc

*-----------------------------------------------------------------------
      PROGRAM TMA
       IMPLICIT NONE
       INTEGER J
       REAL ANG1, ANG2, RAD, MAJARC, ALENG
       REAL DATARR(3,3)     
       DATA DATARR / 120.,  10., 10.,
     $                10., 120., 10.,
     $               180., 270., 10. /

       DO J = 1, 3
         ANG1 = DATARR(1,J)
         ANG2 = DATARR(2,J)
         RAD = DATARR(3,J)
         ALENG = MAJARC (RAD, ANG1, ANG2)        
         PRINT *, 'first angle: ', ANG1, ' second angle: ', ANG2, 
     $     ' radius: ', RAD, ' Length of major arc: ', ALENG
       END DO
      END
Output:
 first angle:    120.000000      second angle:    10.0000000      radius:    10.0000000      Length of major arc:    43.6332321    
 first angle:    10.0000000      second angle:    120.000000      radius:    10.0000000      Length of major arc:    43.6332321    
 first angle:    180.000000      second angle:    270.000000      radius:    10.0000000      Length of major arc:    47.1238899    

FreeBASIC

#define DEG 0.017453292519943295769236907684886127134

function arclength( r as double, a1 as double, a2 as double ) as double
    return (360 - abs(a2 - a1)) * DEG * r
end function
 
print arclength(10, 10, 120)
Output:
 43.63323129985824

Go

Translation of: Julia
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))
}
Output:
43.63323129985823

Haskell

Translation of: Julia
arcLength radius angle1 angle2 = (360.0 - (abs $ angle1 - angle2)) * pi * radius / 180.0

main = putStrLn $ "arcLength 10.0 10.0 120.0 = " ++ show (arcLength 10.0 10.0 120.0)
Output:
arcLength 10.0 10.0 120.0 = 43.63323129985823

Java

public static double arcLength(double r, double a1, double a2){
    return (360.0 - Math.abs(a2-a1))*Math.PI/180.0 * r;
}

JavaScript

Translation of: AWK
function arc_length(radius, angle1, angle2) {
    return (360 - Math.abs(angle2 - angle1)) * Math.PI / 180 * radius;
}

console.log(arc_length(10, 10, 120).toFixed(7));
Output:
43.6332313

jq

Translation of: Julia
Works with: jq

Works with gojq, the Go implementation of jq

As noted in the entry for Julia, the function defined here does not correspond to the arc subtended by an angle.

In case you're wondering why `length` appears below where you might expect `abs`, rest assured that jq's `length` applied to a number yields its absolute value.

# Output is in the same units as radius; angles are in degrees.
def arclength(radius; angle1; angle2):
  def pi: 1 | atan * 4;
  (360 - ((angle2 - angle1)|length)) * (pi/180) * radius;

# The task:
arclength(10; 10; 120)
Output:
43.63323129985824

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.

arclength(r, angle1, angle2) =  (360 - abs(angle2 - angle1)) * π/180 * r
@show arclength(10, 10, 120)   # -->  arclength(10, 10, 120) = 43.63323129985823

Kotlin

Translation of: Go
import kotlin.math.PI
import kotlin.math.abs

fun arcLength(radius: Double, angle1: Double, angle2: Double): Double {
    return (360.0 - abs(angle2 - angle1)) * PI * radius / 180.0
}

fun main() {
    val al = arcLength(10.0, 10.0, 120.0)
    println("arc length: $al")
}
Output:
arc length: 43.63323129985823

Lua

Translation of: D
function arcLength(radius, angle1, angle2)
    return (360.0 - math.abs(angle2 - angle1)) * math.pi * radius / 180.0
end

function main()
    print("arc length: " .. arcLength(10.0, 10.0, 120.0))
end

main()
Output:
arc length: 43.633231299858

Mathematica/Wolfram Language

ClearAll[MajorArcLength]
MajorArcLength[r_, {a1_, a2_}] := Module[{d},
  d = Mod[Abs[a1 - a2], 360];
  d = Max[d, 360 - d]; (* this will select the major arc *)
  d Degree r
  ]
MajorArcLength[10, {10, 120}] // N
Output:
43.6332

Nim

import math, strformat

const TwoPi = 2 * Pi

func arcLength(r, a, b: float): float =
  ## Return the length of the major arc in a circle of radius "r"
  ## between angles "a" and "b" expressed in radians.
  let d = abs(a - b) mod TwoPi
  result = r * (if d >= Pi: d else: TwoPi - d)

echo &"Arc length: {arcLength(10, degToRad(10.0), degToRad(120.0)):.5f}"
Output:
Arc length: 43.63323

Perl

Translation of: Raku
use strict;
use warnings;
use utf8;
binmode STDOUT, ":utf8";
use POSIX 'fmod';

use constant π => 2 * atan2(1, 0);
use constant τ => 2 * π;

sub d2r { $_[0] * τ / 360 }

sub arc {
    my($a1, $a2, $r) = (d2r($_[0]), d2r($_[1]), $_[2]);
    my @a = map { fmod( ($_ + τ), τ) } ($a1, $a2);
    printf "Arc length: %8.5f  Parameters: (%9.7f, %10.7f, %10.7f)\n",
       (fmod(($a[0]-$a[1] + τ), τ) * $r), $a2, $a1, $r;
}

arc(@$_) for
    [ 10, 120,   10],
    [ 10, 120,    1],
    [120,  10,    1],
    [-90, 180, 10/π],
    [-90,   0, 10/π],
    [ 90,   0, 10/π];
Output:
Arc length: 43.63323  Parameters: (2.0943951, 0.1745329, 10.0000000)
Arc length: 43.63323  Parameters: (2.0943951,  0.1745329, 10.0000000)
Arc length:  4.36332  Parameters: (2.0943951,  0.1745329,  1.0000000)
Arc length:  1.91986  Parameters: (0.1745329,  2.0943951,  1.0000000)
Arc length: 15.00000  Parameters: (0.0000000, -1.5707963,  3.1830989)
Arc length:  5.00000  Parameters: (0.0000000,  1.5707963,  3.1830989)

Phix

Translation of: Julia
with javascript_semantics
function arclength(atom r, angle1, angle2)
    return (360 - abs(angle2 - angle1)) * PI/180 * r
end function
?arclength(10, 10, 120) -- 43.6332313

Python

import math

def arc_length(r, angleA, angleB):
    return (360.0 - abs(angleB - angleA)) * math.pi * r / 180.0
radius = 10
angleA = 10
angleB = 120

result = arc_length(radius, angleA, angleB)
print(result)

Output:
43.63323129985823

Raku

Works with: Rakudo version 2020.02

Taking a slightly different approach. Rather than the simplest thing that could possibly work, implements a reusable arc-length routine. Standard notation for angles has the zero to the right along an 'x' axis with a counter-clockwise rotation for increasing angles. This version follows convention and assumes the first given angle is "before" the second when rotating counter-clockwise. In order to return the major swept angle in the task example, you need to supply the "second" angle first. (The measurement will be from the first given angle counter-clockwise to the second.)

If you don't supply a radius, returns the radian arc angle which may then be multiplied by the radius to get actual circumferential length.

Works in radian angles by default but provides a postfix ° operator to convert degrees to radians and a postfix ᵍ to convert gradians to radians.

sub arc ( Real \a1, Real \a2, :r(:$radius) = 1 ) {
    ( ([-] (a2, a1).map((* + τ) % τ)) + τ ) % τ × $radius
}

sub postfix:<°> (\d) { d × τ / 360 }
sub postfix:<ᵍ> (\g) { g × τ / 400 }

say 'Task example: from 120° counter-clockwise to 10° with 10 unit radius';
say arc(:10radius, 120°, 10°), ' engineering units';

say "\nSome test examples:";
for \(120°, 10°), # radian magnitude (unit radius)
    \(10°, 120°), # radian magnitude (unit radius)
    \(:radius(10/π), 180°, -90°), # 20 unit circumference for ease of comparison
    \(0°, -90°, :r(10/π),),       #  ↓  ↓  ↓  ↓  ↓  ↓  ↓
    \(:radius(10/π), 0°, 90°),
    \(π/4, 7*π/4, :r(10/π)),
    \(175, -45, :r(10/π)) {  # test gradian parameters
    printf "Arc length: %8s  Parameters: %s\n", arc(|$_).round(.000001), $_.raku
}
Output:
Task example: from 120° counter-clockwise to 10° with 10 unit radius
43.63323129985824 engineering units

Some test examples:
Arc length: 4.363323  Parameters: \(2.0943951023931953e0, 0.17453292519943295e0)
Arc length: 1.919862  Parameters: \(0.17453292519943295e0, 2.0943951023931953e0)
Arc length:        5  Parameters: \(3.141592653589793e0, -1.5707963267948966e0, :radius(3.183098861837907e0))
Arc length:       15  Parameters: \(0e0, -1.5707963267948966e0, :r(3.183098861837907e0))
Arc length:        5  Parameters: \(0e0, 1.5707963267948966e0, :radius(3.183098861837907e0))
Arc length:       15  Parameters: \(0.7853981633974483e0, 5.497787143782138e0, :r(3.183098861837907e0))
Arc length:        9  Parameters: \(2.7488935718910685e0, -0.7068583470577035e0, :r(3.183098861837907e0))

REXX

Translation of: Julia

This REXX version handles angles (in degrees) that may be   >   360º.

/*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"º"          /*angles may be  negative  or  >  360º.*/
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; #=360; return (#-abs(a1//#-a2//#)) * pi()/180 * r
/*──────────────────────────────────────────────────────────────────────────────────────*/
pi:        pi= 3.1415926535897932384626433832795;  return pi   /*use 32 digs (overkill).*/
output   when using the default inputs:
     circle radius =  10
           angle 1 =  10º
           angle 2 =  120º

        arc length =  43.6332313

Ring

decimals(7)
pi = 3.14159265

see "Length of an arc between two angles:" + nl
see arcLength(10,10,120) + nl

func arcLength(radius,angle1,angle2)
     x = (360 - fabs(angle2-angle1)) * pi / 180 * radius
     return x
Output:
Length of an arc between two angles:
43.6332313

Ruby

Translation of: C
def arc_length(radius, angle1, angle2)
    return (360.0 - (angle2 - angle1).abs) * Math::PI / 180.0 * radius
end

print "%.7f\n" % [arc_length(10, 10, 120)]
Output:
43.6332313

Vlang

Translation of: go
import math

fn arc_length(radius f64, angle1 f64, angle2 f64) f64 {
    return (360 - math.abs(angle2-angle1)) * math.pi * radius/180
}
fn main() {
    println(arc_length(10, 10, 120))
}
Output:
43.633231299858

Wren

Translation of: Julia
var arcLength = Fn.new { |r, angle1, angle2| (360 - (angle2 - angle1).abs) * Num.pi / 180 * r }

System.print(arcLength.call(10, 10, 120))
Output:
43.633231299858

XPL0

def  Pi = 3.14159265358979323846;

func real ArcLen(Radius, Angle1, Angle2); \Length of major arc of circle
real Radius, Angle1, Angle2;
real Diff;
[Diff:= abs(Angle1 - Angle2);
Diff:= 360. - Diff;
return Pi * Radius / 180. * Diff;
];

RlOut(0, ArcLen(10., 10., 120.));
Output:
   43.63323

zkl

Translation of: Julia
fcn arcLength(radius, angle1, angle2){
   (360.0 - (angle2 - angle1).abs()).toRad()*radius
}
println(arcLength(10,10,120));
Output:
43.6332