Re: trig

new topic     » goto parent     » topic index » view thread      » older message » newer message

Lewis Townsend wrote:
> I have broken up the circle (360 deg) into 16 equal angles; each 22.5 degrees.
> I have a sequence of these angles:
> {0,22.5,45,67.5,90,112.5,135,157.5,180,202.5,225,247.5,270,292.5,315,337.5}
> 
> >From that I have evaluated 16 {x,y} vectors with the following formula:
> { sin (angle), cos (angle) }
> The above is simplified for readability; degree/radian conversion is done
> separately.
> This produces the following sequence <approximately>
>
> {{0,1},{0.383,0.924},{0.707,0.707},{0.924,0.383},{1,0},{0.924,-0.383},{0.707,-0.707},{0.383,-0.924},{0,-1},{-0.383,-0.924},{-0.707,-0.707},{-0.924,-0.383},{-1,0},{-0.924,0.383},{-0.707,0.707},{-0.383,0.924}}
> 
> I want to recognize any random {x,y} vector as its closest match in the list.
> I can scale any vector to a distance of 1 with the following code:
> 
> dir = random_vector --{x,y}
> dist = sqrt(power(dir[1],2)+power(dir[2],2))
> if dist != 1 then
> 	dir[1] = dir[1] * 1 / dist
> 	dir[2] = dir[2] * 1 / dist
> end if
> 
> The question is how do I find its closest match in either the vector list or
> the
> angle list.
> All I need is an element number.

1. sequence possible_values = {-1, -0.924, -0.707, -0.383, 0, 0.383, 0.707,
0.924, 1}
2. atom ax = closest match of x in the sequence above (example: -0.707)
3. atom ay = closest match of y in the sequence above (example: 0.707)
4. integer index = find({ax, ay},
{{0,1},{0.383,0.924},{0.707,0.707},{0.924,0.383},{1,0},{0.924,-0.383},{0.707,-0.707},{0.383,-0.924},{0,-1},{-0.383,-0.924},{-0.707,-0.707},{-0.924,-0.383},{-1,0},{-0.924,0.383},{-0.707,0.707},{-0.383,0.924}})

function abs(atom a) -- absolute value
    if a >= 0 then return a else return -a end if
end function

-- atom x, y = dir[1], dir[2]
sequence possible_values
atom ax, ay, dx, dy

possible_values = {-1, -0.924, -0.707, -0.383, 0, 0.383, 0.707, 0.924, 1}

ax = possible_values[1] -- ax = closest approximation of x
ay = possible_values[1] -- ay = closest approximation of y
dx = abs(x - ax) -- dx = difference between x and ax
dy = abs(y - ay) -- dy = difference between y and ay
for i = 2 to length(possible_values) do
    if abs(x - possible_values[i]) < dx then
        ax = possible_values[i] -- find closest approximation of x
        dx = abs(x - ax)
    end if
    if abs(y - possible_values[i]) < dy then
        ay = possible_values[i] -- find closest approximation of y
        dy = abs(y - ay)
    end if
end for

integer index
index = find({ax, ay}, -- find index of closest match
    {{0,1},{0.383,0.924},{0.707,0.707},{0.924,0.383}
    ,{1,0},{0.924,-0.383},{0.707,-0.707},{0.383,-0.924}
    ,{0,-1},{-0.383,-0.924},{-0.707,-0.707},{-0.924,-0.383}
    ,{-1,0},{-0.924,0.383},{-0.707,0.707},{-0.383,0.924}})


new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu