elec-1.00
 All Classes Namespaces Files Functions Variables Macros
elecPoint.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 /* This file is part of rl-lib
4  *
5  * Copyright (C) 2010, Herve FREZZA-BUET
6  *
7  * Author : Herve Frezza-Buet
8  *
9  * Contributor :
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public
13  * License (GPL) as published by the Free Software Foundation; either
14  * version 3 of the License, or any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  *
25  * Contact : Herve.Frezza-Buet@supelec.fr
26  *
27  */
28 
29 #include <vector>
30 #include <cstdlib>
31 #include <cmath>
32 #include <iostream>
33 
34 namespace elec {
35  class Point {
36  public:
37  double x,y;
38 
39  Point() : x(0), y(0) {}
40  Point(const Point& cp) : x(cp.x), y(cp.y) {}
41  Point(double xx, double yy) : x(xx), y(yy) {}
42  Point& operator=(const Point& cp) {
43  x = cp.x;
44  y = cp.y;
45  return *this;
46  }
47 
48  Point& operator=(double val) {
49  x = val;
50  y = val;
51  return *this;
52  }
53 
54  Point operator+(const Point& p) const {
55  return {x+p.x, y+p.y};
56  }
57 
58  Point operator-() const {
59  return {-x,-y};
60  }
61 
62  Point operator+() const {
63  return {x,y};
64  }
65 
66  Point operator-(const Point& p) const {
67  return {x-p.x, y-p.y};
68  }
69 
70  double operator*(const Point& p) const {
71  return x*p.x + y*p.y;
72  }
73 
74  Point operator&(const Point& p) const {
75  return {x*p.x, y*p.y};
76  }
77 
81  Point operator*() const {
82  const Point& me = *this;
83  return me/std::sqrt(me*me);
84  }
85 
86  Point operator*(double a) const {
87  return {x*a, y*a};
88  }
89 
90  Point operator/(double a) const {
91  return {x/a, y/a};
92  }
93 
94  Point& operator+=(const Point& p) {
95  x += p.x;
96  y += p.y;
97  return *this;
98  }
99 
100  Point& operator-=(const Point& p) {
101  x -= p.x;
102  y -= p.y;
103  return *this;
104  }
105 
106  Point& operator*=(double a) {
107  x *= a;
108  y *= a;
109  return *this;
110  }
111 
112  Point& operator/=(double a) {
113  x /= a;
114  y /= a;
115  return *this;
116  }
117  };
118 
119  inline std::ostream& operator<<(std::ostream& os,
120  const Point& p) {
121  os << '(' << p.x << ", " << p.y << ')';
122  return os;
123  }
124 
125  inline Point uniform(const Point& A, const Point& B) {
126  Point d = {std::rand()/(RAND_MAX+1.0),
127  std::rand()/(RAND_MAX+1.0)};
128  return A + (d & (B-A));
129  }
130 
131  inline double d2(const Point& A, const Point& B) {
132  Point tmp = B-A;
133  return tmp*tmp;
134  }
135 
136  inline double d(const Point& A, const Point& B) {
137  return std::sqrt(d2(A,B));
138  }
139 
140  template<typename OutputIt>
141  void mesh(const Point& A, const Point& B,
142  unsigned int nb_x,
143  unsigned int nb_y,
144  OutputIt it) {
145  Point scale;
146  Point D = B-A;
147  for(unsigned int h = 0; h < nb_y; ++h) {
148  scale.y = h/(double)(nb_y-1);
149  for(unsigned int w = 0; w < nb_x; ++w) {
150  scale.x = w/(double)(nb_x-1);
151  *(it++) = A + (scale & D);
152  }
153  }
154  }
155 }
Point & operator*=(double a)
Definition: elecPoint.hpp:106
Point & operator/=(double a)
Definition: elecPoint.hpp:112
Point & operator=(const Point &cp)
Definition: elecPoint.hpp:42
Point operator-() const
Definition: elecPoint.hpp:58
Point & operator+=(const Point &p)
Definition: elecPoint.hpp:94
Point operator+(const Point &p) const
Definition: elecPoint.hpp:54
Point operator+() const
Definition: elecPoint.hpp:62
Definition: elecPoint.hpp:35
double d2(const Point &A, const Point &B)
Definition: elecPoint.hpp:131
Point & operator=(double val)
Definition: elecPoint.hpp:48
void mesh(const Point &A, const Point &B, unsigned int nb_x, unsigned int nb_y, OutputIt it)
Definition: elecPoint.hpp:141
double operator*(const Point &p) const
Definition: elecPoint.hpp:70
Point operator*(double a) const
Definition: elecPoint.hpp:86
Point & operator-=(const Point &p)
Definition: elecPoint.hpp:100
Point operator*() const
Definition: elecPoint.hpp:81
Point()
Definition: elecPoint.hpp:39
double y
Definition: elecPoint.hpp:37
Point operator&(const Point &p) const
Definition: elecPoint.hpp:74
Point(const Point &cp)
Definition: elecPoint.hpp:40
Point operator/(double a) const
Definition: elecPoint.hpp:90
double d(const Point &A, const Point &B)
Definition: elecPoint.hpp:136
Point operator-(const Point &p) const
Definition: elecPoint.hpp:66
Point uniform(const Point &A, const Point &B)
Definition: elecPoint.hpp:125
std::ostream & operator<<(std::ostream &os, const Particle &p)
Definition: elecParticle.hpp:57
double x
Definition: elecPoint.hpp:37
Point(double xx, double yy)
Definition: elecPoint.hpp:41