1

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.

  • If you find yourself writing "to make it more clear"/"that was vague", consider going back and editing that which you feel you need to make more clear instead of adding more exposition below. Perhaps drawing out an ASCII example picture of your problem would be useful? – Jacob Parker Mar 19 '13 at 01:28
  • It wasn't that I didn't understand; I'm just offering pointers on how to write better questions (it tends to help get them answered.) If he/she answered your question please upvote him/her and "accept" it! :) – Jacob Parker Mar 19 '13 at 01:38
  • It's cool! I'm just trying to help - and I upvoted your question :) – Jacob Parker Mar 19 '13 at 01:47

1 Answers1

1

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
Community
  • 1
  • 1
munch1324
  • 1,148
  • 5
  • 10