Simplifying a polygon

February 10th, 2008 by exhuma.twn

As you may know, polygons are sensitive to the ordering in which vertices are put in. What is more, the normals of a polygon face depend on the direction of the face.

This usually is not a problem when the polygon is constructed by a graphics-savvy person. If, however (as in my case), the polygons are edited in a user frontend, and if the target audience for that UI may not be aware of the problem, you might run into trouble.

I strongly believe, that details like this should be hidden from the less technically-inclined people, so they can get thei job done without swearing at the UI. And therefore be more productive. This piece of javascript allows you to “simplify”an array of 2D-Points by sorting it accordingly. The resulting array will construct a simple polygon with always outward-facing normals.

This uses the well-known paradigm of taking a reference point and then “swiping” counter-clockwise over the available points. Depending on the reference point (and the available vertices), the end-results may vary. So it may not really result in what the user expected. Having an undo ready at hand should prove useful. However, this algorithm assumes the most likely case, in which the reference point is set to the centroid of the polygon.

The entry-point function is this algorithm is “simplify_polygon”. Feel free to play around with it. Documentation is sparse, but if you know your trig you should figure it out by yourself. It’s fairly straightforward.

Note: The 4-way branch in the “angle” function could be simplified. It is as it is because my mind was locked in the 4 quadrants when writing it. Didn’t feel like changing it yet ;)

Read the rest of this entry »

Posted in Coding Voodoo | 1 Comment »

Vim script (mapping) to generate python getters and setters

December 31st, 2007 by exhuma.twn

Somethin that I need quite often is to create custom accessors and mutators for class-attributes. For example convert this:

class MyClass(object):
   
   def __init__(self):
      self.has_changes = False
      self.some_attribute = False

into this:

class MyClass(object):
   
   def __init__(self):
      self.__has_changes = False
      self.__some_attribute = False

   def get_some_attribute(self):
      "Accessor: some_attribute"
      return self.__some_attribute

   def set_some_attribute(self, input):
      "Mutator: some_attribute"
      self.__some_attribute = input
      self.__has_changes = True

   some_attribute = property(get_some_attribute, set_some_attribute)

   def get_has_changes(self):
      "Accessor: has_changes"
      return self.__has_changes

   has_changes = property(get_has_changes)

This particular example allows an easy tracking if a class contains changes. Without the need of calling myclass.get_some_attribute() or myclass.set_some_attribute(foo). You can simply do myclass.some_attribute = foo and the has_changes attribut will change accordingly.

If your class has many attributes, writing custom accessors and mutators can be tedious. So here’s a small Vim-mapping that get’s you started. Sure, you may still need to fine-tune some generated code, but the bulk is there.

<font color="#808bed">nmap</font> <font color="#c080d0">&lt;</font><font color="#c080d0">F6</font><font color="#c080d0">&gt;</font> yyP<font color="#c080d0">&lt;</font><font color="#c080d0">home</font><font color="#c080d0">&gt;</font>widef get_<font color="#c080d0">&lt;</font><font color="#c080d0">end</font><font color="#c080d0">&gt;</font>(self):<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>yyP&gt;&gt;I&quot;Accessor: <font color="#c080d0">&lt;</font><font color="#c080d0">end</font><font color="#c080d0">&gt;</font>&quot;<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;</font>yyP&gt;&gt;Ireturn self.__<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>o<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;</font>yyPIdef set_<font color="#c080d0">&lt;</font><font color="#c080d0">end</font><font color="#c080d0">&gt;</font>(self, input):<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;</font>yyP&gt;&gt;I&quot;Mutator: <font color="#c080d0">&lt;</font><font color="#c080d0">end</font><font color="#c080d0">&gt;</font>&quot;<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;</font>yyP&gt;&gt;Iself.__<font color="#c080d0">&lt;</font><font color="#c080d0">end</font><font color="#c080d0">&gt;</font> = input<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>o<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">home</font><font color="#c080d0">&gt;</font>wveyA = property(get_<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>pA, set_<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>pA)<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>o<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>

