summaryrefslogtreecommitdiffstats
path: root/crawl-ref
diff options
context:
space:
mode:
authorRobert Vollmert <rvollmert@gmx.net>2009-10-31 18:17:08 +0100
committerRobert Vollmert <rvollmert@gmx.net>2009-11-01 21:45:25 +0100
commitc8f4dd7a1f5e3c666bc84e81248df7fba78eefeb (patch)
treed52b15c1ed055bfebf86124b2a6d7792fa9812a3 /crawl-ref
parentd106005da5f41332ac26febe398adb0d5bb61de0 (diff)
downloadcrawl-ref-c8f4dd7a1f5e3c666bc84e81248df7fba78eefeb.tar.gz
crawl-ref-c8f4dd7a1f5e3c666bc84e81248df7fba78eefeb.zip
Some geom2d changes.
1. Add function to reflect vector at a line. 2. Implement scalar multiplication as external operator.
Diffstat (limited to 'crawl-ref')
-rw-r--r--crawl-ref/source/geom2d.cc34
-rw-r--r--crawl-ref/source/geom2d.h8
-rw-r--r--crawl-ref/source/ray.cc6
3 files changed, 34 insertions, 14 deletions
diff --git a/crawl-ref/source/geom2d.cc b/crawl-ref/source/geom2d.cc
index f1d4e3b1c9..de8d86724b 100644
--- a/crawl-ref/source/geom2d.cc
+++ b/crawl-ref/source/geom2d.cc
@@ -24,7 +24,7 @@ static bool parallel(vector v, form f)
vector ray::shoot(double t) const
{
- return (start + dir * t);
+ return (start + t*dir);
}
void ray::advance(double t)
@@ -118,6 +118,16 @@ void ray::move_half_cell(const grid &g)
advance(0.5 * t);
}
+vector normal(const form &f)
+{
+ return (vector(f.a, f.b));
+}
+
+vector reflect(const vector &v, const form &f)
+{
+ vector n = normal(f);
+ return (v - 2 * f(v)/f(n) * n);
+}
// vector space implementation
@@ -135,18 +145,24 @@ vector vector::operator+(const vector &v) const
return (copy);
}
-const vector& vector::operator*=(double t)
+vector vector::operator-() const
{
- x *= t;
- y *= t;
- return (*this);
+ return ((-1) * (*this));
}
-vector vector::operator*(double t) const
+const vector& vector::operator-=(const vector &v)
{
- vector copy = *this;
- copy *= t;
- return (copy);
+ return (*this += -v);
+}
+
+vector vector::operator-(const vector &v) const
+{
+ return (*this + (-v));
+}
+
+vector operator*(double t, const vector &v)
+{
+ return (vector(t*v.x, t*v.y));
}
double form::operator()(const vector& v) const
diff --git a/crawl-ref/source/geom2d.h b/crawl-ref/source/geom2d.h
index f1e2310bf4..504f1ed77a 100644
--- a/crawl-ref/source/geom2d.h
+++ b/crawl-ref/source/geom2d.h
@@ -13,10 +13,13 @@ struct vector
const vector& operator+=(const vector &v);
vector operator+(const vector &v) const;
- const vector& operator*=(double t);
- vector operator*(double t) const;
+ vector operator-() const;
+ const vector& operator-=(const vector &v);
+ vector operator-(const vector &v) const;
};
+vector operator*(double t, const vector &v);
+
struct form
{
double a;
@@ -79,6 +82,7 @@ struct grid
double intersect(const ray &r, const line &l);
double nextintersect(const ray &r, const lineseq &ls);
+vector reflect(const vector& v, const form &f);
}
diff --git a/crawl-ref/source/ray.cc b/crawl-ref/source/ray.cc
index e0fa416f91..74d402c7ce 100644
--- a/crawl-ref/source/ray.cc
+++ b/crawl-ref/source/ray.cc
@@ -69,13 +69,13 @@ bool ray_def::advance()
void ray_def::advance_and_bounce()
{
// XXX
- r.dir *= -1;
+ r.dir = -r.dir;
}
void ray_def::regress()
{
- r.dir *= -1;
+ r.dir = -r.dir;
advance();
- r.dir *= -1;
+ r.dir = -r.dir;
}