Finding free space on a PDF page on Android - android

I need some sort of an algorithm, that would try to find free space on a specified page of a PDF.
The space I'm looking for is a square 100x100 pixel large. I would like to start searching from the bottom right, move further left in a row, and then gradually move up rows, until I either find a suitable space (free-space/white-space), or return error, that there is no free space.
Anyone aware of such a possibility in Android? And if not, how could I implement it?
Edit
I have been doing quite a bit of research on this lately, and am I right in assuming, that finding a free spot is not possible, until the document is rendered? Because if I'm getting it, right, every vector and every object should be put in it's place, just to find the free spaces? Meaning, the places which no vectors intersect?

You definitely need to render the PDF. You can use something like poppler, and then search for white regions. Here is an open source Android app using poppler that you could modify/use as inspiration:
http://code.google.com/p/apdfviewer/
Once you have the rendered PDF, you can use something like the Boyer-Moore string search algorithm to find space. I.e. in 1D you are trying to find the string "......." in "+++...++.....+..........." where . = white and + = not white.
In 2D it will be more complicated, the most efficient thing I can think of is something like this:
Search along the row until you find sufficient space.
Go to the next row and search along it up to the point you were at in the row above.
If you find space before that, repeat step 2, then continue.
If you see what I mean... something like this:
void search(int row, int space_pos, int space_height_so_far)
{
for (int x = find_next_space(row, 0); x < space_pos; x = find_next_space(row, x+1))
{
search(row+1, x, 0);
}
if (is_space(row, space_pos))
{
if (space_height_so_far > ?)
cout << "Space found at " << row << ", " << space_pos << endl;
else
search(row+1, previous_space_pos, space_height_so_far);
}
}
Hmm I can see several things wrong with that code already.. but hopefully you get the idea. I expect that algorithm exists already but I don't know the name for it unfortunately.

Related

Android Studio version of an Excel vlookup?

Ok I'm a massive noob and apart from following lots of tutorials I like to set myself a problem and then try to fix it with an app. Therefore I'm trying to make a little app that'll help me when I'm at work.
Basically it needs to breakdown a 4 character string into it's individual characters and then display them phonetically. So if I (the user) type in 5F9A then it'll display FIVE FOXTROT NINE ALPHA. At work we have an excel spreadsheet that does this all and I'm just trying to reverse engineer it. The spreadsheet itself has multiple stages, it reads the characters, converts them into ASCII and then performs a vlookup on a range of cells where each ASCII code is next to it's phonetic pronunciation. It looks for the number 53 (5 in ASCII) and then looks at the cell next to it which says FIVE.
I've managed to translate any user input into ASCII but I just don't know how to store and access this next set of data. I've been looking into SQLite but that is waaaaaay beyond me at the moment and seems far to complicated for something this simple?
Anyway, I know it's cheating asking for the answer, but maybe a push in the right direction?
The dummy way to do that would be:
Get every letter (char) of the word
Have a switch case that gives you the phonetic equivalent (you will have to do that by hand)
String word = yourWord;
String phonetic;
char currentChar;
for(i=0;i<=word.lenth();i++){
currentChar = word.substring(i, i+1);
phonetic = getPhonetic(currentChar)
}
String getPhonetic(char char){
switch char{
case a:
return alpha;
break;
case b:
....
}
}

How can I have a multiple line Label in a PieChart in AChartEngine?

