Implementing Midpoint Displacement - c++

I have been able to successfully implement midpoint displacement in a numbered array in another program and so i'v tried to implement it in a 3D world to create terrain however the outcome of the algorithm isn't what i expected.
void MidPointDisplacement(float grid[][WIDTH], int left, int right, int top, int bottom, int row, int col)
{
int centreX = (left + right) / 2; //Get the centre of the row
int centreY = (top + bottom) / 2; //Get the centre of the column
if (centreX == left)
{
return;
}
if (centreY == bottom)
{
return;
}
grid[top][centreX] = ((grid[top][left] + grid[top][right]) / 2) + jitter; //define top
grid[bottom][centreX] = ((grid[bottom][left] + grid[bottom][right]) / 2) + jitter; //define bottom
grid[centreY][left] = ((grid[top][left] + grid[bottom][left]) / 2) + jitter; //define left
grid[centreY][right] = ((grid[top][right] + grid[bottom][right]) / 2) + jitter; //define right
grid[centreX][centreY] = ((grid[centreY][left] + grid[centreY][right] + grid[top][centreX] + grid[bottom][centreX]) / 4) + jitter; //Get centre
//decreased the random values
RANDMAX / 2;
RANDMIN / 2;
MidPointDisplacement(grid, centreX, right, centreY, bottom, row, col);
MidPointDisplacement(grid, left, centreX, top, centreY, row, col);
MidPointDisplacement(grid, centreX, right, top, centreY, row, col);
MidPointDisplacement(grid, left, centreX, centreY, bottom, row, col);
}
The result of the above is this:
3D Midpoint Displacement
However I expected a terrain like this:
What I expected
Is there any reason why this maybe? I initially thought it could've been the jitter however decreasing the initial value doesn't solve this problem.
float jitter = rand() % (int)(RANDMAX - RANDMIN + 1) + RANDMIN;
Jitter is a global float that takes a random value between RANDMAX (initial value of 5.0f) and RANDMIN (initial value of -5.0f).
Solution:
To fix the above error I deleted the jitter I had created in the program and instead made jitter a float variable of 1.
In the MidpointDisplacement function I changed this:
grid[top][centreX] = ((grid[top][left] + grid[top][right]) / 2) + jitter; //define top
grid[bottom][centreX] = ((grid[bottom][left] + grid[bottom][right]) / 2) + jitter; //define bottom
grid[centreY][left] = ((grid[top][left] + grid[bottom][left]) / 2) + jitter; //define left
grid[centreY][right] = ((grid[top][right] + grid[bottom][right]) / 2) + jitter; //define right
grid[centreX][centreY] = ((grid[centreY][left] + grid[centreY][right] + grid[top][centreX] + grid[bottom][centreX]) / 4) + jitter; //Get centre
To this:
grid[top][centreX] = ((grid[top][left] + grid[top][right]) / 2) + ((rand() % 256) - 128) / 128.0F * JITTER_RANGE * jitter; //define top
grid[bottom][centreX] = ((grid[bottom][left] + grid[bottom][right]) / 2) + ((rand() % 256) - 128) / 128.0F * JITTER_RANGE * jitter; //define bottom
grid[centreY][left] = ((grid[top][left] + grid[bottom][left]) / 2) + ((rand() % 256) - 128) / 128.0F * JITTER_RANGE * jitter; //define left
grid[centreY][right] = ((grid[top][right] + grid[bottom][right]) / 2) + ((rand() % 256) - 128) / 128.0F * JITTER_RANGE * jitter; //define right
grid[centreY][centreX] = ((grid[centreY][left] + grid[centreY][right] + grid[top][centreX] + grid[bottom][centreX]) / 4) + ((rand() % 256) - 128) / 128.0F * JITTER_RANGE * jitter; //Get centre
I then called the function again as done before however this time I divided the jitter by 2.
Also, I discovered that I stupidly had centreX and centreY the wrong way around in the line(Not changed here):
grid[centreX][centreY] = ((grid[centreY][left] + grid[centreY][right] + grid[top][centreX] + grid[bottom][centreX]) / 4) + jitter; //Get centre

Related

How do I resolve a collision's position properly in 2D collision detection?

