Rodrigues’ rotation formula: Difference between revisions

m
syntax highlighting fixup automation
(Added credits)
m (syntax highlighting fixup automation)
Line 12:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Numerics.Elementary_Functions;
Line 55:
Theta => Angle(Source, Target))));
end Rodrigues;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 65:
=={{header|ALGOL 68}}==
{{Trans|JavaScript}}
<langsyntaxhighlight lang="algol68">BEGIN # Rodrigues' Rotation Formula #
MODE VECTOR = [ 3 ]REAL;
MODE MATRIX = [ 3 ]VECTOR;
Line 105:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 113:
=={{header|AutoHotkey}}==
{{Trans|JavaScript}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">v1 := [5,-6,4]
v2 := [8,5,-30]
a := getAngle(v1, v2)
Line 149:
, [z*x*t - y*sa, z*y*t + x*sa, ca + z*z*t]]
return matrixMultiply(r, p)
}</langsyntaxhighlight>
{{out}}
<pre>[2.232221, 1.395138, -8.370829]</pre>
Line 155:
=={{header|C}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 214:
printf("[%.13f, %.13f, %.13f]\n", np.x, np.y, np.z);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 249:
{{trans|JavaScript}}
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: grouping kernel math math.functions math.matrices
math.vectors prettyprint sequences sequences.generalizations ;
 
Line 261:
 
{ 5 -6 4 } { 8 5 -30 }
dupd [ cross normalize ] [ angle-between ] 2bi a-rotate .</langsyntaxhighlight>
{{out}}
<pre>
Line 269:
=={{header|FreeBASIC}}==
This example rotates the vector [-1, 2, -0.4] around the axis [-1, 2, 1] in increments of 18 degrees.
<langsyntaxhighlight lang="freebasic">#define PI 3.14159265358979323
 
type vector
Line 330:
r = rodrigues( v, k, theta )
print using "##.### [##.### ##.### ##.###]"; theta; r.x; r.y; r.z
next theta</langsyntaxhighlight>
{{out}}<pre>
Theta rotated vector
Line 359:
=={{header|Go}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 414:
np := aRotate(v1, ncp, a)
fmt.Println(np)
}</langsyntaxhighlight>
 
{{out}}
Line 423:
=={{header|JavaScript}}==
===JavaScript: ES5===
<langsyntaxhighlight lang="javascript">function norm(v) {
return Math.sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
}
Line 458:
var ncp = normalize(cp);
var np = aRotate(v1, ncp, a);
console.log(np); </langsyntaxhighlight>
 
===JavaScript: ES6===
Line 467:
and is not available to all JavaScript interpreters)
 
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
"use strict";
 
Line 613:
null, 2
);
})();</langsyntaxhighlight>
{{Out}}
<pre>[
Line 629:
of numbers. Some of the functions have been generalized to work with vectors
of arbitrary length.
<syntaxhighlight lang="jq">
<lang jq>
# v1 and v2 should be vectors of the same length.
def dotProduct(v1; v2): [v1, v2] | transpose | map(.[0] * .[1]) | add;
Line 677:
;
 
example</langsyntaxhighlight>
{{out}}
<pre>
Line 686:
=={{header|Julia}}==
{{trans|Perl}}
<langsyntaxhighlight lang="julia">using LinearAlgebra # use builtin library for normalize, cross, dot
using JSON3
 
Line 707:
np = rodrotate(v1, ncp, a)
JSON3.write(np) # "[2.2322210733082284,1.3951381708176411,-8.370829024905854]"
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{trans|Wren}}
Only changed most function names.
<langsyntaxhighlight Nimlang="nim">import math
 
type
Line 753:
nvp = normalized(vp)
np = v1.rotate(nvp, a)
echo np</langsyntaxhighlight>
 
{{out}}
Line 760:
=={{header|Perl}}==
===Task-specific===
<langsyntaxhighlight lang="perl">#!perl -w
use strict;
use Math::Trig; # acos
Line 816:
my $json=JSON->new->canonical;
 
print $json->encode($np) . "\n";</langsyntaxhighlight>
{{out}}
<pre>[2.23222107330823,1.39513817081764,-8.37082902490585]</pre>
===Generalized===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <say signatures>;
Line 852:
 
my($v1,$v2) = ([5, -6, 4], [8, 5, -30]);
say join ' ', @{aRotate $v1, normalize(crossProduct $v1, $v2), getAngle $v1, $v2};</langsyntaxhighlight>
{{out}}
<pre>2.23222107330823 1.39513817081764 -8.37082902490585</pre>
Line 858:
=={{header|Phix}}==
{{trans|JavaScript}}
<!--<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;">norm</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
Line 901:
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">aRotate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ncp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">);</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">np</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 909:
=={{header|Processing}}==
{{trans|C}}
<langsyntaxhighlight lang="java">
//Aamrun, 30th June 2022
 
Line 978:
}
 
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub infix:<⋅> { [+] @^a »×« @^b }
sub norm (@v) { sqrt @v⋅@v }
sub normalize (@v) { @v X/ @v.&norm }
Line 1,004:
my @v1 = [5,-6, 4];
my @v2 = [8, 5,-30];
say join ' ', aRotate @v1, normalize(crossProduct @v1, @v2), getAngle @v1, @v2;</langsyntaxhighlight>
{{out}}
<pre>2.232221073308229 1.3951381708176411 -8.370829024905852</pre>
Line 1,010:
Alternately, differing mostly in style:
 
<syntaxhighlight lang="raku" perl6line>sub infix:<•> { sum @^v1 Z× @^v2 } # dot product
 
sub infix:<❌> (@v1, @v2) { # cross product
Line 1,057:
}).join: "\n"
}
TESTING</langsyntaxhighlight>
{{out}}
<pre>Task example - Point and composite axis / angle:
Line 1,088:
=={{header|Wren}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="ecmascript">var norm = Fn.new { |v| (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]).sqrt }
 
var normalize = Fn.new { |v|
Line 1,128:
var ncp = normalize.call(cp)
var np = aRotate.call(v1, ncp, a)
System.print(np)</langsyntaxhighlight>
 
{{out}}
10,333

edits