Algorithm for selecting tiles outwards center point in a 512*512 map - mysql

I've got a specific problem. My data (map) in mysql is as follows
id table_row table_col tile_type
1 1 1 0
2 2 1 0
3 3 1 0
... ... ... 0
512 512 1 0
513 1 2 0
514 2 2 0
515 3 2 0
... ... ... 0
... 512 2 0
... 1 3 0
... 2 3 0
... 3 3 0
... ... ... 0
... 512 3 0
... 1 4 0
Map is 512*512. I need to come up with an algorithm that selects tiles from the centre(or near centre 256*256) point. So it should look something like
256*256 first - once selected we can update tile_type to 1
255*256 second - update tile_type to 1
256*255 third - update tile_type to 1
257*256 fourth - update tile_type to 1
256*257 fifth - update tile_type to 1
etc. or similar, but it has to start filling in tiles from centre outwards in all directions (can be random). Any ideas appreciated

Your question lacks a few details, but I am assuming you are asking a means of generating an id that is close to the center of your 512x512 grid.
It appears your grid is enumerated in a particular manner: each column is enumerated in increasing order of table_row values, and the enumeration of columns is done in increasing order of table_col values.
Consequently, we can already know the id of the cell for which the table_row and table_col values are 256: it is 255 x 512 + 256. That is correct, because there are 255 full columns that were enumerated before enumeration started for table_col value 256, and each of those columns had 512 rows in them. Finally, within this column, we are interested in row #256.
A more generalized version of this would look like below.
((num_cols + 1) / 2 - 1) * num_rows + (num_rows + 1) / 2
You don't need to care all that much about the +1s and -1s: they are just a numerical hack to handle odd num_rows and num_cols values.
Anyways, to introduce a proximity measure, you can just use two random variables. A random variable P can represent the distance to the center in terms of colums. (i.e. how far the table_col of the point with the generated id will be from the table_col value of the center of the grid) Another random variable Q can represent the distance to the center in terms of rows.
((num_cols + 1) / 2 - 1 + P) * num_rows + ((num_rows + 1) / 2 + Q)
Then you can just generate values for P and Q based on your needs, and get the id of a cell that is P colums and Q rows away from the center of the grid.

