I'm working on a school project using Xcode and for part of it, I need to draw a circle on Xcode using Swift. How do I do that?
The first thing you need to do is decide how large of a circle you want – this will determine the parameters you set. The first thing you do is make a constant named whatever you want and make it a square. Then, you can use variableName.layer.cornerRadius to make your circle. You have to use half of what you made the square as it is the radius of the circle.
Here's an example of a circle with a 10 radius:
let circle = UIView(frame: CGRect(x:0, y:0, width: 20, height: 20))
circle.backgroundColor = UIColor.blue //this is just so the circle is visible
circle.layer.cornerRadius = 10
The really important thing to remember is that the square that you
originally make must have the width and height be twice the size of
the radius. Also, if you are having trouble seeing the circle then it
might be because you didn't set a background color to it, so do
something like the code above by setting the color to anything you
want
.
Related
I work with Forge Autodesk, and I want to apply a texture to some rectangle object. In fact I just needed some plan, but I was given a rectangle. So I want to apply my image on the main face of the rectangle.
const mytex = THREE.ImageUtils.loadTexture(mytexture)
// Repeat image through object
mytex.wrapS = THREE.RepeatWrapping //ClampToEdgeWrapping //MirroredRepeatWrapping //RepeatWrapping
mytex.wrapT = THREE.RepeatWrapping //ClampToEdgeWrapping //MirroredRepeatWrapping //RepeatWrapping
mytex.mapping = THREE.UVMapping
mytex.repeat.set (0.05, 0.05)
console.log("applied texture")
But I get this problem : a part of my image appears on the right upper side (upper and right corners are cut, so not on the rectangle), but the left and bottom sides are stretched across the rest of the rectangle face.
I would like to adapt my image so that its dimension fit the rectangle's dimensions, and not just repeat it.
I read this and this. I think my code is correctly written, but I may be missing a parameter or set the wrong one... The 2 images I am testing are 676x676 and 1024x484 pixels. I cannot access the rectangle dimensions from my function (I don't think so).
I tried to just repeat the image too but it does not work either...
Any idea ?
I'm having a trouble getting the X and Y of a canvas. So originally this is my canvas.
And I'm using a scale() to zoom my canvas, and when I use it, it will become like this.
My goal is to get the X and Y of this part of my zoomed canvas
I tried to get the the width and height of my canvas, however I can't show the object that I locate to the zoomed canvas. I need to zoom it out just to show it. I want to get the x and y of that part of my canvas so even I zoom in or zoom out the canvas, it will stay there. How can I do that? Thank you.
That value is just width/scale, isn't it?
Think about it this way: if your canvas is 1000 pixels wide, and you then call scale(2), what coordinate will be in the upper-right corner? Since you've scaled by 2, the new coordinate will be 500, which is 1000 / 2, or width / scale. Note that you have to keep track of the scale value yourself.
I'm pretty sure this pattern holds no matter what the width or scale is. I'd recommend putting together some example programs to test whether the point shows up where you expect it to. If not, please post a MCVE and we'll go from there.
I've drawn a circle and arranged some text elements radial outside of this circle with Raphael. Each of them are rotated to the center point of the circle. So far so good.
But now I want to rotate them a second time, for example 30deg. But if I do so, then all elements will rotated to 30deg.
A solution could be, that I loop through each text element, calculate "current deg + 30deg" and start the animation…
But it would be better if I could to this in a set() of elements. Unfortunately I will get the problem that every text element is rotated to 30deg…
Does anyone have an suggestion how I could do this job best?
There is indeed a simple method to accomplish this with a set. Instead of setting a rotation value, you can append a incremental rotation to the transform like this:
// assumes you have a variable named 'mySet'
mySet.animate( { transform: '...r30' }, 500, '<>' );
I believe this should do exactly what you intend. If not, feel free to hurl vegetables at me.
So I have been playing around with animations lately and I've come across the anchor point. I understand that the anchor point is (0.5, 0.5) by default, which is the middle of the view, and you can change it so that the anchor point is on one of the borders. My question is, how do I go on about this if I want my view to rotate around a specific point in the view's superview? Any help is greatly appreciated
Checkout this question how to rotate CALayer at one point
Also - although I guess you've probably already done this - have a read of Core Animation Programming Guide, Layer Geometry and Transforms.
Your question differs in that you want to specify a rotation point that is in your view's superview. To do that, you want to convert that superview point to the subview. You can do that as follows:
Take your superview bounds, e.g. (0, 0, 500, 500)
Take your subview frame, e.g. (50, 50, 100, 100)
Take your superview rotation point, e.g. (75, 75)
Convert that to a point relative to the subview as follows:
CGFloat subviewX = 75.0f - subview.frame.x;
CGFloat subviewX = 75.0f - subview.frame.y;
That gives the result expected (25.0f, 25.0f)
To rotate a CALayer with a CABasicAnimation, have a look at the selected answer for this question: CALayer with rotation animation.
I figured it out myself: I wanted the anchor point to be on the left screen border, so I did the following:
CGFloat subviewX = ((1/view.frame.size.width)*view.frame.origin.x) * (-1);
CGFloat subviewY = 0.5;
I'm using GDI+ in my application, and I need to use a rotated LinearGradientBrush to paint several rects in the exact same way. However, although I'm calling the same code to fill each rect, the results aren't what I expect. Here's the code to create the gradient fill, where rcDraw is the rect containing the area to paint for each rect. These coordinates are in the parent window's coordinates, so they are not identical for the 2 rects.
g_hbrLinear = new LinearGradientBrush( Rect( 0, rcDraw.top, 0, rcDraw.bottom - rcDraw.top ),
clrStart, clrEnd, (REAL) 80, FALSE );
What I see on screen looks like this (http://www.nnanime.com/bugs/LinGradBrush-rotate10.png). You can see that it's as if the fill from the first rect continues into the second one. What I really want is to have the 2 rects look identical. I think I can do that if I paint each rect separately using its own client coordinates, but for the purposes of my app, I need to use the parent window's coordinates.
I guess what I'm asking is, how does GDI+ calculate the "origin" of a fill? Is it always based on 0,0 in the coordinate system you use? Is there a way to shift it? I tried TranslateTransform, but it doesn't seem to shift the fill in a way that I find predictable or understandable.
The rect passed to the linear gradient brush determines the where the left and right colors will sit, and the gradient will be painted within this rectangle.
So, I think you need to create a brush for each rectangle you are painting, where the rectangle you are painting is also passed to the constructor for the linear gradient brush.
My experience with the "transform" of linear gradient brushes matches yours; I haven't been able to understand what it's supposed to do.
You can think of a brush in GDI+ as a function mapping world co-ordinates to a color. What the brush looks like at a given point does not change based on the shape being filled.
It does change with the transform of the Graphics object you're drawing on. So, if you don't want to change the brush, you could temporarily change the transform of the Graphics object so that the rectangle you're drawing has a specific, known size and position in world coordinates. The BeginContainer and EndContainer methods should make this easy.
(There is also the RenderingOrigin property but it only affects hatch brushes, which oddly are unaffected by world transforms.)