| 1 |
// original post to the D newsgroup: |
| 2 |
// https://www.digitalmars.com/d/archives/10510.html#N10554 |
| 3 |
// Test to manipulate 3D vectors, in D! |
| 4 |
// by Sean L Palmer ([email protected]) |
| 5 |
// This code is released without any warranty. Use at your own risk. |
| 6 |
import core.stdc.stdio; |
| 7 |
import core.math : sqrt; |
| 8 |
|
| 9 |
template VecTemplate(tfloat, int dim:3) |
| 10 |
{ |
| 11 |
struct Vector |
| 12 |
{ |
| 13 |
tfloat[dim] d; |
| 14 |
|
| 15 |
version(none) |
| 16 |
{ |
| 17 |
// sets the vector to the value of the given array |
| 18 |
void set(tfloat[dim] r) { d[] = r[]; } |
| 19 |
// comparison (a == b, a != b) |
| 20 |
bool opEquals(Vector b) { for (int i=0; i<dim; ++i) if (d[i] != b.d[i]) return |
| 21 |
false; return true; } |
| 22 |
// negate (-a) |
| 23 |
Vector opUnary(string op : "-")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = -d[i]; return |
| 24 |
r; } |
| 25 |
// complement (~a) |
| 26 |
Vector opUnary(string op : "~")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[(i+1)%dim]; |
| 27 |
d[0] = -d[0]; return r; } |
| 28 |
// add (a + b) |
| 29 |
Vector opBinary(string op : "+")(Vector b) { Vector r; r.d[] = d[] + b.d[]; return r; } |
| 30 |
// addto (a += b) |
| 31 |
Vector opOpAssign(string op : "+")(Vector b) { d[] += b.d[]; return r; } |
| 32 |
// subtract (a - b) |
| 33 |
Vector opBinary(string op : "-")(Vector b) { Vector r; r.d[] = d[] - b.d[]; return r; } |
| 34 |
// multiply by scalar (a * 2.0) |
| 35 |
Vector opBinary(string op : "*")(tfloat b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] |
| 36 |
* b; return r; } |
| 37 |
// divide by scalar (a / b) |
| 38 |
Vector opBinary(string op : "/")(tfloat b) { return *this * (1/b); } |
| 39 |
// dot product (a * b) |
| 40 |
tfloat opBinary(string op : "*")(Vector b) { tfloat r=0; for (int i=0; i<dim; ++i) r += d[i]; |
| 41 |
return r; } |
| 42 |
// outer product (a ^ b) |
| 43 |
//Vector opXor(Vector b) { Vector r; for (int i=0; i<dim; ++i) r += d[i]; return r; } |
| 44 |
} |
| 45 |
|
| 46 |
void set(tfloat[dim] r) { for (int i=0; i<dim; ++i) d[i] = r[i]; } |
| 47 |
// comparison (a == b, a != b) |
| 48 |
const bool opEquals(ref const Vector b) { for (int i=0; i<dim; ++i) if (d[i] != b.d[i]) return |
| 49 |
false; return true; } |
| 50 |
// negate (-a) |
| 51 |
Vector opUnary(string op : "-")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = -d[i]; return |
| 52 |
r; } |
| 53 |
// complement (~a) |
| 54 |
Vector opUnary(string op : "~")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[(i+1)%dim]; |
| 55 |
d[0] = -d[0]; return r; } |
| 56 |
// add (a + b) |
| 57 |
Vector opBinary(string op : "+")(Vector b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] + |
| 58 |
b.d[i]; return r; } |
| 59 |
// addto (a += b) |
| 60 |
Vector opOpAssign(string op : "+")(Vector b) { for (int i=0; i<dim; ++i) d[i] += b.d[i]; return |
| 61 |
this; } |
| 62 |
// subtract (a - b) |
| 63 |
Vector opBinary(string op : "-")(Vector b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] - |
| 64 |
b.d[i]; return r; } |
| 65 |
// multiply by scalar (a * 2.0) |
| 66 |
Vector opBinary(string op : "*")(tfloat b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] * |
| 67 |
b; return r; } |
| 68 |
// divide by scalar (a / b) |
| 69 |
Vector opBinary(string op : "/")(tfloat b) { return this * (1/b); } |
| 70 |
// dot product (a * b) |
| 71 |
tfloat opBinary(string op : "*")(Vector b) { tfloat r=0; for (int i=0; i<dim; ++i) r += d[i]; |
| 72 |
return r; } |
| 73 |
// outer product (a ^ b) |
| 74 |
//Vector opXor(Vector b) { Vector r; for (int i=0; i<dim; ++i) r += d[i]; return r; } |
| 75 |
|
| 76 |
void print() { for (int i=0; i<dim; ++i) printf("%f ", d[i]); |
| 77 |
printf("\n"); } |
| 78 |
} |
| 79 |
|
| 80 |
tfloat abs(Vector v) |
| 81 |
{ |
| 82 |
return sqrt(sqr(v)); |
| 83 |
} |
| 84 |
|
| 85 |
tfloat sqr(Vector v) |
| 86 |
{ |
| 87 |
return v * v; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
alias VecTemplate!(float, 3) VcT; |
| 92 |
|
| 93 |
float[3] up = [ 0, 1, 0 ]; |
| 94 |
|
| 95 |
int main(char[][] args) |
| 96 |
{ |
| 97 |
printf("running\n"); |
| 98 |
with (VcT) // try this, I dare ya!! crashes DMD 0.51 |
| 99 |
{ |
| 100 |
VcT.Vector a,b,c; |
| 101 |
c.set(up); |
| 102 |
b.set(up); |
| 103 |
a = b + c; |
| 104 |
a.print(); |
| 105 |
printf("b * c = %f\n",b * c); |
| 106 |
printf("abs(a) = %f\n",VcT.abs(a)); |
| 107 |
printf("sqr(a) = %f\n",VcT.sqr(a)); |
| 108 |
} |
| 109 |
printf("closing\n"); |
| 110 |
return 0; |
| 111 |
} |