Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/chris/core/vector.cc @ 1939

Last change on this file since 1939 was 1939, checked in by chris, 20 years ago

orxonox/branches/chris: classes vector and rotation added

File size: 2.7 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Christian Meyer
15   co-programmer: ...
16*/
17
18
19#include "vector.h"
20
21
22using namespace std;
23
24/**
25   \brief add two vectors
26   \param v: the other vector
27*/
28Vector Vector::operator+ (const Vector& v) const
29{
30  Vector r;
31 
32  r.x = x + v.x;
33  r.y = y + v.y;
34  r.z = z + v.z;
35 
36  return r;
37}
38
39/**
40   \brief subtract a vector from another
41   \param v: the other vector
42*/
43Vector Vector::operator- (const Vector& v) const
44{
45  Vector r;
46 
47  r.x = x - v.x;
48  r.y = y - v.y;
49  r.z = z - v.z;
50 
51  return r;
52}
53
54/**
55   \brief calculate the dot product of two vectors
56   \param v: the other vector
57*/
58float Vector::operator* (const Vector& v) const
59{
60  return x*v.x+y*v.y+z*v.z;
61}
62
63/**
64   \brief multiply a vector with a float
65   \param v: the factor
66*/
67Vector Vector::operator* (float f) const
68{ 
69  Vector r;
70 
71  r.x = x * f;
72  r.y = y * f;
73  r.z = z * f;
74 
75  return r;
76}
77
78/**
79   \brief divide a vector with a float
80   \param v: the factor
81*/
82Vector Vector::operator/ (float f) const
83{
84  Vector r;
85 
86  if( f == 0.0)
87  {
88    // Prevent divide by zero
89    return Vector (0,0,0);
90  }
91 
92  r.x = x / f;
93  r.y = y / f;
94  r.z = z / f;
95 
96  return r;
97}
98
99/**
100   \brief calculate the dot product of two vectors
101   \param v: the other vector
102*/
103float Vector::dot (const Vector& v) const
104{
105  return x*v.x+y*v.y+z*v.z;
106}
107
108/**
109   \brief calculate the cross product of two vectors
110   \param v: the other vector
111*/
112Vector Vector::cross (const Vector& v) const
113{
114  Vector r;
115 
116  r.x = y * v.z - z * v.y;
117  r.y = z * v.x - x * v.z;
118  r.z = x * v.y - y * v.x;
119 
120  return r;
121}
122 
123/**
124   \brief normalize the vector to lenght 1.0
125*/
126void Vector::normalize ()
127{
128  float l = len();
129 
130  if( l == 0.0)
131  {
132    // Prevent divide by zero
133    return;
134  }
135 
136  x = x / l;
137  y = y / l;
138  z = z / l;
139}
140 
141/**
142   \brief calculate the lenght of the vector
143*/
144float Vector::len () const
145{
146  return sqrt (x*x+y*y+z*z);
147}
148
149/**
150   \brief calculate the angle between two vectors in radiances
151   \param v1: a vector
152   \param v2: another vector
153*/
154float angle_rad (const Vector& v1, const Vector& v2)
155{
156  return acos( v1 * v2 / (v1.len() * v2.len()));
157}
158
159/**
160   \brief calculate the angle between two vectors in degrees
161   \param v1: a vector
162   \param v2: another vector
163*/
164float angle_deg (const Vector& v1, const Vector& v2)
165{
166  float f;
167  f = acos( v1 * v2 / (v1.len() * v2.len()));
168  return f * 180 / PI;
169}
Note: See TracBrowser for help on using the repository browser.