I'm currently working on implementing the A* pathfinding algorithm in C++. I tried to run my code to see if the display grid function was working but got the C2678 error: binary '<': no operator found which takes a left-hand operand of type 'const Coord' (or there is no acceptable conversion).
I know that my program is messy and probably not efficient at all however i was trying to get a basic version working before optimising. Is the error because I'm trying to output a boolean value of a Coord structure?
Code:
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
#include <vector>
#include <set>
using std::chrono::milliseconds;
using std::chrono::duration_cast;
using std::this_thread::sleep_for;
typedef std::chrono::steady_clock the_clock;
struct Location {
int g = 0; // Distance covered so far
int h = 0; // Estimate of distance to goal
float f = 0; // Estimated cost of the complete path
bool walkable = 0; // 0 = Walkable, 1 = Wall
};
// Structure
struct Coord {
int x;
int y;
Location location;
};
// Declare size of grid
#define WIDTH 10
#define HEIGHT 10
typedef Location Array[HEIGHT][WIDTH];
Location grid[HEIGHT][WIDTH]; // Create an array of locations
void displayGrid() {
/* Displays the Grid to the console! */
system("CLS");
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
std::cout << grid[y][x].walkable;
}
std::cout << "\n";
}
sleep_for(milliseconds(100)); // Visual delay
}
void initialiseGrid() {
/* Fills the Grid array with values */
srand((unsigned)time(0));
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
grid[y][x].walkable = 0;
}
}
/* Test grid */
grid[4][2].walkable = 1;
grid[5][2].walkable = 1;
grid[4][3].walkable = 1;
grid[5][3].walkable = 1;
grid[4][5].walkable = 1;
grid[5][5].walkable = 1;
grid[4][6].walkable = 1;
grid[5][6].walkable = 1;
}
void Astar(Coord startPoint, Coord endPoint) {
/**/
std::set<Coord> closedSet = {}; // Nodes that do not have to be considered again
std::set<Coord> openSet = {}; // Nodes still to be considered to find the shortest path
Coord currentNode; // Current node
currentNode.x = startPoint.x;
currentNode.y = startPoint.y;
currentNode.location.g = 0; // 0 Distance from starting point
openSet.insert(currentNode); // Insert starting node
while (openSet.empty() == false) { // Loop while open list is not empty
for (std::set<Coord>::iterator it = openSet.begin(); it != openSet.end(); it++) { // Iterate through each element in the open set to find the lowest F value
if ((*it).location.f < currentNode.location.f) { // Check if iterator f value is smaller than the current value
currentNode = *it; // Update the current node
}
}
openSet.erase(currentNode); // Drop from the open set since been checked
closedSet.insert(currentNode); // Add to the closed set
}
}
int main(int argc, char *argv[]) {
// Set start and end points
Coord start;
start.x = 3;
start.y = 3;
Coord end;
end.x = 5;
end.y = 6;
initialiseGrid(); // Put -1 (empty) in
// Start timing
the_clock::time_point startTime = the_clock::now();
// Stop timing
the_clock::time_point endTime = the_clock::now();
// Compute the difference between the two times in milliseconds
auto time_taken = duration_cast<milliseconds>(endTime - startTime).count();
displayGrid();
std::cout << "That took: " << time_taken << " ms" << std::endl;
return 0;
}
The easiest way to solve the issue with std::set requiring a strict-weak-ordering and your Coord class is to provide an operator < comparing the x and y values in Coord, and returning whether one Coord is less than another Coord using these values.
You can do this with std::tie
#include <tuple>
//...
struct Coord {
int x;
int y;
Location location;
bool operator <(const Coord& c) const
// returns true if this->x and this->y < c.x and c.y, false otherwise
{ return std::tie(x,y) < std::tie(c.x,c.y); }
};
The std::tie compares the x components, then if equal, compares the y components. The result of the comparison is returned (either true if the first set of x,y components is less than the second set of x,y components, or false otherwise).
Live Example here
Related
I've begun into C++ from a heavy C background. This is my first program, which I'm using as a learning experience. A simple Snake Game. Everything runs smoothly, the only issue is that no matter what I do, after the players score reaches 4, the next bit of food is ALWAYS spawned inside of the wall, in the bottom left corner (1 row up from the bottom.) Running into it = Game Over.
This is running on a Linux server. I've tried messing with the border parameters, but I'm having a hard time figuring out what to change, because everything looks fine to my eyes. (Clearly something is not.)
I'm pasting the entire program below. It's quite short. I just need one of you guru's to read through/run the program and take me to school on something that's probably a simple fix. Pardon the excessive comments. I use these to teach (I have a following on pastebin as odd as that is to say)
/*
Snake Game - Tragedy
My First Program In C++
I'm Using This Much As A Learning Experience For Myself
And Would Like To Help Those Reading The Code For This Goofy Game
Understand C++ A Bit Better Too
Therefore I'm Trying To Explain As Much As Possible In Real Time
*/
#include <iostream> //Standard
#include <stdlib.h> //Standard
#include <unistd.h> //For POSIX Access
#include <sys/ioctl.h> //For Display Window, TTY Window (Console Window)
#include <termios.h> //For Line Buffering - See Below
#include <stdio.h> //Old Friend
#define CLRSCR "\e[1;1H\e[2J" //Command To Clear Terminal Screen - Change Accordingly
using namespace std; /*
A NameSpace Is Used As Additional Information
To Differentiate Between Similar Functions/Variables
That Have The Same Name In Different Libraries
Using 'namespace' You Can Define The Context
In Which Names Are Defined
Withoug Using The STD NameSpace, The Computer Will Try
To Call cout Or cin As If It Weren't Defined In A NameSpace
Trying To Call Something That Doesn't Exist = Error
So, Without Using namespace std; When You Write For Example:
'cout << value;' You'd Have To Write 'std::cout << value;''
*/
//Create Boundaries
const int width = 50;
const int height = 25;
const char block = 'o';
void ClearScreen(void)
{
cout << CLRSCR;
}
//Global Arrays For Data Records
int background[height][width]; // Background
int snake[50][2]; // Max Snake Length
int food[2] = {0,0}; // Snake Food
int score = 0; // Score
int snakelen = 3; // Snake Starting Length
int snakespeedx = 1; // Horizontal Speed
int snakespeedy = 1; // Vertical Speed
int lap = 200; // Waiting Time Betweeen Frames
//Declaring Global Temporary Variables To Save Memory
int px, py, nx, ny; //Postions
char k;
int h, w;
int x, y;
int movementx = snakespeedx; //Snake Movement
int movementy = 0; //Snake Movement
//Check For Keyboard Press
/*
Reference Link:
https://www.quora.com/With-which-function-can-I-replace-kbhit-in-C++-because-the-header-conio-h-doesnt-exist-in-linux
Ubuntu Users:
sudo apt-get install libncurses5-dev libncursesw5-dev
Life Saver:
http://www.flipcode.com/archives/_kbhit_for_Linux.shtml
*/
int bytesWaiting, i;
int _kbhit()
{
static const int STDIN = 0;
static bool initialized = false; //The Boolean Data Type Is Used To Declare A Variable Whose Value Will Be Set As True (1) Or False (0)
if (! initialized)
{
//Use Termios To Turn Off Line Buffering
termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}
//Initialise background borders Onto Array
void initialise_background(void)
{
//int i;
// Insert Top Border
for(i=0; i<width; i++)
{
background[0][i]=1;
}
//Insert Left Border
for(i=0; i<height; i++)
{
background[i][0]=1;
}
//Insert Right Border
for(i=0; i<height; i++)
{
background[i][width-1]=1;
}
//Insert Bottom Border
for(i=0; i<width; i++)
{
background[height-1][i]=1;
}
}
//Initialise Snake Coordinates
void initialise_snake(void)
{
snake[0][0]=3; //Coordinates X
snake[0][1]=3; //Coordinates Y
snake[1][0]=3+1; //Coordinates X
snake[1][1]=3; //Coordinates Y
snake[2][0]=3+2; //Coordinates X
snake[2][1]=3; //Coordinates Y
snake[3][0]=3+3; //Coordinates X
snake[3][1]=3; //Coordinates Y
snake[4][0]=3+4; //Coordinates X
snake[4][1]=3; //Coordinates Y
}
//Update Snake
void update_snake_coordination(void)
{
//int px,py,nx, ny;
px = snake[0][0];
py = snake[0][1];
snake[0][0] = px + movementx;
snake[0][1] = py + movementy;
nx = snake[0][0];
ny = snake[0][1];
for(i=1; i<snakelen; i++)
{
nx = snake[i][0];
ny = snake[i][1];
snake[i][0] = px;
snake[i][1] = py;
px = nx;
py = ny;
}
}
//Install Snake Coordinates Into Background Array = ( 1 To Draw And 0 To Erase)
void draw_snake_in_background(const int rev)
{
//int x, y;
for(i = 0; i<snakelen; i++)
{
x = snake[i][0];
y = snake[i][1];
if((x!=0)&&(y!=0))
{
background[y][x] = rev;
}
}
}
//Print Array Frame
void print_array_frame(void)
{
for(h=0; h<height; h++)
{
for(w=0; w<width; w++)
{
i=background[h][w];
if(i==1)
{
cout << block;
}
else if (i == 2)
{
cout << "+";
}
else
{
cout << " ";
}
}
cout << endl;
}
}
//Update Loop
void mainloop(void)
{
ClearScreen();
draw_snake_in_background(1); // Install Snake
print_array_frame(); // Print Frame
draw_snake_in_background(0); // Uninstall Snake
}
//Waiting Function
void sleepcp(int milliseconds) // Cross-Platform Sleep Function
{
clock_t time_end;
time_end = clock() + milliseconds * CLOCKS_PER_SEC/1000;
while (clock() < time_end)
{
//
}
}
//Reaction To Keyboard Press
void reaction_on_keyboard(const char k)
{
if(k=='d'||k=='6')
{
//Right Turn
movementx = snakespeedx;
movementy = 0;
}
else if(k=='a'||k=='4')
{
//Left Turn
movementx = -snakespeedx;
movementy = 0;
}
else if(k=='w'||k=='8')
{
//Turn Up
movementx = 0;
movementy = -snakespeedy;
}
else if(k=='s'||k=='2')
{
//Turn Down
movementx = 0;
movementy = snakespeedy;
}
else if(k=='q'||k=='z'||k=='c')
{
cout << "[+] Exit Safely [+]"<<endl;
exit(0);
}
}
//Create Snake Food
void cook_food(void)
{
if (food[0]==0)
{
x = rand() % width + 1;
y = rand() % height + 1;
food[0] = x;
food[1] = y;
background[y][x] = 2;
}
}
//Check Snake & Food Status
void capture_food(void)
{
x = food[0];
y = food[1];
if ((x==snake[0][0])&&(y==snake[0][1]))
{
background[y][x] = 0;
food[0] = 0;
score ++;
snakelen ++;
cook_food();
}
}
//Check Snake is Not Touching Boundary
void check_over_lapping(void)
{
//int px,py;
px = snake[0][0];
py = snake[0][1];
if((px==0)||(px==(width-1))||(py==0)||(py==(height-1)))
{
cout << "[+] Game Over [+]" << endl;
exit(0);
}
}
//Loop
void loop(void)
{
int frame = 0;
x = 0;
y = 0;
while(x<500)
{
sleepcp(lap);
if(_kbhit()) //If Keyboard Pressed
{
cin >> k; //Character
reaction_on_keyboard(k);
}
mainloop(); //RUn Main Loop FUnction
update_snake_coordination();//Update Snake Coordinates
check_over_lapping(); //Check Snake Status
cook_food(); //Make Sure Food is Available
capture_food(); //Snake Eaten Food?
cout << "[ Frame : " << frame << " | Score : " << score << " ] "<< endl; //Print Status
frame ++;
}
}
//Main Trigger Function
main()
{
initialise_background(); //Install All Variables
initialise_snake(); //Install Snake data
loop(); //Run Update Loop
}
This is what happens when running the game:
oooooooooooooooooooooooooooooooooooooooooooooooooo
o o
o o
o o
o o
o o
o o
o o
o o
o o
o o
o o
o o
o o
o o
o o
o o
o o
o o
o ooooooo o
o o
o o
o o
+<---Places Here Every Time o
oooooooooooooooooooooooooooooooooooooooooooooooooo
[ Frame : 169 | Score : 4 ]
Any Help or Input is greatly appreciated!
Your background array is height * width. When you place food into the array, you place it at rand() % width + 1 and rand() % height + 1, which have a range of 1 to width and 1 to height respectively. If you generate food at an x coordinate of width or a y coordinate of height, you will read outside the bounds of your background array. What is happening is that the seed your program is initialised with is generating food at an x position of width and because of the way memory is laid out that is the same array location as background[y + 1][0].
You probably want to change cook_food as follows:
void cook_food(void)
{
if (food[0]==0)
{
x = rand() % (width - 1) + 1;
y = rand() % (height - 1) + 1;
food[0] = x;
food[1] = y;
background[y][x] = 2;
}
}
I would note that this isn't really a C++ program, no matter what you're compiling it with. Your are using a very C style, storing data in a C way, and calling C standard library functions. You may want to read the isocpp C++ FAQ, which has some pointers to resources for people learning C++.
A C++ implementation would probably want to use the std::uniform_int_distribution class in the standard library, which makes it much clearer what your minimum and maximum values are for your food X and Y coordinates. You would also have 'Food' and 'Snake' objects that tracked their X and Y coordinates, rather than storing those values in arrays that you use directly.
EDIT: You've been asking some questions in the comments about collision detection for the snake. I believe this method will detect snake-on-snake collision given the code above:
bool is_snake_touching_itself() {
for (std::size_t i = 1; i < snakelen; ++i) {
if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) {
return true;
}
}
return false;
}
I am reading my hand pose from a Leap Motion sensor and I want to calculate how fast the hand moves (by calculating derivativex = dx / dt) in X direction. My solution is to put 100 hand pose values in an array and keep updating this array with new values when new messages (msg->palmpos.x) arrive in the callback function through topic leapmotion/data.
My question is when I print the derivativex = dx / dt with ROS_ERROR("Hello %f", "derivativex") the output is always: 0
what I've been doing wrong? link for the topic that my callback is listening.
my call back function:
#include "geometry_msgs/TwistStamped.h"
#include "jog_msgs/JogJoint.h"
#include "jog_msgs/leapros.h"
#include "ros/ros.h"
#include <ros/console.h>
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
namespace to_twist
{
class spaceNavToTwist
{
public:
spaceNavToTwist() : spinner_(1)
{
joy_sub_ = n_.subscribe("leapmotion/data", 1, &spaceNavToTwist::joyCallback, this);
// Changed "spacenav/joy" to topic "/leapmotion/data"
twist_pub_ = n_.advertise<geometry_msgs::TwistStamped>("jog_arm_server/delta_jog_cmds", 1);
joint_delta_pub_ = n_.advertise<jog_msgs::JogJoint>("jog_arm_server/joint_delta_jog_cmds", 1);
spinner_.start();
ros::waitForShutdown();
};
const int arraySize = 100;// constant variable can be used to specify array size
double vectorx[ arraySize ] = {};// initialize elements of array n to 0
int resolution = 10;
double derivativex = 0;
double dx = 0;
int dt = 0;
private:
ros::NodeHandle n_;
ros::Subscriber joy_sub_;
ros::Publisher twist_pub_, joint_delta_pub_;
ros::AsyncSpinner spinner_;
// Convert incoming joy commands to TwistStamped commands for jogging.
void joyCallback(const jog_msgs::leapros::ConstPtr& msg)
{
for ( int count = 0; count < arraySize; ++count ) {// store the values of poses
vectorx[ count ] = msg->palmpos.x;
if (count>resolution) {
dx = vectorx[ count-1 ] - vectorx[ count-(resolution-1) ];
dt = resolution;
derivativex = dx / dt;
ROS_ERROR("Hello %f", derivativex);
}
if (count == arraySize) {
count=0;
}
}
Issue 1: The log function ROS_ERROR is misused. You should pass a float instead of a string, otherwise, you will get an undefined behavior :
ROS_ERROR("Hello %f", derivativex); // <-- there is no double quotes.
Issue 2: derivative of X is always 0 because of the assignment at the beginning of the for loop:
for ( int count = 0; count < arraySize; ++count ) {// store the values of poses
//Could you please explain why the program needs this ???
vectorx[ count ] = msg->palmpos.x; // <-- every element in vectorx is set to this values (const in each function call).
if (count>resolution) {
dx = vectorx[ count-1 ] - vectorx[ count-(resolution-1) ]; // is the same as (msg->palmpos.x - msg->palmpos.x) --> 0
dt = resolution;
derivativex = dx / dt;
ROS_ERROR("Hello %f", derivativex);
}
if (count == arraySize) {
count = 0; //<-- never get here because of count is always lesser than arraySize
}
}
I guess that you want to append msg->palmpos.x to the vectorx ? You should use std::vector for vectorx, it will be much easier.
Here is the modified version of your program, using std::vector :
//add this to your file
#include <vector>
//Your program body ...
//...
//As we are using C++, try to use C++ facilities if possible.
//const int arraySize = 100;// constant variable can be used to specify array size
//double vectorx[ arraySize ] = {};// initialize elements of array n to 0
std::vector<double> vectorx;
int resolution = 10;
int max_vector_size = 100; //keep 100 elements in the vectorx.
//...
// Convert incoming joy commands to TwistStamped commands for jogging.
void joyCallback(const jog_msgs::leapros::ConstPtr& msg)
{
//store the x coordinate in the vectorx
vectorx.push_back( msg->palmpos.x );
if( vectorx.size() > resolution ){
int id_back = vectorx.size() - 1;
double dx = vectorx[id_back] - vectorx[ id_back - resolution ];
double dt = resolution;
derivativex = dx / dt;
ROS_ERROR("Hello %f", derivativex);
}
while(vectorx.size() > max_vector_size ) {
vectorx.erase( vectorx.begin() ); //remove the first element
}
}//eof joyCallback
I am trying to compile the files below. The PosLin.cpp contains the SurTriAuto and getSphere functions below. Although they are similar, I am not getting the same results. Is it because the "namespace TPiecesNS" causes them to be different?
I have a tpieces.h file
namespace TPiecesNS
{
class TPieces
{
public:
TPieces();
//other stuff
}
}
tpieces.cpp has:
void TPieces::addPoint(Vertex* point)
{
Vertex* p = new Vertex();
p->Point[0] = point->Point[0]; //similar for Point[1],[2]
p->Normal[0] = point->Normal[0]; //same for 1,2
m_Vertices.push_back(p);
}
geopar.h file has
#include "tpieces.h"
#include "Geo/Geo.h"
class Geo;
namespace TPiecesNS
{
class GeoPar;
{
public:
GeoPar();
TPieces* getSphere(Geo* geo);
TPieces* getSphere(Geo* geo, int permu);
private:
TPieces* SurTriAuto(TPieces* boundary, Geo* geo,int permu);
}
}
geopar.cpp file has
#include "tpieces/geo.h"
#include "tpieces.h"
#include "Geo/Geo.h"
using namespace TPiecesNS;
TPieces* GeoPar::getSphere(Geo* geo) {
return getSphere(geo, 0);
}
TPieces* GeoPar::getSphere(Geo* geo, int permu)
{
TPieces* boundary = new Sphere();
return SurTriAuto(boundary,geo,permu);
}
TPieces* GeoPar::SurTriAuto(TPieces* boundary, Geo* geo, int permu)
{
double maxx, maxy, maxz, minx, miny, minz;
double x,y,z,f,nx,ny,nz;
int number = 6;
ofstream file;
file.open("output.txt");
boundary->numbpts = geo->m_NumTriVerts;
boundary->numbtris = geo->m_NumTris;
file<<"NumVertices "<<boundary->numbpts<<endl;
file<<"NumTrianlges "<<boundary->numbtris<<endl;
for (i = 0 ; i < boundary->numbpts; i++)
{
x = geometry->m_TriVerts[i*3+0];
//also equalities for y,z, but I don't want to type here in order to save space
nx = geometry->m_TriVertNormals[i*3+0];
//ny, nz also
if (x < minx) minx = x;
//comparisons for y,z also and comparing to maxx, maxy, maxz
Vertex* point = new Vertex();
point->Point[0] = x;
point->Point[1] = y;
point->Point[2] = z;
point->Normal[0] = nx; //also assignments for ny, nz
file<<"xyz normals: "<<point->Point[0]<<endl;
//I also printed out y,z,nx,ny,nz
boundary->addPoint(point);
}
for (i = 0 ; i < boundary->numbtris; i++)
{
ii = geo->m_Tris[i*3+0]; //assignments for jj, kk also
if (ii < jj && jj < kk) { i1 = ii; i2 = jj; i3 = kk; }
//similar comparisons for jj and kk also here, but I want to save space
//...
if (kk < ii && ii < jj) { i1 = kk; i2 = ii; i3 = jj; } // result in i1 <= i2 <= i3
Face* facet = new Face();
facet->Index[0] = i1; //i2, i3 are also assigned
facet->IndexInR[0] = ii; //jj, kk also
boundary->addFacet(facet);
} /* end facet (i) loop */
for (i = 0 ; i <boundary->numbtris; i++)
{
for(int j=0;j<3;j++)
{
int index = boundary->m_Faces[i]->Index[j];
for(int k=0;k<3;k++)
{
file<<boundary->m_Faces[i]->Normal[k]<<" "<<boundary->m_Vertices[index]->Normal[k]<<endl;
boundary->m_Faces[i]->Normal[k] += boundary->m_Vertices[index]->Normal[k];
//ERROR IS HERE
file<<boundary->m_Faces[i]->Normal[k]<<endl;
}
}
}
return boundary;
}
and PosLin.h has
#include "TPieces/tpieces.h"
#include "TPieces/geoPar.h"
#include "Geo/Geo.h"
struct PosRotAndQ {
TPiecesNS::TPieces* boundary;
};
class PS{
public:
PosExCode computation(Geo* geo, POpinion* opinion, PositionRotation* matterboundary)
PositionRotation* matterboundary;
}
and PosLin.cpp has
#include "tpieces/tpieces.h"
#include "Geo/Geo.h"
PosExCode PS::computation(Geo* geo, POpinion* opinion, PositionRotation* matterboundary)
{
TPiecesNS::GeoPar* perform = new TPiecesNS::GeoPar();
TPiecesNS::TPieces* boundary = new TPiecesNS::Sphere();
boundary->sphere = perform->SurTriAuto(boundary, geo,0);//if I comment this line out and the line below and un-comment the 2 getSphere lines below, they do not produce the same output
boundary->sphereDark[0] = perform->SurTriAuto(boundary, geo,0); \
//boundary->sphere = perform->getSphere(geo,0);
//boundary->sphereDark[0] = perform->getSphere(geo,0);
}
I noticed that the getSphere and SurTriAuto get different outputs, specifically at the line surface->m_Faces[i]->Normal[k] +=
surface->m_Vertices[index]->Normal[k];
In the outputted textfile, before the += operation takes place, the values surface->m_Faces[i]->Normal[k] and surface->m_Vertices[index]->Normal[k] and are not the same for getSphere and SurTriAuto, even though all the other values (such as the x,y,z,index values) are the same.
I suspect this is because one of the boundary pointers loses values in TPieces* boundary = new Sphere(); in getSphere in GeoPar.cpp and/or TPiecesNS::TPieces* boundary = new TPiecesNS::Sphere(); in PosLin.cpp
In one case you have both functions using the same boundary object. In the other case each function uses a fresh boundary object.
You haven't shown what the TPieces class does, but I assume that addPoint and addFacet change the contents of the TPieces class, such that when you write the faces to the file in the second call, you end up getting the faces that were saved in the first call.
To make the two cases work the same, try using a different boundary object in the second call. Something like this:
TPiecesNS::GeoPar* perform = new TPiecesNS::GeoPar();
TPiecesNS::TPieces* boundary = new TPiecesNS::Sphere();
boundary->sphere = perform->SurTriAuto(boundary, geo,0);//if I comment this line out and the line below and un-comment the 2 getSphere lines below, they do not produce the same output
TPiecesNS::TPieces* boundary2 = new TPiecesNS::Sphere();
boundary->sphereDark[0] = perform->SurTriAuto(boundary2, geo,0);
//boundary->sphere = perform->getSphere(geo,0);
//boundary->sphereDark[0] = perform->getSphere(geo,0);
Lets say I have two AABB based areas, each area defined by two coordinates mins{x, y} and maxs{x, y}, I want to find the middle connection point between them.
Since my english is not good, I can't explain all with my words,
see the following picture for easier understanding:
http://i.*.com/WokivEe.png
All I need to find is the red point coordinates.
so If we move this into programming question, actual data structures would look like this:
struct Vec2D {
float x, y;
}
struct Rectangle {
Vec2D min;
Vec2D max;
}
Rectangle obj[2]
Anyone got an idea for an algorithm?
Along either the X or Y axis, sort the coordinates of the sides that touch into order. Then average the 2nd and 3rd ones in that list to find their midpoint. I hope this answers the question sufficiently.
Here is a little algorithm that first find which sides of the objects are closest, and then uses the 4 points along the common side to make a list, sorted along the common axis. The average of the 2 middle points of the sorted list are the answer. This will work for both horizontal and vertical sides. I added accessor functions to the data structures so that they can be indexed; e.g., for a Vec2D, coordinate(0) is the x value and coordinate(1) is the y value.
#include <math.h>
#include <iostream>
#include <limits>
struct Vec2D {
float x, y;
float coordinate(int axis)
{
return (axis & 1) ? y : x;
}
};
struct Rectangle {
Vec2D min;
Vec2D max;
Vec2D corner(int j)
{
return (j & 1) ? max : min;
}
// Get the other corner along the given axis
Vec2D along(int j, int ax)
{
Vec2D p = corner(j);
if (0 == ax)
{
p.x = corner(1-j).x;
}
else
{
p.y = corner(1-j).y;
}
return p;
}
};
using namespace std;
inline Vec2D* vp(const void* p)
{
return (Vec2D*) p;
}
static int compare_x(const void*a, const void*b)
{
if (vp(a)->x < vp(b)->x)
{
return -1;
}
else
if (vp(a)->x > vp(b)->x)
{
return 1;
}
return 0;
}
static int compare_y(const void*a, const void*b)
{
if (vp(a)->y < vp(b)->y)
{
return -1;
}
else
if (vp(a)->y > vp(b)->y)
{
return 1;
}
return 0;
}
int main(void) {
int ax; // axis index
int c0, c1;
float gap = numeric_limits<float>::max();
struct Rectangle obj[2] = {0,2,10,10,10,5,15,20};
struct
{
int ax,c0,c1;
} closest;
// Find out which sides are the closest to each other
for(ax = 0; 2 > ax; ++ax) // Look at x axis and y axis
{
for(c0 = 0; 2 > c0; ++c0) // Look at both corners of obj[0]
{
for(c1 = 0; 2 > c1; ++c1) // Look at both corners of obj[1]
{
float dist = fabs(obj[0].corner(c0).coordinate(ax) - obj[1].corner(c1).coordinate(ax));
if (dist < gap)
{
gap = dist;
closest.ax = ax;
closest.c0 = c0;
closest.c1 = c1;
}
}
}
}
int other = 1 - closest.ax; // The other axis
cout << "The closest gap is along the " << (closest.ax ? 'y' : 'x') << " axis\n";
cout << "The common side is along the " << (other ? 'y' : 'x') << " direction\n";
// Make a list of the 4 points along the common side
Vec2D list[4];
list[0] = obj[0].corner(closest.c0);
list[1] = obj[0].along(closest.c0, other);
list[2] = obj[1].corner(closest.c1);
list[3] = obj[1].along(closest.c1, other);
// Sort them into order along the common axis
qsort(list, 4, sizeof(Vec2D), closest.ax ? compare_x : compare_y);
// Get the average of the 2 middle points along the common axis.
Vec2D answer = {
(list[1].x + list[2].x) / 2,
(list[1].y + list[2].y) / 2
};
cout << "(" << answer.x << "," << answer.y << ")\n";
}
I'm getting an error saying that "adjacencymatrix' was not used in this scope" right at the end of main (before the function makebond at the end) (the commented line 112 "BROKEN LINE"). Why? Sorry about this being simple. I'm compiling with g++ ($ g++ a.c -o f).
Heres the code:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define PI 3.1415926535897932384626433832795
#define sqr(x) ((x)*(x))
#define count 500
double density;
double volume;
int N;
double beta = 0.1;
double R = 5;
double rob = 1;
int dimension = 2;
double eps=0.1; // Increase in density
double mindensity = 0; // Minimum density
double maxdensity = 8; // max.dens (scaled for the sake of ensuring int()
int makebond(double x);
int main(){
srand(time(0));
for (int rho=mindensity;rho<=(maxdensity/eps);density++){
N = floor(density*volume);
double nodepositions[N][dimension];
// Place nodes in volume (square side L, circle volume *R and obstacle *rob)
for (int i=0;i<N;i++){
int L = 5;
double distancefromorigin;
double x = (L*(rand()/RAND_MAX))-(L/2);
double y = (L*(rand()/RAND_MAX))-(L/2);
distancefromorigin = sqrt((x*x)+(y*y));
if(distancefromorigin<R){
if(distancefromorigin>rob){
nodepositions[i][0] = x;
nodepositions[i][1] = y;
}
}
}
double adjacencymatrix [N][N];
double itzhak; //distance of node 1 from the centre
double isaac; //distance of node 2 from the centre
double vivaldi; //distance between node 1 and node 2
double phi; // a function of the above 3 doubles (see later usage)
double rubicon; // maximum distance nodes within the icecream can be apart before becoming visually indepdendent
double maxtheta; // "in the icecream" means theta < maxtheta
double theta; // angular displacement of inner point from the line bisecting the icecream
// Create adjacency matrix (note alternative implementation using incidence lists)
for (int i=0;i<N;i++){
for (int j=0;j<N;j++){
double x0 = nodepositions[i][0];
double y0 = nodepositions[i][1];
double x1 = nodepositions[j][0];
double y1 = nodepositions[j][1];
itzhak = sqrt(sqr(x0) + sqr(y0));
isaac = sqrt(sqr(x1) + sqr(y1));
vivaldi = sqrt(sqr(x0-x1) + sqr(y0-y1));
phi = ((sqr(vivaldi)+sqr(itzhak)-sqr(isaac))/(2*vivaldi*itzhak));
rubicon = ((itzhak*phi) - sqrt((sqr(rob)) - ((sqr(itzhak))*(1-sqr(phi)))));
maxtheta = asin(rob/itzhak);
theta = acos(phi);
if (x0==x1 && y0==y1){
adjacencymatrix[i][j] = 0;
}
else{
if (isaac<itzhak && theta<maxtheta) {
if (vivaldi>rubicon){
adjacencymatrix[i][j] = 0;}
else {
adjacencymatrix[i][j] = makebond(vivaldi);}
}
else{adjacencymatrix[i][j] = makebond(vivaldi);}
}
}
}
}
FILE *datafc1;
datafc1 = fopen("matrix.dat", "w");
for (int ii = 0; ii<N; ii++){
for (int jj = 0; jj<N; jj++){
int aaa;
aaa = adjacencymatrix[ii][jj];///////////////*******BROKEN LINE******
fprintf(datafc1,"%i", aaa);
}
}
fclose(datafc1);
return 0;
}
/////////////////////////////
////////////////
/////// --End Main--
////////////////
////////////////////////////
int makebond(double x){
// This function takes in the euc. dist. between two nodes and draws a link with prob. H(r)
double randomnumber = (rand()/RAND_MAX); // Random number between 0 and 1
double hr = exp(-beta*sqr(x));// ***Connection function***
int a = 1; // Number to be put into adjacency matrix
if (randomnumber > hr){
a = 0;
}
return a; //Returns 0 or 1 depending on prob. dist.
}
adjacencymatrix is declared in your first for loop, so it's out of scope before the last spot you're using it, in the print-out loop at the bottom.
In addition, you have a useless using namespace std; line. Your code doesn't include any headers that contain std namespace symbols.
Your code in line 57:
double adjacencymatrix [N][N];
is inside a for loop, outside that loop, adjacencymatrix is undefined.
You matrix is defined in the for loop on line 11. Therefore it is out of scope on line 112.
FILE *datafc1;
datafc1 = fopen("matrix.dat", "w");
for (int ii = 0; ii<N; ii++){
for (int jj = 0; jj<N; jj++){
int aaa;
//error adjacencymatrix is declared in your first for loop
aaa = adjacencymatrix[ii][jj];///////////////*******BROKEN LINE******
fprintf(datafc1,"%i", aaa);
}
}