0

I have a midpoint (x,y) and i need to find (in elegant and python style) a vertices of square with rotation for a given angle (t) or random adding to my function a parameter t (angle), when t is equal to r(=random) the vertices are random located.

def get_square_plot(x, y, side):
    return [(x-(side/2), y+(side/2)), (x+(side/2), y+(side/2)), (x-(side/2), y-(side/2)), (x-(side/2), y-(side/2))]

where x,y are the coordinate of midpoint and side the side of the square

print get_square_plot(0, 0, 10)
[(-5.0, 5.0), (5.0, 5.0), (-5.0, -5.0), (-5.0, -5.0)]
Cœur
  • 37,241
  • 25
  • 195
  • 267
Gianni Spear
  • 7,033
  • 22
  • 82
  • 131
  • thanks M4rtini (and sorry if i was not clear). I wish to improve my function and insert a parameter t (angle value) of rotation. if t == r the vertices are random rotate – Gianni Spear Mar 12 '14 at 19:11
  • 2
    Just rotate the points `get_square_plot()` produces by angle `t` about the point `x, y`. See example [Rotate line around center point given two vertices](http://stackoverflow.com/questions/14842090/rotate-line-around-center-point-given-two-vertices/14842362#14842362). – martineau Mar 12 '14 at 19:12
  • Sounds more like a geometry problem that a programming problem. – M4rtini Mar 12 '14 at 19:13
  • Thanks @martineau do you have some suggestion to write in elegant way the function? – Gianni Spear Mar 12 '14 at 19:17
  • What do you mean "if t == r the vertices are random located" (or they "are random rotate"? What's `r`, where does it come from? – martineau Mar 12 '14 at 20:54
  • @martineau you can turn the square for a angle x that you give ex t = 45 degree or you can turn the square random – Gianni Spear Mar 12 '14 at 21:18
  • OK, I think I understand. If you can edit your answer to include that and get it reopened, I have an answer I'll can post. Alternatively, it might be faster to post a new question with your clarifications in it. – martineau Mar 12 '14 at 21:37
  • @martineau i posted a new question. Thanks for your help http://stackoverflow.com/questions/22364828/create-a-square-polygon-random-oriented-from-midpoints-in-python – Gianni Spear Mar 12 '14 at 22:01

1 Answers1

2

You might want to use Numpy to rotate your squares:

import numpy as np
import matplotlib.pyplot as plt

def RotM(alpha):
    """ Rotation Matrix for angle ``alpha`` """
    sa, ca = np.sin(alpha), np.cos(alpha)
    return np.array([[ca, -sa],
                     [sa,  ca]])

def getSquareVertices(mm, h, phi):
    """ Calculate the for vertices for square with center ``mm``,
        side length ``h`` and rotation ``phi`` """
    hh0 = np.ones(2)*h  # initial corner
    vv = [np.asarray(mm) + reduce(np.dot, [RotM(phi), RotM(np.pi/2*c), hh0])
          for c in range(5)]  # rotate initial corner four times by 90°
    return np.asarray(vv)


if __name__ == "__main__":
    """ Test getSquareVertices """

    fg,ax = plt.subplots(1,1)
    for h,phi in zip([1,2,3], [0.,np.pi/6, np.pi/4]):
        cc = (h,h) # center
        vv = getSquareVertices(cc, h, phi)
        print(vv)
        ax.plot(vv[:,0], vv[:,1], 'x-',
                label=u"$\phi$={:.1f}°".format(np.rad2deg(phi)))
    ax.legend(loc="best")
    ax.axis("equal")
    fg.canvas.draw()  # do the drawing
    plt.show() # enter event loop

producesSquares

Dietrich
  • 5,241
  • 3
  • 24
  • 36