Day 20 part 1 done

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2, Sig, Sigo 2 years ago
parent 4e86701fcb
commit 7998b707a5
  1. BIN
      C++ProjectTemplate
  2. 5000
      input
  3. 145
      main.cpp
  4. 2
      sig
  5. 7
      testinput

Binary file not shown.

5000
input

File diff suppressed because it is too large Load Diff

@ -1,89 +1,92 @@
#define OLC_PGE_HEADLESS
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "olcutils.h"
using namespace olc;
class Example : public olc::PixelGameEngine
{
public:
Example()
struct Data{
int value; //Number.
int position; //Current position in the list.
};
int main()
{
sAppName = "Example";
std::ifstream file("input");
std::vector<Data*>originalSet;
std::vector<Data*>newSet;
while (file.good()){
std::string line;
std::getline(file,line);
//std::cout<<line<<std::endl;
if (line.length()>0){
Data*data=new Data{std::atoi(line.c_str()),(int)originalSet.size()};
originalSet.push_back(data);
newSet.push_back(data);
}
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;
}
vf2d originPoint={16,16};
bool OnUserCreate() override
{
// Called once at the start, so create things here
return true;
for (int i=0;i<originalSet.size();i++){
Data*val=originalSet[i];
int pos=val->position;
Data*temp=newSet[pos];
int shift=val->value;
while (shift!=0){
if (shift>0){
//Move it to the right of the list.
int newPos=(pos+1)%newSet.size();
/*std::cout<<" Swap "<<newSet[pos]->value<<" ["<<newSet[pos]->position<<"]";
std::cout<<" with "<<newSet[newPos]->value<<" ["<<newSet[newPos]->position<<"]"<<std::endl;*/
newSet[pos]=newSet[newPos];
newSet[newPos]=temp;
newSet[newPos]->position=newPos;
newSet[pos]->position=pos;
shift--;
pos=newPos;
} else {
//Move it to the left of the list.
int newPos=pos-1;
if (newPos<0){
newPos=newSet.size()-1;
}
/*std::cout<<" Swap "<<newSet[pos]->value<<" ["<<newSet[pos]->position<<"]";
std::cout<<" with "<<newSet[newPos]->value<<" ["<<newSet[newPos]->position<<"]";*/
newSet[pos]=newSet[newPos];
newSet[newPos]=temp;
newSet[newPos]->position=newPos;
newSet[pos]->position=pos;
shift++;
pos=newPos;
}
}
/*std::cout<<std::endl;
for (int i=0;i<newSet.size();i++){
if (i!=0){
std::cout<<",";
}
std::cout<<newSet[i]->value<<" ["<<newSet[i]->position<<"]";
}
std::cout<<std::endl;*/
}
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;
Data*zeroVal;
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);
for (int i=0;i<newSet.size();i++){
if (newSet[i]->value==0){
zeroVal=newSet[i];
break;
}
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 offset=(zeroVal->position+1000)%newSet.size();
int offset2=(zeroVal->position+2000)%newSet.size();
int offset3=(zeroVal->position+3000)%newSet.size();
std::cout<<"1000: "<<newSet[offset]->value<<std::endl;
std::cout<<"2000: "<<newSet[offset2]->value<<std::endl;
std::cout<<"3000: "<<newSet[offset3]->value<<std::endl;
int sum=newSet[offset]->value+newSet[offset2]->value+newSet[offset3]->value;
std::cout<<"Sum: "<<sum<<std::endl;
int main()
{
Example demo;
if (demo.Construct(128, 120, 8, 8))
demo.Start();
return 0;
}

2
sig

@ -3,7 +3,7 @@ export AUTO_UPDATE=true
source utils/define.sh
define PROJECT_NAME "C++ProjectTemplate"
define CUSTOM_PARAMS "-std=c++17 -lX11 -lGL -lpthread -lpng -lstdc++fs -lpulse -lpulse-simple -I/usr/include/lua5.3"
define CUSTOM_PARAMS "-std=c++17 -lX11 -lpthread -lpng -lstdc++fs -I/usr/include/lua5.3"
define LANGUAGE "C++"
source utils/main.sh

@ -0,0 +1,7 @@
1
2
-3
3
-2
0
4
Loading…
Cancel
Save