What would you use to create an interactive geometry program with python? - python

What libraries/modules would you recommend for creating an interactive geometry program?
What I have found includes: Pyglet, Pygame, Pycairo, Sympy
I'll illustrate the basic requirements with an example:
Create two Point objects by clicking two locations on a canvas.
Create two intersecting Circle objects based on those two Points.
Detect the two circles and calculate their intersections.
I would like to work with a canvas that supports anti-aliasing.
I would also like the ability to produce(export) an image with a transparent background.

If you have a bit of experience with OpenGL I would seriously recommend pyglet. Very customizable. If you don't, get it, because in Python there doesn't really exist a game/rendering library that has a "canvas-style" interface, except, to a certain point, pygame. But pygame...
Stay away from pygame. Stay away. pygame has horrendous design, documentation, code written with it and performance.
I have no experience with the two others (or any others, for that fact). There aren't many options for this domain in Python sadly (this also is the reason why I started a project like this, but it's still too unstable and WIP). Most notably the built-in support for things like anti-aliasing, primitives and intersection calculating is non-existent.

I have created a library for interactive geometry board using tkinter.
http://bitbucket.org/zambu/pygraph

Related

Simple animation in Python using wxPython objects

I'm writing a gravity simulator in Python 2.7 and at the moment I finished the mathematical part. I'm trying to find a way to display the results while the simulation is running, it should consist of some colored circles representing the various bodies and optionally some curved lines to represent orbits, that can be shown or hidden while the simulation is running.
I pictured a way to obtain this result, but I can't seem to find a way to even start.
My idea is to use wxPython. The window should be divided into four sectors (2x2), the first three contain the simulation viewed in the XY, XZ and YZ planes, while the last contains the controls (start/stop simulation, show/hide orbits, ...).
The controls should not be a problem, I just need a way to display the animation. So how can I display moving circles and curved lines using wxPython objects? Which objects should I use? I don't need much more than a couple names, the rest should follow easily.
I know that an animation purely with wxPython will probably require some multithreading, I'm already prepared for that. I also want to stress that I need the animation to be shown while the simulation is running, not after, because the simulation has no definite end at the moment: I don't know when to stop it if I don't see the results first.
If it's somehow useful, I'm using Ubuntu Linux 17.10.
Edit: Since I was asked to choose one approach, I discarded Matplotlib because it requires two different windows. Hope this helps.

Simple animations in Python 2.7 [duplicate]

