My question is simple: What is the difference between using pygame.draw and pygame.gfxdraw? I've looked on the pygame documentation, but no one has told what the difference between them is.
The draw function is a bit more stable, and also a bit faster that gfxdraw, although gfxdraw has more options, not only along the lines of antialiasing, but also drawing shapes. pygame.draw has only nine functions, whereas pygame.gfxdraw has 22 options, which include all 9 of the options supplied by draw. I recommend using gfxdraw even though its is "experimental" because it has more capabilities.
gfxdraw allows anti-aliasing for all shapes, where drawonly adds antialiasing to lines. I use gfxdraw by default. It has been marked 'experimental' for a long time now, but I've not had any issues.
Disclaimer: I Don't use pygame!
According to the gfxdraw doc page, it "Wraps SDL_gfx primatives", and so offers it as an API for those already familiar with it. It also appears to offers a little bit more than the more primitive normal draw. There also appears to be a few semantic differences here and there, so you'll need to compare each to its closest counterpart, and not assume anything.
As a final note, gfxdraw is experimental, and you should always take that into consideration before using it.
I hope this helps! :)
Related
I am currently designing basic pygame projects. Random projects actually, which are just random ideas pop into my head during common daytime. One of them is a wall clock. Where I use sin, cos functions to draw hour hand, minute hand etc..
As a result of these functions, I obtain float values. Which pygame does not allow. So I round and int() all values before using them in drawing functions. This makes sense because of pixels can't be partially filled. However, I think this results in bad drawings.
The Image above is the clock drawn by the pygame module. As you see the handles are somewhat leaning. Not straight.
I also implemented this in codeskulptor. Which is a python2 environment for basic game(or any graphical) programming. Has nice and clear functions, very easy to use indeed. The float values I use in that no needs conversion and accepted directly. I do not know how it handles it, but it is clearly better. Obviously not just rounding and integering(does this word exist ?) values. Let me show you the clock drawn in codeskuptor:
As you can see, the edges are more smooth. The lines does not end sloped, but straight. It is clearly a better drawing than the pygame one.
But the thing is, codeskuptor does not implement many modules and built-in functions that is harder than beginning level knowledge. Also it doesn't support a well-known compiler(basically not a python module). So can't work on computer and can not be compiled with py2exe, pyinstaller as such.
So I wan't to implement all this in pygame, and get smooth results just like in codeskulptor. Maybe a better way to handle float points for drawing. Any idea or knowledge in this area would be greatly appreciated.
My code for codeskuptor(Does not show realtime)
My code for pygame
The codes does not supply proper commenting because they were just a late-night fun nothing more. I'll explain any necessary parts.
To summarize the comments above, the issue you've noticed here is antialiasing. For the lines, pygame provides the aaline function, which can be used to draw a single line. Unfortunately, this does not support a varying thickness. Potential workarounds could be drawing a line of the correct thickness and drawing an aaline on either side of it, or drawing the line using an aapolygon (from the gfxdraw module).
The standard draw module does not include antialiased circles, but you can use another function from gfxdraw to draw an antialiased circle, aacircle.
It is important to note that the gfxdraw module is labeled "experimental", so the functions are not guaranteed to persist across versions of pygame. But for your quick projects that might not be a concern.
As for CodeSkulptor, they are rendering the lines on an HTML5 <canvas> element, which according to this question has anti-aliasing turned on by default.
I understand that this is a very simple question, but how do you draw a triangle using wxpython? A simple example would be much appreciated.
I tried using dc.DrawPolygon(self, points, xoffset, yoffset, fillStyle), but I didn't know what to pass in for fillstyle. All my efforts resulted in odd error messages.
Sorry for such a novice question!
As the docs say:
The last argument specifies the fill rule: wx.ODDEVEN_RULE (the default) or wx.WINDING_RULE.
So, those are the only two values you can pass for fillstyle. If you pass anything else, you'll probably get an exception.
But notice that it has a perfectly good default. If you're drawing complicated concave polygons sometimes you need the other rule. (If that comes up, you will have to learn what the two rules mean; I don't think the wx docs cover it, but Wikipedia might be a good place to start.) But most of the time, you can just leave it off and use the default:
my_dc.DrawPolygon(my_points, my_xoffset, my_yoffset)
I'm interested in using python to make diagrams representing the size of values based on the size of squares (and optionally their colour). Basically I'm looking for a way to make overviews of a bunch of values like the good old program windirstat does with hard-drive usage (it basically makes a big square representing your harddrive and then smaller squares making up the area inside of it representing different programs, the bigger the square the larger the file, colour indicates the type of file). I'm fairly familiar with matplotlib, and I don't think it's possible to do something like this with it. Is there any other python package that would help? Any suggestions for something more low level if it's not? I guess I could do it manually if I could find a way to draw the boxes programatically (I don't really care about the format, but the option to export SVG as well as PNG would be nice).
Ultimately, it would be nice to have it be interactive like windirstat is, where if you were to hover over a particular square you get more information on it, and if you clicked on it maybe you'd go in and see the makeup of that particular square. I'm only familiar with wxpython for GUI stuff, not sure if it could be used for something like this. For now I'd be happy with just outputting them though.
Thanks a lot!
Alex
Edit:
Thanks guys, both your answers helped a lot.
You're looking for Treemapping algorithms. Once implemented, you can transform the output (which should be rectangles) into plotting commands to anything that can draw layered rectangles.
Edit:
More links and information:
If you don't mind reading papers, the browser-based d3 library provides for 'squarified' treemaps (js implementation). They reference this paper by Bruls, Huizing, and van Wijk. (This is also citation 3 on the wikipedia article)
I'd search on the algorithms listed on the linked Wikipedia article. For instance, they also link to this article, which describes an algorithm for "mixed treemaps". The paper also includes some interesting portions at the end describing transformations into other-than-rectangular shapes.
Squarified certainly appears to be the most common variety around. The above links should give you enough to work towards a solution or, even, directly port the d3 implementation. However, the cost of grokking d3's model (which is something like a declarative form of jQuery) may be somewhat high. At first glance, though, the implementation appears relatively straightforward.
Squaremap does this. I haven't used it (I only know it from RunSnakeRun) and its documentation is severely lacking, but it seems to work.
On a Google Search, I found this article:
http://docs.python.org/release/1.4/lib/node201.html
Which showed examples of using the rect class, to perform union/intersections/checking if points are inside rect. Importing rect fails in Python 2.7. Is this class in another package?
I assume the question isn't really "what happened to it?", but "where can I find a class like this that I can use?".
Most GUI libraries have a class like this. For example: wx.Rect, QRect/QRectF, gtk.gdk.Rectangle, PyGame rect.
If you want a generic rectangle class without the overhead of a GUI library, I think your only option is Sympy, which has a Geometry module that may suit your needs.
Finally, you may just choose to roll your own. It's not very hard to make such a class.
It's part of the Standard Windowing interface. To quote from the documentation you yourself linked to:
Using STDWIN is not recommended for
new applications. It has never been
ported to Microsoft Windows or Windows
NT, and for X11 or the Macintosh it
lacks important functionality -- in
particular, it has no tools for the
construction of dialogs.
The stdwin module containing rect class was obsolete since version 1.6 (see these notes) and I believe it was removed in some later version.
I think the rest of the answers have covered you. However I was in your shoes, and made a generic module to benefit more people. You can find the project here. The module works with both negative and positive numbers on a screen coordinate system (y grows downwards of screen, x grows to the right).
Some methods supported:
Distance from one rectangle to an other
See if rectangles overlap on x, y or both axis (default)
See if a point is inside a rectangle
Align rectangles
I want to transform photos in python to look like this:
taken from doctype.com
I will use it in django, PIL is installed.
How can I achieve this?
This is a combination of several subtle effects. It starts by a nonlinear deformation, and then a tasteful drop shadow is added. There's also a small border. I'd start by drawing one straight, vertical line on the above picture, and then seeing how you would transform to that from the original picture. Then, apply that transformation to the whole photo, add some drop shadow, and figure out what else is left...
I've had a bit better luck with ImageMagick when it comes to more complex transformations. You would have to install it separately and though there is a python library it doesn't appear well documented so I've just used command line calls.
Here is a good tutorial on warping image:
And another tutorial on general use
The drop shadow is probably just a second image which is composited with the first image.