Skip to content

Instantly share code, notes, and snippets.

@LifeMoroz
Last active December 28, 2015 05:58
Show Gist options
  • Save LifeMoroz/7453262 to your computer and use it in GitHub Desktop.
Save LifeMoroz/7453262 to your computer and use it in GitHub Desktop.
Project collapse
#include <stdio.h>
#include <vector>
class Impact;
struct Circle
{
float x, y, r;
};
struct Point
{
float x, y;
};
class Snake
{
Circle head;
std::vector<Point> points;
friend Impact;
};
class Bonus
{
Circle pos;
friend Impact;
};
enum type_of_impact {snake_snake_pat, snake_1_won, snake_2_won, snake_bonus, undef_collapse};
class Impact{
type_of_impact _type;
Point _coords;
Impact(type_of_impact type, Point coords){
_type = type;
_coords = coords;
}
static float from_point_to_line(float x0, float y0, float x1, float y1, float x2, float y2){
return ((y1-y2)*x0+(x1-x2)*y0+(y1*x2-y2*x1))/sqrt((y1-y2)*(y1-y2)+(x1-x2)*(x1-x2));
}
public:
static Impact* impact(Snake sn1, Snake sn2){
//голова в голову
if(pow(sn1.head.x-sn2.head.x,2) + pow(sn1.head.y-sn2.head.y,2) <= sn1.head.r+sn2.head.r ){
Point temp;
temp.x = (sn1.head.x+sn2.head.x)/2;
temp.y = (sn1.head.y+sn2.head.y)/2;
return new Impact(type_of_impact(0),temp);
}
for(int i = 1; i <sn1.points.size(); i++){
//вторая змейка "догнала" первую
if(from_point_to_line(sn2.head.x,sn2.head.y,sn1.points[i].x,sn1.points[i].y,sn1.points[i-1].x,sn1.points[i-1].x) <= sn1.head.r+sn2.head.r ){
Point temp;
temp.x = (sn1.points[i].x+sn2.head.x)/2;
temp.y = (sn1.points[i].y+sn2.head.y)/2;
return new Impact(type_of_impact(1),temp);
}
//первая змейка "догнала" вторую
if(from_point_to_line(sn1.head.x,sn1.head.y,sn2.points[i].x,sn2.points[i].y,sn2.points[i-1].x,sn2.points[i-1].x) <= sn2.head.r+sn1.head.r ){
Point temp;
temp.x = (sn2.points[i].x+sn1.head.x)/2;
temp.y = (sn2.points[i].y+sn1.head.y)/2;
return new Impact(type_of_impact(2),temp);
}
}
return NULL;
}
static Impact* impact(Snake sn1, Bonus bonus){
if(pow(sn1.head.x-bonus.pos.x,2) + pow(sn1.head.y-bonus.pos.y,2) <= sn1.head.r+bonus.pos.r ){
Point temp;
temp.x = (sn1.head.x+bonus.pos.x)/2;
temp.y = (sn1.head.y+bonus.pos.y)/2;
return new Impact(type_of_impact(3),temp);
}
return NULL;
}
static Impact* impact(Snake sn1, Point pos){
if(pow(sn1.head.x-pos.x,2) + pow(sn1.head.y-pos.y,2) <= sn1.head.r )
return new Impact(type_of_impact(4),pos);
return NULL;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment