-1

So to give further context lets say I have an image that is 200px by 200px with a rectangle on it, its red below:

enter image description here

I know the height and width of the image, the coordinates of the rectangle and also the height and width of the red rectangle.

So what I need to know is if I flip this whole image (including the rectangle) is there a way to work out what the new coordinates are of the red rectangle? I'd imagine there must be some kind of formula or algorithm I can apply to get these new coordinates.

Dr Johnson
  • 17
  • 2

2 Answers2

1

This was already answered over here. The below is the function that worked best for my use case which is very similar to yours.

def rotate(point, origin, degrees):
    radians = np.deg2rad(degrees)
    x,y = point
    offset_x, offset_y = origin
    adjusted_x = (x - offset_x)
    adjusted_y = (y - offset_y)
    cos_rad = np.cos(radians)
    sin_rad = np.sin(radians)
    qx = offset_x + cos_rad * adjusted_x + sin_rad * adjusted_y
    qy = offset_y + -sin_rad * adjusted_x + cos_rad * adjusted_y
    return int(qx), int(qy)

In addition to this sometimes when you rotate the points you get negative values(depending on degrees of rotation), in cases like these you need to add the height and or width of the image you are rotating to the value. In my case below the images were of fixed size (416x416)

def cord_checker(pt1):
    for item in pt1:
        if item<0:
            pt1[pt1.index(item)]=416+item
        else: pass
    return pt1

finally to get the coordinates of the rotated point

pt1=tuple(cord_checker(list(rotate((xmi,ymi),origin=(0,0),degrees*=))))

*degrees can be 90,180 etc

Shreyas H.V
  • 109
  • 1
  • 3
0

If the image is centered around the origin (0,0), you can just flip the signs of the x-coordinates to do a horizontal flip or the y-coordinates to do a vertical flip while preserving the origin as your center.

You can also flip an arbitrary image by flipping the signs:

# Horizontal flip
new_x = -x
new_y = y

# Vertical flip
new_x = x
new_y = -y

but the center coordinates will not be the same. If you want the same center coordinates, you'd have to shift it back.

Jason K Lai
  • 1,500
  • 5
  • 15
  • so the 200x200px image would be rotated 90 degrees on its centre but I need the coordinates of the smaller red square in the example given, I'm thinking I need to calculate the centre coordinates of the red square first, perform the rotation of the 200x200 image and then workout where the centre point of the little rectangle should be, once I figure it out I shall update... – Dr Johnson Jan 14 '21 at 12:30
  • this is pretty much what I needed https://gamedev.stackexchange.com/questions/86755/how-to-calculate-corner-positions-marks-of-a-rotated-tilted-rectangle – Dr Johnson Jan 14 '21 at 14:31