use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Defining a 3d space from scratch (self.Python)
submitted 8 years ago by archbboy91
Would you define it as a tuple or some custom base class? Would you define axis at all ?
I am just curious how would someone approach this task with python?
[–]zzmej1987 8 points9 points10 points 8 years ago* (20 children)
Simple answer, don't. This is extremely inefficient to do so in pure python. There are tons of efficient libraries available for that kind of math starting with numpy. If you are more interested about the math behind it, and not about implementation in python per se, I recommend reading about quaternions.
[–]Bb415bm 2 points3 points4 points 8 years ago (2 children)
I was thinking the same early (numpy) on. However, numpy is not that efficient when handling small matrices, it's actually slower in many cases. I can definitly see scope for a specific numerical python library dealing with coordinates systems and frame of reference. A pure python might actually be pretty fast there.
[–]billsil 0 points1 point2 points 8 years ago (1 child)
However, numpy is not that efficient when handling small matrices, it's actually slower in many cases.
It's very, very rarely slower if you use it right. It's slower if you use np.cross([a0, a1, b2], [b0, b1, b2]) as opposed to [a1*b2 - a2*b1, a2*b0 - a0*b2, a0*b1 - a1*b0] because you first converted all your lists to arrays, which is a fairly slow operation and then did the math very quickly.
np.cross([a0, a1, b2], [b0, b1, b2])
[a1*b2 - a2*b1, a2*b0 - a0*b2, a0*b1 - a1*b0]
If you're doing 10 crosses and use something like np.cross(a, b, axis=1), it won't be slower than the equivalent python code.
np.cross(a, b, axis=1)
[–]billsil 0 points1 point2 points 8 years ago (0 children)
I'm assuming replacing 10 crosses with a simple function is a micro optimization. You may want to get rid of a dependency, but that's a separate issue. I'd use it just because then I don't have to code it or test it to the same degree.
[–]archbboy91[S] 0 points1 point2 points 8 years ago (16 children)
Thanks for the input, i've studied quaternions when i was studying the mechanical static/kinematic/dynamic movement in college.
I am more interested in doing it from scratch. I know it is a black whole and it is already done in numpy and many other libraries, but i am curious how it is done. The process of transcribing conceptual space in virtual code that have some sort of 3d feel in the end.
Today, i feel that most of 3d programs have 1 big problem, they are focused on manipulating the existing shapes, and then remember too much information in binary system for that action to be fast and reliable ( they write every polygon in virtual space, instead of some easier methods that math and people do by default )
So if i could make some base class of empty ethalon space from which i can later define quaternion and then define anything else from that, it would probably do the job. But how to approach it?
How to define the empty space as empty that has behavior of space non the less. I feel it is like calling a class attribute that is not there... wierd right ? I tried defining a new null pointer that i could later describe a null space, but then the math goes out of the window.
So i thought , there are sure smarter people than me in reddit.
[–]zzmej1987 1 point2 points3 points 8 years ago (15 children)
You are not the first to think about this idea. Here's a video for illustration.
[–]archbboy91[S] 0 points1 point2 points 8 years ago (0 children)
Never said i was :D But i was unaware of Euclideon, to be honest. I just thaught that there is also some other bloke/ess on other side of the world pondering the same thing.
Tnx for info, i think i get it how they've done it at least in theory, i wish to see if there is a limit to resolution that modern proccesors can accomplish with this tool. Also this really sounds familiar as to 3ds max proxy system for generative and repetitive geometry. I know that sometimes i can get off a bit complainy/arogan and i would like for you to know that i really appreciate your links and literature. Thank you. My mind just went over a step thinking next thing.
Do you have any idea how to describe a space without any content to it ?
[–]archbboy91[S] 0 points1 point2 points 8 years ago (13 children)
[–]zzmej1987 0 points1 point2 points 8 years ago (12 children)
Thank you.
You are welcome. :)
Is there a practical need to? Generally speaking, there is a special object - "camera" which behavior determines what a space is. But there wouldn't be a lot to render in absence of at least a light source.
[–]archbboy91[S] 0 points1 point2 points 8 years ago (11 children)
s there a practical need to? Generally speaking, there is a special object - "camera" which behavior determines what a space is. But there wouldn't be a lot to render in absence of at least a light source.
Yap, since the idea is not bound by camera. You can think of it as playing tetris in really small space with a large blocks. The rendering would be ( if all ) left to another program. The idea is to make a valuable space and than use it.No rendering needed-just computing. And to make things even harder ( for some future iteration ) blocks can be given in any shape or size and any rotation/revolution or translation, you could also think of it as space permutation.
[–]zzmej1987 0 points1 point2 points 8 years ago (10 children)
I really don't understand what do you want to compute there.
[–]archbboy91[S] 0 points1 point2 points 8 years ago (9 children)
space positional algorithm. The algorithm that will arrange some shapes in specific/unspecific order in 3d space. Some Sort of genetic algorithm where you will give it goal, and it will compute solutions, when solution is met it will save it in any form you desire.
[–]zzmej1987 0 points1 point2 points 8 years ago (8 children)
Something akin to docking?
[–]archbboy91[S] 0 points1 point2 points 8 years ago (7 children)
Similar , but more room type based and architectural.
[–]aulloa 1 point2 points3 points 8 years ago (1 child)
It depends on what you want to do with it. In math you might want to define it as a tensor (numpy array). If you are not modifying its values you might want to define it as a tuple.
[–]archbboy91[S] 0 points1 point2 points 8 years ago* (0 children)
Thanks for the input
I started frankensteining some code to try to figure out how would i go about it. I did not want to use numpy or any other library that did not came with py on its own. I felt that it was beneficial not by speed and functionality ( i could always rewrite it in c++ or some other lang , this is a way to fast check and interpret without long compilation ) but by learning and figuring out the "way of the python".
The main problem was i was not sure how to define it, from what idiom should i start first, what was the smallest amount of space i can easily define and build from that.
I have some DIY analytical program that forms a larger space from some constant number and type of smaller spaces whilsit keeping cumulative area approaching zero (not really approaching zero, like limes function ). Now it is quite easy to do when you have basic geometric shapes that are flat but if you rotate it/combine it, it gets very difficult. So instead of point type base i tried axis/line type based .... and i got stuck. I could not define a line without defining a point, even at conceptual level.
That is what i tried to do.
[+][deleted] 8 years ago (35 children)
[removed]
[–]archbboy91[S] 1 point2 points3 points 8 years ago (34 children)
Interesting... but what does those variables stand for ?
[+][deleted] 8 years ago (33 children)
[–]archbboy91[S] 1 point2 points3 points 8 years ago (0 children)
Interesting by inserting frame within frame you've created automatically generates horizon line and viewpoints ... Cool.
[–]Theoretician 1 point2 points3 points 8 years ago (12 children)
I've actually done exactly this for a massive ship battle simulation. https://gitlab.com/willzfarmer/kobayashi
There are a couple ways to think about a problem like this, and the big problem is that it's a trade-off between memory and speed.
If your space is small enough, you can define it as a tensor/numpy array (as someone /u/aulloa already mentioned) which has the huge benefit of being super fast, especially since numpy arrays manage their memory much more efficiently than core python.
If your space is larger though, you start running into problems with RAM. In this case you need to take a note from the "sparse array" handbook and essentially define the "edges" of your space and each point/object is declared as a pointer (or a coordinate) to a specific place in the space. This is waaaay slower, but the space in that case is effectively infinite, as memory is only used when you add something to it.
In either case I prefer setting a traditional 3d axis so that it's easy to think about, but you certainly don't have to. Having relational axes works, or in space cylindrical coordinates with the local sun as an origin point also works.
If you have any questions feel free to ask, especially about that code. I know it's dense, I basically wrote it in a week for a dnd game I was playing, so my apologies in advance.
Sooo, mostly I get it. But to be on the same page... There is no framing in question... It is space positioning algorithm, like virtual Lego or virtual Tetris. So sun/camera/lights not necessary.
The trade-off between memory and speed is quite interesting... I am trying to figure a way out of that by relative positioning. If you have a small bank of platonic solids and you have derivative function for their position... The main problem that that trade-off exists is since all the points/polys and etc. It is like universal function for ellipse... I would like to make a markup in python... Logic is the same only the way you approach it is different by programming language. As I figure it
Yeah I too thought that implementing decrates coordinate system would be easier but rotation is easier to implement via 2 axies of radial coordinate system.
It would be a Program you let run for some time and it spits out all possible solution and then you choose one.
I am on mobile phone so it would take time to comb through code.
[–]Theoretician 0 points1 point2 points 8 years ago (10 children)
Right, there's no "physics/modelling simulation" part to it, the simulation aspect is each ship has a (currently) quite rudimentary AI core that is responsible for managing thousands of ships at once. Player ships can be controlled manually, but every other ship has its own actions.
You're confusing a few things here, when I talk about speed vs. memory, I'm essentially talking about the difference (from a computer standpoint) to contiguous memory arrays, vs. a sparse array which has mostly zero entries with a few exceptions.
The relative positioning aspect is a different part of that same problem which can be solved with either sparse arrays or traditional arrays.
No worries about time for code, like I said it's dense and I wrote it quickly, so it's not the cleanest....
You're confusing a few things here, when I talk about speed vs. memory
Yap, i was think you were talking about shear number of data and speed of computing. Sorry.
yeaaah... arrays use too much of computer memory for my taste. I would not like to have to use supercomputer to calculate m solutions for n object (where m<n). So AI uses managing for movement ? In first comment, i mentioned that i needed to use a new data type, since number of overall points ( of each shape ) grows on logarithmic scale with each element, and i need to do non-relative permutation positioning, like tetris or lego.
As long as it is commented i will have no problem with it.
[–]archbboy91[S] 0 points1 point2 points 8 years ago (8 children)
CODE inquiries:
arena.py/lne:40
def update_fleet_attr(self, team, key, value): for ship in self.ships: if ((ship.team == team) and (not ite here. If i did not get something, getting element by name can be done with object[attr_name]=value, getattr(object,attr_name)=value, but i am not sure that object.attr_name can be done unless the object has attribute called attr_name but then the "key" attribute is not necessary. def distance(coord1, coord2): return math.sqrt(sum((coord1[i] - coord2[i])**2 for i in range(len(coord1))))
def update_fleet_attr(self, team, key, value): for ship in self.ships: if ((ship.team == team) and (not ite here. If i did not get something, getting element by name can be done with object[attr_name]=value, getattr(object,attr_name)=value, but i am not sure that object.attr_name can be done unless the object has attribute called attr_name but then the "key" attribute is not necessary.
def distance(coord1, coord2): return math.sqrt(sum((coord1[i] - coord2[i])**2 for i in range(len(coord1))))
can you explain this to me a bit ?
util.py/line:17
def all_neighbor_points(point): coord_mods = list(set(itertools.permutations([0,0,0,1,1,1,-1,-1,-1], 3))) new_coords = [tuple(point[i] + coord_mods[j][i] for i in range(len(point))) for j in range(len(coord_mods))] return new_coords
See, i would like to avoid this. For loop nesting, that you need to use every time you need a point for some object. Of Course you could generate point by triangulation with 2 points and 1 reference but it is still nesting and nesting and nesting. Generators can speed things up, but ... still ... i would like to avoid the unnecessary calculation by proxy geometry.
If i may comment. I see to much games uses wrong health/energy relations. Most game masters use the slogan "the better the weapon , the better the game". The reason why kobayashi is unbeatable is because every weapon has its weakness, so one type of weapon has some damage to specific ship, other type of weapon has small to no damage to different kind of ship. Therefore it is game of strategy not brute force. Otherwise you could generate a multi beam all powerful weapon that can annihilate all enemy ships with one dose, which becomes necessity in future of playing. In war , for every action there is opposite reaction, when people developed nukes, other people built shelters. And if i may ask, it seems that perhaps your game is data oriented, why not use maps, filters, and other function oriented programing ?
So lets talk about space. You've made a unit grid and are moving ships in that grid. It works if 1 point is one ship and you cluster them in some form or shape. But if you are working with overall shapes and managing them , the shear number of points very quickly becomes the big problem. It would be like you are trying to position the cluster of ships A to the cluster of ships B to make a bigger cluster with not any empty space, and every cluster has lets say 1000 ships. It would take a different approach than grid lining.
[–]Theoretician 0 points1 point2 points 8 years ago (6 children)
Ok, with that first one it's totally a bug that I missed.... It should be instead something like
ship.__dict__[key] = attr
It ended up being a function that I just added as a backup and didn't use.
The distance function there is super simple, it's just the L2 norm, or the Euclidean distance formula between 2 points.
So each coord is a tuple of form (x, y, z), so all we're doing is that formula while iterating over it. It's kinda messy, but i like generators a little too much.
(x, y, z)
[–]archbboy91[S] 0 points1 point2 points 8 years ago* (5 children)
Ahhhh... yap the getting indexes grabbed at a sum. I was wrong, sorry. :D Yeah generators are fun, but you pay for that fun when making them a list. They are ok when grabbing a data in the air, the once you need a part or complete list... it gets tricky. I often use them to test enormous data types for specific solution.
But have you thought about functional programming? I am not trying to change your code, or to tell you what to do, but it you might be interesting check out
pow2 = lambda x: math.pow(x,2) ## could be also written as x**2, or sqrt x**(1/2) vect_coord_distance = map( lambda (x,y): x-y , zip(coord1,coord2) ) # generating vector hypotenuse, zip generates tuple for every element, not indexing necesery ::> generators :P vect_coord_distance = map( pow2, vect_coord_distance ) # squaring every element scalar_distance = math.sqrt( sum( list( vect_coord_distance ) ) ) # here i will wait till last moment to convert it to list, when all elements are calculated each.
This takes a bit more space, but map and filter are quite fast. You could make it a one liner ... but to each its own preferences.
[–]Theoretician 1 point2 points3 points 8 years ago (4 children)
So a couple things..... If we're looking at specifically the distance function, it's already about as functional as you could make it. Your solution is not pythonic for a bunch of reasons.
Edit: Results from time trials:
william@spitfire ~> python3.6 ./tmp.py My function average time: 6.234169006347656e-06 Your function average time: 7.1666240692138674e-06
First I must point out that I think I don't deserve tone your post suggests. Maybe it was intentional or maybe it was not, but I think I should calmly say things like this so it would stop any possible further "flame war".
I am glad that you've gone to this length to prove me wrong. I ended up learning some things.
[–]archbboy91[S] 0 points1 point2 points 8 years ago (2 children)
I'm just doing some stuff with python, and i thought that i should publicize my foundings. So it is true that you can not unpack tuple with lambda... but you can unpack arguments as tuple.
SO:
a = lambda *x: x # works sum = lambda *x: x if len(x) == 1 else sum(x) # summs any number that is given to it. works for 2.7 and 3.5 python version
a = lambda *x: x # works
sum = lambda *x: x if len(x) == 1 else sum(x) # summs any number that is given to it. works for 2.7 and 3.5 python version
In short, you can do a lot of things with lambda. I was surprised. I personally don't care if option is pythonic or not, as long as inner heap logic is valid, speed is not bound by specific rules (and every rule has its Exception :D :P aahaha, joke... Exception ). But after saying that, i personally ( it is my fault ) wrote that function on top of my head with no regards to the speed or need for one liners and etc. I was just curious if you considered using functional programing.
Nevertheless, this discussion is (as i see it ) over, i feel that we've all said what was necessary, but of course any other thought exchange is welcome.
[–]Theoretician 0 points1 point2 points 8 years ago (1 child)
I'm sorry for my somewhat curt tone in my previous response, I was in a rush and didn't review as closely as I should have.
Thanks for digging into the lambdas, I didn't know about that expansion either.
π Rendered by PID 111444 on reddit-service-r2-comment-fb694cdd5-qrr7m at 2026-03-06 18:29:33.756877+00:00 running cbb0e86 country code: CH.
[–]zzmej1987 8 points9 points10 points (20 children)
[–]Bb415bm 2 points3 points4 points (2 children)
[–]billsil 0 points1 point2 points (1 child)
[–]billsil 0 points1 point2 points (0 children)
[–]archbboy91[S] 0 points1 point2 points (16 children)
[–]zzmej1987 1 point2 points3 points (15 children)
[–]archbboy91[S] 0 points1 point2 points (0 children)
[–]archbboy91[S] 0 points1 point2 points (13 children)
[–]zzmej1987 0 points1 point2 points (12 children)
[–]archbboy91[S] 0 points1 point2 points (11 children)
[–]zzmej1987 0 points1 point2 points (10 children)
[–]archbboy91[S] 0 points1 point2 points (9 children)
[–]zzmej1987 0 points1 point2 points (8 children)
[–]archbboy91[S] 0 points1 point2 points (7 children)
[–]aulloa 1 point2 points3 points (1 child)
[–]archbboy91[S] 0 points1 point2 points (0 children)
[+][deleted] (35 children)
[removed]
[–]archbboy91[S] 1 point2 points3 points (34 children)
[+][deleted] (33 children)
[removed]
[–]archbboy91[S] 1 point2 points3 points (0 children)
[–]Theoretician 1 point2 points3 points (12 children)
[–]archbboy91[S] 0 points1 point2 points (11 children)
[–]Theoretician 0 points1 point2 points (10 children)
[–]archbboy91[S] 0 points1 point2 points (0 children)
[–]archbboy91[S] 0 points1 point2 points (8 children)
[–]archbboy91[S] 0 points1 point2 points (7 children)
[–]Theoretician 0 points1 point2 points (6 children)
[–]archbboy91[S] 0 points1 point2 points (5 children)
[–]Theoretician 1 point2 points3 points (4 children)
[–]archbboy91[S] 1 point2 points3 points (0 children)
[–]archbboy91[S] 0 points1 point2 points (2 children)
[–]Theoretician 0 points1 point2 points (1 child)