Re: [PyOpenGL-Users] Converting C++ code to python code
Brought to you by:
mcfletch
From: Derakon <de...@gm...> - 2011-04-17 19:24:34
|
On Sun, Apr 17, 2011 at 11:44 AM, Abhijeet Rastogi <abh...@gm...> wrote: > > Original author has implemented figureSet as vector, I implemented it as > python list while converting the code. In the below code snippet, "in the > if-else part", I am not able to make out, how will I convert the code in > python? > > void drawSel() { > glColor3f(0, 0, 0); > int size = figureSet.size(); > Figure *f = figureSet[selected]; > > /* getPoint() is a virtual method: every figure has at least > two points (is at least a line) */ > int *pt1 = f->getPoint(1)->getCoords(); > int *pt2 = f->getPoint(2)->getCoords(); > > if (Triangle *t = dynamic_cast<Triangle*>(f)) { // triangle: one more > point > int *pt3 = t->getPoint(3)->getCoords(); > //something happens here > } > else if (Quad *q = dynamic_cast<Quad*>(f)) { // quad: two more point > int *pt3 = q->getPoint(3)->getCoords(); > int *pt4 = q->getPoint(4)->getCoords(); > //Something happens here > } > } In this code we have a guarantee that there are at least 2 points to draw, but if the figure is a triangle, then there's only one more, while if it's a quad then there's two more. These need different drawing logic, hence the if/else split. The conditionals are basically saying "How many points are in this polygon?" and then drawing differently depending on the result. In other words, dynamic_cast is being used to check if the figure variable is representing a Triangle or a Quad. The direct way to do this in Python would be something along the lines of if type(figure) == Triangle: draw as if it's a triangle elif type(figure) == Quad: draw as if it's a quad Assuming you have Triangle and Quad classes declared, of course. The more elegant way would be to have Triangle and Quad have their own draw functions; then you just do figure.draw() and the appropriate function is automatically invoked. -Chris |