As part of my journey to learn j I implemented a technique for computing the area of a polygon I came across in Futility Closet. I came up with a solution, but it's quite inelegant, so I'm interested in better methods:
polyarea =: -:#((+/#((1&{&|:)*(0{&|:1&|.)))-(+/#((0&{&|:)*(1{&|:1&|.))))
y =: 2 7 9 5 6,.5 7 1 0 4
polyarea y
20
This technique rotates one column and takes the dot product of the columns, then does the same after rotating the other column. The area is half the difference of these two results.
Interested in suggestions!
I think that their technique boils down to using the determinant to find the area of the polygon http://mathworld.wolfram.com/PolygonArea.html
But using the Futility Closet technique I would first close the polygon by adding the first point to the end.
y =: 2 7 9 5 6,.5 7 1 0 4
close=: (, {.)
close is a hook that takes the first pair and appends it to the end
Then take the determinants two points at a time, which is essentially what they are doing with their columns and rotations
dets=: 2 (-/ . *)\ close
dets takes the determinant of each pair of points - result is negative if the points are in clockwise order
Then take those values and process for the answer.
clean=: |#:-:#:(+/)
clean sums up the determinants, divides by 2 and returns the absolute value of the result.
clean #: dets y
20
To see the result in complete tacit form we can lean on the f. adverb (Fix) to flatten our definitions.
clean #: dets f.
|#:-:#:(+/)#:(2 -/ .*\ (, {.))
It is just a different way of looking at what they are doing, but it allows J to use the . conjunction (Dot Product) and \ adverb (Infix) to handle all of those rotations with determinants.
Hope this helps.
Related
I have 3 towers [A, B, C] which I will label in numbers as [1,2,3].
I want to find the position a disk will be in if it moves n times in the reverse direction. So if it starts at B or 2 position and moves back 7 times, it will be in A or 1 position.
Is there a general formula I can use to compute this for any given starting position and any number of moves n? To move forward, I just use
(start_pos + n) % 3.
However, I am not sure about the reverse direction.
The result of a modulus operation between a negative and a positive is a positive. I.e., you can use the same general formula, just subtract the number of steps instead of adding them:
(start_pos - n) % 3
Or, alternatively, define the number of steps moved backwards as a negative number of steps.
For affine 4*4 transformation, I saw two representation in different text
one is
L T
0 1
Another is
L 0
T 1
L is the linear part, T is the translation part; I am wondering which is correct?
Both forms are correct. The first is used in left-multiply matrix by column vector
ResultVector = Matrix*V (for example, in OpenGL), and the second - in right-multiply convention with row vector V*Matrix (for example, in DirectX)
We use libpuzzle ( http://www.pureftpd.org/project/libpuzzle/doc ) to compare 4 million images against each other for similarity.
It works quite well.
But rather then doing a image vs image compare using the libpuzzle functions, there is another method of comparing the images.
Here is some quick background:
Libpuzzle creates a rather small (544 bytes) hash of any given image. This hash can in turn be used to compare against other hashes using libpuzzles functions. There are a few APIs... PHP, C, etc etc... We are using the PHP API.
The other method of comparing the images is by creating vectors from the given hash, here is a paste from the docs:
Cut the vector in fixed-length words. For instance, let's consider the
following vector:
[ a b c d e f g h i j k l m n o p q r s t u v w x y z ]
With a word length (K) of 10, you can get the following words:
[ a b c d e f g h i j ] found at position 0
[ b c d e f g h i j k ] found at position 1
[ c d e f g h i j k l ] found at position 2
etc. until position N-1
Then, index your vector with a compound index of (word + position).
Even with millions of images, K = 10 and N = 100 should be enough to
have very little entries sharing the same index.
So, we have the vector method working. Its actually works a bit better then the image vs image compare since when we do the image vs image compare, we use other data to reduce our sample size. Its a bit irrelevant and application specific what other data we use to reduce the sample size, but with the vector method... we would not have to do so, we could do a real test of each of the 4 million hashes against each other.
The issue we have is as follows:
With 4 million images, 100 vectors per image, this becomes 400 million rows. We have found MySQL tends to choke after about 60000 images (60000 x 100 = 6 million rows).
The query we use is as follows:
SELECT isw.itemid, COUNT(isw.word) as strength
FROM vectors isw
JOIN vectors isw_search ON isw.word = isw_search.word
WHERE isw_search.itemid = {ITEM ID TO COMPARE AGAINST ALL OTHER ENTRIES}
GROUP BY isw.itemid;
As mentioned, even with proper indexes, the above is quite slow when it comes to 400 million rows.
So, can anyone suggest any other technologies / algos to test these for similarity?
We are willing to give anything a shot.
Some things worth mentioning:
Hashes are binary.
Hashes are always the same length, 544 bytes.
The best we have been able to come up with is:
Convert image hash from binary to ascii.
Create vectors.
Create a string as follows: VECTOR1 VECTOR2 VECTOR3 etc etc.
Search using sphinx.
We have not yet tried the above, but this should probably yield a bit better results than doing the mysql query.
Any ideas? As mentioned, we are willing to install any new service (postgresql? hadoop?).
Final note, an outline of exactly how this vector + compare method works can be found in question Libpuzzle Indexing millions of pictures?. We are in essence using the exact method provided by Jason (currently the last answer, awarded 200+ so points).
Don't do this in a database, just use a simple file. Below i have shown a file with some of the words from the two vectores [abcdefghijklmnopqrst] (image 1) and [xxcdefghijklxxxxxxxx] (image 2)
<index> <image>
0abcdefghij 1
1bcdefghijk 1
2cdefghijkl 1
3defghijklm 1
4efghijklmn 1
...
...
0xxcdefghij 2
1xcdefghijk 2
2cdefghijkl 2
3defghijklx 2
4efghijklxx 2
...
Now sort the file:
<index> <image>
0abcdefghij 1
0xxcdefghij 2
1bcdefghijk 1
1xcdefghijk 2
2cdefghijkl 1
2cdefghijkl 2 <= the index is repeated, those we have a match
3defghijklm 1
3defghijklx 2
4efghijklmn 1
4efghijklxx 2
When the file have been sorted it's easy to find the records that have the same index. Write a small program or something that can run through the sorted list and find the duplicates.
i have opted to 'answer my own' question as we have found a solution that works quite well.
in the initial question, i mentioned we were thinking of doing this via sphinx search.
well, we went ahead and did it and the results are MUCH better then doing this via mysql.
so, in essence the process looks like this:
a) generate hash from image.
b) 'vectorize' this hash into 100 parts.
c) binhex (binary to hex) each of these vectors since they are in binary format.
d) store in sphinx search like so:
itemid | 0_vector0 1_vector1 2_vec... etc
e) search using sphinx search.
initially... once we had this sphinxbase full of 4 million records, it would still take about 1 second per search.
we then enabled distributed indexing for this sphinxbase, on 8 cores, and now are about to query about 10+ searches per second. this is good enough for us.
one final step would be to further distribute this sphinxbase over the multiple servers we have, further utilizing the unused cpu cycles we have available.
but for the time being, good enough. we add about 1000-2000 'items' per day, so searching thru 'just the new ones' will happen quite quickly... after we do the initial scan.
I was wondering if someone could help with explaining in simple terms what interpolation is and how its used in 3d computer graphics
Simply put: given two points A and B, find a point between them.
For example, if I want to move something along a line from a position x=1 to x=4 in one step:
1-----------------------4
The first step is at location 1, the second step is at location 4, so the object moves instantly from one location to the other. However, if I want the object to take a certain amount of time or number of frames to make the transition, I'll need to refine that by finding intermediate points that are evenly spaced.
If I want the object to take two steps (or frames) to move from 1 to 4,
1-----------X-----------4
I need to calculate what the new point (X) is so I can draw the object there at the appropriate time. In this case, the point X will be
(max-min)
location = min + (current_step) * --------
steps
location is what we're trying to find. min=1, max=4, and in this example steps=2 since we want to divide the span into two steps:
step: location:
0 1
1 2.5
2 4
1------------(2.5)-----------4
If we want to take 4 steps:
step: location:
0 1
1 1.75
2 2.5
3 3.25
4 4
1---(1.75)---(2.5)---(3.25)---4
And so forth. For four steps, the object moves 25% of the total distance per frame. For 10 steps, 10%, etc ad nauseum.
For multiple dimensions (when an object has a 2- or 3-dimensional trajectory), just apply this to each X,Y,Z axis independently.
This is linear interpolation. There are other kinds. As always, Google can help you out.
Other applications include texture mapping, anti-aliasing, image smoothing and scaling, etc., and of course many other uses outside of games and graphics.
Note: a lot of frameworks already provide this. In XNA, for instance, it's Matrix.Lerp.
Interpolation is the smooth adjustment from one thing to another. It is used in animation.
For example, if an object is at location 1, and we want to move it to location 2 over the course of six seconds, we need to slowly interpolate its location between the two endpoints. Interpolation also refers to any search for a location on that path.
Interpolation is the 'guessing' of points based on other points.
for example when you have the points (0,0) and (2,2) you might 'guess' that the point (1,1) also belongs to the set.
The simples application is to deduce a line from two points.
The same thing works in 3 or actually n-dimension.
In 3D graphics it will be used
for animations, to calculate the position of things based on start and end coordinations
calculating lines
gradients
scaling of graphics
and probably many more
General Definition
Interpolation (in mathematics) can be regarded as a transition from one value to another. Interpolation usually uses a value in the 0 to 1 range like a percentage. 0 is the starting value and 1 is the end value. The main purpose of interpolation is to find values in between given values.
Types of Interpolation
There are many types of interpolation used in various programs, the most common being linear interpolation. This type of interpolation is the most simple and straight-forward; It is used to find values in a line segment between two points or numbers. There are also: cubic interpolation, quadratic interpolation, bilinear, trilinear, etc. For more information go here: https://en.wikipedia.org/wiki/Interpolation.
Application in 3D Graphics
Interpolation, especially linear, bilinear and trilinear, is important for computing fragments in geometry (the textures and visuals of the geometry), blending volumetric textures, mip-mapping (a depth of field effect on texture), and lighting (like unreal engine's volumetric lightmaps). The results of the interpolation may vary, but it could potentially yield very realistic results. It is a rather large computation, especially when the interpolation is in 3-dimensions or above (hyperspace).
Example of Interpolation
In 1 Dimension:
n1 = 1
n2 = 2
i = 0.5
n3 = (n1 - n1 * i) + n2 * i
///////////////////////////////////////
n3
├────────┼────────┼────────┼────────┤
1 1.25 1.5 1.75 2
///////////////////////////////////////
In 2 Dimensions:
v1 = {1, 1}
v2 = {1.5, 2}
i = 0.5
d = √((v1.x - v2.x)^2 + (v1.y - v2.y)^2)
v3 = {v1.x + -d * i * ((v1.x - v2.x) / d),v1.y + -d * i * ((v1.y - v2.y) / d)}
///////////////////////////////
2 ┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼
┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼
┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼
┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼
┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼ v2
1.5 ┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─●
┼─┼─┼─┼─┼─┼─┼v3─┼─┼─┼─┼─┼
┼─┼─┼─┼─┼─┼─●─┼─┼─┼─┼─┼─┼
┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼
┼v1─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼
●─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼
1 1.5 2
///////////////////////////////
Would it be reasonable to systematically try all possible placements in a word search?
Grids commonly have dimensions of 15*15 (15 cells wide, 15 cells tall) and contain about 15 words to be placed, each of which can be placed in 8 possible directions. So in general it seems like you can calculate all possible placements by the following:
width*height*8_directions_to_place_word*number of words
So for such a grid it seems like we only need to try 15*15*8*15 = 27,000 which doesn't seem that bad at all. I am expecting some huge number so either the grid size and number of words is really small or there is something fishy with my math.
Formally speaking, assuming that x is number of rows and y is number of columns you should sum all the probabilities of every possible direction for every possible word.
Inputs are: x, y, l (average length of a word), n (total words)
so you have
horizontally a word can start from 0 to x-l and going right or from l to x going left for each row: 2x(x-l)
same approach is used for vertical words: they can go from 0 to y-l going down or from l to y going up. So it's 2y(y-l)
for diagonal words you shoul consider all possible start positions x*y and subtract l^2 since a rect of the field can't be used. As before you multiply by 4 since you have got 4 possible directions: 4*(x*y - l^2).
Then you multiply the whole result for the number of words included:
total = n*(2*x*(x-l)+2*y*(y-l)+4*(x*y-l^2)