]>
Dogcows Code - chaz/yoink/blob - Line.hh
7902ea90d969e2a6c7170f9f970c25f215613eb1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #ifndef _MOOF_LINE_HH_
13 #define _MOOF_LINE_HH_
15 #include <Moof/Drawable.hh>
16 #include <Moof/Math.hh>
17 #include <Moof/OpenGL.hh>
18 #include <Moof/Ray.hh>
19 #include <Moof/Shape.hh>
20 #include <Moof/Texture.hh>
27 struct Line
: public Drawable
, public Shape
<D
>
29 typedef cml::vector
< Scalar
, cml::fixed
<D
> > Vector
;
37 Line(const Vector
& point1
, const Vector
& point2
) :
41 bool intersectRay(const Ray
<2>& ray
, Ray
<2>::Intersection
& hit
) const
43 // solve: Cx + r*Dx = Ax + s(Bx - Ax)
44 // Cy + r*Dy = Ay + s(By - Ay)
45 // where: 0 <= s <= 1 if intersection
51 Scalar denom
= ray
.direction
[0] * (b
[1] - a
[1]) +
52 ray
.direction
[1] * (a
[0] - b
[0]);
54 // check if the ray and line are parallel
55 //if (isEqual(denom, SCALAR(0.0)))
56 if (denom
== SCALAR(0.0))
58 Scalar numer
= a
[0] * (ray
.point
[1] - b
[1]) +
59 b
[0] * (a
[1] - ray
.point
[1]) +
60 ray
.point
[0] * (b
[1] - a
[1]);
62 // check if they are collinear
63 if (isEqual(numer
, SCALAR(0.0)))
65 hit
.distance
= SCALAR(0.0);
66 hit
.normal
.set(0.0, 0.0);
73 Scalar s
= (ray
.direction
[0] * (ray
.point
[1] - a
[1]) +
74 ray
.direction
[1] * (a
[0] - ray
.point
[0])) / denom
;
76 // check if the ray hits the segment
77 if (s
< SCALAR(0.0) || s
> SCALAR(1.0)) return false;
79 hit
.distance
= -(a
[0] * (ray
.point
[1] - b
[1]) +
80 b
[0] * (a
[1] - ray
.point
[1]) +
81 ray
.point
[0] * (b
[1] - a
[1])) / denom
;
82 if (hit
.distance
< SCALAR(0.0)) return false;
84 Vector normal
= cml::perp(a
- b
);
85 if (cml::dot(a
- ray
.point
, normal
) < 0) hit
.normal
= normal
;
86 else hit
.normal
= -normal
;
90 void draw(Scalar alpha
= 0.0) const
92 Mf::Texture::resetBind();
101 template <int D
, int N
>
102 struct Polygon
: public Shape
<D
>
104 typedef cml::vector
< Scalar
, cml::fixed
<D
> > Vector
;
112 #endif // _MOOF_LINE_HH_
This page took 0.037158 seconds and 3 git commands to generate.