NSRect vs CGRect: inverse of the y axis

So I'm trying to convert NSRect to CGRect. I used NSRectToCGRect() , which copies everything finely, but does not take into account the y-origin axis.

Problem: The beginning of CGRect (0,0) is the upper left. NSRect origin (0,0) bottom left.

Thus, the NSRect (0,0,100,100) field NSRect (0,0,100,100) is located in the lower left corner of the screen, and the NSRect (0,0,100,100) field NSRect (0,0,100,100) is located in the upper left corner of the screen.

I have a hack that captures y-origin through basic math:

 fixedOriginY = screenHeight - NSRect.size.height - NSRect.origin.y 

So the equivalent of NSRect (0,0,100,100) is actually CGRect (0,800,100,100) (in my MBA 900px high). However, I do not like it, I have a feeling that I will have problems with the retina or future complications.

Does anyone have a solution or idea on how to properly convert NSRect to CGRect?

+6
source share
1 answer

If the beginning of the rectangle is in the upper left or lower left corner, it depends on the coordinate system in which you use the rectangle. It does not depend on whether you CGRect or NSRect .

Typically, AppKit uses coordinate systems where the bottom left corner is the source. Similarly, the CGContext you CGContext puts the beginning at the bottom left.

Quartz Event Services and Quartz Display Services, on the other hand, place a start in the upper left corner. UIKit (on iOS) puts a start in the upper left. You can override isFlipped in a subclass of NSView (on OS X) to place its beginning in the upper left corner.

Your basic math is necessary and correct when you convert a rectangle from a traditional (lower left start) to a coordinate system with inversion (upper left start).

+6
source

Source: https://habr.com/ru/post/947073/


All Articles