3

I have a problem with getting the rotation around a point right.

What I want to achieve is a function that rotates 4 corners of a square around its centre. I then display the rotated square. I need this for testing of some other functions.

What happens now is that the distances between the points are not preserved. For pi/2 I get a diagonal and for pi/5 a "diamond".

I think it might have to do with the fact that the y-axis in the coordinate system used by imshow is flipped (increasing values downwards). I played with signs but didn't manage to get it right. Please help me find the error!

## Define corners of a square
p1 = [200,200]  #[x,y]
p2 = [200,300]
p3 = [300,300]
p4 = [300,200]     

## Choose the angle of rotation
rotAng = math.pi/3

## Rotate the corners
p1 = rotPt(p1, rotAng)
p2 = rotPt(p2, rotAng)
p3 = rotPt(p3, rotAng)
p4 = rotPt(p4, rotAng)

## Display square as a polyline
img = np.zeros((640,480,3), np.uint8)   #creates an empty image

pts = np.array([p1,p2,p3,p4], np.int32)

pts = pts.reshape((-1,1,2))
img = cv2.polylines(img,[pts],True,(255,255,255), thickness = 3)

## View the image
depth_image = np.asanyarray(img)
cv2.imshow('depth_image', depth_image)



def rotPt((x, y), th):
    cx = 250
    cy = 250    #centre of square coords

    x -= cx
    y -= cy

    x = x*math.cos(th) - y*math.sin(th)
    y = x*math.sin(th) + y*math.cos(th)

    x += cx
    y += cy

    return [x,y]
marlot
  • 33
  • 3

1 Answers1

2

In the second line you use changed value of x.

x = x*math.cos(th) - y*math.sin(th)
y = x*math.sin(th) + y*math.cos(th)

Just remember and use old value.

xx = x
x = x*math.cos(th) - y*math.sin(th)
y = xx * math.sin(th) + y*math.cos(th)
MBo
  • 77,366
  • 5
  • 53
  • 86