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;
}
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 ''
- 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.
- If you're using OpenGL, you can probably port your code quite literally with the python OpenGL bindings.
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.
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!
精彩评论