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)
Related
I am an extreme novice to OpenGL, just trying to hack something together for a personal project. When I enabled GL_CULL_FACE I mostly got what I wanted, except a big triangle chunk is now missing from my cube!
What might be happening here and how can I fix it? I made this cube with 6 GL_QUADS, so I never expected to be missing a triangle like this...
Each polygon has a "front" and "back" side, and when culling is on you only see polygons whose "front" is toward you (the ones for which the normal is pointing toward the camera).
The fact that this face is getting culled from this angle suggests that its normal points inside the cube instead of outside; to flip it around, reverse the order in which you specify the vertices.
I have seen some ways of making colored text in textview in Python pygtk. the issue seems that it will just print text in that colour or make the whole line that color rather than for certain items make them a certain colour.
I want it to where I type "" that is will colour is blue. or if there is "string" in the text view it will be orange or any kind of
and if there is an '#comment' then it will be italicized and grey.
not sure if it helps, but I have a part where as I am typing it writes the text to a page. is it possible to to keep this kind of syntax coloring active?
I hope this makes sense.
any help is much appreciated! Thank you!
Use GtkSourceView for syntax highlighting. Don't reinvent the wheel.
In general, what you are looking for, I'd say, is to use regular expressions (re module, there are abundant of questions on this here...probably some for the exact patterns you need) to find the patterns you mention above in your TextBuffer. That means you need to connect a signal to the buffer so you see what the user types. Then you'll need a set of TextTags (one tag per formatting rule/pattern) to apply to regions of the buffer where the regular expressions match the patterns you've described. Finally you want to apply the tags to the buffer and those TextTags can reformat the text-display in the TextView in an array of ways (as the documentation says here).
Without any supplied code, it's hard to be precise on where you might be having a problem.
Hope it points you in the right direction...
Mind though that if you overwrite the GTK-theme, that another user could have a theme with e.g. orange background in the TextViews, so you should be careful with making sure that it will work visually independent of what theme you have.
I'm looking to create a survey program in Python that outputs the results of a series of questions into a few broad categories. As an example, I'm looking for something specifically along the lines of:
The idea is that the survey will give you a score between 3 and -3 and you lean towards one category or the other, depending on whether you scored positively or negatively.
Now, my idea is to manually use PIL to:
Iterate through appropriate category names and draw them
Draw the associated lines
Draw the rectangles according to score.
Also, it's worth mentioning that I'm trying to keep this as flexible as possible; we might add or remove categories at a later stage and I'd like to keep it so it would take minimal programming effort to do (as I likely will not be the one maintaining this in the future).
I suppose what my question would be is... does anyone know of any packages that might do this nicely? Or perhaps have any other suggestions or ideas? I didn't see anything suitable off of matplotlib. Admittedly, however, my expertise in graphing with Python is not extensive by any means!
Thanks for any ideas you might have!
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.
Hey everyone. I'm really struggling to figure out the logic with this one and was hoping you could help me out. Before I continue I just want to let you know that I am amateur programmer and a beginner at that, with no formal Computer Science training of any sort, so please bear with me. :D Also, I'm using Python, but I could use Java or something similar.
Anywho, I am looking to implement a Region Growing for use in a rudimentary Drawbot.
Here is an article on region growing: http://en.wikipedia.org/wiki/Region_growing
The way I envision it, the image the draw is based upon will meet the following criteria:
The image will be at most 3x3 inches in size at an arbitrary Color Depth
The image will be a black continuous shape on a white background
The shape can be located anywhere on the background.
I've considered the following solutions to this problem. While some work to an extent, each has some considerable flaws in either their performance or feasibility (at least they don't seem feasible to me). Furthermore, because this is a Drawbot, this needs to be done with a single continuous line. This doesn't mean however that I can't backtrack, it only eliminates the possibility of multiple starting points (seeds).
Considered Approaches:
Random Walk:
Solving this problem with a random walk was my first instinct. A random walk program accomplishing this would, I imagine, look something like this:
pseudo python...
Cells To Visit = Number of Black Cells
Cells Visited = 0
MarkColor = red
While Cells Visited < Cells To Visit:
if currentcell is black:
Mark Current Cell As Visited #change pixel to red
Cells Visited +=1
neighbors = Get_Adjacent_Cells() #returns cells either black or red
next cell = random.choose(neighbors)
currentCell = next cell
While I suppose this is feasible, it seems to me to be highly ineffective and doesn't guarantee good results, but in the interest of actually getting something done I may end up trying this... Is my logic in the pseudocode even vaguely correct?
Sweeping Pattern:
This method to me seemed to be the most trivial to implement. My idea here is that I could choose a starting point at one extreme of the shape (e.g. the lowest most left point). From there it would draw to the right, moving only on the x axis until it hit a white pixel. From here it would move up one pixel on the y axis, and then move left on the x axis until it reached a white pixel. If the pixel directly above it happend to be white, backtrack on the x axis until it finds a black pixel above it.
This method upon further inspection has some major short comings.
When faced with a shape such as this:
The result will look like this:
And even if I were to tell it to start sweeping down after awhile, the middle leg would still be overlooked.
4/8 Connected Neighborhood:
http://en.wikipedia.org/wiki/8-connected_neighborhood
This method appears to me to be the most powerful and effective, but at this point I can't figure it out fully, nor can I think of how I would implement it without potentially leaving some overlooked areas
At every cell I would look at the neighboring black cells, devise some method for ranking which one I should visit first, visit all of them, and repeat the process until all cells are covered.
The problems I could see here is first of all dealing with the data structure necessary to accomplish this, and also merely figuring out the logic behind it.
Those are the best solutions I've been able to think of. Thank you for taking the time to read this, I realize it is long, but I thought that I should make it as explicit as possible. Any and all suggestions will be greatly appreciated... Thanks!
Edit:
I also looked into maze generating and solving algorithms, but wasn't sure how to implement that here. My understanding of the maze solving algorithms is that they rely on the passages of the maze to be of equal width. I could of course be wrong about that.
Basic region growing, in pseudocode looks something like:
seed_point // starting point
visited // boolean array/matrix, same size as image
point_queue // empty queue
point_queue.enqueue( seed_point )
visited( seed_point ) = true
while( point_queue is not empty ) {
this_point = point_queue.dequeue()
for each neighbour of this_point {
if not visited( neighbour ) and neighbour is black/red/whatever
point_queue.enqueue( neighbour )
visited( neighbour ) = true
}
}
// we are done. the "visited" matrix tells
// us which pixels are in the region
I don't understand where the ranking that you've mentioned comes into it though. Am I missing something?
I'm confused by the very long question.
Are you sure you aren't just trying to do a flood fill?
Here's a really nice little screencast on writing a recursive maze solver: http://thinkcode.tv/catalog/amazing-python/
I think it might give you some ideas for the problem you are trying to solve.
Also, here's a little recursive maze solving script that I wrote after watching the screencast http://pastie.org/1854582. Equal width passages are not necessary, the only things that are necessary are open space, walls, and some kind of an ending condition, in this case, finding the end of the maze.
If you don't want to go recursive, the other thing you can do is use a "backtracking" method. You can see a little example of it being used in the random generation of mazes on this page:
http://weblog.jamisbuck.org/2011/2/7/maze-generation-algorithm-recap (First example on the page).
Is this sounding relevant? If it is, let me know if you want me to explain anything in more detail.
Edit:
This seems like a really good discussion on doing flood fills in python http://www.daniweb.com/software-development/python/threads/148874
A simple technique that can help with some maze solving problems, of keeping one hand on the wall, might help.
Note however that if you chose a random starting point, you might chose a point that whichever way you travel from there, you block off a portion. i.e. if you were to start in the middle of an hour-glass shape, you would only be able to fill in one half.