Converting from NSPoint to CGPoint? (and rects)

Member
Posts: 312
Joined: 2006.10
Post: #1
Does anyone know how to convert between these types?

i.e.
Code:
CGPoint myPoint = myNSPointHere;
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #2
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #3
I've read that, but when i try this
Code:
NSRect cocoaRect = *(NSRect*)&myCGRect;
But switch it around like this
Code:
CGPoint cocoaPoint = *(CGPont*)&myNSPoint;
I get errors
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
IIRC, all of the Cocoa datatypes that have a Carbon equivalent can simply be typecasted across.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #5
bronxbomber92 Wrote:I've read that, but when i try this
Code:
NSRect cocoaRect = *(NSRect*)&myCGRect;
But switch it around like this
Code:
CGPoint cocoaPoint = *(CGPont*)&myNSPoint;
I get errors

Try CGPoint instead. Wink

This works just fine for me:
Code:
    NSPoint myNSPoint;
    CGPoint cocoaPoint = *(CGPoint*)&myNSPoint;
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2007.01
Post: #6
akb825 Wrote:IIRC, all of the Cocoa datatypes that have a Carbon equivalent can simply be typecasted across.

that's what i was thinking, too. try searching the apple docs for info about 'toll-free bridging'. you're probably making it harder than it needs to be :]
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #7
MacGoober Wrote:that's what i was thinking, too. try searching the apple docs for info about 'toll-free bridging'. you're probably making it harder than it needs to be :]

Nope, not making it harder at all, the cast is actually what the docs say:
Quote:Although in each case the structure layout is the same, you cannot pass the Quartz data type directly to a method expecting the Cocoa type. To convert, you must cast from one type to another, as shown in the following example:

NSRect cocoaRect = *(NSRect*)&myCGRect;

Anyway, the only reason bronxbomber92's code didn't work was because he misspelled CGPoint in the cast Wink
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #8
Heh, didn't see that >.<
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #9
PowerMacX Wrote:Third link from a Google search of Converting from NSPoint to CGPoint

LOL ... Just thought you might get a kick out of the fact that ATM this thread happens to be the top search result for cgpoint nspoint

And even better yet, your original search for "Converting from NSPoint to CGPoint" now points to this thread as the top result. Man, that is funny!

I had forgotten the crazy casting for it, so I had to do a quick search. Sneaky
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #10
Heh, that's quite funny. A little embarrassing to. Blush
Quote this message in a reply
Moderator
Posts: 624
Joined: 2007.09
Post: #11
Of course, now you can just do

Quote:NSPoint point = NSPointFromCGPoint(myCGPoint);

&

Quote:CGPoint point = NSPointToCGPoint(myNSPoint);

But you knew that Wink

-LG

- Lincoln Green
http://www.binkworks.com/
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #12
Actually, no, I was not aware of that (or I forgot) -- good catch HairBall!

It looks important too because the docs mention that in 32 bit code NSRects and CGRects are distinct types, which I did not know.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  converting quartz to openGL ES aerospaceman 2 3,995 Apr 12, 2010 10:57 AM
Last Post: ThemsAllTook