user2188311
user2188311

Reputation: 1

Drawing at specific coordinates in a loop

I am trying to write a program which simply reads through a multidimensional array (using 'for' loops) and if it encounters a certain letter (like 'A'), it would draw a rectangular shape (im using canvas library and functions to draw the shapes).

I'm trying to actually draw a maze-like map using canvas. But for some odd reason, it is not coming out right. I'm assuming it's a problem with reading the array, which I don't know why.

the multidimensional array read input from a text file which looks like this (a sample): This is the layout of the maze map I am attempting to create using canvas.

AAAAAAAAAAAAAAAAAAAAA
A.........A.........A
A.........A.........A
A...............AAAAA
AAAAA...............A
AAAAAAAAAAAAAAAAAAAAA

of course the 'A' will be replaced with a filled rectangle to represent the wall

the '.' represents the path.

here is my code:

void MazeGame::DrawMap(Canvas &canvas, char (&map)[20][26]) 
{ 
    double drawx = 200; 
    double drawy = 200;

    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < 26; j++)
        {
            if(map[i][j] == 'A') 
            {
                canvas.DrawFillRect(drawx+10, drawy, 10, 10);
            } 

            if(map[i][j] == '.')
            {
                map[i][j] = ' ';  
            }
        }
    }
}

i wrote this function in a class.

I initialized two 'double' variables, drawx and drawy, as 200. That is considered to be the x and y origin coordinate where the shape will be drawn. Of course as the array of chars is being read, the x & y coordinate has to move accordingly to how the maze layout is displayed in the text file or else, when the shape is drawn, it would simply draw over or overwrite the previously drawn shapes. I don't want that

I feel that this is so simple but the output is really confusing me. Any suggestions?

Upvotes: 0

Views: 1747

Answers (3)

JeremyFromEarth
JeremyFromEarth

Reputation: 14354

How about something like this:

if(map[i][j] == 'A')
{
    canvas.DrawFillRect(i * 10 + drawx, j * 10 + drawy, 10, 10);
} 

This way you are translating the x and y coordinates of each rectangle.

Think about it this way:

where: 
    i = 0, x = 0 * 10 + 200 = 200
    i = 1, x = 1 * 10 + 200 = 210
    i = 2, x = 2 * 10 + 200 = 220
    i = 3, x = 3 * 10 + 200 = 230
    ect...

Upvotes: 1

Grzegorz Piwowarek
Grzegorz Piwowarek

Reputation: 13873

You're not incrementing drawx and drawy so the program is always drawing at (210,200).

canvas.DrawFillRect(10*i + drawx, j*10 + drawy, 10, 10);

Also You might want to have a look at: http://en.wikipedia.org/wiki/Single_responsibility_principle

and move the dot replacement module into another function.

Upvotes: 2

XNargaHuntress
XNargaHuntress

Reputation: 751

You're never drawing anything at any other position than (210, 200).

When drawing, you should be doing something like:

canvas.DrawFillRect(drawx + 10 * j, drawy + 10 * i, 10, 10)

Assuming the array is represented in the standard [height][width] form.

Upvotes: 0

Related Questions