I have a pie chart using AChartEngine that I'm very happy with. I've chosen to style it without a Legend and with Labels (the text connected to its corresponding Pie wedge with a little line) formatted like "Example Data Point - 25%"
I get this Label string via:
mSeries.add(name + " - " + Double.toString(value), value);
But often the name is too long so the value doesn't display. I get "...". Since the value is important, I want to put it on a new line so it will always show.
I'm picturing Labels that look something like:
Short Name
10%
Too Long of A Na...
25%
Also fine:
Too Long of A
Name
25%
Or:
Too Long of A
Name - 25%
If I could wrap long names to new lines, that would be even better! But I really am mostly concerned with getting the Value to always display.
I tried to add a new line with \n or System.getProperty("line.separator") but the PieChart still renders the Label as one line. The \n is not getting displayed as text, it's like it's not even there. According to other Questions, this supposedly works on other Chart types, but no one is claiming it works for Pies, which it doesn't seem to based on my efforts.
I've also played with other rendering options built into AChartEngine. Such as 'setDisplayValues(true)', which puts the values right over the pie wedges. I don't like how that looks, as my pie has some tiny wedges, so the values get rendered on top of each other.
Any insight on how I can make the Labels on AChartEngine PieChart multi-line (or any other way to force the value to display) would be very appreciated! Thank you so much to anyone who can help!
It's not what I really wanted, but since a few months have gone by, I've come up with an alternative. For posterity, I will leave it here as an answer to my own question but not accept it in case someone out there does someday share a real solution.
Since I couldn't find a way to ensure that the labels on my pie chart always showed percent values, I have the info Toast when a wedge is clicked. Here is the simple enough code:
private void addPieClickListeners(final GraphicalView mChartView){
mChartView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
// Do Nothing, Possible Toast:
// Toast.makeText(context,"No chart element was clicked",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context,
Integer.toString((int)Math.round(seriesSelection.getValue())) +
"% " +
mSeries.getCategory(seriesSelection.getPointIndex()),
Toast.LENGTH_SHORT).show();
}
}
});
I call this method, along with my other functions to set up my Pie Chart, in onCreateView(), since this is in a Fragment.
Cheers.

Android - Randomly setting a button in a grid

this is an extension to this question: Android - Handling a grid
Basically, I am wondering how to make a grid of 4x4 buttons randomly change their features(text, color, etc.) I don't need help setting the actual change in text or color, I just need a way to go about something like this. I don't know if I should write an array and choose from there, or use the GridView. Just a little start to something is all I need, I'm not asking for lots of code. Thanks in advance, I really appreciate it.
If your 4 ids are sequential as is
btnId1 = 8006;
btnId2 = 8007;
btnId3 = 8008;
btnId4 = 8009;
Random rn = new Random();
int chooseId = btnId1 + rn.nextInt(btnId4 - btnId1 + 1);
You can generate a random number like so:
Random r=new Random();
r.nextInt(4);
this will give you a random number in the range [0,4).
Based on this random number, you change the properties of the appropriate tile/square which you can enumerate yourself from 0 to 3 since you have 4 tiles in total.
From both your questions it seems that you lack some basic programming knowledge of algorithms/approaches to problems. You should try to fill that gap, which will make you progress MUCH MUCH faster in your programming endeavors!

Android - Determine tile bitmap based on surrounding tiles

