Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!


lightxbulb

Member Since 20 Apr 2013
Offline Last Active Aug 24 2015 09:05 AM

Topics I've Started

What can I improve in this code

03 November 2014 - 05:21 AM

What could I improve in this code? Any constructive criticism is highly appreciated, especially about the recursion and how I could make the code more legible/understandable - because even I'll have issues to understand what's what if I look at it in a few days (and I couldn't really find a way to explain in comments the recursive methods):

 
#include <vector>
#include <string>
#include <iostream>
#include <fstream>

class Branch
{
	friend class Tree;

private:
	Branch* parent;
	std::vector<Branch*> children;
	
	int y;
	int x;

private:
	Branch()
		:parent(0), x(0), y(0)
	{
	}

	Branch(const Branch& another)
	{
	}

	~Branch()
	{
		parent = 0;
		children.clear();
		x = 0;
		y = 0;
	}

private:

	//find the index in the children
	unsigned int findIndex()
	{
		//assume parent != 0
		for (unsigned int k = 0; k < parent->children.size(); k++)
		{
			if (parent->children[k] == this)
				return k;
		}
		//if it's not part of the children I messed somewhere else in the code
		//so you'll get an out of bounds index
		return parent->children.size(); 
	}

	void shiftChildren(int shift)
	{
		for (auto iter = children.begin(); iter != children.end(); iter++)
		{
			(*iter)->x += 2 * shift;
			(*iter)->shiftChildren(shift);
		}
		return;
	}
	
	//shift the parents by shift
	//and every other cell right of our cell by shift
	void shiftRightSide( int shift )
	{
		if (parent == 0)
			return;
		parent->x += shift;
		for (unsigned int k = findIndex()+1; k<parent->children.size(); k++)
		{
			parent->children[k]->x += 2 * shift;
			parent->children[k]->shiftChildren(shift);
		}
		return parent->shiftRightSide( shift );
	}

	Branch* findRightmost()
	{
		if (children.empty())
			return this;
		children.back()->findRightmost();
	}

	//ignore this
	int countSpaces(const std::string& str)
	{
		if (str == "")
			return 0;
		unsigned int counter = 0;
		for (unsigned int k = 0; k < str.size(); k++)
		{
			if (str[k] == ' ')
				counter++;
		}
		return counter+1;
	}

	void displayChildren(std::vector<std::string>& map, unsigned int level)
	{
		if (children.empty())
			return;
		level++;
		if (map.size()-1 < level)
		{
			map.push_back("");
		}

		int prevX = map[level].size();
		for (auto iter = children.begin(); iter != children.end(); iter++)
		{
			for (int k = 0; k < (*iter)->x - prevX; k++)
			{
				map[level] += " ";
			}
			map[level] += "*";
			(*iter)->displayChildren(map, level);
			prevX = (*iter)->x+1;
		}

		return;
	}


};

class Tree
{


private:
	std::vector<Branch*> branches;

public:

	Branch root;
	int dx;
	int dy;

public:

	Tree( int dx = 1, int dy = 1)
		:dx(dx), dy(dy)
	{
	}

	Branch* add(Branch& another)
	{
		Branch* temp = new Branch();
		branches.push_back(temp);

		temp->parent = &another;
		temp->x = another.x;
		temp->y = another.y + dy;
		if (!another.children.empty())
		{
			//just for spacing - doesn't work
			//if (another.children.back()->findRightmost() == another.children.back())
			//{
				temp->x = another.children.back()->findRightmost()->x + 2*dx;
				temp->shiftRightSide(dx);
			/*}
			else
			{
				temp->x = another.children.back()->findRightmost()->x + 4 * dx;
				temp->shiftRightSide(2*dx);
			}*/
			
			
		}
		another.children.push_back(temp);

		return temp;
	}

	void printStructure( std::ostream& os )
	{
		std::vector<std::string> map;
		map.push_back("");
		for (int k = 0; k < root.x; k++)
		{
			map[0] += " ";
		}
		map[0] += "*";
		root.displayChildren(map, 0);

		for (unsigned int k = 0; k < map.size(); k++)
		{
			/*map[k] += "\n";
			os.write(map[k].c_str(), map[k].size());*/
			os << map[k] << std::endl;
		}

		return;
	}

	void populate(Branch& another, int level)
	{
		level--;
		if (level == 0)
			return;
		Branch* temp = 0;
		for (int k = 0; k < std::rand() % 5; k++)
		{
			temp = add(another);
			populate(*temp, level);
		}
	}

	~Tree()
	{
		for (auto iter = branches.begin(); iter != branches.end(); iter++)
		{
			delete *iter;
		}

		branches.clear();
		dx = 0;
		dy = 0;
	}
};


int main()
{
	Tree myTree;
	myTree.populate(myTree.root, 10);
	std::ofstream file("tree.txt");
	myTree.printStructure(file);
	file.close();

	//std::cin.sync();
	//std::cin.ignore();
	return 0;
}
 

It's supposed to create a structure of a tree and print it (use notepad to open it).


Physics - bouncing ball, error accumulation

12 June 2013 - 06:30 AM

Hi!

I've been doing a simulation of a perfect case of a ball bouncing against a flat plane(representing the ground) accelerating under the force of gravity. Here are the important parts: 

I chose one of the simplest cases where the gravity vector is equal to 1 and points down the Y axis(opposite direction - basically points at the ground). The ball starts in mid air with a velocity vector equal to 0. It accelerates towards the "ground" under the force of gravity and bounces back up - then the whole thing repeats. Here's something that might help me explain what I want:

Collision normal vector: n

Velocity vector of the ball: v

Gravity vector: g

 

Here's how I calculate how the ball's velocity vector changes after the collision with the ground(it is basically mirrored around the normal):

v1 = v -2*(v dot n)*n, where (v dot n) is the dot product between 'v' and 'n'. For every iteration of the main loop I do an update for the velocity of the ball. It is important to note that in this simple case:

n(0,1,0) ; v(0, x, 0); g(0, -1, 0)

 

I've got some function like this in the main loop:

Function updateMovement(in.Body, world.Physics, deltaTime#)
	
	If( EntityCollided(in\obj, colWorld) ) Then
		
		Local nx# = CollisionNX(in\obj)
		Local ny# = CollisionNY(in\obj)
		Local nz# = CollisionNZ(in\obj)
		
		Local vPn# = in\vx*nx + in\vy*ny + in\vz*nz
		
		in\vx = in\vx - 2*vPn*nx
		in\vy = in\vy - 2*vPn*ny
		in\vz = in\vz - 2*vPn*nz
		
	EndIf
	in\vx = in\vx + world\gravityx*deltaTime
	in\vy = in\vy + world\gravityy*deltaTime
	in\vz = in\vz + world\gravityz*deltaTime
	
	TranslateEntity in\obj,TranslateEntity in\obj, in\vx*deltaTime*world\speedScale, in\vy*deltaTime*world\speedScale, in\vz*deltaTime*world\speedScale
End Function 

Don't mind really the strange syntax - the various variables are:

g(world\gravityx, world\gravityy, world\gravityz);  v(in\vx, in\vy, in\vz), n(nx, ny, nz)

deltaTime is the average time it takes for 1 frame.

world\speedScale is just a coefficient that scales the movement for the ball.

TranslateEntity is the function that "makes the ball move" given an entity and a vector in the format TranslateEntity entity,x,y,z.

CollisionN* returns the normal for the collision. EntityCollided evaluates to true if there is a collision between the ball and the plane.

 

So it's something like:

void updateMovement()
{
    //if the ball touches the ground:
    //change the velocity vector of the ball accordingly(basically a bounce off)
    //it is important to note that the velocity vector's size doesn't change -
    //just its direction
    //end if

    //calculate the change in speed
    //move the ball accordingly
}

The problem is error accumulates with every bounce off the ground and rather than have the ball retaining a constant energy level(kinetic+potential) it starts gaining some. If I make deltaTime constant such things don't happen, however isn't this a pretty bad idea?

Would it be bad if I just fix it to 1/60 and I just cap fps at 60, there will be a problem if fps go under 60 though.

 

P.S. Btw first I was wondering where this error accumulation was coming from(in the beginning I thought I messed up something with the equations), I actually understood that I should put a constant deltaTime to fix the problem while trying to formulate the question.

 

And btw, am I missing something or should it be:

TranslateEntity in\obj,(in\vx+world\gravityx/2*deltaTime)*deltaTime*world\speedScale,(in\vy+world\gravityy/2*deltaTime)*deltaTime*world\speedScale,(in\vz+world\gravityz/2*deltaTime)*deltaTime*world\speedScale

	in\vx = in\vx + world\gravityx*deltaTime
	in\vy = in\vy + world\gravityy*deltaTime
	in\vz = in\vz + world\gravityz*deltaTime

Rather than the way I wrote it. I mean S = v0*t+1/2*a*t*t and not S = v0*t+a*t*t

Problem is I get an error accumulation with the equation that should be right...


template functions and STL list.sort

31 May 2013 - 12:12 PM

Hi!

With C++ I've currently gotten to the part  where I am learning different STL containers. I've been playing around with lists but I get an error, I just want to know why do I get an error when I do this:

template <typename T>
bool sortDescending(const T& in1, const T& in2)
{
    return in1>in2;
}

 

and then I just try to sort some list with it:

myList.sort(sortDescending);

 

If I change my sort function it's all fine:

bool sortDescending(const int& in1, const int& in2)
{
    return in1>in2;
}

 

 

Can anybody explain why exactly C++ forbids me from writing a more generic sort function?


else if question

22 May 2013 - 04:04 AM

First of all, I am reading a book on C++ - there was this one example in it:

#include <iostream>
using namespace std;

class Date
{
private:
	int day, month, year;
public:
	Date(int inputDay, int inputMonth, int inputYear)
		: day(inputDay), month(inputMonth), year(inputYear){}


	bool operator== (const Date& input)
	{
		if( (day == input.day) && (month == input.month) && (year == input.year) )
			return true;
		else
			return false;
	}

	bool operator< (const Date& input)
	{
		if(year < input.year)
			return true;
		else if(month < input.month)
			return true;
		else if(day < input.day)
			return true;
		else 
			return false;
	}

	bool operator<= (const Date& input)
	{
		if(this->operator== (input))
			return true;
		else 
			return this->operator< (input);
	}

};

int main()
{
	Date d1(19 , 4, 2013);
	Date d2(19, 5, 2012);
	cout << (d1<d2) << endl;

	cin.ignore();
	return 0;
}

 

 

Btw it's me that changed the main a little - just to show what a ridiculous output I get.

Is it me or is the inequality operator coded wrong?

	bool operator< (const Date& input)
	{
		if(year < input.year)
			return true;
		else if(month < input.month)
			return true;
		else if(day < input.day)
			return true;
		else 
			return false;
	}

 

If it were me I'd code it like this:

	bool operator< (const Date& input)
	{
		if(year < input.year)
			return true;
		else if(year > input.year)
			return false;
		else //when year == input.year
		{
			if(month < input.month)
				return true;
			else if(month > input.month)
				return false;
			else //when month == input.month
			{
				if(day < input.day)
					return true;
				else 
					return false;
			}
		}
	}

 

Is it the book's example that is wrong or is it me missing something crucial?


Super slow compiling?

13 May 2013 - 03:32 PM

Hi!

Since a few days ago I've been experiencing some extreme slow compile on Visual C++. I think that even when I try to play a music file with vlc it starts extremely slow - but it could be just me. The games I have start up normally though and I'm not experiencing any fps drop... I've noticed that if I restart the PC the compile time goes back to normal. Any advice to what may be causing this and how to fix it?

 

P.S. The ram usage is under 50% when that happens and the CPU may be even at 0% and at the same time I am waiting inexplicably long for some simple thing like properties showing up... Seems like Windows is "slow" and only at doing some tasks. My HDD works fine IMO as copying has a normal speed. The last things I did before this happened was install some custom windows themes, installed skype and copied around 100Gb from my external HDD - btw I still have around 500Gb free space (all of these could have just coincided with that).


PARTNERS