Put this into your vimrc, or (like I do) into the ~/.vim/ftplugin/python.vim file so it get’s only loaded for python files. Then you only need to write the attribute name of the class, put your cursor on that line, be sure to be in normal mode (hit a few time <esc>) ;) and hit F6

If you want to change the shortcut, simply change the first parameter to this mapping line.

Posted in Python | No Comments »

Schneiderman’s Golden Rules

October 19th, 2007 by exhuma.twn

NEVER forget these simple rules. Every computer application with a user interface will benefit from them.

It’s really worth it. People will love you for it. Trust me ;)

I personally find that using an ordered list is somewhat misleading. Every point should be considered with equal importance. That’s why I will change to “ol” tag to an “ul” tag now ;)

  • Strive for consistency. As we shall see below, it is important for a user interface to be consistent on many levels. For example, screen layouts should be consistent from one screen to another. In an environment using a graphical user interface (GUI), this also implies consistency from one application to another.
  • Enable frequent users to use shortcuts. Frequent users (or, power users) may be turned off by overly tedious procedures. Allow those users a less tedious procedure for accomplishing a given task.
  • Offer informative feedback. Users need to see the consequences of their actions. If a user enters a command but the computer does not show that it is either processing or has processed that command, this can leave the user confused and disoriented.
  • Design dialogues to yield closure. Interacting with a computer is somewhat like a dialogue or conversation. Every task should have a beginning, a middle and an end. It is important for the user to know when a task is at its end. The user needs to have the feeling that a task has reached closure.
  • Offer simple error handling. User errors should be designed into the system. Another way of stating this is that no user action should be considered an error that is beyond the ability of the system to manage. If the user makes a mistake, the user should receive useful, concise and clear information about the nature of the mistake. It should be easy for the user to undo his or her mistake.
  • Permit easy reversal of actions. More generally, users must be permitted to undo what they have done, whether it is in the nature of an error or not.
  • Support internal locus of control. User satisfaction is high when the user feels that he or she is in control and user satisfaction is low when the user feels that the computer is in control. Design interfaces to reinforce the feeling that the user is the focus of control in the human-computer interaction.
  • Reduce short-term memory load. Human short-term memory is remarkably limited. Psychologists often quote Miller’s law to the effect that short-term memory is limited to seven discrete pieces of information. Do everything possible to free the user’s memory burden. For example, instead of asking the user to type in the name of a file which is going to be retrieved, present the user with a list of files currently available.

See page 74-75 Scheiderman: Designing the User Interface, 3rd

Posted in Coding Voodoo | No Comments »

Why vim?

October 18th, 2007 by exhuma.twn
  • Misconception #1: modal editing
  • Misconception #2: it’s not all about regular expressions
  • Misconception #3: you gotta be nuts and/or a genius to use it
  • Misconception #4: hjkl to move around?
  • Misconception #5: since you are thinking 90% of the time, and editing 10%, the productivity gain might be there, but it’s useless anyway
  • Misconception #6: it’s just sticking to a disappearing past
  • Correct-conception #1: steep learning curve

Tickled your interest? Looky there!

Posted in Coding Voodoo | No Comments »

Calculate the distance between two GPS-Coordinates (ctd.)

September 17th, 2007 by exhuma.twn

And here’s the same thing as plpgsql function for Postgres:

CREATE OR REPLACE FUNCTION lldistance( a point, b point)
RETURNS FLOAT AS $$
DECLARE    x FLOAT;
        dlat FLOAT;
        dlon FLOAT;
BEGIN
   dlat = radians(a[0]-b[0]);
   dlon = radians(a[1]-b[1]);
   x := sin(dlat/2)^2 +
        cos(radians(a[0])) * cos(radians(b[0])) *
        sin(dlon/2)^2;
   RETURN 6367442.5 * (2*atan2(SQRT(x), SQRT(1-x)));
END;
$$ LANGUAGE plpgsql

This results in more accurate distances than simply using

SELECT point1 <-> point2 FROM sometable;

You could also use plpython as function language and copy/paste the earlier post, but plpgsql is more portable.

