This site requires JavaScript, please enable it in your browser!
Greenfoot back
Entity1037
Entity1037 wrote ...

2013/12/14

How to order actors in order based on position?

Entity1037 Entity1037

2013/12/14

#
I'm trying to make a method that returns an array with all of the objects in the world in order from down-left of the screen to top-right of the screen, with the objects being from left to right, and then the next lowest y value from left to right, etc.. I have NO idea of how to make one efficient enough to not lag the scenario, though. Does anyone know any way?
erdelf erdelf

2013/12/14

#
well, just get all objects in an array, and then implement a quicksort code
erdelf erdelf

2013/12/14

#
	static void qsort (Actor[] array, int le, int ri) {
		int lo = le, hi = ri;

		if (hi > lo) {

			int mid = array[(lo + hi) / 2].getX();
			while (array[lo].getX() <= array[hi].getX()) {

				while (array[lo].getX() < array[ri].getX() && array[lo].getX() < mid)
					++lo;

				while (array[hi].getX() > array[le].getX() && array[hi].getX() > mid)
					--hi;

				if (array[lo].getX() <= array[hi].getX()) {
					swap(array, array[lo].getX(), array[hi].getX());
					++lo;
					--hi;
				}
			}
			if (array[le].getX() < array[hi].getX()) {
				qsort (array, le, hi);
			}

			if (array[lo].getX() < array[ri].getX()) {
				qsort( array, lo, ri);
			}
		}
	}

	static void quickSort (Actor[] array) {
		qsort (array, 0, array.length - 1);
	}
this should sort the x coordinates, but i am too tired too write it for the y-coordinates
Entity1037 Entity1037

2013/12/14

#
Thank you!
You need to login to post a reply.