My current implementation looks like this:
if (shapesCollide) {
if (velocity.y > 0) entity.position.y = other.position.y - entity.size.y;
else entity.position.y = other.position.y + other.size.y;
velocity.y = 0;
if (velocity.x > 0) entity.position.x = other.position.x - entity.size.x;
else entity.position.x = other.position.x + other.size.x;
velocity.x = 0;
}
However, this leads to weird handling when movement is happening on both axes - for example, having entity moving downward to the left of object, and then moving it to collide with object, will correctly resolve the horizontal collision, but will break the vertical movement.
I previously simply went
if (shapesCollide) {
position = oldPosition;
velocity = { 0, 0 };
}
But this lead to another multi-axis issue: if I have my entity resting atop the object, it will be unable to move, as the gravity-induced movement will constantly cancel out both velocities. I also tried considering both axes separately, but this lead to issues whenever the collision only occurs when both velocities are taken into account.
What is the best solution to resolving collision on two axes?
I assume that the entities can be considered to be more or less round and that size is the radius of the entities?
We probably need a little vector math to resolve this. (I don't know the square-root function in c++, so be aware at sqrt.) Try replacing your code inside if(shapesCollide) with this and see how it works for you.
float rEntity = sqrt(entity.size.x * entity.size.x + entity.size.y * entity.size.y);
float rOther = sqrt(other.size.x * other.size.x + other.size.y * other.size.y);
float midX = (entity.position.x + other.position.x) / 2.0;
float midY = (entity.position.y + other.position.y) / 2.0;
float dx = entity.position.x - midX;
float dy = entity.position.y - midY;
float D = sqrt(dx * dx + dy * dy);
rEntity and rOther are the radii of the objects, and midX and midY are their center coordinates. dx and dy are the distances to the center from the entity.
Then do:
entity.position.x = midX + dx * rEntity / D;
entity.position.y = midY + dy * rEntity / D;
other.position.x = midX - dx * rOther / D;
other.position.y = midY - dy * rOther / D;
You should probably check that D is not 0, and if it is, just set dx = 1, dy = 0, D = 1 or something like that.
You should also still do:
velocity.x = 0;
velocity.y = 0;
if you want the entities to stop.
For more accurate modelling, you could also try the following:
float rEntity = sqrt(entity.size.x * entity.size.x + entity.size.y * entity.size.y);
float rOther = sqrt(other.size.x * other.size.x + other.size.y * other.size.y);
float midX = (entity.position.x * rOther + other.position.x * rEntity) / (rEntity + rOther);
float midY = (entity.position.y * rOther + other.position.y * rEntity) / (rEntity + rOther);
float dxEntity = entity.position.x - midX;
float dyEntity = entity.position.y - midY;
float dEntity = sqrt(dxEntity * dxEntity + dyEntity * dyEntity);
float dxOther = other.position.x - midX;
float dyOther = other.position.y - midY;
float dOther = sqrt(dxOther * dxOther + dyOther * dyOther);
entity.position.x = midX + dxEntity * rEntity / dEntity;
entity.position.y = midY + dyEntity * rEntity / dEntity;
other.position.x = midX + dxOther * rOther / dOther;
other.position.y = midY + dyOther * rOther / dOther;
which finds the midpoints when the radii are taken into account. But I won't guarantee that that works. Also, the signs on the last additions are important.
I hope this helps (and works). Let me know if something is unclear.

Animating along an elliptical path in OpenGL

I am trying to make a red circle follow the path of a semi-circle using the DDA algorithm in OpenGL. I almost have it, though the circle is slightly offset on its X-axis, which increases as the angle of the semi-circle increases.
Any assistance would be greatly appreciated! Here's my code:
scrPt movecircle (scrPt p1, scrPt p2)
{
scrPt circlePos;
float angle, x = p1.x, y = p1.y, vectorX, vectorY;
// Get tahe x distance between the two points
int dx = p2.x - p1.x, steps;
// Get the y distance between the two points
int dy = p2.y - p1.y;
// Get the length between the points
float length = sqrt(dx*dx + dy*dy);
if (fabs (dx) > fabs (dy))
steps = fabs (dx);
else
steps = fabs (dy);
// calculate the direction
float xIncrement = float (dx) / float (steps);
float yIncrement = float (dy) / float (steps);
if (nextPos == 0)
{
for(int i = 0; i < steps; i++)
{
glClear(GL_COLOR_BUFFER_BIT);
angle = PI * i / steps;
vectorX = x + (length / 2) * cos(angle + theta);
vectorY = y + dy / 2 + (length / 2) * sin(angle + theta);
circlePos.x = round(vectorX - length / 2);
circlePos.y = round(vectorY);
drawCircle (circlePos.x, circlePos.y);
drawArch();
glFlush();
usleep(3000);
}
}
else
{
for (int i = 0; i < steps; i++)
{
glClear(GL_COLOR_BUFFER_BIT);
drawCircle (round(x),round(y));
glFlush();
usleep(3000);
x += xIncrement;
y += yIncrement;
}
}
return circlePos;
}
There were a couple of errors in the for-loop that were causing the issue. I needed to change
this:
vectorX = x + (length / 2) * cos(angle + theta);
to this:
vectorX = x + (dx / 2) + (length / 2) * cos(angle + theta);
and this:
circlePos.x = round(vectorX - (length / 2));
to this:
circlePos.x = round(vectorX);

Half of my ellipse drawn in the wrong place

Here is the code for an oval drawing method I am working on. I am applying the Bresenham method to plot its co-ordinates, and taking advantage of the ellipse's symmetrical properties to draw the same pixel in four different places.
void cRenderClass::plotEllipse(int xCentre, int yCentre, int width, int height, float angle, float xScale, float yScale)
{
if ((height == width) && (abs(xScale - yScale) < 0.005))
plotCircle(xCentre, yCentre, width, xScale);
std::vector<std::vector <float>> rotate;
if (angle > 360.0f)
{
angle -= 180.0f;
}
rotate = maths.rotateMatrix(angle, 'z');
//rotate[0][0] = cos(angle)
//rotate[0][1] = sin(angle)
float theta = atan2(-height*rotate[0][1], width*rotate[0][0]);
if (angle > 90.0f && angle < 180.0f)
{
theta += PI;
}
//add scalation in at a later date
float xShear = (width * (cos(theta) * rotate[0][0])) - (height * (sin(theta) * rotate[0][1]));
float yShear = (width * (cos(theta) * rotate[0][1])) + (height * (sin(theta) * rotate[0][0]));
float widthAxis = abs(sqrt(((rotate[0][0] * width) * (rotate[0][0] * width)) + ((rotate[0][1] * height) * (rotate[0][1] * height))));
float heightAxis = (width * height) / widthAxis;
int aSquared = widthAxis * widthAxis;
int fourASquared = 4*aSquared;
int bSquared = heightAxis * heightAxis;
int fourBSquared = 4*bSquared;
x0 = 0;
y0 = heightAxis;
int sigma = (bSquared * 2) + (aSquared * (1 - (2 * heightAxis)));
while ((bSquared * x0) <= (aSquared * y0))
{
drawPixel(xCentre + x0, yCentre + ((floor((x0 * yShear) / xShear)) + y0));
drawPixel(xCentre - x0, yCentre + ((floor((x0 * yShear) / xShear)) + y0));
drawPixel(xCentre + x0, yCentre + ((floor((x0 * yShear) / xShear)) - y0));
drawPixel(xCentre - x0, yCentre + ((floor((x0 * yShear) / xShear)) - y0));
if (sigma >= 0)
{
sigma += (fourASquared * (1 - y0));
y0--;
}
sigma += (bSquared * ((4 * x0) + 6));
x0++;
}
x0 = widthAxis;
y0 = 0;
sigma = (aSquared * 2) + (bSquared * (1 - (2 * widthAxis)));
while ((aSquared * y0) <= (bSquared * x0))
{
drawPixel(xCentre + x0, yCentre + ((floor((x0 * yShear) / xShear)) + y0));
drawPixel(xCentre - x0, yCentre + ((floor((x0 * yShear) / xShear)) + y0));
drawPixel(xCentre + x0, yCentre + ((floor((x0 * yShear) / xShear)) - y0));
drawPixel(xCentre - x0, yCentre + ((floor((x0 * yShear) / xShear)) - y0));
if (sigma >= 0)
{
sigma += (fourBSquared * (1 - x0));
x0--;
}
sigma += (aSquared * (4 * y0) + 6);
y0++;
}
//the above algorithm hasn't been quite completed
//there are still a few things I want to enquire Andy about
//before I move on
//this other algorithm definitely works
//however
//it is computationally expensive
//and the line drawing isn't as refined as the first one
//only use this as a last resort
/* std::vector<std::vector <float>> rotate;
rotate = maths.rotateMatrix(angle, 'z');
float s = rotate[0][1];
float c = rotate[0][0];
float ratio = (float)height / (float)width;
float px, py, xNew, yNew;
for (int theta = 0; theta <= 360; theta++)
{
px = (xCentre + (cos(maths.degToRad(theta)) * (width / 2))) - xCentre;
py = (yCentre - (ratio * (sin(maths.degToRad(theta)) * (width / 2)))) - yCentre;
x0 = (px * c) - (py * s);
y0 = (px * s) + (py * c);
drawPixel(x0 + xCentre, y0 + yCentre);
}*/
}
Here's the problem. When testing the rotation matrix on my oval drawing function, I expect it to draw an ellipse at a slant from its original horizontal position as signified by 'angle'. Instead, it makes a heart shape. This is sweet, but not the result I want.
I have managed to get the other algorithm (as seen in the bottom part of that code sample) working successfully, but it takes more time to compute, and doesn't draw lines quite as nicely. I only plan to use that if I can't get this Bresenham one working.
Can anyone help?

What are some tips to make a function with a lot of calculation clean? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
How can functions like this:
void Map::Display()
{
if(initialized)
{
HRESULT hr;
int hScrollPos = GetScrollPos(M_HWnd, SB_HORZ);
int vScrollPos = GetScrollPos(M_HWnd, SB_VERT);
D2D1_RECT_F region = {0,0,TILE_WIDTH,TILE_HEIGHT};
D2D1_RECT_F tFRegion = {0,0,TILE_WIDTH,21}; // tile front's region
Coor coor;
int tileHeight;
RECT rect;
GetWindowRect(M_HWnd, &rect);
int HWndWidth = rect.right - rect.left;
int HWndHeight = rect.bottom - rect.top;
pRT->BeginDraw();
pRT->Clear(D2D1::ColorF(0.45f, 0.76f, 0.98f, 1.0f));
pRT->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
for(int x=0; x<nTiles; x++)
{
coor = ppTile[x]->Getcoor();
tileHeight = ppTile[x]->Getheight();
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
if((coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos > 0 - TILE_WIDTH &&
(coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos < HWndWidth &&
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos > 0 - (TILE_HEIGHT * 2.5) &&
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos < HWndHeight)
{
/* Draws tiles */
pRT->SetTransform(D2D1::Matrix3x2F::Translation(
(coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos,
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos
));
pRT->FillRectangle( &region, pBmpTileBrush[ppTile[x]->GetType() + 1]);
/* Draws tiles' front */
if((coor.Y - 1) / 2 < mapSizeY - 1) // If we are not in the front row,
{
if(coor.X > 1)
{
for(int diffH = tileHeight - ppTile[x + mapSizeX - 1]->Getheight(); diffH == 0; diffH--)
{
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->SetTransform(D2D1::Matrix3x2F::Translation(
(coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos,
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos + (TILE_HEIGHT * 0.75) + (diffH * TILE_PIXEL_PER_LAYER)
));
pRT->FillRectangle( &tFRegion, pBmpTileFrontBrush[ppTile[x]->GetType()]);
}
}
if(((coor.X -1) / 2) + 1 < mapSizeX)
{
for(int diffH = tileHeight - ppTile[x + mapSizeX]->Getheight(); diffH == 0; diffH--)
{
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->SetTransform(D2D1::Matrix3x2F::Translation(
(coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos,
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos + (TILE_HEIGHT * 0.75) + (diffH * TILE_PIXEL_PER_LAYER)
));
pRT->FillRectangle( &tFRegion, pBmpTileFrontBrush[ppTile[x]->GetType()]);
}
}
if(coor.X == 1 || (coor.X - 1) / 2 == mapSizeY - 1) // If the tile if at any of left or right edge,
{
for(int n = ((TH * 1.5) / TPPL) - (ppTile[x + mapSizeY + mapSizeY - 1]->Getheight() - tileHeight); n>=0; n--)
{
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->SetTransform(D2D1::Matrix3x2F::Translation(
(coor.X - 1) * (TILE_WIDTH * 0.5) - hScrollPos,
((coor.Y - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos + (TILE_HEIGHT * 0.75) + (n * TILE_PIXEL_PER_LAYER)
));
pRT->FillRectangle( &tFRegion, pBmpTileFrontBrush[ppTile[x]->GetType()]);
}
}
}
else // If we are in the front row
{
for(int h = tileHeight; h >= 0; h--)
{
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->SetTransform(D2D1::Matrix3x2F::Translation(
(coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos,
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos + (TILE_HEIGHT * 0.75) + (h * TILE_PIXEL_PER_LAYER)
));
pRT->FillRectangle( &tFRegion, pBmpTileFrontBrush[ppTile[x]->GetType()]);
}
}
}
}
pRT->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
hr = pRT->EndDraw();
}
}
this:
Tile* Map::GetClickedTile(short xPos, short yPos)
{
Tile* pNoClickedTile = NULL;
int hScrollPos = GetScrollPos(M_HWnd, SB_HORZ);
int vScrollPos = GetScrollPos(M_HWnd, SB_VERT);
if(xPos < (mapSizeX * TILE_WIDTH) - hScrollPos) // If the click is within width of the map then...
{
Coor coor;
int height;
int currentTile;
int tileDistanceFromTop;
/* Checks if click is in an odd row of tiles */
int column = (xPos + hScrollPos) / TILE_WIDTH;
for (int y=mapSizeY-1; y>=0; y--)
{
currentTile = column + (y * (mapSizeX+mapSizeX-1));
coor = ppTile[currentTile]->Getcoor();
height = ppTile[currentTile]->Getheight();
tileDistanceFromTop = ((coor.Y / 2) * TILE_HEIGHT * 1.5f) + // Distance between two tiles
( (MAX_MAP_HEIGHT - height) * TILE_PIXEL_PER_LAYER) -
vScrollPos +
SPACE_LEFT_FOR_BACKGROUND;
/*if (tileDistanceFromTop < 0) // If the tile is partially hidden,
tileDistanceFromTop = tileDistanceFromTop % TILE_HEIGHT; // then % TILE_HEIGHT*/
if( yPos > tileDistanceFromTop &&
yPos < tileDistanceFromTop + TILE_HEIGHT)
{
/* Get relative coordinates */
int rpx = xPos % TILE_WIDTH;
int rpy = ( (yPos - SPACE_LEFT_FOR_BACKGROUND) -
(y * (TILE_HEIGHT /2) ) -
( ( MAX_MAP_HEIGHT - height) * TILE_PIXEL_PER_LAYER) +
vScrollPos) %
TILE_HEIGHT;
/* Checks if click is withing area of current tile */
if (rpy + (rpx / (TILE_WIDTH /16)) > TILE_HEIGHT * 0.25f && // if click is Down Right the Upper Left slope and,
rpy + (rpx / (TILE_WIDTH /16)) < TILE_HEIGHT * 1.25f && // it is UL the LR slope and,
rpy - (rpx / (TILE_WIDTH /16)) < TILE_HEIGHT * 0.75f && // it is UR the LL slope and,
rpy - (rpx / (TILE_WIDTH /16)) > TILE_HEIGHT * -0.25f) // it is DL the UR slope,
return ppTile[currentTile]; // Then return currentTile
}
}
/* Checks if click is in an even row of tiles */
column = (xPos + hScrollPos - (TILE_WIDTH/2)) / TILE_WIDTH;
for (int y=mapSizeY-2; y>=0; y--)
{
currentTile = column + (y * (mapSizeX+mapSizeX-1)) + mapSizeX;
coor = ppTile[currentTile]->Getcoor();
height = ppTile[currentTile]->Getheight();
tileDistanceFromTop = (((coor.Y - 1) / 2) * TILE_HEIGHT * 1.5f) + // Distance between two tiles
( (MAX_MAP_HEIGHT - height) * TILE_PIXEL_PER_LAYER) +
(TILE_HEIGHT * 0.75) -
vScrollPos +
SPACE_LEFT_FOR_BACKGROUND;
/*if (tileDistanceFromTop < 0)
tileDistanceFromTop = tileDistanceFromTop % TILE_HEIGHT;*/
if( yPos > tileDistanceFromTop &&
yPos < tileDistanceFromTop + TILE_HEIGHT)
{
/* Get relative coordinates */
int rpx = xPos % TILE_WIDTH;
int rpy = (int)((yPos - SPACE_LEFT_FOR_BACKGROUND) -
(y * (TILE_HEIGHT /2) ) -
( ( MAX_MAP_HEIGHT - height) * TILE_PIXEL_PER_LAYER) -
(TILE_HEIGHT * 0.675) +
vScrollPos) %
TILE_HEIGHT;
/* Checks if click is withing area of current tile */
if (rpy + (rpx / (TILE_WIDTH /16)) > TILE_HEIGHT * 0.25f && // if click is Down Right the Upper Left slope and,
rpy + (rpx / (TILE_WIDTH /16)) < TILE_HEIGHT * 1.25f && // it is UL the LR slope and,
rpy - (rpx / (TILE_WIDTH /16)) < TILE_HEIGHT * 0.75f && // it is UR the LL slope and,
rpy - (rpx / (TILE_WIDTH /16)) > TILE_HEIGHT * -0.25f) // it is DL the UR slope,
return ppTile[currentTile]; // Then return currentTile // Then return currentTile
}
}
}
return pNoClickedTile;
}
Or even this:
int Map::GetTileNByCoor(Coor coor)
{
return ((coor.X / 2 + ((coor.Y - 1) * mapSizeY) - (coor.Y / 2));
}
be made easier to read? As my code grows bigger, I realize how important, if not at times necessary, it is to have a clean, easy to read code. What are some tips to make codes like the ones above cleaner?
My general refactoring practices is usually to do the following:
Pull out names for things that aren't apparent in the code. You can use local variables to give defining names to small pieces of code. So, in cases like your last example, what does (coor.X / 2 + ((coor.Y - 1) * mapSizeY) represent?
In most cases its better to have things names well, than worry about storing local variables (they will be deleted when the stack leaves the function, and usually you are not going to be too worried about memory space/speed of the code at such a fine grain).
Pull out groups of executing code into methods. A good rule of thumb is if your function is more than 6 lines of code, you can probably pull out a smaller function inside of it. Then your code will read better to what it's actually doing.
A very common place to look at this is loops. You can almost always pull the code inside a loop into it's own function, with a good descriptive name.
After you have pulled out methods, you can group common shared functionality into smaller objects. It's almost always better to have smaller objects working together to do the work, than to have giant objects that do a lot of work. You want your objects to each have a single responsibility.
Pretty solid code, well done. I would consider:
Comment the function itself at a high-level, and then add better comments for all the significant blocks in the code, and for anything unusually tricky.
Use descriptive consts or #defines for all the magic variables you're using. Why multiply by 0.675? What does 0.675 represent? Ditto 0.25, 1.25, -0.25 etc.
Turn things like the "Checks if click is withing area of current tile" test (and others) into a separate method that you call, for example isClickInsideTile(x,y,tile).
Add debug trace so that the next person responsible can enable debug to get diagnostics.
PS good job with your variable names and method names.

Cairo Matrix equivillant of GlOrtho Matrix?

Given that I do something like this:
void glOrtho( GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble nearVal,
GLdouble farVal);
and the result is: http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xmlw could I achieve a matrix like this:
http://cairographics.org/manual/cairo-matrix.html
I tried this:
cairo_matrix_t mat;
mat.xx = 2 / (right - left);
mat.yx = 0;
mat.xy = 2 / (top - bottom);
mat.yy = 0;
mat.x0 = 0;
mat.y0 = 0;
cairo_set_matrix(cr,&mat);
But it did not work. How could I acheive the same matrix that GlOrtho makes in Cairo?
Thanks
I don't know Cairo so I'll delete my answer if a better one comes.
According to the docs of Cairo:
x_new = xx * x + xy * y + x0;
y_new = yx * x + yy * y + y0;
When you use OpenGL, the formula is like: (m being the matrix)
x_new = m(1,1) * x + m(1,2) * y + m(1,3) * z + m(1,4)
y_new = m(2,1) * x + m(2,2) * y + m(2,3) * z + m(2,4)
z_new = m(3,1) * x + m(3,2) * y + m(3,3) * z + m(3,4)
(note that for the sake of simplicity I did not mention the fourth coordinate)
So what you have to do is simply match the two formulas:
mat.xx = 2 / (right - left);
mat.yy = 2 / (top - bottom);
mat.xy = 0;
mat.yx = 0;
mat.x0 = -(right + left) / (right - left);
mat.y0 = -(top + bottom) / (top - bottom);
Please try this