Length of an arc between two angles: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 12:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F arc_length(r, angleA, angleB)
R (360.0 - abs(angleB - angleA)) * math:pi * r / 180.0
 
print(arc_length(10, 10, 120))</langsyntaxhighlight>
 
{{out}}
Line 25:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:REALMATH.ACT"
 
PROC ArcLength(REAL POINTER r,a1,a2,len)
Line 50:
ArcLength(r,a1,a2,len)
PrintR(len)
RETURN</langsyntaxhighlight>
{{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]
Line 58:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Numerics;
 
Line 89:
Distance_Io.Put (Arc_Length, Exp => 0, Aft => 4);
New_Line;
end Calculate_Arc_Length;</langsyntaxhighlight>
{{out}}
<pre>
Line 97:
=={{header|ALGOL W}}==
Follows the Fortran interpretation of the task and finds the length of the major arc.
<langsyntaxhighlight lang="algolw">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 %
Line 110:
% task test case %
write( r_w := 10, r_d := 4, r_format := "A", majorArcLength( 10, 120, 10 ) )
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 118:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">arc ← (○÷180)×⊣×360-(|(-/⊢))</langsyntaxhighlight>
{{out}}
<pre> 10 arc 10 120
Line 124:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % result := arcLength(10, 10, 120)
return
 
arcLength(radius, angle1, angle2){
return (360 - Abs(angle2-angle1)) * (π := 3.141592653589793) * radius / 180
}</langsyntaxhighlight>
{{out}}
<pre>43.633231</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LENGTH_OF_AN_ARC_BETWEEN_TWO_ANGLES.AWK
# converted from PHIX
Line 145:
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 152:
 
=={{header|BASIC}}==
<langsyntaxhighlight BASIClang="basic">10 DATA 10, 10, 120
20 READ R, A1, A2
30 GOSUB 100
Line 159:
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</langsyntaxhighlight>
{{out}}
<pre> 43.6332</pre>
Line 165:
=={{header|C}}==
{{Trans|AWK}}
<syntaxhighlight lang="c">
<lang c>
#define PI 3.14159265358979323846
#define ABS(x) (x<0?-x:x)
Line 177:
printf("%.7f\n",arc_length(10, 10, 120));
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 185:
=={{header|C++}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
#define _USE_MATH_DEFINES
Line 198:
std::cout << "arc length: " << al << '\n';
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>arc length: 43.6332</pre>
Line 204:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.math;
import std.stdio;
 
Line 213:
void main() {
writeln("arc length: ", arcLength(10.0, 10.0, 120.0));
}</langsyntaxhighlight>
{{out}}
<pre>arc length: 43.6332</pre>
Line 219:
=={{header|Delphi}}==
{{Trans|AWK}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Length_of_an_arc;
 
Line 237:
Readln;
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 244:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="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 .</langsyntaxhighlight>
{{out}}
<pre>
Line 256:
 
=={{header|FOCAL}}==
<langsyntaxhighlight FOCALlang="focal">01.10 S A1=10 ;C SET PARAMETERS
01.20 S A2=120
01.30 S R=10
Line 264:
 
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</langsyntaxhighlight>
{{out}}
<pre>= 43.6332</pre>
Line 270:
=={{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.
<langsyntaxhighlight lang="fortran">*-----------------------------------------------------------------------
* given: polar coordinates of two points on a circle of known radius
* find: length of the major arc between these points
Line 317:
END
 
</syntaxhighlight>
</lang>
{{out}}
<pre> first angle: 120.000000 second angle: 10.0000000 radius: 10.0000000 Length of major arc: 43.6332321
Line 326:
=={{Header|FreeBASIC}}==
 
<langsyntaxhighlight lang="freebasic">
#define DEG 0.017453292519943295769236907684886127134
 
Line 334:
print arclength(10, 10, 120)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 342:
=={{header|Go}}==
{{trans|Julia}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 355:
func main() {
fmt.Println(arcLength(10, 10, 120))
}</langsyntaxhighlight>
 
{{out}}
Line 364:
=={{header|Haskell}}==
{{Trans|Julia}}
<langsyntaxhighlight Haskelllang="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)</langsyntaxhighlight>
{{out}}
<pre>arcLength 10.0 10.0 120.0 = 43.63323129985823</pre>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public static double arcLength(double r, double a1, double a2){
return (360.0 - Math.abs(a2-a1))*Math.PI/180.0 * r;
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
{{Trans|AWK}}
<syntaxhighlight lang="javascript">
<lang JavaScript>
function arc_length(radius, angle1, angle2) {
return (360 - Math.abs(angle2 - angle1)) * Math.PI / 180 * radius;
Line 383:
 
console.log(arc_length(10, 10, 120).toFixed(7));
</syntaxhighlight>
</lang>
 
{{out}}
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.
<langsyntaxhighlight lang="jq"># Output is in the same units as radius; angles are in degrees.
def arclength(radius; angle1; angle2):
def pi: 1 | atan * 4;
Line 404:
 
# The task:
arclength(10; 10; 120)</langsyntaxhighlight>
# {{out}}
<pre>
Line 412:
=={{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.
<langsyntaxhighlight lang="julia">
arclength(r, angle1, angle2) = (360 - abs(angle2 - angle1)) * π/180 * r
@show arclength(10, 10, 120) # --> arclength(10, 10, 120) = 43.63323129985823
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">import kotlin.math.PI
import kotlin.math.abs
 
Line 429:
val al = arcLength(10.0, 10.0, 120.0)
println("arc length: $al")
}</langsyntaxhighlight>
{{out}}
<pre>arc length: 43.63323129985823</pre>
Line 435:
=={{header|Lua}}==
{{trans|D}}
<langsyntaxhighlight lang="lua">function arcLength(radius, angle1, angle2)
return (360.0 - math.abs(angle2 - angle1)) * math.pi * radius / 180.0
end
Line 443:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>arc length: 43.633231299858</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[MajorArcLength]
MajorArcLength[r_, {a1_, a2_}] := Module[{d},
d = Mod[Abs[a1 - a2], 360];
Line 454:
d Degree r
]
MajorArcLength[10, {10, 120}] // N</langsyntaxhighlight>
{{out}}
<pre>43.6332</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
const TwoPi = 2 * Pi
Line 469:
result = r * (if d >= Pi: d else: TwoPi - d)
 
echo &"Arc length: {arcLength(10, degToRad(10.0), degToRad(120.0)):.5f}"</langsyntaxhighlight>
 
{{out}}
Line 476:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use utf8;
Line 500:
[-90, 180, 10/π],
[-90, 0, 10/π],
[ 90, 0, 10/π];</langsyntaxhighlight>
{{out}}
<pre>Arc length: 43.63323 Parameters: (2.0943951, 0.1745329, 10.0000000)
Line 511:
=={{header|Phix}}==
{{trans|Julia}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 517:
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|Python}}==
<langsyntaxhighlight Pythonlang="python">import math
 
def arc_length(r, angleA, angleB):
Line 526:
 
 
</syntaxhighlight>
</lang>
<pre>
radius = 10
Line 556:
degrees to radians and a postfix ᵍ to convert gradians to radians.
 
<syntaxhighlight lang="raku" perl6line>sub arc ( Real \a1, Real \a2, :r(:$radius) = 1 ) {
( ([-] (a2, a1).map((* + τ) % τ)) + τ ) % τ × $radius
}
Line 575:
\(175ᵍ, -45ᵍ, :r(10/π)) { # test gradian parameters
printf "Arc length: %8s Parameters: %s\n", arc(|$_).round(.000001), $_.raku
}</langsyntaxhighlight>
{{out}}
<pre>Task example: from 120° counter-clockwise to 10° with 10 unit radius
Line 593:
 
This REXX version handles angles (in degrees) that may be &nbsp; <big> &gt; </big> &nbsp; 360º.
<langsyntaxhighlight 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.*/
Line 608:
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).*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 619:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(7)
pi = 3.14159265
Line 629:
x = (360 - fabs(angle2-angle1)) * pi / 180 * radius
return x
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 638:
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang="ruby">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)]</langsyntaxhighlight>
{{out}}
<pre>43.6332313</pre>
Line 648:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">import math
 
fn arc_length(radius f64, angle1 f64, angle2 f64) f64 {
Line 655:
fn main() {
println(arc_length(10, 10, 120))
}</langsyntaxhighlight>
{{out}}
<pre>43.633231299858</pre>
Line 661:
=={{header|Wren}}==
{{trans|Julia}}
<langsyntaxhighlight lang="ecmascript">var arcLength = Fn.new { |r, angle1, angle2| (360 - (angle2 - angle1).abs) * Num.pi / 180 * r }
 
System.print(arcLength.call(10, 10, 120))</langsyntaxhighlight>
 
{{out}}
Line 671:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">def Pi = 3.14159265358979323846;
 
func real ArcLen(Radius, Angle1, Angle2); \Length of major arc of circle
Line 682:
 
RlOut(0, ArcLen(10., 10., 120.));
</syntaxhighlight>
</lang>
 
{{out}}
Line 691:
=={{header|zkl}}==
{{trans|Julia}}
<langsyntaxhighlight lang="zkl">fcn arcLength(radius, angle1, angle2){
(360.0 - (angle2 - angle1).abs()).toRad()*radius
}
println(arcLength(10,10,120));</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits