TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
straight2_t.h
Go to the documentation of this file.
1/***************************************************************************
2
3 file : straight2_t.h
4 created : Due Apr 5 13:51:00 CET 2005
5 copyright : (C) 2005 by Bernhard Wymann
6 email : berniw@bluewin.ch
7 version : $Id$
8
9 ***************************************************************************/
10
11/***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
20/*
21 Template for 2d-straight, to be used with float or double 2-d vectors. This template is NOT
22 intended to work with classes which allocate memory. Be aware that there are more
23 efficient methods for doing most of the operations (avoiding temp verctors and make
24 better use of registers). Later I will try to improve the performance (SSE,
25 "abuse" of templates to avoid temporaries).
26*/
27
28
29#ifndef _STRAIGHT_2T_H_
30#define _STRAIGHT_2T_H_
31
32#include <tmath/v2_t.h>
33
34template<class T> class straight2t {
35 public:
36 // Constructors.
38 straight2t(T x, T y, T dx, T dy)
39 { p.x = x; p.y = y; d.x = dx; d.y = dy; d.normalize(); }
40 straight2t(const v2t<T> &anchor, const v2t<T> &dir)
41 { p = anchor; d = dir; d.normalize(); }
42
43 // Methods.
44 v2t<T> intersect(const straight2t<T> &s) const; // Intersection of 2 straights: does not check for NaN's!
45 T dist(const v2t<T> &p) const; // Distance of p to straight this.
46
47 // Data.
48 v2t<T> p; // Point on the straight.
49 v2t<T> d; // Direction of the straight.
50};
51
52
53// intersection point of *this and s
54template<class T> inline v2t<T> straight2t<T>::intersect(const straight2t<T> &s) const
55{
56 T t = -(d.x*(s.p.y-p.y)+d.y*(p.x-s.p.x))/(d.x*s.d.y-d.y*s.d.x);
57 return s.p + s.d*t;
58}
59
60
61// distance of point s from straight *this
62template<class T> inline T straight2t<T>::dist(const v2t<T> &s) const
63{
64 v2t<T> d1 = s - p;
65 v2t<T> d3 = d1 - d*d1*d;
66 return d3.len();
67}
68
69#endif //_STRAIGHT_2T_H_
70
71
static Vector y[4]
Definition Convex.cpp:56
static const char * d3
v2t< T > intersect(const straight2t< T > &s) const
Definition straight2_t.h:54
straight2t(T x, T y, T dx, T dy)
Definition straight2_t.h:38
straight2t(const v2t< T > &anchor, const v2t< T > &dir)
Definition straight2_t.h:40
T dist(const v2t< T > &p) const
Definition straight2_t.h:62
Definition v2_t.h:40