I have been working on a tile-based terrain generation system , and have run into a bit of a snag. I am hoping to create a series of transition tiles that will mark the transition between water and land, and am having trouble figuring out an efficient way to figure out which tile should be which.
My first attempt (illustrated below) would basically run each tile through a series of if statements to figure out which one it should be. The main problem with this is that, with a 100 tile x 100 tile world map, it would be running though 10,000 iterations, accessing the data on the 8 surrounding tiles (80,000 operations), and then running through up to four if statements (320,000 operations). It just seems to me that this would be horribly inefficient and slow.
The upside of this method is that it would only run on land tiles, and would first check to make sure that it is adjacent to at least one water tile, which would greatly reduce the number of required operations.
Here's the basic chart I drew up that walks through the surrounding tiles and picks out the appropriate tile.
My second idea was to essentially start walking through tiles and, once I hit a coastal tile, follow the coast in both directions, assigning tiles as I went. this method would make sure that the tile hasn't already been figured out before starting. The problem with this is that, one, I can't figure out quite how that would work, and two, as a result I have no idea how efficient it would be.
A friend told me about a third method that might work. It takes water tiles and sets them equal to 0 and land tiles, which are set to 1. Then, it takes the surrounding tiles and numbers them from 1 to 9. From there were walk through them and create a string on 0's and 1's:
W W W
W L L
L L L
would be: 000011111
0*2^0 + 0*2^1 + 0*2^2 + 0*2^3 + 1*2^4 + 1*2^5 + 1*2^6 + 1*2^7 + 1*2^8
0*1 + 0*2 + 0*4 + 0*8 + 1*16 + 1*32 + 1*64 + 1*128 + 1*126 = 496
The theory is that I would assign the tile associated with that combination the number 496 and load it in response. The problem is that each edge has 13 or 14 combinations taht would result in its use. for instance:
W W L L W W
W L L and W L L Both need the same tile as the above example, but
L L L L L L produce different numbers.
Essentially, to make this method work I would have to figure out the final number for each possible combination of water and land that would result in a particular tile, and then run the final number through a series of ifs / cases to pick out the appropriate bitmap. This would be just as, or more, inefficient than the if blocks.
So, coming to the actual question in all of this. Does anyone know of an alternate way of doing this, or a way to make either of these methods more efficient?
I actually just wrote this part of a game I started working on. Took me a little while to think of the solution but when I did I couldn't believe I didn't think of it earlier. If you think about it, each tile has 8 possible "connections":
1 2 3
4 x 5
6 7 8
And each connection can either contain a tile it connects to or not. So this is sweet, it means its a very binary system and we can represent every combination with 8 bits (or 1 byte), and we know every combination is represented sequentially from 0-255, so we can easily use a lookup table (well, lookup array) to find the correct image. Though this does mean there are 256 possible combinations of connection (not 256 different images mind you, like you said above different combinations will use the same tile graphic) we have to generate. What I did (and I recommend), is wrote a small script to generate the lookup array.
Now the way to maintain this is that every tile has an 8 length array of the tiles it is connected to. When loading the map, fill in the "connections" array of the tiles as you go. Once a tile has all its connections (or once all tiles are loaded) then calculate the "connection mask" or "connection value" for each tile (which is the index into the lookup array). This is done by looping through the "connections" array and building the mask using bit shifts. After that you should only ever need to do very minimal processing again, and only on the individual tiles that are added/removed/changed and the 8 possible tiles its connected to.
Hope this helps. If you need more details or example code I can provide that later!

Randomly Generating Sentences for Display

I have a few questions concerning the application I'm designing. I have many different routes I could try in order to get what I wanted, but I thought I would get suggestions instead of doing trial and error.
I'm developing a simple app that has one Game screen (activity) and a main menu to start the game. The game screen will have two buttons, "Next" and "Repeat". Every time "Next" is hit, a new sentence in a different language (with the english translation below it) will appear, audio will pronounce the sentence, and hopefully I can get a highlighter to highlight the part of the sentence being spoken. You can guess what the Repeat button does.
My question is, what do you guys think would be the best way to store these sentences so they can be randomly picked out? I thought about making an array of structures or classes with the English definition, audio, and sentence in each structure. Then using a random iterator to pick one out. However, it would take a long time to do this approach and I wanted to get some ideas before I tried it.
Also, I'm not sure how I would print the sentence and definition on the screen.
Thanks!
Using an array of structs/classes seems like that would be the normal way to go.
Not really sure what you mean by a random iterator, but when picking out random sentences from the array of sentences, you might want to avoid repeats until you've gone through all the elements. To do that, you can make a second array of indices, select one at random from those, use the element that corresponds to that index, and remove that number from the array of indices.
In Java, that would look something like
ArrayList<Sentence> sentences;
ArrayList<Integer> indices;
Random rand;
private void populateIndices() {
for(int i = 0; i < sentences.size(); i++)
indices.add(i);
}
public Sentence getNextSentence() {
if(indices.isEmpty())
populateIndices();
int idx = rand.nextInt(indices.size());
int val = indices.get(idx);
indices.remove(idx);
return sentences.get(val);
}
Quite frankly I would load out of copyright books from Project Gutenberg and randomly pull sentences from them. I would then pass the sentences into Google APIs to translate and pronounce the sentences. Relying on external services is at the very heart of what a connected OS like Android is made for. It would be a much more compelling use of the platform than a canned Rosetta Stone like CD solution and your ability to tap into a broader amount of content would be increased exponentially.

Categories

Resources