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. :-)
Related
When I first started working with AutoLayout, I couldn't find any example which helps understanding for what stands the Aspect Ratio NSLayoutConstraint.
Does someone have an example of usage? Thanks!
I have a demo example of keeping a square view centered in all devices and orientations here.
The constraints are set up in IB, but you could just as easily set them up in code. Most of this was meant to explain how to prioritize margin constraints to let the auto layout engine know what to break and when, but what makes the view square is the aspect ration - it's 1:1. If you want a rectangle, you make it 2:1 (or 1:2 depending).
Once you understand these two pieces (prioritization and aspect ratio), the last piece is the actual margins values - I set them for 10 points, meaning the square will have 10 point margins on the smallest axis. If the device is an iPad Pro 12.9 inch, you get a very large square. If it's an iPhone SE, you get a very small one. No matter what, you get the 10 point margin. Set the margin values up for 50, and the square view is relatively smaller. The important thing is with an aspect ratio of 1:1, it will always be a square.
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.
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.
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.
How to design a circle in the 3-D space?
For a circle in the 2-D space, two members are enough.
1 center;
2 radius;
but in 3D, how can I define the direction and the position of the circle?
One possibility would be to include a vector that's normal to the plane on which the circle lies. This has the advantage that if you ever decide to render the circle, the normal will be used to determine things like reflections off the surface defined by that circle.
Do you really want a Circle (a 2D Shape) in a 3D Space? Then this could be solution:
x,y,z: Coordinates of the center of the circle
dx,dy,dz: Direction of the plane the circle is located in
r: radius
Depending on what you want to do with the object - I have another alternative.
Model the circle as a unit circle in the xy plane with z=0.
with scaling, translating, and rotating operations done to it.
If you plan on doing many matrix operations on your objects, this may be the way to go.
You could also keep the unit circle's position, radius, and normal to plane information as properties. and have a method to convert between the two describing methods .
Circle() - Default Unit circle at (0,0,0), radius 1, xy plane at z=0
Circle(scale, translate, rotate) - my constructor
Circle(location, radius, normal) - the other proposed constructor