Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
general.c
Go to the documentation of this file.
1 
11  /* -- Includes -- */
12 /* system libraries */
13 #include <stdio.h>
14 #include <math.h>
15 #include <stdlib.h>
16 /* project libraries */
17 #include "general.h"
18 
19 /* -- Defines -- */
20 
21 /* -- Functions -- */
22 
29 int general_bytes2int(unsigned char byte_high, unsigned char byte_low)
30 {
31  int num;
32 
33  //Convert to int (two's complement)
34  if((byte_high & (1 << 7)) == (1 << 7))
35  {
36  num = (byte_low + (byte_high << 8)) - 0xFFFF;
37  }
38  else
39  {
40  num = byte_low + (byte_high << 8);
41  }
42 
43  return num;
44 }
45 
52 unsigned int general_bytes2uint(unsigned char byte_high, unsigned char byte_low)
53 {
54  int num;
55 
56  //Convert to unsigned int
57  num = byte_low + (byte_high << 8);
58 
59  return num;
60 }
61 
69 void general_int2bytes(int integer, unsigned char *byte_high, unsigned char *byte_low)
70 {
71  int num = integer;
72 
73  // Çonevrt integer to 2 bytes
74  // If number is negative
75  if(num < 0)
76  {
77  num += 0xFFFF;
78  }
79  // Split
80  *byte_high = (num >> 8) & 0xFF;
81  *byte_low = (num) & 0xFF;
82 }
83 
91 double general_gaussrand(double mu, double sigma)
92 {
93  static double V1, V2, S;
94  static int phase = 0;
95  double X;
96 
97  if(phase == 0)
98  {
99  do
100  {
101  double U1 = (double)rand() / RAND_MAX;
102  double U2 = (double)rand() / RAND_MAX;
103 
104  V1 = 2 * U1 - 1;
105  V2 = 2 * U2 - 1;
106  S = V1 * V1 + V2 * V2;
107  } while(S >= 1 || S == 0);
108 
109  X = V1 * sqrt(-2 * log(S) / S);
110  }
111  else
112  {
113  X = V2 * sqrt(-2 * log(S) / S);
114  }
115 
116  phase = 1 - phase;
117 
118  return X * sigma + mu;
119 }
120 
128 float general_gaussian(float mu, float sigma, int x)
129 {
130  return exp(- (pow((mu - x), 2)) / (pow(sigma, 2)) / 2.0) / sqrt(M_PI * 2 * (pow(sigma, 2)));
131 }
132 
141 float general_dist_squared(int x1, int y1, int x2, int y2)
142 {
143  return pow(x1 - x2, 2) + pow(y1 - y2, 2);
144 }
145 
154 float general_dist2seg(int x, int y, int p1[], int p2[])
155 {
156  float l2 = general_dist_squared(p1[0], p1[1], p2[0], p2[1]);
157 
158  if (l2 == 0)
159  return general_dist_squared(x, y, p1[0], p1[1]);
160 
161  float t = ((x - p1[0]) * (p2[0] - p1[0]) + (y - p1[1]) * (p2[1] - p1[1])) / l2;
162 
163  if (t < 0)
164  return general_dist_squared(x, y, p1[0], p1[1]);
165 
166  if (t > 1)
167  return general_dist_squared(x, y, p2[0], p2[1]);
168 
169  return general_dist_squared(x, y, (p1[0] + t * (p2[0] - p1[0])), (p1[1] + t * (p2[1] - p1[1])));
170 }
171 
180 void general_circle(int **arr, int cx, int cy, int radius)
181 {
182  int error = -radius;
183  int x = radius;
184  int y = 0;
185 
186  while (x >= y)
187  {
188  general_circle_eight_points(arr, cx, cy, x, y);
189 
190  error += y;
191  ++y;
192  error += y;
193 
194  if (error >= 0)
195  {
196  error -= x;
197  --x;
198  error -= x;
199  }
200  }
201 }
202 
212 void general_circle_eight_points(int **arr, int cx, int cy, int x, int y)
213 {
214  general_circle_four_points(arr, cx, cy, x, y);
215  if (x != y)
216  {
217  general_circle_four_points(arr, cx, cy, y, x);
218  }
219 }
220 
230 void general_circle_four_points(int **arr, int cx, int cy, int x, int y)
231 {
232  general_circle_horizontal_line(arr, cx - x, cy + y, 2 * x + 1);
233  general_circle_horizontal_line(arr, cx - x, cy - y, 2 * x + 1);
234 }
235 
244 void general_circle_horizontal_line(int **arr, int x, int y, int len)
245 {
246  // Local variables
247  int i;
248 
249  for(i = 0; i < len; i++)
250  {
251  arr[x + i][y] = 1;
252  }
253 }
254 
255 
void general_circle_four_points(int **arr, int cx, int cy, int x, int y)
Definition: general.c:230
float general_dist_squared(int x1, int y1, int x2, int y2)
Definition: general.c:141
float general_dist2seg(int x, int y, int p1[], int p2[])
Definition: general.c:154
double general_gaussrand(double mu, double sigma)
Definition: general.c:91
unsigned int general_bytes2uint(unsigned char byte_high, unsigned char byte_low)
Definition: general.c:52
void general_int2bytes(int integer, unsigned char *byte_high, unsigned char *byte_low)
Definition: general.c:69
void general_circle(int **arr, int cx, int cy, int radius)
Definition: general.c:180
int general_bytes2int(unsigned char byte_high, unsigned char byte_low)
Definition: general.c:29
void general_circle_eight_points(int **arr, int cx, int cy, int x, int y)
Definition: general.c:212
float general_gaussian(float mu, float sigma, int x)
Definition: general.c:128
void general_circle_horizontal_line(int **arr, int x, int y, int len)
Definition: general.c:244