Really cool!

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 95e9c58458
commit dd278bd15a
  1. BIN
      C++ProjectTemplate
  2. 2000
      input
  3. 212
      main.cpp
  4. 8
      testinput

Binary file not shown.

2000
input

File diff suppressed because it is too large Load Diff

@ -4,86 +4,150 @@
using namespace olc;
class Example : public olc::PixelGameEngine
{
public:
Example()
{
sAppName = "Example";
}
public:
bool RayVsRect(const vf2d ray_origin, const vf2d ray_dir, const olc::utils::geom2d::rect<float> target, vf2d&contact_point, vf2d&contact_normal, float&t_hit_near){
contact_normal = { 0, 0 };
contact_point = { 0, 0 };
vf2d t_near = {(target.pos.x - ray_origin.x) / ray_dir.x, (target.pos.y - ray_origin.y) / ray_dir.y};
vf2d t_far = {(target.pos.x + target.size.x - ray_origin.x) / ray_dir.x, (target.pos.y + target.size.y - ray_origin.y) / ray_dir.y};
if (t_near.x > t_far.x) {float b; b = t_near.x; t_near.x = t_far.x; t_far.x = b;};
if (t_near.y > t_far.y) {float b; b = t_near.y; t_near.y = t_far.y; t_far.y = b;};
if (t_near.x > t_far.y || t_near.y > t_far.x) return false;
t_hit_near = fmax(t_near.x, t_near.y);
float t_hit_far = fmin(t_far.x, t_far.y);
if (t_hit_far < 0) return false;
contact_point.x = ray_origin.x + t_hit_near * ray_dir.x;
contact_point.y = ray_origin.y + t_hit_near * ray_dir.y;
if (t_near.x > t_near.y)
if ( 1.0f / ray_dir.x < 0)
contact_normal = { 1, 0 };
else
contact_normal = { -1, 0};
else
if ( t_near.x < t_near.y)
if ( 1.0f / ray_dir.y < 0)
contact_normal = { 0, 1 };
else
contact_normal = { 0, -1 };
return true;
void tailFollow(vi2d&tail,std::array<vi2d,9>&head,std::map<std::string,bool>&posmap){
//std::cout<<head<<"//"<<tail<<std::endl;
for (int i=1;i<9;i++){
if (std::abs(head[i-1].x-head[i].x)>=2&&std::abs(head[i-1].y-head[i].y)>=1||
std::abs(head[i-1].x-head[i].x)>=1&&std::abs(head[i-1].y-head[i].y)>=2){
head[i].x+=(head[i-1].x-head[i].x)/std::abs(head[i-1].x-head[i].x);
head[i].y+=(head[i-1].y-head[i].y)/std::abs(head[i-1].y-head[i].y);
} else
if (std::abs(head[i-1].x-head[i].x)>=2){
//std::cout<<" "<<"Move X: "<<std::endl;
//std::cout<<" "<<head<<"//"<<head[i]<<std::endl;
head[i].x+=(head[i-1].x-head[i].x)/std::abs(head[i-1].x-head[i].x);
} else
if (std::abs(head[i-1].y-head[i].y)>=2){
//std::cout<<" "<<"Move Y: "<<std::endl;
//std::cout<<" "<<head<<"//"<<head[i]<<std::endl;
head[i].y+=(head[i-1].y-head[i].y)/std::abs(head[i-1].y-head[i].y);
}
}
vf2d originPoint={16,16};
bool OnUserCreate() override
{
// Called once at the start, so create things here
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
vf2d velocity={(GetKey(D).bHeld-GetKey(A).bHeld)*20*fElapsedTime,(GetKey(S).bHeld-GetKey(W).bHeld)*20*fElapsedTime};
vf2d contact_point;
vf2d contact_normal;
float t_hit_near;
if (std::abs(head[8].x-tail.x)>=2&&std::abs(head[8].y-tail.y)>=1||
std::abs(head[8].x-tail.x)>=1&&std::abs(head[8].y-tail.y)>=2){
tail.x+=(head[8].x-tail.x)/std::abs(head[8].x-tail.x);
tail.y+=(head[8].y-tail.y)/std::abs(head[8].y-tail.y);
} else
if (std::abs(head[8].x-tail.x)>=2){
//std::cout<<" "<<"Move X: "<<std::endl;
//std::cout<<" "<<head<<"//"<<tail<<std::endl;
tail.x+=(head[8].x-tail.x)/std::abs(head[8].x-tail.x);
} else
if (std::abs(head[8].y-tail.y)>=2){
//std::cout<<" "<<"Move Y: "<<std::endl;
//std::cout<<" "<<head<<"//"<<tail<<std::endl;
tail.y+=(head[8].y-tail.y)/std::abs(head[8].y-tail.y);
}
//std::cout<<"Added "<<std::to_string(tail.x)+"_"+std::to_string(tail.y)<<std::endl;
posmap[std::to_string(tail.x)+"_"+std::to_string(tail.y)]=true;
}
Clear(Pixel(64,64,255));
if (!olc::utils::geom2d::overlaps(olc::utils::geom2d::circle<float>{originPoint+velocity,5},olc::utils::geom2d::rect<float>{{32,32},{64,32}})) {
originPoint+=velocity;
DrawCircle(originPoint,5);
} else {
DrawCircle(originPoint,5,RED);
void DrawMap(vi2d&tail,std::array<vi2d,9>&head,std::map<std::string,bool>&posmap){
int maxX=tail.x;
int maxY=tail.y;
int minX=tail.x;
int minY=tail.y;
for (int i=0;i<9;i++){
maxX=std::max(maxX,head[i].x);
maxY=std::max(maxY,head[i].y);
minX=std::min(minX,head[i].x);
minY=std::min(minY,head[i].y);
}
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
if (std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str())>maxX){
maxX=std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str());
}
DrawLine(originPoint,GetMousePos());
DrawRect({32,32},{64,32},RayVsRect(originPoint, GetMousePos()-originPoint, olc::utils::geom2d::rect<float>{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE);
return true;
}
};
}
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
if (std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str())>maxY){
maxY=std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str());
}
}
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
if (std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str())<minX){
minX=std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str());
}
}
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
if (std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str())<minY){
minY=std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str());
}
}
for (int y=minY;y<=maxX;y++){
for (int x=minX;x<=maxX;x++){
if (tail.x==x&&tail.y==y){
std::cout<<"T";
continue;
}
for (int i=0;i<9;i++){
if (head[i].x==x&&head[i].y==y){
std::cout<<i;
goto next;
}
}
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
if (std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str())==x&&
std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str())==y){
std::cout<<"#";
goto next;
}
}
std::cout<<".";
next:;
}
std::cout<<std::endl;
}
}
int main()
{
Example demo;
if (demo.Construct(128, 120, 8, 8))
demo.Start();
std::ifstream file("input");
std::map<std::string,bool>posmap;
vi2d tail={0,0};
std::array<vi2d,9>head={vi2d{0,0}};
while (file.good()){
std::string line;
std::getline(file,line);
posmap[std::to_string(tail.x)+"_"+std::to_string(tail.y)]=true;
if (line.length()>0){
char dir=line[0];
int amt=std::atoi(line.substr(1,std::string::npos).c_str());
std::cout<<dir<<" "<<amt<<std::endl;
switch (dir){
case 'D':{
while (amt>0){
head[0].y++;
tailFollow(tail,head,posmap);
amt--;
}
}break;
case 'R':{
while (amt>0){
head[0].x++;
tailFollow(tail,head,posmap);
amt--;
}
}break;
case 'L':{
while (amt>0){
head[0].x--;
tailFollow(tail,head,posmap);
amt--;
}
}break;
case 'U':{
while (amt>0){
head[0].y--;
tailFollow(tail,head,posmap);
amt--;
}
}break;
}
//DrawMap(tail,head,posmap);
}
}
DrawMap(tail,head,posmap);
std::cout<<"Key count:"<<posmap.size()<<std::endl;
return 0;
}

@ -0,0 +1,8 @@
R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2
Loading…
Cancel
Save