About CGRectMake - objective-c

This will sound extremely silly and stupid, but I have touched a couple languages and they kind of messed up my sense of.... orientation or something:
In Objective-C, you can use CGRectMake(x,y,w,h) to make a rect. Cool.
So x,y set the... top left corner of the rect, or.... the center of the rect?

In UIKit, (x,y) is the upper-left corner.

Cocoa uses the Cartesian coordinate plane as its coordinate system (so (x, y) is set to the bottom-left corner). You can read more about the coordinate system in the Cocoa Drawing Guide.

Related

In iOS is it possible to change a View's coordinate system so that 0,0 is top right corner?

Normally the 0,0 coordinate refers to the top left corner of a view. Higher x coordinates are further right. A frame / rectangle in the view has its leftmost point being its x coordinate and its rightmost point being its x coordinate plus its width.
Is it possible to reverse that, or better yet, reverse just the x axis? Make the 0,0 be the top right. Make the higher origins be further to the left. AND make it so a frame / rectangle in the view has its rightmost point as its x coordinate and its leftmost point as its x coordinate plus its width.
I know I could transform this stuff myself with pure math, but I was wondering if iOS offers this capability.
Not really.
iOS 9 has some new flipping stuff for supporting right-to-left languages, but I don't think you can force it.
You can flip the drawing of a view by setting its transform property to CGAffineTransformMakeScale(-1, 1), but that won't change the underlying coordinate system.
SpriteKit has a different coordinate system than normal UIViews, but its coordinate system isn't what you want.
You may want to read Coordinate Systems and Transforms, which discusses some techniques for mapping points between different coordinate systems. This MSDN article covers mapping points using matrices, which can help you on a theoretical level.
It's not documented, but UIView has an instance variable, _flipsHorizontalAxis, that does exactly what it sounds like it would do. It looks like it just passes through the CALayer variable of the same name.

Change NSImage Origin

Is it possible to change the origin of an NSImage? If so how would I go about doing this. I have coordinates in regular cartesian system some of them with negative values and I am trying to draw them at the corresponding point in the NSImage but since the origin is at (0,0) there are some missing.
EDIT:Say I have an drawing aspect that needs to be done to an image at the point (-10,-10), currently this doesn't show up. Is there a way to fix that?
If it's like in iOS (you may have to adapt a little the code) and if my memory is still good, you have to do this, since origin is readOnly:
CGRect myFrame = yourImage.frame;
myFrame.origin.x=newX; myFrame.origin.y=newY;
yourImage.frame = myFrame;
I think you are confusing an NSImage with it's container. An NSImage has no bounds or frame, and thus no origin. It does have a size which may represent the pixel dimensions of its birtmap representation ( if it has one) or otherwise could represent it's bounding box ( if it is a vector image). Drawing in an image at a pixel location of (-10,-10) doesn't really make sense.
An NSImage is displayed in a container ( typically an NSImageView), and the container's bounds.origin will dictate the placement of the image relative to the imageView, but you can't modify pixels beyond the edge of the bitmap plane.
In any case you probably want to be using a subclassed NSView in which you would override the drawRect method for your custom drawing. NSView does have a bounds.origin but this is not relevant to your in-drawing coordinates, but rather to the position of the drawn content as a whole to the view's bounding box. The coordinate system that you will be drawing into will be referenced to your graphics context which will (usually) pin the origin (0,0) to the bottom left corner (OSX) or top left corner (iOS). If you are trying to represent negative points on a Cartesian plane, you will need to apply a translation transform to map your points into this positive coordinate space.
I'm trying to explain in a few words, badly, something which Apple explains in great detail in their Quartz 2D Programming Guide.

UIView cornerRadius - of what?

I am familiar with the cornerRadius property of UIView layers, exposed with the QuartzCore framework. The general rule of thumb, of course, is larger values create more rounded edges, and smaller values create less rounded edges. I'm curious though, the property is called cornerRadius, and I often find myself asking, radius of what? What and where is this circle whose radius I'm changing?
Imagine a circle at each corner of the rectangle framing the view. The rounded edge of the corner follows the arc of the circle. cornerRadius is the radius of that circle.
Take the 4 corners of a rectangle.
Starting at the corner points, you pass it a length to which the rounded corner should extend to.
This is the radius of the corner, or cornerRadius.
That's how I interpret it. Don't know if that's correct :P
This is more of a math question, but the above answer is mostly correct, except that the lines aren't circular. A radius is:
A radial line from the focus to any point of a curve.
(Source: Oxford)
As an example, note the non-circular curvature of some non-circular gears, which still have radii.
So, it's similar to what's described in the other answers, but the corner curve is not circular. If you're curious about the equation of the curve, just do the radius equation backwards. :-)

Three.js camera rotation is weird

I've just started with Three.js. Like really just now.
After playing with it for an hour or so and building a tool that helps me understand how the different elements work together (Camera, Light, Objects), I found something strange.
The tool: http://hotblocks.nl/tests/three/cubes.html
This is the current default set up:
the Camera is positioned 210 upwards and
500 backwards and
246 to the right
the Camera is rotated slightly to the left
the light is directly above and shines in all directions
As you can see, the objects are at the very bottom of the viewport. So I want to turn the Camera downward, so I can see more of them.
Try that: turn camera.rotation.x down.
That works, but the angle of rotation is wrong! Instead of the Camera rotating, it's the World rotating around its Z axis.
That's not right, is it?
The Y axis is also wrong. It rotates the World around its Y axis.
Rotating the Camera around its Z axis, works perfectly: the Camera rotates, not the World.
Am I doing it wrong? Or understanding it wrong?
PS Since the Camera rotation is only around its Y axis, the objects' vertical edges should be vertical in the result as well. In the default set up, they are. Rotating the camera around its X axis, shouldn't change that, but it does. Only rotating around its Z axis should change that (and it does). Am I wrong?
PPS I know about Camera.lookAt( THREE.Vector3 target ), but that changes the rotation of the camera, including its Z axis, and that shouldn't be necessary, logically.
Answer received on Github: https://github.com/mrdoob/three.js/issues/1163

finding rgb value of cut point of diagonal of rectangle in objective-c

I have to find the RGB of point where two diagonals of a rectangle cut each other in objective-c.
more explanation of my requirements:
lets take a rectangular image draw two diagonal from the corner like we make cross. I need the RGB value of the cut point.
Regards
Satya
Here's how I'd do it on the Mac. With focus locked on the graphics context in question:
NSRect aRect;
NSPoint centerOfRect = NSMakePoint(NSMidX(aRect), NSMidY(aRect));
NSColor *aColor = NSReadPixel(centerOfRect);
If you're working on the iPhone, then you'll want to find the equivalent macros for CGRect instead of NSRect, and the related CG Function.