Vector products: Difference between revisions
Content deleted Content added
→{{header|Pascal}}: add example |
Updated D code |
||
Line 388: | Line 388: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio |
<lang d>import std.stdio, std.conv, std.numeric; |
||
struct V3 { |
immutable struct V3 { |
||
union { |
union { |
||
struct { double x, y, z; } |
static struct { double x, y, z; } |
||
double[3] v; |
double[3] v; |
||
} |
} |
||
double dot(in V3 rhs) const pure nothrow { |
|||
return dotProduct(v, rhs.v); |
return dotProduct(v, rhs.v); |
||
} |
} |
||
V3 cross(in V3 rhs) const pure nothrow { |
|||
return V3(y*rhs.z - z*rhs.y, |
return V3(y*rhs.z - z*rhs.y, |
||
z*rhs.x - x*rhs.z, |
z*rhs.x - x*rhs.z, |
||
x*rhs.y - y*rhs.x); |
x*rhs.y - y*rhs.x); |
||
} |
} |
||
string toString() const { return text(v); } |
string toString() const { return text(v); } |
||
} |
} |
||
double scalarTriple(in V3 a, in V3 b, in V3 c) pure nothrow { |
|||
return a.dot(b.cross(c)); |
return a.dot(b.cross(c)); |
||
} |
} |
||
V3 vectorTriple(in V3 a, in V3 b, in V3 c) pure nothrow { |
|||
return a.cross(b.cross(c)); |
return a.cross(b.cross(c)); |
||
} |
} |
||
void main() { |
void main() { |
||
immutable V3 a = {3, 4, 5}, |
|||
b = {4, 3, 5}, |
b = {4, 3, 5}, |
||
c = {-5, -12, -13}; |
c = {-5, -12, -13}; |
||
writeln("a = ", a); |
writeln("a = ", a); |
||
writeln("b = ", b); |
writeln("b = ", b); |