MCPcopy Create free account
hub / github.com/SimHacker/micropolis / getDir

Method getDir

micropolis-java/src/micropolisj/engine/Sprite.java:94–116  ·  view source on GitHub ↗

Computes direction from one point to another. @return integer between 1 and 8, with 1 == north, 3 == east, 5 == south, 7 == west.

(int orgX, int orgY, int desX, int desY)

Source from the content-addressed store, hash-verified

92 * 7 == west.
93 */
94 static final int getDir(int orgX, int orgY, int desX, int desY)
95 {
96 final int Gdtab [] = { 0, 3, 2, 1, 3, 4, 5, 7, 6, 5, 7, 8, 1 };
97 int dispX = desX - orgX;
98 int dispY = desY - orgY;
99
100 int z = dispX < 0 ? (dispY < 0 ? 11 : 8) : (dispY < 0 ? 2 : 5);
101
102 dispX = Math.abs(dispX);
103 dispY = Math.abs(dispY);
104 int absDist = dispX + dispY;
105
106 if (dispX * 2 < dispY) z++;
107 else if (dispY * 2 < dispX) z--;
108
109 if (z >= 1 && z <= 12) {
110 return Gdtab[z];
111 }
112 else {
113 assert false;
114 return 0;
115 }
116 }
117
118 /**
119 * Computes manhatten distance between two points.

Callers 3

moveImplMethod · 0.45
moveImplMethod · 0.45
moveImplMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected