analytics

Wednesday, September 17, 2014

Honeycomb coordinates

The basic idea is to traverse the honey comb grid. Going up is adding to an i component, going down and right is adding to a j component, and going down and left is adding to a k component, and if you go down, up and left, or up and right, it is -1 times the i,j,k components respectively.
For example consider the black path one unit at a time:
You go up:
1,0,0
Up and right:
1,0,-1
Up:
2,0,-1
Up and right:
2,0,-2
etc...
2,1,-2
2,1,-3
2,2,-3
1,2,-3
So the final coordinate is 1,2,-3
Now consider the red path:
0,0,1
-1,0,1
-1,1,1
-2,1,1
-2,2,1
-2,2,0
-1,2,0
-1,2,-1
-1,3,-1
-1,3,-2
-1,4,-2
-1,4,-3
0,4,-3
0,3,-3
1,3,-3
1,2,-3
You still end up at 1,2,-3!

So it's kind of interesting to consider which coordinates are actually possible, for example -1,0,0 isn't possible to reach from the starting position's type of vertex in the above picture, but it is starting from the other type of vertex. I notice the possible coordinates are 0,0,0 and every other coordinate's components sum to an odd number for either type of starting vertex.

Tuesday, September 16, 2014

Spectrum x Spectrum Blend

I made the plot above by averaging the color from the spectrum on the x axis with the one on the y axis, and as you see in the circled portion blown up you get some non spectrum colors such as brown. I don't know if this is every fully saturated color or if you get more colors adding another axis...
I'm thinking if you have two prisms shining their rainbow on a piece of white paper, there might be a way to hold them so their rainbows cross to make every color...
I'm not sure the cause of the roughly 3x3 grid that appears, whether that is because humans have 3 color receptors or because computers add 3 colors of light to make every color or both...

Sunday, September 7, 2014

2,2,2 smooth curve source code

The main difference between this and Bezier curves or Nurbs is this doesn't use control points to guide the curve, it just goes through the points given directly. The way it does it is given 4 points it finds the best quadratic fit through the first 3 points parameterized from t=0..2 and the best quadractic fit for the last 3 points taken in reverse order parameterized from t=0..2 and makes a new point at the average of both curves at t=1.5. Then it does that for every group of 4 points to get twice the number of points, and recurses until it reaches the required resolution... 
The source code below takes for example these points:
points = [[50,300],[190,210],[344,256],[446,384],[580,294],[510,115]]
and generates a smooth curve through them in constant time with respect to the final number of points in the curve as described in the blog post before this...
here's a plot of the points, y increases as you move down because in image programs that's how they arrange the axis for some reason.

After the first iteration we find points between those given:
second iteration:
third:
After 10 iterations:

**Runtime complexity**
The program runs in constant time vs the final number of points generated.
**Source Code(Python) This source code requires PIL image library for python 2.7**
import Image
def e(coeff, t):
    return coeff[2]*t**2 + coeff[1]*t+coeff[0]

def m(a,b,c,d):
    coefflx = [b[0], -.5*a[0] + .5*c[0], .5*c[0]+.5*a[0]-b[0]]
    coeffly =  [b[1], -.5*a[1] + .5*c[1], .5*c[1]+.5*a[1]-b[1]]
    a,b,c
    d,c,b
    coeffrx = [c[0], -.5*d[0] + .5*b[0], .5*b[0]+.5*d[0]-c[0]]
    coeffry =  [c[1], -.5*d[1] + .5*b[1], .5*b[1]+.5*d[1]-c[1]]
    Xl = [e(coefflx, .5), e(coeffly, .5)]
    Xr = [e(coeffrx, .5), e(coeffry, .5)]
    return [.5*(Xl[0]+Xr[0]), .5*(Xl[1]+Xr[1])]
def n(a,b,c, side):
    coeffx = [b[0], -.5*a[0] + .5*c[0], .5*c[0]+.5*a[0]-b[0]]
    coeffy =  [b[1], -.5*a[1] + .5*c[1], .5*c[1]+.5*a[1]-b[1]]
    if side ==0:
        return [e(coeffx, -.5), e(coeffy, -.5)]
    else:
        return [e(coeffx, .5), e(coeffy, .5)]
def twotwotwo(plot, points, iter):
    points2 = [points[0], n(points[0], points[1], points[2], 0), points[1]]
    for i in range(0, len(points)-3):
        points2.append(m(points[i], points[i+1], points[i+2], points[i+3]))
        points2.append(points[i+2])
    points2.append(n(points[len(points)-3], points[len(points)-2], points[len(points)-1], 1))
    points2.append(points[len(points)-1])
    for i in points2:
        plot.putpixel((int(i[0]), int(i[1])), (255,255,255))
    plot.save("plot"+str(iter)+".png")
    return points2
def main():
    plot = Image.new("RGB", (640,640))
    points = [[50,300],[190,210],[344,256],[446,384],[580,294],[510,115]]
    for iter in range(1, 5):
        points = twotwotwo(plot, points, iter)
    
main()


Friday, September 5, 2014

2,2,2 Interpolation

Suppose you have a set of points and you want to find a smooth curve through them.

The dashed line is a sort of guess what it might look like, one of the points on it will be X somewhere between B and C.  First we find a parametric curve through points A,B,C called P[L,x] and P[L,y] with respect to t.
solve the above equations for the coefficients...
Plug these values for the coefficients back in the original equations...
Now plug in the actual values for A,B,C from the plot...
Let's look at the graph again...

So now for point X we consider the parametric equation going from D to C to B called P[R,x] and P[R,y] of t. The L and R are considering approaching point X form the left and from the right now as you can see. 

skipping a couple steps because it works the same the relevant part is...
and similarly
Now point X should be the average of t=.5 on all the P equations...
So we've found X...
All the points except between A and B and between E and F are found the same way. For the point between A and B we just have to use t=-.5 on P[L,x] and P[L,y] because that's the best information we have.

So once we've found all the points we'll have roughly twice as many points for our smooth curve, now we can repeat the process again on our new set of points and keep doubling the number of points until the curve is completely filled in down to the resolution we want.

**about the name**
I called it 2,2,2 interpolation because it uses a set of 2 2 dimensional parameterization's of degree 2 that are averaged to give 2 times the number of points. I suppose it could be taken to 3,3,3 and so on...

Friday, August 15, 2014

smooth curve fitting by triangularization of derivative

Suppose you have some points, for this example I'll use:
[[1,1],[3,3],[4,2]]
Like so:
First connect them with straight lines:
Now imagine you are considering the derivative:
The next step is to do what I'm calling triangularizing the above step function. This means to find the midpoints of AB and CD, E and F. Then find the point G that preserves the total area under the original step function. 
This makes a new curve when you remove B and C, 
Now this is a new approximation for the derivative, so we integrate and set the constant of integration so A goes to [1,1] where it was originally:
To get smoother and smoother approximation, iterate the derivative and triangularization step and integrate the corresponding number of times. 





Monday, August 11, 2014

Dimensional interpolation

Suppose you have a list of points symmetrically distributed around x=0 at integers...
For this method you need (n-1)/2 variables so in this case for 5 points we need 2, let's call them x and y....
Consider the following polynomial in 2 variables with 9 a(n) coefficients:
I generate the following table based on the points to be interpolated, x and y can each have values [-1,0,1], and the sum x+y is related to the range of the function. So x=-1 y=-1 x+y = -2, from the list of points the value of the function at -2 is 3 so A is three there. 
So I can solve for the 9 unknowns a1..a9 with 9 equations made from the table above:

So we can vary x between -1..1 and y between -1..1 with every possible pairwise combination to generate a plot:

Note that it is not single valued, because there are a lot of ways to add to 0 with x equaling a number between -1 and 1 and y also a number from -1 to 1. But it still does nicely interpolate the points other than that.
I haven't tried but I imagine wider ranges with more variables would work as well. I also suppose you could narrow the thickness of the band by ranging one variable over a smaller interval like y from -.5...5 and x from -1.5..1.5

Sunday, August 10, 2014

ruler

Every distance between 1 and 50 is between some pair of lines on this ruler:
Some are duplicates because there are 13 lines and 13*12/2 = 78 possible pairings and only 50 possible markings. It seemed not to be possible to make one where every distance is only marked once, as I showed for a bunch of cases on the computer by exhaustion.