tags:

views:

230

answers:

3

I am learning python because it looks very nice and i know a bit of ruby and perl.

The following is a C program to plot 3d points onto the screen. I used this as an example because it uses arrays, structures and function calls. I left out classes because i believe that sort of (OOP) thing is used simularly in all languages (correct me if im wrong). Understand that i do know how to write the following in python (with a bit of help from google), but i would like to know what sort of methods you would use and why.

Please dont feel the need to answer if you don't have the time or don't enjoy this kind of thing!

#include <stdlib.h>

int screenWidth = 10;
int screenHeight = 10;

char *buf;

inline void draw3dPoint(int *i3dPoint) {
    buf[
        (((i3dPoint[1] / i3dPoint[2]) + (screenHeight/2)) * screenWidth) +
        (i3dPoint[0] / i3dPoint[2]) + (screenWidth/2)] = 1;
}

main() {
    int x, y;
    buf = malloc(sizeof(char)*screenWidth*screenHeight);
    for(x = 0; x < screenWidth * screenHeight; x++)
        buf[x] =0;

    int *i3dPoint = malloc(sizeof(int)*3);

    i3dPoint[0] = 3; i3dPoint[1] = 0; i3dPoint[2] = 5;
    draw3dPoint(i3dPoint);

    i3dPoint[0] = 0; i3dPoint[1] = 4; i3dPoint[2] = 2;
    draw3dPoint(i3dPoint);

    i3dPoint[0] = -3; i3dPoint[1] = -4; i3dPoint[2] = 3;
    draw3dPoint(i3dPoint);

    for(y = 0; y < screenHeight; y++) {
        for(x = 0; x < screenWidth; x++)
            if(buf[(y*screenWidth)+x])
                printf("#");
            else
                printf(" ");
        printf(".\n");
    }

    free(buf);
    free(i3dPoint);

    return 0;
}
+1  A: 
  • If this is for plotting (scientific) data, i would use numpy and matplotlib, combined in the scipy package. More information can be find on the SciPy page, and its 3D plotting examples.

WHY: provides easy math and plotting functionality, similar to matlab.

WHY: since it doesn't require you to adopt an extra framework, plus you can reuse most knowledge of C-style OpenGL that you already have.

catchmeifyoutry
I think you missed the point. The question is aimed to learn python. The 3D example is just an excuse to do something that the original poster finds interesting.
Remo.D
@Remo, point taken, but learning what third-party packages are available, and learning when to recognize that there *must* be a suitable third-party package already out there that you should look for, and use, is a big part of "learning Python", so this is still a valid answer.
Peter Hansen
The questions are what _kind_ of methods, and _why_. I've given two kind of methods (as provided in the packages), with a short explanation. I have no idea what kind of answer is expected otherwise, but just giving a python translation of this "arbitrary" example code (we agree here) doesn't answer either question IMHO.
catchmeifyoutry
A: 

OK, I realise you're giving a custom example, but I feel that perhaps learning graphics concepts would help you easily translate from one language to another.

Quite simply, start with http://pyopengl.sourceforge.net/.

OpenGL is a standardised library of graphics functions. It's what is used in many games, rendering kits, phones and more. It's simple, it's powerful and it's been ported to almost every popular language out there.

If you're wanting to learn a graphics kit - OpenGL is a great place to start. It's also helpful that many examples can easily be ported between C and Python or Java or whatever else takes your fancy, and there are countless examples all over the net from people who love to use it and code in it.

One of the best tutorials is the NeHe tutorials - tens of tutorials from a simple box up to smoke engines and more in OpenGL. For every example, they include downloadable projects or code for the tutorial in tens of languages - including both C and Python, so this should be a great place for you to start your learning. It's where I learnt OpenGL, and can highly recommend it. It's not just the code, it's the theory and maths behind the graphics as well!

Mark Mayo
+2  A: 

Here's a direct port, only works in python3 due to the print thing combined with x-if-a-else-y

screenWidth = 10
screenHeight = 10

buf = [False] * screenWidth * screenHeight
def draw3dpoint(x,y,z):
    pt = int((((y / z) + (screenHeight/2)) * screenWidth) + (x / z) + (screenWidth/2))
    buf[pt] = True

draw3dpoint(3,0,5)
draw3dpoint(0,4,2)
draw3dpoint(-3,-4,3)

for y in range(screenHeight):
    for x in range(screenWidth):
        print(('#' if buf[y * screenWidth + x] else ' '), end='')
    print()

If you want the print to work in python below 3.0, do the following (note that the output is different due to weird division behavour, as pointed out in the first comment to this answer). Also thanks to the third comment I could refine this piece a little.

for y in range(screenHeight):
    for x in range(screenWidth):
        print (buf[y * screenWidth + x] and '#' or ' '),
    print ''
Tor Valamo
Ternary syntax works fine in Python 2.5 and later. But division of int by int will behave differently in 2.x, so it's not just the print() statements that would need to be fixed for 2.x :)
Jeffrey Harris
I couldn't use the ternary syntax with print if I wanted to override the newline, but I made a python 2.x version too. :)
Tor Valamo
in pre python 3.x you can use `(buf[y * screenWidth + x] and '#' or ' ')` for inline if-then-else.
catchmeifyoutry
Perhaps at the cost of being overly pedantic: post 2.5, `test and test_is_true or test_is_false` is very much discouraged. It doesn't do anything unexpected in this case (because '#' evaluates True), but ternary syntax is preferred because it's both more readable and less error prone.
Jeffrey Harris