Length of an arc between two angles: Difference between revisions
Added Easylang
(Added Easylang) |
|||
(21 intermediate revisions by 14 users not shown) | |||
Line 12:
{{trans|Python}}
<
R (360.0 - abs(angleB - angleA)) * math:pi * r / 180.0
print(arc_length(10, 10, 120))</
{{out}}
<pre>
43.6332
</pre>
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang="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</syntaxhighlight>
{{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]
<pre>
Length of arc: 43.63323122
</pre>
=={{header|Ada}}==
<
with Ada.Numerics;
Line 54 ⟶ 89:
Distance_Io.Put (Arc_Length, Exp => 0, Aft => 4);
New_Line;
end Calculate_Arc_Length;</
{{out}}
<pre>
Arc length : 43.6332
</pre>
Ada can be simple and concise:
<syntaxhighlight lang="ada">
with ada.float_text_io; use ada.float_text_io; -- for put()
with ada.numerics; use ada.numerics; -- for pi
with ada.text_io; use ada.text_io; -- for new_line
procedure arc_length_simple is
function arc_length(radius, deg1, deg2: Float) return Float is
((360.0 - abs(deg1 - deg2)) * pi * radius / 180.0);
begin
put(arc_length(10.0, 120.0, 10.0), fore=>0, aft=>15, exp=>0);
new_line;
end arc_length_simple;
</syntaxhighlight>
Ada can do automatic compile-time checking of types. In this example, degrees and radians cannot be accidentally mixed:
<syntaxhighlight lang="ada">
with ada.float_text_io; use ada.float_text_io; -- for put()
with ada.numerics; use ada.numerics; -- for pi
with ada.text_io; use ada.text_io; -- for new_line
procedure arc_length_both is
type Degree is new Float;
type Radian is new Float;
function arc_length(radius: Float; deg1, deg2: Degree) return Float is
((360.0 - abs(Float(deg1) - Float(deg2))) * radius * pi / 180.0);
function arc_length(radius: Float; rad1, rad2: Radian) return Float is
((2.0 * pi - abs(Float(rad1) - Float(rad2))) * radius);
d1 : Degree := 120.0;
d2 : Degree := 10.0;
r1 : Radian := Radian(d1) * pi / 180.0;
r2 : Radian := Radian(d2) * pi / 180.0;
begin
put(arc_length(10.0, d1, d2), fore=>0, aft=>15, exp=>0);
new_line;
put(arc_length(10.0, r1, r2), fore=>0, aft=>15, exp=>0);
new_line;
-- Next line will not compile as you cannot mix Degree and Radian
-- put(arc_length(10.0, d1, r2), fore=>0, aft=>15, exp=>0);
end arc_length_both;
</syntaxhighlight>
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
<syntaxhighlight lang="algol68">
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 #
PROC major arc length = ( REAL a, b, r )REAL:
BEGIN
REAL angle := ABS ( a - b );
WHILE angle > 360 DO angle -:= 360 OD;
IF angle < 180 THEN angle := 360 - angle FI;
( r * angle * pi ) / 180
END # majorArcLength # ;
# task test case #
print( ( fixed( major arc length( 10, 120, 10 ), -10, 4 ), newline ) )
END
</syntaxhighlight>
{{out}}
<pre>
43.6332
</pre>
=={{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 75 ⟶ 179:
% task test case %
write( r_w := 10, r_d := 4, r_format := "A", majorArcLength( 10, 120, 10 ) )
end.</
{{out}}
<pre>
Line 83 ⟶ 187:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{out}}
<pre> 10 arc 10 120
43.6332313</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">degToRad: function [deg]-> deg * pi // 180
doublePi: 2 * pi
arcLength: function [r, a, b][
d: (abs a-b) % doublePi
return r * (d >= pi)? -> d -> doublePi - d
]
print ["Arc length:" to :string .format:".5f" arcLength 10 degToRad 10.0 degToRad 120.0]</syntaxhighlight>
{{out}}
<pre>Arc length: 43.63323</pre>
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">MsgBox % result := arcLength(10, 10, 120)
return
arcLength(radius, angle1, angle2){
return (360 - Abs(angle2-angle1)) * (π := 3.141592653589793) * radius / 180
}</syntaxhighlight>
{{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 100 ⟶ 230:
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
</syntaxhighlight>
{{out}}
<pre>
Line 107 ⟶ 237:
=={{header|BASIC}}==
<
20 READ R, A1, A2
30 GOSUB 100
Line 114 ⟶ 244:
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 120 ⟶ 250:
=={{header|C}}==
{{Trans|AWK}}
<syntaxhighlight lang="c">
#define PI 3.14159265358979323846
#define ABS(x) (x<0?-x:x)
Line 132 ⟶ 262:
printf("%.7f\n",arc_length(10, 10, 120));
}
</syntaxhighlight>
{{out}}
<pre>
Line 140 ⟶ 270:
=={{header|C++}}==
{{trans|Kotlin}}
<
#define _USE_MATH_DEFINES
Line 153 ⟶ 283:
std::cout << "arc length: " << al << '\n';
return 0;
}</
{{out}}
<pre>arc length: 43.6332</pre>
Line 159 ⟶ 289:
=={{header|D}}==
{{trans|C++}}
<
import std.stdio;
Line 168 ⟶ 298:
void main() {
writeln("arc length: ", arcLength(10.0, 10.0, 120.0));
}</
{{out}}
<pre>arc length: 43.6332</pre>
Line 174 ⟶ 304:
=={{header|Delphi}}==
{{Trans|AWK}}
<syntaxhighlight lang="delphi">
program Length_of_an_arc;
Line 192 ⟶ 322:
Readln;
end.
</syntaxhighlight>
{{out}}
<pre>
43.6332313
</pre>
=={{header|EasyLang}}==
{{trans|Python}}
<syntaxhighlight>
func arc_length r angleA angleB .
return (360 - abs (angleB - angleA)) * pi * r / 180
.
print arc_length 10 10 120
</syntaxhighlight>
{{out}}
<pre>
43.63
</pre>
=={{header|Factor}}==
<
: arc-length ( radius angle angle -- x )
- abs deg>rad 2pi swap - * ;
10 10 120 arc-length .</
{{out}}
<pre>
Line 211 ⟶ 354:
=={{header|FOCAL}}==
<
01.20 S A2=120
01.30 S R=10
Line 219 ⟶ 362:
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 225 ⟶ 368:
=={{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 272 ⟶ 415:
END
</syntaxhighlight>
{{out}}
<pre> first angle: 120.000000 second angle: 10.0000000 radius: 10.0000000 Length of major arc: 43.6332321
Line 281 ⟶ 424:
=={{Header|FreeBASIC}}==
<
#define DEG 0.017453292519943295769236907684886127134
Line 289 ⟶ 432:
print arclength(10, 10, 120)
</syntaxhighlight>
{{out}}
<pre>
Line 297 ⟶ 440:
=={{header|Go}}==
{{trans|Julia}}
<
import (
Line 310 ⟶ 453:
func main() {
fmt.Println(arcLength(10, 10, 120))
}</
{{out}}
Line 319 ⟶ 462:
=={{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|J}}==
Interpreting the task requirements as asking for the length of a clockwise arc between two angles whose values are provided in degrees.
<syntaxhighlight lang=J>clockwise=: - + 360 * < NB. clockwise effective angle between two provided angles
length=: * 2r360p1 * ]</syntaxhighlight> NB. use radius to find length of angle
Example use:
<syntaxhighlight lang=J> 10 length 10 clockwise 120
43.6332
5 length 10 clockwise 120
21.8166
10 length 120 clockwise 90
5.23599</syntaxhighlight>
=={{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 338 ⟶ 496:
console.log(arc_length(10, 10, 120).toFixed(7));
</syntaxhighlight>
{{out}}
Line 353 ⟶ 511:
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 359 ⟶ 517:
# The task:
arclength(10; 10; 120)</
# {{out}}
<pre>
Line 367 ⟶ 525:
=={{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 384 ⟶ 542:
val al = arcLength(10.0, 10.0, 120.0)
println("arc length: $al")
}</
{{out}}
<pre>arc length: 43.63323129985823</pre>
Line 390 ⟶ 548:
=={{header|Lua}}==
{{trans|D}}
<
return (360.0 - math.abs(angle2 - angle1)) * math.pi * radius / 180.0
end
Line 398 ⟶ 556:
end
main()</
{{out}}
<pre>arc length: 43.633231299858</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">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</syntaxhighlight>
{{out}}
<pre>43.6332</pre>
=={{header|Nim}}==
<
const TwoPi = 2 * Pi
Line 413 ⟶ 582:
result = r * (if d >= Pi: d else: TwoPi - d)
echo &"Arc length: {arcLength(10, degToRad(10.0), degToRad(120.0)):.5f}"</
{{out}}
Line 420 ⟶ 589:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use utf8;
Line 444 ⟶ 613:
[-90, 180, 10/π],
[-90, 0, 10/π],
[ 90, 0, 10/π];</
{{out}}
<pre>Arc length: 43.63323 Parameters: (2.0943951, 0.1745329, 10.0000000)
Line 455 ⟶ 624:
=={{header|Phix}}==
{{trans|Julia}}
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">360</span> <span style="color: #0000FF;">-</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angle2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">angle1</span><span style="color: #0000FF;">))</span> <span style="color: #0000FF;">*</span> <span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">180</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">r</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>
<!--</syntaxhighlight>-->
=={{header|Python}}==
<
def arc_length(r, angleA, angleB):
Line 467 ⟶ 639:
</syntaxhighlight>
<pre>
radius = 10
Line 497 ⟶ 669:
degrees to radians and a postfix ᵍ to convert gradians to radians.
<syntaxhighlight lang="raku"
( ([-] (a2, a1).map((* + τ) % τ)) + τ ) % τ × $radius
}
Line 516 ⟶ 688:
\(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 534 ⟶ 706:
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 549 ⟶ 721:
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 560 ⟶ 732:
=={{header|Ring}}==
<
decimals(7)
pi = 3.14159265
Line 570 ⟶ 742:
x = (360 - fabs(angle2-angle1)) * pi / 180 * radius
return x
</syntaxhighlight>
{{out}}
<pre>
Length of an arc between two angles:
43.6332313
</pre>
=={{header|RPL}}==
The function also works when the difference between the two angles is greater than 180 degrees.
≪ DEG - ABS 360 OVER - MAX
* 180 / π * →NUM
≫ '<span style="color:blue">MAJARC</span>' STO
10 10 120 <span style="color:blue">MAJARC</span>
10 10 350 <span style="color:blue">MAJARC</span>
{{out}}
<pre>
2: 43.6332312999
1: 59.3411945678
</pre>
=={{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>
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">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))
}</syntaxhighlight>
{{out}}
<pre>43.633231299858</pre>
=={{header|Wren}}==
{{trans|Julia}}
<
System.print(arcLength.call(10, 10, 120))</
{{out}}
<pre>
43.633231299858
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang="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.));
</syntaxhighlight>
{{out}}
<pre>
43.63323
</pre>
=={{header|zkl}}==
{{trans|Julia}}
<
(360.0 - (angle2 - angle1).abs()).toRad()*radius
}
println(arcLength(10,10,120));</
{{out}}
<pre>
|