Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
888 views
in Technique[技术] by (71.8m points)

python - Get coordinates of the rotated patch (rectangle) matplotlib

I want to achieve the following:

  • 1) get coordinates of the rotated patch
  • 2) get all points of the patch (here: rectangle)
  • ** I have an impression that the rotated rectangle does not have 90 degree between faces. Is that just visualization?

My snippet below. The coordinates of the rotated patch are the same as of the original though .. How to achieve 1) and 2) ?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib as mpl

from matplotlib.transforms import Affine2D

fig = plt.figure()
ax = fig.add_subplot(111)
angle = np.deg2rad(45)

r1 = patches.Rectangle((8,4), 5,3, fill=False, color="red", alpha=0.50)
r2 = patches.Rectangle((8,4), 5,3, fill=False, color="blue",  alpha=0.50)

trafo = mpl.transforms.Affine2D().rotate_around(8,4,angle) + ax.transData
r2.set_transform(trafo)

ax.add_patch(r1)
ax.add_patch(r2)

plt.xlim(0,15)
plt.ylim(0,15)

plt.grid(False)

plt.show()
print(r1.get_bbox())
print(r1.get_xy())
print(r2.get_bbox()) # why are they the same as for r1?
print(r2.get_xy())
#print(r1.get_all_points()) # how to achieve it?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Rectangle's coordinates

A Rectangle is defined through a coordinate pair of the lower left corner (x,y), and a width and height. To get the coordinates of its corners, you may

  • calculate them from the corner, width and height,

    r1 = patches.Rectangle((8,4), 5,3)
    ax.add_patch(r1)
    coords = np.array([r1.get_xy(), [r1.get_x()+r1.get_width(), r1.get_y()],
                       [r1.get_x()+r1.get_width(), r1.get_y()+r1.get_height()],
                       [r1.get_x(), r1.get_y()+r1.get_height()]])
    print(coords)
    
  • get them from the transformed path,

    r1 = patches.Rectangle((8,4), 5,3)
    ax.add_patch(r1)
    coords = r1.get_patch_transform().transform(r1.get_path().vertices[:-1])
    print(coords)
    

In both cases the printed result will be

[[  8.   4.]
 [ 13.   4.]
 [ 13.   7.]
 [  8.   7.]]

You may also get the two points of the lower left and upper right corner from the rectangle's bounding box (due to a box being a Rectangle itself),

r1 = patches.Rectangle((8,4), 5,3)
ax.add_patch(r1)
coords = r1.get_bbox().get_points()
print(coords)

with will result in

[[  8.   4.]
 [ 13.   7.]]

Transformed rectangle's coordinates.

Now if you transform the rectangle, the above methods need to take the transform into account to provide the correct coordinates of the transformed rectangle.

  • transform the manually obtained coordinates,

    r2 = patches.Rectangle((8,4), 5,3)
    
    trafo = mpl.transforms.Affine2D().rotate_around(8,4,angle)
    r2.set_transform(trafo + ax.transData)
    ax.add_patch(r2)
    coords = np.array([r2.get_xy(), [r2.get_x()+r2.get_width(), r2.get_y()],
     [r2.get_x()+r2.get_width(), r2.get_y()+r2.get_height()],
     [r2.get_x(), r2.get_y()+r2.get_height()]])
    print(trafo.transform(coords))
    
  • transform the coordinates obtained from the path

    r2 = patches.Rectangle((8,4), 5,3)
    
    trafo = mpl.transforms.Affine2D().rotate_around(8,4,angle)
    r2.set_transform(trafo + ax.transData)
    ax.add_patch(r2)
    
    coords = r2.get_patch_transform().transform(r2.get_path().vertices[:-1])
    print(trafo.transform(coords))
    

In those cases, the printed coordinates will be

[[  8.           4.        ]
 [ 11.53553391   7.53553391]
 [  9.41421356   9.65685425]
 [  5.87867966   6.12132034]]

Or, in case of getting the coodinates from the bounding box

coords = r2.get_bbox().get_points()
print(trafo.transform(coords))

prints

[[ 8.          4.        ]
 [ 9.41421356  9.65685425]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...