-/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+/*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
**] All rights reserved.
*
-* vi:ts=4 sw=4 tw=75
-*
* Distributable under the terms and conditions of the 2-clause BSD license;
* see the file COPYING for a complete text of the license.
*
-**************************************************************************/
+*****************************************************************************/
#ifndef _MOOF_LINE_HH_
#define _MOOF_LINE_HH_
-/**
- * \file line.hh
- * Classes related to line segments.
- */
-
#include <moof/contact.hh>
#include <moof/drawable.hh>
#include <moof/image.hh>
#include <moof/sphere.hh>
+/**
+ * \file line.hh
+ * Classes related to line segments.
+ */
+
namespace moof {
{
typedef moof::vector< scalar, fixed<D> > vector;
-
vector a;
vector b;
-
line() {}
line(const vector& point1, const vector& point2) :
a(point1),
b(point2) {}
-
vector direction() const
{
return b - a;
return direction().length();
}
-
bool intersect(const line& other, contact<D>& hit) const
{
scalar d = (other.b[1] - other.a[1]) * (b[0] - a[0]) -
- (other.b[0] - other.a[0]) * (b[1] - a[1]);
+ (other.b[0] - other.a[0]) * (b[1] - a[1]);
- if (d == SCALAR(0.0)) return false; // lines are parallel
+ if (d == SCALAR(0.0)) return false; // lines are parallel
// ignoring the (somewhat remote) possibility of coincidence
- scalar m = ((other.b[0] - other.a[0]) * (a[1] - other.a[1]) -
- (other.b[1] - other.a[1]) * (a[0] - other.a[0])) / d;
+ scalar m = ((other.b[0] - other.a[0]) *
+ (a[1] - other.a[1]) -
+ (other.b[1] - other.a[1]) *
+ (a[0] - other.a[0])) / d;
scalar n = ((b[0] - a[0]) * (b[1] - other.a[1]) -
- (b[1] - a[1]) * (b[0] - other.a[0])) / d;
+ (b[1] - a[1]) * (b[0] - other.a[0])) / d;
if (m < SCALAR(0.0) || m > SCALAR(1.0) || // not intersecting
- n < SCALAR(0.0) || n > SCALAR(1.0)) return false;
+ n < SCALAR(0.0) || n > SCALAR(1.0))
+ return false;
vector2 tangent = b - a;
vector2 normal = perp(tangent).normalize();
if (dot(normal, other.a - other.b) < SCALAR(0.0))
- {
normal = -normal;
- }
hit.point = a + m * tangent;
hit.normal = normal;
if (projection < SCALAR(0.0) || projection > surfaceLength)
{
// try endpoints
-
if (other.intersect(a, hit))
{
hit.normal = -hit.normal;
hit.point = b;
return true;
}
-
return false;
}
scalar distance = normal.length();
- if (distance > other.radius) false; // not intersecting
+ if (distance > other.radius) false; // not intersecting
normal.normalize();
return true;
}
-
- bool intersect_ray(const ray<2>& ray, moof::ray<2>::contact& hit) const
+ bool
+ intersect_ray(const ray<2>& ray, moof::ray<2>::contact& hit) const
{
vector2 v1 = a - ray.point;
scalar a1 = signed_angle_2D(v1, b - ray.point);
hit.normal = perp(a - b);
return true;
-
/*
// solve: Cx + r*Dx = Ax + s(Bx - Ax)
// Cy + r*Dy = Ay + s(By - Ay)
*/
}
-
void draw(scalar alpha = 0.0) const
{
image::reset_binding();
}
};
-
typedef line<2> line2;
typedef line<3> line3;
polygon() {}
bool intersect_ray(const ray<D>& ray,
- typename moof::ray<D>::contact& hit)
+ typename moof::ray<D>::contact& hit)
{
return false;
}
{
image::reset_binding();
glBegin(GL_POLYGON);
- for (int i = 0; i < D; ++i)
- {
- glVertex(points[0]);
- }
+ for (int i = 0; i < D; ++i) glVertex(points[0]);
glEnd();
}
};
-
typedef polygon<2,3> triangle2;
typedef polygon<3,3> triangle3;
template <int D>
-bool intersect(const line<D>& line, const sphere<D>& sphere,
- contact<D>& hit)
+bool intersect(const line<D>& line, const sphere<D>& sphere, contact<D>& hit)
{
return false;
}