help me

To help me better understand the lambda, I wrote this short snippet that rotates and transforms the quad (I hope I got the math on the right). Now I want to replace the three steps below with a single liner, possibly in combination with map ().

Im using a vector class , but hopefully the functions are clear what they do.

self.orientation = vector(1,0)
self.orientation.rotate(90.0)

#the four corners of a quad
points = (vector(-1,-1),vector(1,-1),vector(1,1),vector(-1,1))
print points

#apply rotation to points according to orientation
rot_points = []
for i in points:
    rot_points.append(i.rotated(self.orientation.get_angle()))
print rot_points

#transform the point according to world position and scale
real_points = []
for i in rot_points:
    real_points.append(self.pos+i*self.scale)
print real_points

return real_points
+3
source share
2 answers

You can use map, reduceetc., but these days lists of lists are the preferred way to do things in Python:

rot_points  = (i.rotated(self.orientation.get_angle()) for i in points)
real_points = [self.pos+i*self.scale for i in rot_points]

, (parentheses) [brackets] . . rot_points " ", , rot_points , . , , .

+8

, get_angle() , .

:

angle = self.orientation.get_angle()
real_points = [self.pos+point.rotated(angle)*self.scale for point in points]

, , . :

angle = self.orientation.get_angle()

def adjust_point(point):
     point = point.rotated(angle)
     point *= self.scale
     point += self.pos
     return point

real_points = [adjust_point(p) for p in point]
0

Source: https://habr.com/ru/post/1712111/


All Articles