-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.cpp
More file actions
58 lines (49 loc) · 971 Bytes
/
Utils.cpp
File metadata and controls
58 lines (49 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdlib.h>
#include <math.h>
#include "Utils.h"
using namespace std;
//Swaps 2 variables
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
//Returns min of two values
int min(int a, int b){
return a < b ? a : b;
}
//Returns max of 2 values
int max(int a, int b){
return a > b ? a : b;
}
//Returns absoulte value
int abs(int a){
return a > 0 ? a : (-1)*a;
}
//Returns random integer from a to b(inclusive)
int getRand(int a, int b){
return rand()%(b-a+1) + a;
}
//Checks if 2 points are adjacent
bool isAdj(int x1,int y1, int x2, int y2){
if(x1==x2)
return abs(y1-y2) == 1;
else if(y1==y2)
return abs(x1-x2) == 1;
else
return false;
}
//Returns distance fo 2 points
float dist(int x1,int y1, int x2, int y2){
return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
}
//Returns index of minimum value in a array
int min(float a[],int n){
int mi = 0;
for(int i = 1 ; i < n ; i++){
if(a[i] < a[mi]){
mi = i;
}
}
return mi;
}