struct Vector2
{
float x, y;
this (float x, float y)
{
this.x = x;
this.y = y;
}
/*
operatörlere farklı görev yükleme
*/
Vector2 opMul(ref const Vector2 soldaki, float sağdaki)
{
// ?? Vector2 sonuç(soldaki)
Vector2 sonuç = soldaki;
sonuç *= sağdaki;
return sonuç;
}
Vector2 opMul(float soldaki, ref const Vector2 sağdaki)
{
Vector2 sonuç = sağdakih;
sonuç *= soldaki;
return sonuç;
}
Vector2 opMulAssign(ref const float sağdaki)
{
x *= sağdaki;
y *= sağdaki;
return this;
}
Vector2 opAddAssign(ref const Vector2 sağdaki)
{
x += sağdaki.x;
y += sağdaki.y;
return this;
}
/*
bu vektöre dik bir vektör döndürür
*/
Vector2 dik() const
{
return Vector2(y, -x);
}
/*
skaler çarpımı (dot product) hesaplar
*/
float skalerÇarpım(ref const Vector2 v2) const
{
return x * v2.x + y * v2.y;
}
/*
bu vektörün tersi olan vektörü bulur
*/
Vector2 tersiniBul() const
{
return Vector2(-this.x, -this.y);
}
/*
void yansıt(ref const Vector2 normal)
{
this += 2.0 * this.skalerÇarpım(normal) * normal.tersiniBul();
}
*/
}
unittest
{
/*
bu şekilde atama yapamıyoruz sanırım
Vector2 ikinci(Vector2(1, 1);
*/
auto ilk = Vector2(2, 2);
auto ikinci = Vector2(-3, -3);
ilk = ilk.tersiniBul();
assert(ilk == Vector2(-2, -2));
float sayı = -1.0;
ilk *= sayı;
assert(ilk == Vector2(2, 2));
ilk = 2.0f * ikinci;
}
void main()
{
}
Yukarıdaki birim testinde en alttaki ifade 'no property 'opMul_r' for type 'float'' diye bir hata veriyor. Ayrıca C++'de olduğu gibi () arasında atama yapamıyoruz galiba. Gene yansıt işlevi de yorum kaldırılınca hata veriyor.
Digitalmars'ın dökümanlarına baktım ama gene çoğu zaman olduğu gibi pek bir şey anlamadım <_<
Biraz da tembellik ederek daha TDLP'ye bakmadan yazdım :)
--
[ Bu gönderi, http://ddili.org/forum'dan dönüştürülmüştür. ]