Rodrigues’ rotation formula: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(add FreeBASIC)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 5 users not shown)
Line 10:
 
 
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Numerics.Elementary_Functions;
use Ada.Numerics.Elementary_Functions;
procedure Rodrigues is
type Vector is record
X, Y, Z : Float;
end record;
function Image (V : in Vector) return String is
('[' & V.X'Image & ',' & V.Y'Image & ',' & V.Z'Image & ']');
-- Basic operations
function "+" (V1, V2 : in Vector) return Vector is ((V1.X + V2.X,
V1.Y + V2.Y,
V1.Z + V2.Z));
function "*" (V : in Vector; A : in Float) return Vector is ((V.X*A, V.Y*A, V.Z*A));
function "/" (V : in Vector; A : in Float) return Vector is (V*(1.0/A));
function "*" (V1, V2 : in Vector) return Float is (-- dot-product
(V1.X*V2.X + V1.Y*V2.Y + V1.Z*V2.Z));
function Norm(V : in Vector) return Float is (Sqrt(V*V));
function Normalize(V : in Vector) return Vector is (V /Norm(V));
function Cross_Product (V1, V2 : in Vector) return Vector is (-- cross-product
(V1.Y*V2.Z - V1.Z*V2.Y,
V1.Z*V2.X - V1.X*V2.Z,
V1.X*V2.Y - V1.Y*V2.X));
function Angle (V1, V2 : in Vector) return Float is (Arccos((V1*V2) / (Norm(V1)*Norm(V2))));
-- Rodrigues' rotation formula
function Rotate (V, Axis : in Vector;
Theta : in Float) return Vector is
K : constant Vector := Normalize(Axis);
begin
return V*Cos(Theta) + Cross_Product(K,V)*Sin(Theta) + K*(K*V)*(1.0-Cos(Theta));
end Rotate;
--
-- Rotate vector Source on Target
Source : constant Vector := ( 0.0, 2.0, 1.0);
Target : constant Vector := (-1.0, 2.0, 0.4);
begin
Put_Line ("Vector " & Image(Source));
Put_Line ("rotated on " & Image(Target));
Put_Line (" = " & Image(Rotate(V => Source,
Axis => Cross_Product(Source, Target),
Theta => Angle(Source, Target))));
end Rodrigues;
</syntaxhighlight>
{{out}}
<pre>
Vector [ 0.00000E+00, 2.00000E+00, 1.00000E+00]
rotated on [-1.00000E+00, 2.00000E+00, 4.00000E-01]
= [-9.84374E-01, 1.96875E+00, 3.93750E-01]
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|JavaScript}}
<langsyntaxhighlight lang="algol68">BEGIN # Rodrigues' Rotation Formula #
MODE VECTOR = [ 3 ]REAL;
MODE MATRIX = [ 3 ]VECTOR;
Line 53 ⟶ 105:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
( 2.232221, 1.395138, -8.370829 )
</pre>
 
=={{header|AutoHotkey}}==
{{Trans|JavaScript}}
<syntaxhighlight lang="autohotkey">v1 := [5,-6,4]
v2 := [8,5,-30]
a := getAngle(v1, v2)
cp := crossProduct(v1, v2)
ncp := normalize(cp)
np := aRotate(v1, ncp, a)
for i, v in np
result .= v ", "
MsgBox % result := "[" Trim(result, ", ") "]"
return
 
norm(v) {
return Sqrt(v[1]*v[1] + v[2]*v[2] + v[3]*v[3])
}
normalize(v) {
length := norm(v)
return [v[1]/length, v[2]/length, v[3]/length]
}
dotProduct(v1, v2) {
return v1[1]*v2[1] + v1[2]*v2[2] + v1[3]*v2[3]
}
crossProduct(v1, v2) {
return [v1[2]*v2[3] - v1[3]*v2[2], v1[3]*v2[1] - v1[1]*v2[3], v1[1]*v2[2] - v1[2]*v2[1]]
}
getAngle(v1, v2) {
return ACos(dotProduct(v1, v2) / (norm(v1)*norm(v2)))
}
matrixMultiply(matrix, v) {
return [dotProduct(matrix[1], v), dotProduct(matrix[2], v), dotProduct(matrix[3], v)]
}
aRotate(p, v, a) {
ca:=Cos(a), sa:=Sin(a), t:=1-ca, x:=v[1], y:=v[2], z:=v[3]
r := [[ca + x*x*t, x*y*t - z*sa, x*z*t + y*sa]
, [x*y*t + z*sa, ca + y*y*t, y*z*t - x*sa]
, [z*x*t - y*sa, z*y*t + x*sa, ca + z*z*t]]
return matrixMultiply(r, p)
}</syntaxhighlight>
{{out}}
<pre>[2.232221, 1.395138, -8.370829]</pre>
 
=={{header|C}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 120 ⟶ 214:
printf("[%.13f, %.13f, %.13f]\n", np.x, np.y, np.z);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 155 ⟶ 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 167 ⟶ 261:
 
{ 5 -6 4 } { 8 5 -30 }
dupd [ cross normalize ] [ angle-between ] 2bi a-rotate .</langsyntaxhighlight>
{{out}}
<pre>
Line 175 ⟶ 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 236 ⟶ 330:
r = rodrigues( v, k, theta )
print using "##.### [##.### ##.### ##.###]"; theta; r.x; r.y; r.z
next theta</langsyntaxhighlight>
{{out}}<pre>
Theta rotated vector
Line 265 ⟶ 359:
=={{header|Go}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 320 ⟶ 414:
np := aRotate(v1, ncp, a)
fmt.Println(np)
}</langsyntaxhighlight>
 
{{out}}
Line 329 ⟶ 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 364 ⟶ 458:
var ncp = normalize(cp);
var np = aRotate(v1, ncp, a);
console.log(np); </langsyntaxhighlight>
 
===JavaScript: ES6===
Line 373 ⟶ 467:
and is not available to all JavaScript interpreters)
 
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
"use strict";
 
Line 519 ⟶ 613:
null, 2
);
})();</langsyntaxhighlight>
{{Out}}
<pre>[
Line 535 ⟶ 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 583 ⟶ 677:
;
 
example</langsyntaxhighlight>
{{out}}
<pre>
Line 592 ⟶ 686:
=={{header|Julia}}==
{{trans|Perl}}
<langsyntaxhighlight lang="julia">using LinearAlgebra # use builtin library for normalize, cross, dot
using JSON3
 
Line 613 ⟶ 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 659 ⟶ 753:
nvp = normalized(vp)
np = v1.rotate(nvp, a)
echo np</langsyntaxhighlight>
 
{{out}}
Line 666 ⟶ 760:
=={{header|Perl}}==
===Task-specific===
<langsyntaxhighlight lang="perl">#!perl -w
use strict;
use Math::Trig; # acos
Line 722 ⟶ 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 758 ⟶ 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 764 ⟶ 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 807 ⟶ 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 837 ⟶ 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 843 ⟶ 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 890 ⟶ 1,057:
}).join: "\n"
}
TESTING</langsyntaxhighlight>
{{out}}
<pre>Task example - Point and composite axis / angle:
Line 918 ⟶ 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]
</pre>
 
=={{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 961 ⟶ 1,144:
var ncp = normalize.call(cp)
var np = aRotate.call(v1, ncp, a)
System.print(np)</langsyntaxhighlight>
 
{{out}}
9,485

edits