Try Below query.
SELECT (MAX(t.`row`+1)/2), (MAX(t.`column`+1)/2) INTO #max_row, #max_col
FROM tiles t;
SELECT t.`row`, t.`column`, ceil(IF(ABS(#max_row - t.`row`) < ABS(#max_col - t.`column`), ABS(#max_col - t.`column`), ABS(#max_row - t.`row`))) as tbl_order
FROM tiles t
ORDER BY 3

Related

MySQL per shift counter query

I'm looking for some advice with a query I'm trying to write. I would like to count the occurrence (number of rows) of a column based on whose shift (6-2, 2-10 & 10-6) it falls into. The trouble is the shifts rotate every two weeks, e.g. if I just started on the 6-2 in two weeks time I would be on the 10-6. I have a query that will count said occurrence depending on the shift but I cannot get my head around how to make it count based on whos shift it happened on.
Any advice would be very helpful!
Thanks in advance.
Edit:
Table headings
Serial Number | Date & Time | Part Number | Type | Result
Edit 2 (a bit more detail):
I would like to count how many instances of unknown occur in part number for each our shifts. The part number column contains various numbers (always length 8) identifying the product running through the machine. If the machine for whatever reason doesn't see or read the part number correctly it writes unknown to the table.
e.g.
Week 13: Tom is on 6-2 shift / Alex is on 2-10 shift / Cam is on 10-6
Week 15: Alex is on 6-2 shift / Cam is on 2-10 / Tom is on 10-6 shift
Week 17: Cam is on 6-2 / Tom is on 2-10 shift / Alex is on 10-6 shift.
This pattern continues indefinitely and I want to some the number of unknowns in part number for Cam's shift, Tom's shift and Alex's shift. So I should end up with three numbers. I hope this helps.
OK, so we can get the week number in MySQL. For instance:
WEEK(<Date & Time colum>, 1)
have a look at the link above to decide which mode you need.
There are three shifts and three workers. I'll give them numbers:
Tom = 0
Alex = 1
Cam = 2
6-2 = 0
2-10 = 1
10-6 = 2
I then get this pattern:
week
shift
worker
rotation
0
0
0
0
0
1
1
0
0
2
2
0
1
0
1
1
1
1
2
1
1
2
0
1
2
0
2
2
2
1
0
2
2
2
1
2
3
0
0
3
3
1
1
3
3
2
2
3
4
0
0
0
Now we need to try and compute the 'shift' and 'worker' numbers from the 'week' number. Let's start with the 'shift'. There is no way to get it from week, we just have to define it:
SET shift0 = 0
SET shift1 = 1
SET shift2 = 2
This seems pointless, but bear with me. Now we need to compute the workers. How would we do this, given the 'week' and 'shift'? Here we have to use modulo 3:
SET worker0 = MOD('week' + 'shift0', 3)
SET worker1 = MOD('week' + 'shift1', 3)
SET worker2 = MOD('week' + 'shift2', 3)
Written full out with the WEEK() function:
SET worker0 = MOD(WEEK(<Date & Time colum>, 1) + 0, 3)
SET worker1 = MOD(WEEK(<Date & Time colum>, 1) + 1, 3)
SET worker2 = MOD(WEEK(<Date & Time colum>, 1) + 2, 3)
This is as far as I am willing to take it.
I would try to reconstruct the missing information using the above and store it in the database. Create tables to hold the workers and the shifts. Otherwise you'll have to write very complicated queries, which is not what you want.

How to store a binary grid using a float variable

This is not connected to any particular programming language, I'm trying to look for the most efficient way to store a binary grid (It will look like this...)
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Into an int with a limit of 9,999,999
So each "1" Have a Boolean of true/false a.k.a "1" or "0" state so it can be displayed as also
1 0 0 1 1
1 1 0 1 1
1 1 1 1 1
All this is expected to be stored in an int so it can be transformed binary into digits.
So at the end of the day, I should be able to first set a binary grid then turn the binary grid into digits where I can store them into a variable, And then be able to extract them after from the variable back to the binary grid.
And also to answer any question related to why I just don't simply use an array or anything in relation, Well basically this is the only way the engine lets me store things to then extract them later.
Things I try: I already know I can store things within the digit system, What I lack is getting the most out of it, right now I can store 23 Bits which means that I got to get a binary grid that looks like this
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1
And remember this is having the limit of 9,999,999
I found out the system can have it from -9,999,999 to 9,999,999.
Another thing is that the limit of 9,999,999 is all-around any function so adding 9,999,999 + 9,999,999 is out of the question for that limitation.
So I'm going to add all the relevant details:
I can only use digits to store data in a variable.
The limit of the digits go only to 9,999,999
If I were to use floats it will have to be < 999,999.00 note that the limit on the float is 2 decimal points (0.00)
If I want, I can use multiple variables in terms that have more capacity
What I'm looking for is to try to get the most amount of bits out of the digits limit in the variable.

Average of rows Mysql ignore zero values

Hello I have a problem with the function avg. I have a table like this and I would like to take the average of each row. I also have the zero in some cells and would like to avoid count them.
data rep val1 val2 val3
1 a 0 3 3
2 a 1 4 0
3 a 1 1 1
4 a 1 3 0
And I would like this result
data AVG
1 3
2 2.5
3 1
4 2
thank you
Assuming you have at least one non-zero value:
SELECT data, (val1+val2+val3)/((val1!=0) + (val2!=0) + (val3!=0)) avg
FROM **table_name**
I think divide by zero returns null see manual, depending on your db settings, so you could do:
SELECT data, COALESCE((val1+val2+val3)/((val1!=0) + (val2!=0) + (val3!=0)),0) avg
FROM **table_name**
Any null values in a row will cause each query to always return null and 0 for the row respectively.

mysql query to calculate values local to Cartesian products of logical groups of rows

I'm trying to write a query to process a single table that looks like this:
record_id item_id part_id part_length
----------- ------- -------- ------------
1 0 0 123.12
2 0 0 123.09
3 0 1 231.24
4 0 1 239.14
5 1 0 45.91
6 1 0 46.12
7 1 1 62.24
8 1 1 59.40
which is basically a table of inaccurate length measurements of some parts of some items recorded multiple times (not twice, actually each part has 100s of measurements). With a single select, I want to get a result like this:
record_id item_id part_id unit part_length_ratio
----------- ------- -------- ----- ----------------
1 0 0 1 123.12 / 231.24
2 0 0 1 123.09 / 239.14
3 0 1 0 231.24 / 123.12
4 0 1 0 239.14 / 123.09
5 1 0 1 45.91 / 62.24
6 1 0 1 46.12 / 59.40
7 1 1 0 62.24 / 45.91
8 1 1 0 59.40 / 46.12
which is basically selecting each part of an item as the unit and calculates the ratio of the length of other parts of the same item to this unit while matching the measurement times. I wrote a script which computes this kind of table but would like to do it with sql. I can understand if you fail to understand the question :)
for each item i
for each part unit of i
for each part other of i
if unit != other
print i.id other.part_id unit.part_id other.length / unit.length
As I said in a comment, tables are unordered sets: there is no first or second row...
... unless if you want to use the id column to explicitly order the rows.
However, can you guarantee that there will always be (exactly) two samples for each case and that the "lower ID" always match the first sample? This appears to be quite fragile as in real-life, there will probably have cases where a test will be performed twice or a test will be missing or done "late". Not mentioning concurrent access to your DB.
Can't you simply add a "sample number" column?

Why Do You Need To Minus 1 When Determining The Maximum Number N Bits Can Represent?

So in binary to find the largest number you can represent given N amount of bits, you would use:
2^N - 1
But why the -1. To try understand it i created a 3 Bit systems and tried some examples:
2^1 = (2) - 1
0 0 1 --> 1
2^2 = (4) - 1
0 1 0 --> 2
0 1 1 --> 3
2^3 = (8) - 1
1 0 0 --> 4
1 0 1 --> 5
1 1 0 --> 6
1 1 1 --> 7
So it all works out as planned, but why the -1. This probably sounds like a stupid question but as you can see above i have done a fair amount of research.
Because you can represent 0 which always takes up one spot in all the permutations.
The research shown should reveal the answer already, but you have forgotten about the zero.
Three bits are able to represent 2^3 different values. The smallest value is zero, so the largest must be 2^3-1.
Note that if you use a different system (such as signed binary), the smallest and largest value may change, but the count of values does not.