Traversing through variable-sized lists

Dave Angel davea at ieee.org
Wed Feb 17 17:56:14 EST 2010



Andrej Mitrovic wrote:
> On Feb 17, 8:24 pm, John Posner <jjpos... at optimum.net> wrote:
>   
>> On 2/17/2010 1:10 PM, Andrej Mitrovic wrote:
>>
>> <snip>
>
> However the values list might have an uneven number of items. I would
> like to make it as evenly distributed as possible, e.g.:
>
> values =-2, -1, 0]
> frames =obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8]
>
> frames[0].func(values[0])  # func.(values[-2])
> frames[1].func(values[0])  # func.(values[-2])
> frames[2].func(values[1])  # func.(values[-2])
> frames[3].func(values[1])  # func.(values[-1])
> frames[4].func(values[1])  # func.(values[-1])
> frames[5].func(values[2])  # func.(values[-1])
> frames[6].func(values[2])  # func.(values[0])
> frames[7].func(values[2])  # func.(values[0])
>
>
> I'll be even more specific. I have a Minimum and Maximum value that
> the user enters. The frame.func() function is a "translate" function,
> it basically moves a frame in the application in one direction or
> another depending on the argument value. So frame[0].func(2) would
> move the frame[0] 2 pixels to the right. So what I want is the
> function to create a smooth transition of all the frames from the
> Minimum to the Maximum value. If minimum was 0, and maximum was 10,
> I'd want the first frame moved 0 pixels (it stays in place), the last
> frame to move 10 pixels, and the frames between are gradually moved
> from 1 pixels to 9 pixels relative from their positions.
>
> Perhaps I'm just overcomplicating. I'll have a look at some drawing
> apps and see how they've implemented drawing straight lines under an
> angle, I guess that could be called a gradual change of values.
>
> Thanks for all the suggestions everyone, I'll have a look at the rest
> shortly.
>
>   
I think you're overcomplicating.  If you have 27 frames, and you want 
frame 0 to move 0 pixels, and frame 27 to move 10 pixels, then you want 
to move frame[i] by i*10/27.  And since you do the multiply first, the 
fact that Python 2.x division gives you integers isn't a problem.

There are fancier methods for drawing lines (bresenham for example), but 
the main purpose of them is to to avoid multiply and divide, as well as 
floats.  But in Python, an integer multiply is just as fast as an add or 
subtract, so there's no point.

DaveA



More information about the Python-list mailing list