user2175569
user2175569

Reputation: 13

Drawing on the android screen with a coordinate system

My question is probably going to be initially very confusing to read, so just bear with me. I'll start it off with a little preface for context:

Preface:

I have an app that will be using an array for path-finding from a map.

^That is very vague: There will be an array of characters, representing walls, stairs, etc., and there will be a function that finds the best path.

I want to display the path on the android screen.

There will be characters that are generated by the array function that represent the generated path (probably "x" or something).


Okay, to make it more clear: There will be a "path" of 'x's in an array. These 'x's represent the path that is going to show up on the Android screen.


My actual question:

How do I translate an 'x' in the array to displaying a line on the screen? I had the idea to use a for loop/if statement that checks if there is an 'x' and if there is, then to display a little red dot/line in a second array that represents the actual screen.

I was trying to find this, but it's such an awkward thing to type into google, so I finished my research with nothing.

Is there some sort of built-in android function that lets you assign different colours to different coordinates?


This is kind of what I want to appear on the screen. If this were the app, the blue would be represented by 'x's in the first array.

Upvotes: 1

Views: 2473

Answers (1)

munch1324
munch1324

Reputation: 1148

there could be several ways to achieve this effect of having a coordinate system mapping to a matrix that describes a path.

Depending on the size of the array and the frequency of update calls (it sounds like the path finding runs once with a single render after), it probably wouldn't be too expensive to just loop through. What I personally would do is start to look at how to draw on a canvas, get the screen size, and adjust the bounds accordingly.

Get screen dimensions in pixels - How to get screen dimensions http://danielnadeau.blogspot.com/2012/01/android-canvas-beginners-tutorial.html - A nice tutorial on canvases

Once you can draw to a scaled canvas, it is simply a matter of running a loop that looks something like:

float scale_x = screen_width/columns;
float scale_y = screen_height/rows; //pixels per grid square
for( int x = 0; x < columns; x++)
for( int y = 0; y < rows; y++)
if( data[x][y] == 'x') drawRect(x*scale_x, y*scale_y, scale_x,scale_y) //if something found, draw a colored square

Upvotes: 1

Related Questions