Points and Vectors in 3D
Simen kjaeraas
simen.kjaras at gmail.com
Sun Mar 13 07:11:32 PDT 2011
Spacen Jasset <spacenjasset at yahoo.co.uk> wrote:
> On 13/03/2011 00:06, Bekenn wrote:
>> On 3/12/2011 2:20 PM, Simon wrote:
>>> I've done lots of 3d over the years and used quite a lot of different
>>> libraries and I've come to prefer code that makes a distinction between
>>> points and vectors.
>>
>> Agreed. This has some nice benefits with operator overloading, as well:
>>
>> vec v = ...;
>> pt p = ...;
>> auto p2 = p + v; // p2 is pt
>> auto p3 = p + p2; // error
>> auto v2 = v + v; // v2 is vec
>>
>> ...and with properties:
>>
>> p.x = 5; // p is pt, sets p._vals[0]
>> v.dx = 3; // v is vec, sets v._vals[0]
> Would you then go on to define things like a cross product as an
> operator overload?
Can't see a fitting operator in D. Multiplication (*) is ambiguous at best
and no other operator seems fitting.
I'd like to have more of unicode's mathematical operators and symbols[1]
as operators in D, but currently that's beyond the horizon.
Use cases:
Vector a, b;
auto angle = a ∠ b;
assert( a == (b ∓ .1) );
assert( ( a ∟ b ) == ( angle == degrees( 90 ) ) );
assert( a ∥ b );
auto v = a ⋅ b;
Set c = ∅, d = ∅;
auto union = c ∪ d;
auto intersection = c ∩ d;
assert( "foo" ∈ c );
assert( "foo" ∉ d );
assert( d ∌ "foo" );
assert( c ∋ "foo" );
assert( d ⊂ c );
assert( d ⊅ c );
assert( ∏[1,2,3] == ∑[1,2,3] );
Of course, this requires a method for typing these symbols, something
which TeX has already solved for us.
[1]:
http://en.wikipedia.org/wiki/Mathematical_operators_and_symbols_in_Unicode
--
Simen
More information about the Digitalmars-d-learn
mailing list