I have two UIViews and I need to draw a rectangle (or) get the frame of the smallest rectangle outside both the UIViews. How can I get this?
You can use:
CGRect smallestRectangle = CGRectUnion(view1.frame, view2.frame);
According to the docs, this function
Returns the smallest rectangle that contains the two source rectangles.
here are some steps that should work to find the rectangle you want
find the left most origin.x... = new origin.x
find the top most origin.y... = new origin.y
find the largest (origin.x + size.width)... = new size.width
find the largest (origin.y + size.height)... = new size.height
Related
I want to draw a line perpendicular to an arc at a particular point. I'm using path.arcTo to draw the arc. I have to say, I already managed to do something similar, by drawing the arc, and then increase the radius and tell flutter to draw another arc, but with an invalid sweepAngle, in that way flutter draws the perpendicular line I want. However, this at the beginning worked, but later on gave me some bugs. I'm sharing with you the initial code that I'm using. I'd like to know if there is a specific method or way to achieve what the first image shows, after drawing the arc:
Offset center = Offset(screenSize.width / 2, screenSize.height / 2);
double degreesToRadians(double degrees) => degrees * (math.pi / 180);
path.arcTo(
Rect.fromCircle(center: center, radius: 150),
degreesToRadians(180),
degreesToRadians(135),
true);
I'm working directly with degrees. Therefore the function degreesToRadians.
My current result is just this:
Thanks in advance.
So, what I did was to declare final outerRadiusPoint (the point where I wanted to take my line to go to) by establishing a sizeToIncrease for the radius, obtaining cosine of startingAngle and sine of startingAngle, adding center.dx and center.dy respectively and multiplying each by the size we want our new radius to be.
After that, we draw our arc with path.arcTo. Then we obtain the offset of the end of the arc with PathMetrics and PathMetric. With that we are now able to move the path to that position and start drawing again to the Offset of outerRadiusPoint.
final outerRadiusPoint = new Point(
(sizeForRadius + sizeToIncrease) * cos(degreesToRadians(startingAngle)) + center.dx,
(sizeForRadius + sizeToIncrease) * sin(degreesToRadians(startingAngle)) + center.dy);
path.arcTo(
Rect.fromCircle(center: center, radius: sizeForRadius),
degreesToRadians(initialPoint),
degreesToRadians(buttonArcLength),
true);
PathMetrics pms = path.computeMetrics();
PathMetric pm = pms.last;
Offset pmoffset = pm.getTangentForOffset(pm.length).position;
path.lineTo(outerRadiusPoint.x, outerRadiusPoint.y);
path.moveTo(pmoffset.dx, pmoffset.dy);
I've managed to get a cube (SCNNode) placed on a surface where the camera is pointed, however I am finding it very difficult to do the simple (?) task of also placing text in the same position.
I've created the SCNText and subsequent SCNNode, however when I add it to the rootNode the text always seems to be added above my head and off the camera to the right (which tells me thats the global origin point).
Even when I use the exact same values of position I used for the the cube, the SCNText node still gets placed above my head in the same spot.
Apologies if this is a basic question, I've never worked in SceneKit before.
The coordinate center for an SCNGeometry is its center point. But when you are creating a SCNText the center point is somewhere in the bottom left corner:
You need to center the text first. This can be done by checking the bounding box of the node containing your text and setting a pivot transform to change the texts center to its actual center:
func center(node: SCNNode) {
let (min, max) = node.boundingBox
let dx = min.x + 0.5 * (max.x - min.x)
let dy = min.y + 0.5 * (max.y - min.y)
let dz = min.z + 0.5 * (max.z - min.z)
node.pivot = SCNMatrix4MakeTranslation(dx, dy, dz)
}
Edit:
Also note this answer that explains some additional pitfalls:
A text with 16 pts font size is 16 SceneKit units tall. But in ARKit 1 SceneKit units = 1 meter!
The question I am about to ask could be somewhat challenging. I will try to make this as clear and cohesive as possible.
I am currently making a game, in which I have a 'laser ring,' as shown here:
This laser ring, when prompted, will fire a 'grappling hook' which is simply the image shown below. This image's frame.width property is adjusted to make it fire (lengthen) and retract (shorten.) It starts at a width of 0, and as the frames progress, it lengthens until reaching the desired point.
This grappling hook, when fired, should line up with the ring so that they appear to be one item. Refer to the image below for clarity:
*Note that the grappling hook's width changes almost every frame, so a constant width cannot be assumed.
Something else to note is that, for reasons that are difficult to explain, I can only access the frame.center property of the grappling hook and not the frame.origin property.
So, my question to you all is this: How can I, accessing only the frame.center.x and frame.center.y properties of the grappling hook, place it around the laser ring in such a way that it appears to be seamlessly extending from the ring as shown in the above image - presumably calculated based on the angle and width of the grappling hook at any given frame?
Any help is immensely appreciated.
OK, I've done this exact same thing in my own app.
The trick I did to make it easier was to have a function to calculate the "unitVector" of the line.
i.e. the vector change in the line based on a line length of 1.
It just uses simple pythagorus...
- (CGSize)unitVectorFromPoint:(CGPoint)start toPoint:(CGPoint)end
{
//distance between start an end
float dX = end.x - start.x;
float dY = end.y - start.y;
float distance = sqrtf(dX * dX + dY * dY); // simple pythagorus
//unit vector is just the difference divided by the distance
CGSize unitVector = CGSizeMake(dX/distance, dY/distance);
return unitVector;
}
Note... it doesn't matter which way round the start and end are as squaring the numbers will only give positive values.
Now you can use this vector to get to any point along the line between the two points (centre of the circle and target).
So, the start of the line is ...
CGPoint center = // center of circle
CGPoint target = // target
float radius = //radius of circle
float dX = center.x - target.x;
float dY = center.y - target.y;
float distance = sqrtf(dX * dX + dY * dY);
CGSize unitVector = [self unitVectorFromPoint:center toPoint:target];
CGPoint startOfLaser = CGPointMake(center.x + unitVector.x * radius, center.y + unitVector.y * radius).
CGPoint midPointOfLaser = CGPointMake(center.x + unitVecotr.x * distance * 0.5, center.y + unitVector.y * distance * 0.5);
This just multiplies the unit vector by how far you want to go (radius) to get to the point on the line at that distance.
Hope this helps :D
If you want the mid point between the two points then you just need to change "radius" to be the distance that you want to calculate and it will give you the mid point. (and so on).
I use open source, iCarousel in my application to bring the carousel control. The carousel type which I use is iCarouselTypeRotary and the images are arranged linearly in this type. But, I need the images to bring like the carousel in the attached images. What should I do to make my carousel little tilted to the top view as the style in the below images? Kindly help. Thanks in advance.
You can implement 3D tilt manually:
In iCarousel.m: 574
return CATransform3DTranslate(transform, radius * sin(angle), 0.0f, radius * cos(angle) - radius);
change to:
float tilt = MAX_TILT_VALUE * cos(angle); // greater angle means greater vertical offset
return CATransform3DTranslate(transform, radius * sin(angle), tilt, radius * cos(angle) - radius);
To make the code clear and reusable, implement tilt offset as option (similar to iCarouselOptionArc).
PS: If you want perspective scaling, you will need to add scale transform that depends on cos(angle) similarly to tilt.
Check by using the style:iCarouselTypeWheel use the horizontal wheel set the radius of the wheel as you want. I have done this in vertical wheel type. But i think it should the appearance as above using horizontal wheel type.
iPhone SDK and Objective-C
Goal:
I'm trying to calculate the 'x' and 'y' coordinates of 2 circles. I have the inner circle dimensions and want to calculate what the 'x' and 'y' coordinates of the larger outer circle circumference would be to match the same width (distance) along the edge of the larger circle as it does with the inner circle.
In the end, I just need to figure out what the edge x/y points would be for the large circles edge. So that it matches the same as the inner smaller circle. If the width is 10 high on the inner circle, I need to know the x/y points to make it 10 high to the larger circle. To make a rectangle that will extend. Perpendicular lines.
Example:
I'm using the following to calculate the first 2 sets of x/y for the arc on the inner circle to plot points:
- (CGPoint)coordinatePoints:(CGFloat)radius angleDegrees:(CGFloat)degrees xAxis:(CGFloat)x yAxis:(CGFloat)y {
CGFloat pointX = (CGFloat) ((radius * cos((degrees * M_PI) / 180.0f)) + x);
CGFloat pointY = (CGFloat) ((radius * sin((degrees * M_PI) / 180.0f)) + y);
CGPoint points = CGPointMake(pointX, pointY);
return points;
}
I call it for the first 2 positions on the inner circle. I need to figure out how to make it have the distance on the outer circle as well.
CGPoint innerPoints1 = [self coordinatePoints:innerRadius angleDegrees:startingPoint xAxis:x yAxis:y];
CGPoint innerPoints2 = [self coordinatePoints:innerRadius angleDegrees:endingPoint xAxis:x yAxis:y];
If the inner circle radius is 200, and the outer circle radius is 500, I want it to still be the same thickness from the inner circle to the larger outer circle when I plot the points.
// I have these calculated.
CGContextMoveToPoint(context, innerPoints1.x, innerPoints1.y);
CGContextAddLineToPoint(context, innerPoints2.x, innerPoints2.y);
// I need to find the solution for making innerPoints3 and innerPoints4 correctly.
CGContextAddLineToPoint(context, innerPoints3.x, innerPoints3.y);
CGContextAddLineToPoint(context, innerPoints4.x, innerPoints4.y);
I have the coordinates for the inner circle lines for spaced out x/y points. I need to find the proper way to get the same width plotted for the larger circle locations. Circle sizes will always change. Lengths of the lines will be dynamic. As I'm trying to create a polygon, I need to find 2 coordinates on the larger circles, for each segment.
Any help with this would be greatly appreciated.
Information graphics: a comprehensive illustrated reference
Page 74: In the section "Circular Column Graph", my end goal is to be able to produce the same result as displayed in the 3 images.
If the spokes are not too thick, then the arc length is a good approximation of the spoke width:
So first you construct your 2 inner points, with 2 angles (a1 and a2) centered around a main spoke angle (a).
Then you calculate the distance D between these points (or you approximate it by R1*(a2-a1))
Then you take the points on the outer circle with angle values centered around the same main spoke angle: a-0.5*D/R2 and a+0.5*D/R2. These points will be D apart (measured on the arc)