Python Forum
Animating 2D object movement with matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Animating 2D object movement with matplotlib
#1
Hello there. I'm a super-fresh computer science student and I have been given the following task: I need to create an animation using matplotlib in python (Jupiter Notebook). The animation has to transfer a 2D object for point A to point B using the Brezenham algorithm. The coordinates (x,y) are entered by the user. The following things should be visible: the trajectory of the object, the movement of the object and the final destination of the object.
So far I have not gotten very far with this task. I have a simple program that draws lines based on the Brezenham algorithm. But I have no idea how to structure the code in such a way that:
a) The user has the ability to enter the coordinates (I'm guessing x = input() is supposed to be implemented somehow)
b) How to actually make python's matplotlib.animation module animate the movement of some object. I'm guessing it has to calculate each coordinate individually and place the object there frame by frame. But structuring the body of the code is a nightmare to me at this point. All the tutorial videos show how to animate the built-in animations, not a custom code, like I have to use. And all the codes I've seen have parts of code that I simply fail to understand. And the errors that Jupiter throws at me are beyond my understanding so far as well.

Here's the algorithm I have written so far:

import numpy as np                 
import matplotlib.pyplot as plt   

img=np.ones((600,600,3)) 

def DrawLine(x1,y1,x2,y2):
    dx = abs(x2-x1) 
    dy = abs(y2-y1)
    if x1<x2:
        xs=1 
    else:
        xs=-1   
        
    if y1<y2:
        ys=1
    else:
        ys=-1
     
    
    x=x1
    y=y1  
    
    p=2*dy-dx   
    

    if dx>dy:
        while x<x2:
            x=x+xs
            if p > 0:
                y=y+ys
                p=p+2*dy-2*dx
            else:
                p=p+2*dy  
                

            img[y,x]= 0
     
    else:
        while y<y2:
            y=y+ys
            if p > 0:
                x=x+xs
                p=p+2*dx-2*dy
            else:
                p=p+2*dx  
            img[y,x]= 0
   
    return;

%matplotlib qt

DrawLine(100,150,200,100)

plt.imshow(img)
plt.show()
I'm running Python 3, Jupiter Notebook 6.0.3 on Windows 10.

P.S. The teachers said that Google is my friend and I should freely roam the internet to get all the answers I need. So after a week of searching and not finding anything comprehensible regarding custom function animations, I turned to this forum. Any help would be appreciated!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  animating 2d heat map schniefen 0 1,354 Nov-20-2022, 10:00 PM
Last Post: schniefen
  ROS ROBOTIC MOVEMENT PYTHON frkndmrsln 0 1,566 Apr-04-2022, 09:33 AM
Last Post: frkndmrsln
  Trouble with a turtle movement assignment. JosephSondgeroth 1 1,402 Jan-27-2022, 11:56 PM
Last Post: JosephSondgeroth

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020