Reputation: 13063
I transformed some UIView with a CGAffineTransform. Now I want to know if it possible to get the smallest x point of the UIView when proving some y value in the UIView.
For example in the picture below, I want to know the smallest x value for a y value which is 0.75 the view's height.
someView.frame.origin.x provides only the x value for the top-left position, but I need something to change that position to get the smallest x value of a given y value.
Upvotes: 0
Views: 128
Reputation: 23485
I assume you can calculate the minimum X and its corresponding Y on the rotated square.. Then calculate the X2, Y2 as shown in the picture below once you know the angle the square has rotated and the length of its sides (then its Pythagorean theorem)..
I used let rotatedRect = CGRectApplyAffineTransform(rotationTransformation, square.frame)
to get the rotated rectangle's coordinates. Then I compared all 4 points to find the one with the smallest X value and the equation of the two lines of the square that go through that point (one has positive slope, the other has negative).
Once you have X1, Y1 and X2, Y2, X3, Y3 as shown below, you can calculate the interception of two lines by substitution or elimination as shown below..
To make it simple, I assumed the slope of the line passing through the square was 0 (horizontal-line).
To figure out which is the correct minimum X value, you compare if the X >= X1. If it is, that is the minimum that satisfies your requirements. See below for example..
Upvotes: 1
Reputation: 7714
as the UIView is always a rectangle, I suppose you want to track the minimum x for a given y just for rectangles.
SOLUTION
you can track the position of the 4 vertexes of the UIView always you rotate it. This tracking can be done by simply multiplying the points for a 2x2 rotation matrix.
| cosθ -senθ | | x | = | x * cosθ - y * senθ |
| senθ cosθ | | y | | x * senθ + y * cosθ |
Then, you can get the vertex with minimum x (lets call it V1).
then you need to get other 2 vertexes with the minimum x (called V2 and V3).
if V1.y > (given y)
write linear equation from V1 to vertex with minimum y between V2 and V3
else
write linear equation from V1 to vertex with maximum y between V2 and V3
in the end you will have f(x) = ax + b
replace the given y for f(x) and you get x = (y - b)/a
hope that helps :)
Upvotes: 1