Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS X Cocoa, Flipping Y Coordinate in Global Screen Coordinate Space

How do I flip the y value of an NSPoint to a flipped coordinate space.

I have two sources of points, both in global screen space, but one is flipped:

  1. one from a screen coordinate space with 0,0 = top left.

  2. one from a screen coordinate space with 0,0 = bottom left.

I need to flip the second one (bottom left) to be in the same space as the first one, top left. How can I do that on an NSPoint or CGPoint?

like image 368
gngrwzrd Avatar asked Oct 14 '25 16:10

gngrwzrd


2 Answers

For Mac OSX Cocoa, you can flip the y Coordinate for a view, just override isFlipped to return YES for each view:

- (BOOL)isFlipped {
    return YES;
}
like image 93
zaph Avatar answered Oct 17 '25 11:10

zaph


If you have multiple monitors, the overall height of the global coordinate space may be larger than the vertical height on any individual display. I found it necessary to use the following code (given originalPoint) to find that:

let globalHeight = screens.map {$0.frame.origin.y + $0.frame.height}.max()!
let flippedY = globalHeight - originalPoint.y
let convertedPoint = NSPoint(x: originalPoint.x, y: flippedY)
like image 40
Ian Avatar answered Oct 17 '25 11:10

Ian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!