Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 4fe39cd2fb
commit e624e47022
  1. BIN
      C++ProjectTemplate
  2. 1000
      input
  3. 108
      main.cpp

Binary file not shown.

1000
input

File diff suppressed because it is too large Load Diff

@ -4,86 +4,42 @@
using namespace olc;
class Example : public olc::PixelGameEngine
int main()
{
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 };
std::ifstream file("input");
int sum=0;
while (file.good()) {
std::string line;
std::getline(file,line);
int marker=0;
int prevmarker=0;
prevmarker=marker;marker=line.find_first_of('-');
int digit1=std::atoi(line.substr(0,marker).c_str());
prevmarker=marker;marker=line.find_first_of(',',marker);
int digit2=std::atoi(line.substr(prevmarker+1,marker-prevmarker).c_str());
prevmarker=marker;marker=line.find_first_of('-',marker);
int digit3=std::atoi(line.substr(prevmarker+1,marker-prevmarker).c_str());
prevmarker=marker;marker=line.find_first_of('\n',marker);
int digit4=std::atoi(line.substr(prevmarker+1,marker-prevmarker).c_str());
//1-3 3-5 // 3-5 1-3
//1-4 4-7
//1-3 4-9
//4-6 5-9
//6-9 1-7 // 1-7 6-9
return true;
}
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;
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);
if (digit1<=digit4&&digit2>=digit3||digit2>=digit3&&digit1<=digit4) {
sum++;
std::cout<<line<<std::endl;
std::cout<<digit1<<","<<digit2<<","<<digit3<<","<<digit4<<std::endl;
std::cout<<"In Range"<<std::endl;
} else {
DrawCircle(originPoint,5,RED);
std::cout<<line<<std::endl;
std::cout<<digit1<<","<<digit2<<","<<digit3<<","<<digit4<<std::endl;
std::cout<<"NOT In Range"<<std::endl;
}
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;
}
};
int main()
{
Example demo;
if (demo.Construct(128, 120, 8, 8))
demo.Start();
}
std::cout<<"Sum: "<<sum<<std::endl;
return 0;
}

Loading…
Cancel
Save