-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathraycasting.hpp
63 lines (59 loc) · 1.55 KB
/
raycasting.hpp
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
59
60
61
62
63
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <cmath>
#define IMAGE "map.jpg"
using namespace cv;
using namespace std;
Mat img = imread(IMAGE,1);
Mat imgg = imread(IMAGE,0);
Mat imgv(img.rows,img.cols,CV_8UC1,Scalar(0));
template <class T>
bool isValid(T y, T x)
{
if(y < 0 || y >= img.rows)
return false;
else if(x < 0 || x >= img.cols)
return false;
else
return true;
}
void castRay(Point source, float theta, int type)
{
float y = source.y;
float x = source.x;
float dx = cos(CV_PI*theta/180);
float dy = sin(CV_PI*theta/180);
float r;
while(isValid(y,x))
{
if(imgv.at<uchar>(y,x) == 0)
{
r = 1 + (sqrt(pow(x-source.x,2)+pow(y-source.y,2))/100);
if(imgg.at<uchar>(y,x) < 128)
{
if(type == 0)
img.at<Vec3b>(y,x)[1] = min(255, (int)(img.at<Vec3b>(y,x)[1] + 255/pow(r,2)));
if(type == 1)
img.at<Vec3b>(y,x)[2] = min(255, (int)(img.at<Vec3b>(y,x)[2] + 127/pow(r,2)));
if(type == 2)
img.at<Vec3b>(y,x)[0] = min(255, (int)(img.at<Vec3b>(y,x)[0] + 255/pow(r,2)));
if(type == 3)
{
img.at<Vec3b>(y,x)[1] = min(255, (int)(img.at<Vec3b>(y,x)[1] + 200/pow(r,2)));
img.at<Vec3b>(y,x)[2] = min(255, (int)(img.at<Vec3b>(y,x)[2] + 255/pow(r,2)));
}
if(type==4)
{
img.at<Vec3b>(y,x)[0] = min(255, (int)(img.at<Vec3b>(y,x)[0] + 200/pow(r,2)));
img.at<Vec3b>(y,x)[2] = min(255, (int)(img.at<Vec3b>(y,x)[2] + 255/pow(r,2)));
}
}
else
break;
imgv.at<uchar>(y,x) = 255;
}
x += dx;
y += dy;
}
}