This question already has answers here:
simple animation using tkinter
(2 answers)
Closed 6 years ago.
In Python 2.7, I want to display some simple graphics, such as a red square moving around on a blue window. On every frame, I want to update the position of the square, and render the new window. I am looking for something simple and lightweight.
I have seen people use matplotlib for drawing shapes, but I don't want to have to deal with axes and data points. I have also seen pygame suggested, but this seems to heavyweight for what I want, as I do not want to create a game, just a simple animation.
So what I really want is something where on every frame, I just indicate the colour of every pixel on an image, and then display that image. What are some good suggestions?
Tkinter is not good for setting individual pixels. If you want to move rectangles or ovals though (a small oval will look like a pixel, but it doesn't scale for updating a whole image).
def update(x,y):
canvas.delete('all')
canvas.create_rectangle(x-1,y-1,x+1,y+1)
You can, of course, be more judicious, saving the return value of the rectangle and then only clear the appropriate elements. Or you can move existing elements directly as Bryan points out. As he explains elsewhere Tkinter of course, supports drawing images, ovals, and a slew of other things. Here's a canonical source edit: that is old and not canoncial This one's slightly better For a general source on animating with a timer loop, here's Bryan agian
Bryan also noted that you can work with pixels directly You can do that with PhotoImage.
Array-Like Pixel Access Without Graphical Extensions
A robust module like pygame will be the most scalable option. However, I've had success (in educational settings only) writing graphics engines by modifying the elements of a numpy array and then displaying it as an image (you also need this link to display the images).
This lets you do pixel level modifications; since it's relatively trivial to write C-extensions that modify numpy arrays, you can prototype fast image processing doing custom manipulations. While I've written whole graphics engines using just tkinter this way, again I can only reccomend it for educational purposes.
Otherwise, just bite the bullet and pull in openGl or pygame. You'll save yourself a ton of time in the long run.
Summary
Very simple animations can be done right in tkinter
For educational purposes, you can do arbitrary graphics with numpy and tkinter
For rhobust animations, check out a full library (openGl, matplotlib, pygame) that suits your needs (graphical rendering, statistical graphing, game development, etc.)

Pygame image collision

I have a pygame program where there's a face in the center. What I want the program to do is have a bunch of objects on the screen, all irregular. Some would be circles, others would be cut-out pictures of objects like surf boards, chairs, bananas, etc. The user would be able to drag the objects around, and they'd collide with each other and the face in the center, and so be unable to pass through them. Could anyone show me how I would do this? Thanks!
-EDIT- And by not be able to pass through, I mean they'd move along the edge of the object, trying to follow the mouse.
What you are looking for is functionality usually provided by a so-called physics engine. For very basic shapes, it is simple enough to code the basic functionality yourself. (The simplest case for 2D shapes is the collision detection between circles).
Collision detection gets pretty hard pretty quickly, especially if you want to do it at a reasonably fast rate (such as you would need for the sort of project you are describing) and also especially if you are dealing with arbitrary, non-regular shapes (which your description seems to indicate). So, unless you are interested in learning how to code an optimized collision detection system, I suggest you google for python physics engines. I have never used any, so I can't personally recommend one.
Good luck!

PYGAME - Edit colours of an image (makes white red at 255,0,0) without numerical python?

I'm making a simple game, whereby I want my characters quite customizable. I want my characters to be able to be fully edited in colours, for example, if a player wants their character to have cyan skin, they just put into the sliders, or what I choose to use, "0,255,255", or purple "255,0,255", or something random "25,125, 156", and have their character be that colour. I haven't even started creating the game, but I've got the basis down and I know exactly what I must do for pretty much every EXCEPT this.
I done a search in Google, and it turns out, I need numerical python for this? Well this is a whole new package, and in order for the average player to play, I must change it to EXE form... (or have python, pygame and numerical python installed onto their PC, which will be a problem if they have a later version...). Now, it's already getting complex with just pygame, but with numerical python as well, is there even a tutorial on how to do this?
Any suggestions? Thanks!
Of course you can use brute force method to do this. Here is function to replace color with another in pygame surface.
def color_replace(surface, find_color, replace_color):
for x in range(surface.get_size()[0]):
for y in range(surface.get_size()[1]):
if surface.get_at([x, y]) == find_color:
surface.set_at([x, y], replace_color)
return surface
I assume by image you mean pygame.Surface. You have several options:
pygame.Surface.set_at(...)
Use a palettized surface. Then change the palette. Based on your use case, this is actually probably what I'd suggest.
Use the pygame.PixelArray PyGame module. I don't think it requires NumPy.
Just use NumPy. It's really not that huge of a requirement; lots of projects require it and it's simple to set up. You get access to the powerful pygame.surfarray module

Python: Draw a 2d grid and allow coloring of cells

I want to simulate the game of life problem using python. I want to draw a grid and be able to color its cells as the simulation progresses. How do I do that in Python?
You can use pygame to do that.
To display the state of your simulation, you should create an 8 bit surface with a palette, and access it with the pygame.surfarray module.
Take a look at PyGame too.
I suggest Gloss, it's like PyGame except hardware accelerated. It also provides many other game-programming goodies.
I suggest taking a look at the Python Imaging Library (PIL) documentation
I know this is an old post, but in the name of building the perfect QA site, I would suggest matplotlib.
As of this writing, it looks like matplotlib development is getting more attention than pygame development.

Categories

Resources