Length of an arc between two angles: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 12:
{{trans|Python}}
<
R (360.0 - abs(angleB - angleA)) * math:pi * r / 180.0
print(arc_length(10, 10, 120))</
{{out}}
Line 25:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<
PROC ArcLength(REAL POINTER r,a1,a2,len)
Line 50:
ArcLength(r,a1,a2,len)
PrintR(len)
RETURN</
{{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}}==
<
with Ada.Numerics;
Line 89:
Distance_Io.Put (Arc_Length, Exp => 0, Aft => 4);
New_Line;
end Calculate_Arc_Length;</
{{out}}
<pre>
Line 97:
=={{header|ALGOL W}}==
Follows the Fortran interpretation of the task and finds the length of the major arc.
<
% 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.</
{{out}}
<pre>
Line 118:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{out}}
<pre> 10 arc 10 120
Line 124:
=={{header|AutoHotkey}}==
<
return
arcLength(radius, angle1, angle2){
return (360 - Abs(angle2-angle1)) * (π := 3.141592653589793) * radius / 180
}</
{{out}}
<pre>43.633231</pre>
=={{header|AWK}}==
<syntaxhighlight 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>
{{out}}
<pre>
Line 152:
=={{header|BASIC}}==
<
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</
{{out}}
<pre> 43.6332</pre>
Line 165:
=={{header|C}}==
{{Trans|AWK}}
<syntaxhighlight lang="c">
#define PI 3.14159265358979323846
#define ABS(x) (x<0?-x:x)
Line 177:
printf("%.7f\n",arc_length(10, 10, 120));
}
</syntaxhighlight>
{{out}}
<pre>
Line 185:
=={{header|C++}}==
{{trans|Kotlin}}
<
#define _USE_MATH_DEFINES
Line 198:
std::cout << "arc length: " << al << '\n';
return 0;
}</
{{out}}
<pre>arc length: 43.6332</pre>
Line 204:
=={{header|D}}==
{{trans|C++}}
<
import std.stdio;
Line 213:
void main() {
writeln("arc length: ", arcLength(10.0, 10.0, 120.0));
}</
{{out}}
<pre>arc length: 43.6332</pre>
Line 219:
=={{header|Delphi}}==
{{Trans|AWK}}
<syntaxhighlight lang="delphi">
program Length_of_an_arc;
Line 237:
Readln;
end.
</syntaxhighlight>
{{out}}
<pre>
Line 244:
=={{header|Factor}}==
<
: arc-length ( radius angle angle -- x )
- abs deg>rad 2pi swap - * ;
10 10 120 arc-length .</
{{out}}
<pre>
Line 256:
=={{header|FOCAL}}==
<
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</
{{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.
<
* 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>
{{out}}
<pre> first angle: 120.000000 second angle: 10.0000000 radius: 10.0000000 Length of major arc: 43.6332321
Line 326:
=={{Header|FreeBASIC}}==
<
#define DEG 0.017453292519943295769236907684886127134
Line 334:
print arclength(10, 10, 120)
</syntaxhighlight>
{{out}}
<pre>
Line 342:
=={{header|Go}}==
{{trans|Julia}}
<
import (
Line 355:
func main() {
fmt.Println(arcLength(10, 10, 120))
}</
{{out}}
Line 364:
=={{header|Haskell}}==
{{Trans|Julia}}
<
main = putStrLn $ "arcLength 10.0 10.0 120.0 = " ++ show (arcLength 10.0 10.0 120.0)</
{{out}}
<pre>arcLength 10.0 10.0 120.0 = 43.63323129985823</pre>
=={{header|Java}}==
<
return (360.0 - Math.abs(a2-a1))*Math.PI/180.0 * r;
}</
=={{header|JavaScript}}==
{{Trans|AWK}}
<syntaxhighlight 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>
{{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.
<
def arclength(radius; angle1; angle2):
def pi: 1 | atan * 4;
Line 404:
# The task:
arclength(10; 10; 120)</
# {{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.
<
arclength(r, angle1, angle2) = (360 - abs(angle2 - angle1)) * π/180 * r
@show arclength(10, 10, 120) # --> arclength(10, 10, 120) = 43.63323129985823
</syntaxhighlight>
=={{header|Kotlin}}==
{{trans|Go}}
<
import kotlin.math.abs
Line 429:
val al = arcLength(10.0, 10.0, 120.0)
println("arc length: $al")
}</
{{out}}
<pre>arc length: 43.63323129985823</pre>
Line 435:
=={{header|Lua}}==
{{trans|D}}
<
return (360.0 - math.abs(angle2 - angle1)) * math.pi * radius / 180.0
end
Line 443:
end
main()</
{{out}}
<pre>arc length: 43.633231299858</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
MajorArcLength[r_, {a1_, a2_}] := Module[{d},
d = Mod[Abs[a1 - a2], 360];
Line 454:
d Degree r
]
MajorArcLength[10, {10, 120}] // N</
{{out}}
<pre>43.6332</pre>
=={{header|Nim}}==
<
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}"</
{{out}}
Line 476:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use utf8;
Line 500:
[-90, 180, 10/π],
[-90, 0, 10/π],
[ 90, 0, 10/π];</
{{out}}
<pre>Arc length: 43.63323 Parameters: (2.0943951, 0.1745329, 10.0000000)
Line 511:
=={{header|Phix}}==
{{trans|Julia}}
<!--<
<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>
<!--</
=={{header|Python}}==
<
def arc_length(r, angleA, angleB):
Line 526:
</syntaxhighlight>
<pre>
radius = 10
Line 556:
degrees to radians and a postfix ᵍ to convert gradians to radians.
<syntaxhighlight lang="raku"
( ([-] (a2, a1).map((* + τ) % τ)) + τ ) % τ × $radius
}
Line 575:
\(175ᵍ, -45ᵍ, :r(10/π)) { # test gradian parameters
printf "Arc length: %8s Parameters: %s\n", arc(|$_).round(.000001), $_.raku
}</
{{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 <big> > </big> 360º.
<
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).*/</
{{out|output|text= when using the default inputs:}}
<pre>
Line 619:
=={{header|Ring}}==
<
decimals(7)
pi = 3.14159265
Line 629:
x = (360 - fabs(angle2-angle1)) * pi / 180 * radius
return x
</syntaxhighlight>
{{out}}
<pre>
Line 638:
=={{header|Ruby}}==
{{trans|C}}
<
return (360.0 - (angle2 - angle1).abs) * Math::PI / 180.0 * radius
end
print "%.7f\n" % [arc_length(10, 10, 120)]</
{{out}}
<pre>43.6332313</pre>
Line 648:
=={{header|Vlang}}==
{{trans|go}}
<
fn arc_length(radius f64, angle1 f64, angle2 f64) f64 {
Line 655:
fn main() {
println(arc_length(10, 10, 120))
}</
{{out}}
<pre>43.633231299858</pre>
Line 661:
=={{header|Wren}}==
{{trans|Julia}}
<
System.print(arcLength.call(10, 10, 120))</
{{out}}
Line 671:
=={{header|XPL0}}==
<
func real ArcLen(Radius, Angle1, Angle2); \Length of major arc of circle
Line 682:
RlOut(0, ArcLen(10., 10., 120.));
</syntaxhighlight>
{{out}}
Line 691:
=={{header|zkl}}==
{{trans|Julia}}
<
(360.0 - (angle2 - angle1).abs()).toRad()*radius
}
println(arcLength(10,10,120));</
{{out}}
<pre>
|