forked from NonEuclideanDreamer/Mandelbulb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuaternion.java
More file actions
156 lines (124 loc) · 3.65 KB
/
Quaternion.java
File metadata and controls
156 lines (124 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//***************************************************************************************************************
// Author: Non-Euclidean Dreamer
// Gathering handy methods to deal with Quaternions
//****************************************************************************************************************
import java.text.DecimalFormat;
import java.util.Random;
public class Quaternion {
public double x,y,z,w,r,phi,psi,theta;//Attributes: real part, imaginary part, radius, argument
//Constants: often used numbers
public static Quaternion one=new Quaternion(1,0,0,0), i=new Quaternion(0,1,0,0),j=new Quaternion(0,0,1,0),k=new Quaternion(0,0,0,1),
zero=new Quaternion(0,0,0,0), half=new Quaternion(0.5,0,0,0),
mi= new Quaternion(0,-1,0,0),mone=new Quaternion(-1,0,0,0);
public Quaternion(double x0,double y0,double z0,double w0)
{
x=x0;
y=y0;
z=z0;
w=w0;
r=Math.sqrt(x0*x0+y0*y0+w0*w0+z0*z0);
phi=Math.atan2(y,x);
if(phi>Math.PI)phi-=2*Math.PI;//I prefer discontinuity to be left...
theta=Math.asin(w/r);
psi=Math.asin(z/r/Math.cos(theta));
}
public Quaternion()
{}
public Quaternion(double[] l) {
x=l[0];
y=l[1];
z=l[2];
w=l[3];
r=Math.sqrt(x*x+y*y+w*w+z*z);
phi=Math.atan2(y,x);
if(phi>Math.PI)phi-=2*Math.PI;//I prefer discontinuity to be left...
theta=Math.asin(w/r);
psi=Math.asin(z/r/Math.cos(theta));
}
public static Quaternion polar(double r0,double phi0,double psi0,double theta0)
{
Quaternion out=new Quaternion();
out.x=r0*Math.cos(phi0)*Math.cos(psi0)*Math.cos(theta0);
out.y= r0*Math.sin(phi0)*Math.cos(psi0)*Math.cos(theta0);
out.z= r0*Math.sin(psi0)*Math.cos(theta0);
out.w=r0*Math.sin(theta0);
out.r=r0;
out.phi=phi0;
out.psi=psi0;
out.theta=theta0;
return out;
}
public Quaternion Re()
{
return new Quaternion(x,0,0,0);
}
public Quaternion Im()
{
return new Quaternion(0,y,z,w);
}
public Quaternion add(Quaternion z)
{
return new Quaternion(x+z.x,y+z.y,this.z+z.z,w+z.w);
}
public Quaternion times(Quaternion q)
{
return new Quaternion(x*q.x-y*q.y-z*q.z-w*q.w,x*q.y+y*q.x+z*q.w-w*q.z,x*q.z-y*q.w+z*q.x+w*q.y,x*q.w+y*q.z-z*q.y+w*q.x);
}
public Quaternion copy() {
return new Quaternion(x,y,z,w);
}
public Quaternion times(double j)
{
return new Quaternion(x*j,y*j,z*j,w*j);
}
public Quaternion power(double n)
{
return polar(Math.pow(r,n),n*phi,n*psi,n*theta);
}
public Quaternion conjugate()
{
return new Quaternion(x,-y,-z,-w);
}
public String toString()
{
return x+"+"+y+"i"+z+"j"+w+"k";
}
public Quaternion subtract(Quaternion z)
{
return new Quaternion(x-z.x,y-z.y,this.z-z.z,w-z.w);
}
public double norm()
{
return Math.sqrt(x*x+y*y+z*z+w*w);
}
public void print(DecimalFormat df)
{
System.out.print(df.format(x));
System.out.print("+");
System.out.print(df.format(y));
System.out.print("i+");
System.out.print(df.format(z));
System.out.print("j+");
System.out.print(df.format(w));
System.out.print("k");
}
//Gives unit quaternion for rotation
public void vary(double d) {
Random rand=new Random();
y+=d*(0.5-rand.nextDouble());
z+=d*(0.5-rand.nextDouble());
w+=d*(0.5-rand.nextDouble());
normalize();
}
private void normalize() {
double norm=norm();
if(norm>0)times(1/norm);
}
public Quaternion function(Quaternion par, double t)
{
//t=Mandelbulb.t/96.0;
double y1=y+Math.abs(z)*(Math.abs(z)-1);
return new Quaternion(//z,Math.abs(x),Math.log(Math.abs((Math.sin(z))%Math.min(x*Math.E, Math.min(x,(y%Math.min(x, z)))-(y*2)* w)))*Math.min(x, Math.max(Math.sinh(z), Math.abs(z))), x);
(y/(z)),Math.signum(y1)*Math.pow(Math.abs(y1),z),z*(Math.atan(w)),((Math.max(w, z))%w-x));
}
}