Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- struct Vec(size_t dim) {
- float[dim] data;
- //forward all unsupported operations to data
- alias data this;
- //operate over variadic arguments as if they were an array
- //at the same time, restrict the amount of arguments to dim
- this(float[dim] floats...) {
- data[] = floats;
- }
- auto crossProduct(ref Vec!dim other) {
- static if (dim == 2) {
- return data[0]*other[1] - data[1]*other[0];
- }
- else static if (dim == 3) {
- return Vec!dim(data[1]*other[2] - data[2]*other[1],
- data[2]*other[0] - data[0]*other[2],
- data[0]*other[1] - data[1]*other[0]);
- }
- else static assert (0, "Not implemented");
- }
- }
- void main() {
- Vec!2 xy1 = Vec!2(1, 2), xy2 = Vec!2(3, 4);
- Vec!3 xyz1 = Vec!3(5, 6, 7), xyz2 = Vec!3(8, 9, 10);
- float res1 = xy1.crossProduct(xy2);
- Vec!3 res2 = xyz1.crossProduct(xyz2);
- writeln(res1, " | ", res2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement