Rodrigues’ rotation formula: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
({{header|Ada}})
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(5 intermediate revisions by 3 users not shown)
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>
{2.232221073,1.395138171,-8.370829025}
</pre>
 
=={{header|Processing}}==
{{trans|C}}
<syntaxhighlight lang="java">
//Aamrun, 30th June 2022
 
class Vector{
private double x, y, z;
 
public Vector(double x1,double y1,double z1){
x = x1;
y = y1;
z = z1;
}
void printVector(int x,int y){
text("( " + this.x + " ) \u00ee + ( " + this.y + " ) + \u0135 ( " + this.z + ") \u006b\u0302",x,y);
}
 
public double norm() {
return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
}
public Vector normalize(){
double length = this.norm();
return new Vector(this.x / length, this.y / length, this.z / length);
}
public double dotProduct(Vector v2) {
return this.x*v2.x + this.y*v2.y + this.z*v2.z;
}
public Vector crossProduct(Vector v2) {
return new Vector(this.y*v2.z - this.z*v2.y, this.z*v2.x - this.x*v2.z, this.x*v2.y - this.y*v2.x);
}
public double getAngle(Vector v2) {
return Math.acos(this.dotProduct(v2) / (this.norm()*v2.norm()));
}
public Vector aRotate(Vector v, double a) {
double ca = Math.cos(a), sa = Math.sin(a);
double t = 1.0 - ca;
double x = v.x, y = v.y, z = v.z;
Vector[] r = {
new Vector(ca + x*x*t, x*y*t - z*sa, x*z*t + y*sa),
new Vector(x*y*t + z*sa, ca + y*y*t, y*z*t - x*sa),
new Vector(z*x*t - y*sa, z*y*t + x*sa, ca + z*z*t)
};
return new Vector(this.dotProduct(r[0]), this.dotProduct(r[1]), this.dotProduct(r[2]));
}
}
 
void setup(){
Vector v1 = new Vector(5d, -6d, 4d),v2 = new Vector(8d, 5d, -30d);
double a = v1.getAngle(v2);
Vector cp = v1.crossProduct(v2);
Vector normCP = cp.normalize();
Vector np = v1.aRotate(normCP,a);
size(1200,600);
fill(#000000);
textSize(30);
text("v1 = ",10,100);
v1.printVector(60,100);
text("v2 = ",10,150);
v2.printVector(60,150);
text("rV = ",10,200);
np.printVector(60,200);
}
 
</syntaxhighlight>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub infix:<⋅> { [+] @^a »×« @^b }
sub norm (@v) { sqrt @v⋅@v }
sub normalize (@v) { @v X/ @v.&norm }
Line 931 ⟶ 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 937 ⟶ 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 984 ⟶ 1,057:
}).join: "\n"
}
TESTING</langsyntaxhighlight>
{{out}}
<pre>Task example - Point and composite axis / angle:
Line 1,012 ⟶ 1,085:
Rotated 170°: -4.1240220834695, 5.7201633384526, -5.2222766334692
Rotated 180°: -5.0000000000000, 6.0000000000000, -4.0000000000000</pre>
 
=={{header|RPL}}==
This is a direct transcription from Wikipedia's formula.
≪ DEG SWAP DUP ABS / <span style="color:grey">@ set degrees mode and normalize k</span>
→ v θ k
≪ v θ COS *
k v CROSS θ SIN * +
k DUP v DOT * 1 θ COS - * +
→NUM <span style="color:grey">@ can be removed if using HP-28/48 ROM versions</span>
≫ ≫ '<span style="color:blue">ROTV</span>' STO <span style="color:grey">@ ''( vector axis-vector angle → rotated-vector )''</span>
 
[-1 2 .4] [0 2 1] 18 <span style="color:blue">ROTV</span>
{{out}}
<pre>
[-1.11689243765 1.85005696279 .699886074428]
</langpre>
 
=={{header|Wren}}==
{{trans|JavaScript}}
<langsyntaxhighlight ecmascriptlang="wren">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,055 ⟶ 1,144:
var ncp = normalize.call(cp)
var np = aRotate.call(v1, ncp, a)
System.print(np)</langsyntaxhighlight>
 
{{out}}
9,485

edits