Reputation: 311
I have the following scenario:
public class World{
public City[,] cities { get; set; }
}
public class City
{
public int number_city { get; set; }
public int x { get; set; }
public int y { get; set; }
public World world { get; set; }
public Slot[,] slots { get; set; }
}
public class Slot
{
public City city { get; set; }
public int x { get; set; }
public int y { get; set; }
}
That is, it functions as a chess field, I have the object world with several cities and each city has a number of slots. The slots in each city are arranged in a grid, and each city's grid has the same number of rows and columns.
With this method I can get the distance between the Slots of the same city:
public static double GetDistance(int x0, int y0, int x1, int y1)
{
int dX = y1 - y0;
int dY = x1 - x0;
return Math.Sqrt(dX * dX + dY * dY);
}
Only I need to get the distance between slots of different cities
In each city the positions of the slots starts at zero, so I can not use them directly for calculating between cities.
There is no need for distance between worlds.
World (A world has several cities)
City and slot
I need for example the distance between the yellow PCs, the result would be 7
Upvotes: 0
Views: 242
Reputation: 5986
If i understood your question properly that should do the trick:
public static double GetDistance(int x0, int y0, int x1, int y1)
{
// mirror image dictionary to manipulate the parallel columns
Dictionary<int, int> mirrorMatrixCols = new Dictionary<int, int>();
mirrorMatrixCols.Add(0, 7);
mirrorMatrixCols.Add(1, 8);
mirrorMatrixCols.Add(2, 9);
mirrorMatrixCols.Add(3, 10);
mirrorMatrixCols.Add(4, 11);
mirrorMatrixCols.Add(5, 12);
mirrorMatrixCols.Add(6, 13);
var result = 0;
// the distance of cells to the point of y1 column
result = (x1 - x0) * 7;
// add the distance to the manipulated column
result = result + (mirrorMatrixCols[y1] - y0);
return result;
}
Upvotes: 0
Reputation: 70691
Based on your clarification posted in the comments, I have edited your question to better reflect the scenario. You have City
objects that occupy the World
. Each City
has an X/Y coordinate within the World
, and contains Slot
objects that have X/Y coordinates within the City
. Each City
is the same size, i.e. has the same number of rows and columns, and within the world each City
is exactly adjacent to its neighbors. I.e. no gaps, no overlap, no odd-sized cities, etc.
Given that, your problem is easily solved once you treat it as a simple frame-of-reference problem. That is, while the Slot
coordinates are relative to the City
in which the Slot
is contained, they still exist within the World
. Borrowing a concept from physics or computer graphics, both of which have "frames of reference", where coordinates in one frame of reference can be mapped to another frame of reference with a simple transformation, we can apply this concept to your problem.
In particular, you can convert a Slot
's X/Y coordinates from a City
frame of reference to a World
frame of reference simply by multiplying the City
X/Y coordinates by the width and height of a single city, and then adding to that the Slot
X/Y coordinates:
struct SlotPoint
{
public readonly int X;
public readonly int Y;
public SlotPoint(int x, int y)
{
X = x; Y = y;
}
}
const int _kcityWidth = 7;
const int _kcityHeight = 7;
SlotPoint ConvertCityToWorld(Slot slot)
{
return new SlotPoint(
slot.city.x * _kcityWidth + slot.x, slot.city.y * _kcityHeight + slot.y);
}
With this transformation available, it's simple to calculate the distance between any two slots:
double GetDistance(Slot slot1, Slot slot2)
{
SlotPoint point1 = ConvertCityToWorld(slot1), point2 = ConvertCityToWorld(slot2);
return GetDistance(point1.X, point1.Y, point2.X, point2.Y);
}
Upvotes: 1