Posted in Coding Voodoo | 1 Comment »

Calculate the distance between two GPS-Coordinates

September 17th, 2007 by exhuma.twn

This function uses the Haversine formula to calculate the distance which takes into account the spherical nature of the earth.
As the earth is not a perfect sphere, this function approximates this by using the average radius.

from math import sin, cos, radians, sqrt, asin

def lldistance(a, b):
   """
   Calculates the distance between two GPS points (decimal)
   @param a: 2-tuple of point A
   @param b: 2-tuple of point B
   @return: distance in m
   """

   r = 6367442.5             # average earth radius in m
   dLat = radians(a[0]-b[0])
   dLon = radians(a[1]-b[1])
   x = sin(dLat/2) ** 2 + \
       cos(radians(a[0])) * cos(radians(b[0])) *\
       sin(dLon/2) ** 2
   y = 2 * asin(sqrt(x))
   d = r * y

   return d

Posted in Python | 2 Comments »

Centering a window on the screen with Qt

September 10th, 2007 by exhuma.twn

Yet another thing that’s not automagic in Qt.
Here’s a python solution:

if __name__ == "__main__":

   app = QtGui.QApplication(sys.argv)

   dw = app.desktop().width()
   dh = app.desktop().height()

   myapp = MainWindow()
   myapp.setGeometry(
         int((dw - (dw - (dw / 2)) * 1.5) / 2),
         int((dh - (dh - (dh / 2)) * 1.5) / 2),
         int((dw - (dw / 2)) * 1.5),
         int((dh - (dh / 2)) * 1.5))
   myapp.show()
   sys.exit(app.exec_())

This will also resize the window, adapting to the desktop size.
Found and adapted from the Qt interest archive.

Posted in Python | No Comments »

Adding Scrollbars to a QFrame in Qt

September 10th, 2007 by exhuma.twn

Many things are very easy to accomplish in Qt. Qt’s designer is a great help with that. But the is no option to simply “enable” scrollbars for a frame. Also, the QScrollArea can nowhere be found in the designer. The solution is actually quite simple:

  1. Add the component you want to be scrollable (QFrame, QLabel, …) in the designer as usual
  2. In the application code:
    1. Create a new QScrollArea
    2. Set the parent of the widget you created in the designer to “None”
    3. Set the widget of the QScrollArea to the widget from step 1
    4. Insert the QScrollArea to you ui

And here’s some example code (boiled down to the essentials):

sa = QtGui.QScrollArea()
self.ui.myWidget.setParent(None)
sa.setWidget(self.ui.myWidget)
self.ui.vboxlayout1.insertWidget(0, sa)

You would probably write this somewhere in you application-constructor right after you call setupUi.

This will insert the scroll area at the top of vboxlayout1 (see the doc for insertWidget). You can of course also use addWidget.

It’s a shame that there’s no direct support for QScrollArea in designer. Maybe we will see it in some future release.

An example for C++ can be found in the Qt Forums.

Posted in Python | 1 Comment »

VIM rc-file

August 10th, 2007 by exhuma.twn

Due to inane network restrictions I am maintaining several copies of my vimrc file. I would syncronize it though other means. Alas, the restrictions not only cover the network. So I have to resort to simply posting it here.

This is not meant to be an explanation of vim configuration file. However, it contains lots of comments, and might prove a good primer/starting-point. Eventually I will also include other modifications I made in my vim-home-folder as they are either used in the vimrc or just plain useful ;)

Read the rest of this entry »

Posted in Babble, Coding Voodoo | 2 Comments »

Ohloh Open Source Statistics

July 26th, 2007 by exhuma.twn

ohlo language statsOholoh is a site that keeps code statistics of open-source projetcs. It’s very interesting to browse though the different projects and look at their metrics.

It can also be a nice incentive to get people do commit on a regular basis, as it adds a sort of healthy competition to the development cycle.

Anyhow…. Great idea, and it already keeps track of quite a number of projects.

Posted in Coding Voodoo | No Comments »

« Previous Entries Next Entries »

Pages

Recent Posts

Categories

Links


Archives

Meta