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!
Related
I am beginner and trying to write some calculations with App Inventor 2.
I am trying to write a code to calculate Net present value.
The formula of NPV = - investment + CF/(1+i)power up by years of investment, which means if years of investment are > 1 the second part of formula will repeat until it reached the number of years.
I successfully code the formula for one year that works correct, but have problem with the "repeating" the second part powered by number of years.
I tried to declare years as variable to use it as powering number but think something is wrong with it.
In my opinion I need to split the powering number somewhere to memory and then increase it by 1 until the required number. However have no clue how to do it.
Can anyone help?
Screenshot of the blocks
Following the calculation from the NPB Calculator,
this is converted into blocks the following
Note: for a better clarity and to avoid such long calculation blocks as in your screenshot, I used External Inputs instead of Inline Inputs, which is the default. You can switch that from the context menu after doing a right mouse click onto one of the calculation blocks.
EDIT: screenshot updated for changing cashflows using a list.See also
How to work with Lists by Saj and
How to work with Lists and Lists of lists (pdf) by appinventor.org
I working on an app that I need to calculate distance travelled from point A to point B (by car).
I asked Elm Electronics (chipset manufacturer) and they said there is no standard OBD-II PID to return mileage from odometer, although car manufacturers might provide a PID. Since this way is not standard then I found another way.
PID 0131 (Distance traveled since codes cleared), is returning something that I think might be helpful. IF i'm able to clear it at point A and read its value at point B then I'm done :)
I thought a lot to guess what does "codes cleared" mean but I couldn't realize what does it mean? and my another important question, how to clear code or reset this PID?
Any suggestion would be appreciated. Thanks.
Update
I just tested on two Cars.
On Benz car no OBD-II command works. I couldn't get any data :(
I got correct reply on Persona car (Local Malaysia) but 0x0131 PID was always returned 7F01 which is 16608KM even after passing few Kms. I tried to reset it by sending 04 command (as Eric suggested on his answer), However, nothing got clear :) and I still got 7F01 as response.
My Library can be used for anyone who is looking for OBD-II lib from here.
So, What I plan to do is, since I'm able to get speed (v) then I'm able to calculate distance based on d = v * t formula.
Elm Electronics are right. The resetting trouble codes solution is a possible, but maybe unwanted workaround though.
Mode 04 is for resetting the codes. Sending 04 will reset the MIL (Malfunction Indicator Light) and reset the codes you want to reset.
In the comments, Chris suggested to use the value, and than keep track of this value yourself. That way you don't need to misuse the Mode 04.
Th 0131 value overflows at 65535 km. But when you bring you car in for maintenance, they could reset this value, depending on who is maintaining your car ofcourse.
Source: Mode 04 - Wikipedia
There are two PIds: 0x0121 Distance travelled with malfunction indicator lamp (MIL) on which keeps the distance with MIL on and 0x0131 Distance travelled since codes cleared which keeps the distance after clearing the MIL by using mode 0x04. By saying code, it meant the Diagnostics Trouble Code (DTC). When one of them keeps counting the distance the other one is fixed and activation is done for them only based on MIL on or off.
For having the milage, afaik, you need to have current mileage from the odometer as the reference, in addition to those two PIDs. For example, if the current mileage on the odometer* is X and the first time readings for those two PIDs are Y and Z respectively, and x and y are real-time readings from those two PIDs, these two formulas can give you the mileage and trip distance:
Real-Time mileage** = X + (y - Y) + (z - Z)
Trip distance (MIL Off) = x(end) - x(start)
Trip distance (MIL On) = y(end) - y(start)
*The odometer is supposed to be available by PID 0x01A6 Odometer, but in almost all the vehicles, it's not supported.
**The overflow in readings from those two PIDs should be considered as well.
I think You can use the PID 0x011F (Run time since engine start) and PID 0x010D (Vehicle speed). If you save these values in an sd card then you can multiply these two values.
I have completed a phonegap app which pools gps cordinate data every 10 sec to the server. now according to the calculations 8 hours of tracking it will store around 8*60*6=2880 records per user. my current requirement is limited to use of 20 user. (basically it tracks a users rout travelled)
There are two parts to the question:
what is the best way to store the data and retrieve it as fast as possible.
is it possible to display 2880 coordinates at a time on google maps API v3 ? if not, what is the best way to display the rout traveled?
I am having good results with 90 or so points, for one of my demos, but the enormous 2880 records per user per 8 hours is what worries me.
Thanks
EDIT 1
Although this is an old question , I recently worked on a project where I displayed about 10K points on the map, I hope my observations would help the future visitors:
The google maps as if now do not have a hard limit on the number of points you can display on the client side.
The number of points you can display on the client side is entirely dependent on the client side `Hardware` , bigger the number of points using a jpeg or gif marker , slower will be the renders , when moving around or zooming in and out
To have huge number of pointers on the map with a minimal performance hit, precomputing the number of points needed to be rendered before and after pan or zoom occurs will help a lot.
So here is a possible solution:
First of all, you need to find out how many points Google Maps API can handle and still show the line. I think this will just take some testing or researching. Anyways once you find your magical number of points to display to plot your path then take that number and multiply it by 2/3.
For instance if a good path needs have say 90 points then calculate 90*2/3
The reason for 2/3 is that the following loop will return a max number of points that is averagely equal to 3/2 times the variable we use so using. 60 would give us on average 90 plots. There is a case where the most returned plots would be (2 * (magical number of points)) - 1 for instance say we want on average of 90 points then we could in some rare cases have (2*(90*2/3))-1 = 119 points You will just have to do some testing after implementation to make sure that your magical number of points works good for maps with 2/3 of the magical number of points and 2 * magical number of points -1. I hope this isn't too confusing... I tried to explain as best I can.
The rest of this is going to be sudo code. You will have to adapt it for whatever language you connect to MySQL with:
//get the total number of rows returned
var total_rows = mysql->num_rows;
//calculate max to be 2/3 times your magic number for max plots, i.e. 90
var max_plots = 90*2/3;
//define empty plots array to fill with coordinates
var plots = array();
//check if total_rows is less than max_plots then:
if(total_rows > max_plots){
//find the quotient of the the divident total_rows and the divisor max_plots rounded down to the nearest whole int
var quotient = floor(total_rows/max_plots);
//define variable i to use in loop
var i = 1;
//loop through returned rows
while(row = mysql->fetch_row()){
//return only rows that are the first, last, or are dividable by the quotient evenly; Note: if your language supports it, use the Modulus operator like (i % quotient) == 0 for the last or statement.
if(i == 1 || 1 == total_rows || (i - (i * (floor(i/quotient)))) == 0){
//set plots to use on map
plots[] = array(
'lat' => row['lat'],
'lon' => row['lon'],
);
}
//increment counting variable
i++;
}
// else if total_rows less than or equal to max_plots retrieve all plots
} else {
while(row = mysql->fetch_row()){
plots[] = array(
'lat' => row['lat'],
'lon' => row['lon'],
);
}
}
This may not be the best way as it still requires to retrieve all of the rows from the database, but it does solve how to only print a selected maximum amount evenly spaced on the Google map.
Note: Be sure that your query orders the rows by an auto incrementing key or some other way so that the plots will be in order that they were entered into the database.
The most detailed maps would be a map with (2 * magic_plot_number) - 1 and your least details map would contain magic_plot_number or if lower, the number of total_plots. That being said an 8 hour tracking would plot a path with points every 7 minutes and 51 seconds totaling 61 points over 8 hours using the magic plot number of 90. The more plots the closer number of points will be to 2/3 * the magic plot number
I hope this helps you with this situation.
In my application i want to know the transmit power of a mobile phone when i received from it data (beacon, file, ..). I need to know the transmit power, to use it in distance calculations using Received Signal Strength Indicator (RSSI).
How i can do that?
Ok, to measure distance using signal dampening:
There are different way of doing this. The most basic way is using open space dampening, where you get reference values inside a certain space, e.g a room. Any new furniture or change in surface material affect these values. This document is some guys thesis or something about the subject, you might want to check this out: http://www.s2is.org/Issues/v1/n2/papers/paper14.pdf. Good location is difficult especially, because the dampening is not linear.
This function is for theoretican dampening in open space:
L = 20 log(d) + 20 log(f) + 36.6,
where L is dampening in Db
f is frequency in MHz
and d is distance in miles.
This is the radio theory
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.