Functions and constants for NSPoint.
- import <Cocoa/Cocoa.h>
- import <math.h>
- import <float.h> / Standard C lib that contains some constants, look at http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.4.html -- JP /
return NSMakePoint(firstPoint.x+secondPoint.x, firstPoint.y+secondPoint.y);
return NSMakePoint(firstPoint.x-secondPoint.x, firstPoint.y-secondPoint.y);
return NSAddPoints(point, NSMakePoint(amountX, amountY));
return NSMakePoint(-point.x, point.y);
return NSMakePoint(point.x, -point.y);
return NSMakePoint(-point.x, -point.y);
return [transform transformPoint:point];
return NSMakePoint(sqrtf(cartesianPoint.xcartesianPoint.x+cartesianPoint.ycartesianPoint.y), atan2f(cartesianPoint.y,cartesianPoint.x));
return NSMakePoint(polarPoint.xcosf(polarPoint.y), polarPoint.xsinf(polarPoint.y));
Personally, I don't care that much. (: But someone else can if they want, certainly. Conversion between Cartesian and Polar coordinates would be nice, though, if a new struct were made.
Something should be done : split or new struct
Isn't there some function for converting a point to a string? NSStringFromPoint() in the Foundation API
Otherwise this could be wrapped in a class with four methods returning the different co-ordinates. But if we did that it may as well handle more than two dimensions, and I suspect the whole point of these functions is that they're just little fast things to make the point that we don't need an NSPoint class. :)
How about using hypot(a, b) for the radius part of the cartesian-to-polar conversion? It does the whole sqrt(a^2+b^2) thing for you pretty fast. Plus its a whole lot easier to type. Also, there is an issue with using atan(y/x), since nothing over Pi radians will be correct. And when x is 0 there is a minor issue with infinity. A cartesian to polar conversion isn't possible in one (efficient) line as far as I can figure out.
What I usually do is find the asin of fabs(y)/hypot, then put in a case for each quadrant. For quadrant I, keep the asin, for II, it's pi - asin, for III, pi + asin, and for IV, 2pi - asin. That usually works out alright for me. I have missed the conversion a lot of times before, though, so feel free to correct me. And if you have a simpler method, I'd love to hear it. Adios!
Some changes on the cartesian to polar function�:
- xx is more efficient than x^2
- atanf is a bad solution : return an angle between 0 and pi, not between 0 and 2pi. Use atan2f instead.