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