|
|
|
Return to General Discussion
by ale870 » Mon Mar 12, 2012 6:53 pm
Hello,
maybe the question is trivial, but is there any function to convert a Vec3() to int?
Vec3(1.2, 2.33, 10.123) --> (1, 2, 10)
I know I can use int() or math.floor() but they work on float, not integer.
Since there are many operations that work on vectors out-of-the-box (like +, *, etc...) I wish to know if I can convert Vec3() to int?
Another question: maybe exists a Vec3i()?
(vector 3 containing integer values?)
Thank you for your help!
-
ale870
-
- Posts: 40
- Joined: Thu May 07, 2009 5:36 pm
-
by bluskies » Mon Mar 12, 2012 8:55 pm
Simple helper function:
- Code: Select all
def intvec(vec3): return int(vec3.getX()), int(vec3.getY()), int(vec3.getZ())
Example use: - Code: Select all
from panda3d.core import Vec3
def intvec(vec3): return int(vec3.getX()), int(vec3.getY()), int(vec3.getZ())
s = Vec3(1.1, 2.2, 3.3) print intvec(s) == (1, 2, 3) # will be True
>>> print capitalOfCanada == 'Toronto'
False
>>> print capitalOfCanada
'Ottawa'
-
bluskies
-
- Posts: 109
- Joined: Thu Jun 17, 2010 9:28 am
- Location: 45.32, -75.67
-
by bluskies » Mon Mar 12, 2012 9:07 pm
Unless by "convert a Vec3() to int" you mean "make the components of a Vec3() integers"...in which case you will have to do the above but then add a line to convert the tuple of ints back into a Vec3 instance. Really though, if that's the case then don't bother - find a way to ignore the non-ints coming out of the Vec3, or make them ints before they are in the Vec3.
>>> print capitalOfCanada == 'Toronto'
False
>>> print capitalOfCanada
'Ottawa'
-
bluskies
-
- Posts: 109
- Joined: Thu Jun 17, 2010 9:28 am
- Location: 45.32, -75.67
-
by ale870 » Tue Mar 13, 2012 2:06 am
Ok, thank you. I used that solution but I wished a method such this one:
newVec = math.vec_to_int(myVecFloat)
I will do as you suggested, thanks!
-
ale870
-
- Posts: 40
- Joined: Thu May 07, 2009 5:36 pm
-
by rdb » Tue Mar 13, 2012 3:44 am
- Code: Select all
vec_to_int = lambda v: (int(v.x), int(v.y), int(v.z))
newVec = vec_to_int(myVecFloat)
-
rdb
-
- Posts: 8636
- Joined: Mon Dec 04, 2006 5:58 am
- Location: Netherlands
-
by ale870 » Tue Mar 13, 2012 4:48 am
rdb wrote:- Code: Select all
vec_to_int = lambda v: (int(v.x), int(v.y), int(v.z))
newVec = vec_to_int(myVecFloat)
Python docet. Great piece of code.
Thank you!
-
ale870
-
- Posts: 40
- Joined: Thu May 07, 2009 5:36 pm
-
by enn0x » Tue Mar 13, 2012 6:35 am
Another one, works with vectors of any dimension (e.g. 2,3,4):
- Code: Select all
vi = map(int, v)
-
enn0x
-
- Posts: 1278
- Joined: Wed Nov 08, 2006 1:39 am
- Location: Germany, Munich
by rdb » Tue Mar 13, 2012 7:04 am
Oo, I like that one. Very clever.
-
rdb
-
- Posts: 8636
- Joined: Mon Dec 04, 2006 5:58 am
- Location: Netherlands
-
by ale870 » Tue Mar 13, 2012 7:20 am
enn0x wrote:Another one, works with vectors of any dimension (e.g. 2,3,4): - Code: Select all
vi = map(int, v)
Wow! I love Lisp, and I didn't know Python had a "map" function (functional-language style ) 
-
ale870
-
- Posts: 40
- Joined: Thu May 07, 2009 5:36 pm
-
Return to General Discussion
Who is online
Users browsing this forum: No registered users and 0 guests
| | |