久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在與中心點(diǎn)成給定角度的直線相交的 UIView 矩形上

Find the CGPoint on a UIView rectangle intersected by a straight line at a given angle from the center point(在與中心點(diǎn)成給定角度的直線相交的 UIView 矩形上找到 CGPoint)
本文介紹了在與中心點(diǎn)成給定角度的直線相交的 UIView 矩形上找到 CGPoint的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在 iOS 中,我試圖確定矩形上的點(diǎn),該點(diǎn)與從中心點(diǎn)到矩形周邊的假想線以預(yù)定角度相交.

假設(shè)我知道中心點(diǎn)、矩形的大小和角度(從東方的 0 度開始,逆時(shí)針經(jīng)過北方的 90 度、西方的 180 度和南方的 270 度,再到東方的 360 度).我需要知道相交點(diǎn)的坐標(biāo).

看起來像這樣:

In iOS, I'm trying to determine the point on a rectangle intersected by an imaginary line from the center point to the perimeter of the rectangle at a predetermined angle.

Say that I know the center point, the size of the rectangle, and the angle (starting from 0 degrees for East and going counterclockwise through 90 for North and 180 for West and 270 for South to 360 degrees for East again). I need to know the coordinates of the intersecting point.

The somewhat confusing (to me) mathematical but presumably accurate answer at Finding points on a rectangle at a given angle led me to try the following code, but it doesn't work properly. This question is similar to that one, but I'm looking for a corrected Objective-C / iOS method rather than a general mathematical response.

I think a part of the code problem has to do with using the single 0 to 360 degree angle (in radians with no possibility of a negative number) input, but there are likely to be other problems. The code below mostly uses notation defined in the answer from belisarius, including my attempt to calculate intersecting points for each of the four regions defined there.

This code is in my UIImageView subclass:

- (CGPoint) startingPointGivenAngleInDegrees:(double)angle {
    double angleInRads = angle/180.0*M_PI;
    float height = self.frame.size.height;
    float width = self.frame.size.width;
    float x0 = self.center.x;
    float y0 = self.center.y;
    // region 1 
    if (angleInRads >= -atan2(height, width) && angleInRads <= atan2(height, width)) {
        return CGPointMake(x0 + width/2, y0 + width/2 * tan(angleInRads));
    }
    // region 2
    if (angleInRads >= atan2(height, width) && angleInRads <= M_PI - atan2(height, width)) {
        return CGPointMake(x0 + height / (2*tan(angleInRads)),y0+height/2);
    }
    // region 3
    if (angleInRads >= M_PI - atan2(height, width) && angleInRads <= M_PI + atan2(height, width)) {
        return CGPointMake(x0 - width/2, y0 + width/2 * tan(angleInRads));
    }
    // region 4
    return CGPointMake(x0 + height / (2*tan(angleInRads)),y0-height/2);    
}

解決方案

Instead of debugging your code, I'll just explain how I would do this.

First, a few terms. Let's define xRadius as half the width of the frame, and yRadius as half the height of the frame.

Now consider the four edges of the frame, and extend them as infinite lines. On top of those four lines, lay a line that passes through the center of the frame at your specified angle:

Let's say the frame is centered at the origin - the center of the frame is at coordinates (0,0). We can easily compute where the diagonal line intersects the right edge of the frame: the coordinates are (xRadius, xRadius * tan(angle)). And we can easily compute where the diagonal line intersects the top edge of the frame: the coordinates are (-yRadius / tan(angle), -yRadius).

(Why do we negate the coordinates for the top-edge intersection? Because the UIView coordinate system is flipped from the normal mathematical coordinate system. In math, y coordinates increase towards the top of the page. In a UIView, y coordinates increase toward the bottom of the view.)

So we can simply compute the intersection of the line with the right edge of the frame. If that intersection is outside of the frame, then we know the line must intersect the top edge before it intersects the right edge. How do we tell if the right-edge intersection is out of bounds? If its y coordinate (xRadius * tan(angle)) is greater than yRadius (or less than -yRadius), it's out of bounds.

So to put it all together in a method, we start by computing xRadius and yRadius:

- (CGPoint)radialIntersectionWithConstrainedRadians:(CGFloat)radians {
    // This method requires 0 <= radians < 2 * π.

    CGRect frame = self.frame;
    CGFloat xRadius = frame.size.width / 2;
    CGFloat yRadius = frame.size.height / 2;

Then we compute the y coordinate of the intersection with the right edge:

    CGPoint pointRelativeToCenter;
    CGFloat tangent = tanf(radians);
    CGFloat y = xRadius * tangent;

We check whether the intersection is in the frame:

    if (fabsf(y) <= yRadius) {

Once we know it's in the frame, we have to figure out whether we want the intersection with the right edge or the left edge. If the angle is less than π/2 (90°) or greater than 3π/2 (270°), we want the right edge. Otherwise we want the left edge.

        if (radians < (CGFloat)M_PI_2 || radians > (CGFloat)(M_PI + M_PI_2)) {
            pointRelativeToCenter = CGPointMake(xRadius, y);
        } else {
            pointRelativeToCenter = CGPointMake(-xRadius, -y);
        }

If the y coordinate of the right edge intersection ?was* out-of-bounds, we compute the x coordinate of the intersection with the bottom edge.

    } else {
        CGFloat x = yRadius / tangent;

Next we figure out whether we want the top edge or the bottom edge. If the angle is less than π (180°), we want the bottom edge. Otherwise, we want the top edge.

        if (radians < (CGFloat)M_PI) {
            pointRelativeToCenter = CGPointMake(x, yRadius);
        } else {
            pointRelativeToCenter = CGPointMake(-x, -yRadius);
        }
    }

Finally, we offset the computed point by the actual center of the frame and return it.

    return CGPointMake(pointRelativeToCenter.x + CGRectGetMidX(frame),
        pointRelativeToCenter.y + CGRectGetMidY(frame));
}

Test project here: https://github.com/mayoff/stackoverflow-radial-intersection

Looks like this:

這篇關(guān)于在與中心點(diǎn)成給定角度的直線相交的 UIView 矩形上找到 CGPoint的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw stars using Quartz Core?(如何使用 Quartz Core 繪制星星?)
Why does giving addArcWithCenter a startAngle of 0 degrees make it start at 90 degrees?(為什么給 addArcWithCenter 一個(gè) 0 度的 startAngle 使它從 90 度開始?)
No such acos function exists(不存在這樣的 acos 函數(shù))
Which is the best way to estimate measure of photographed things?(哪種方法是估計(jì)拍攝物體尺寸的最佳方法?)
How to convert an Int to a Character in Swift(如何在 Swift 中將 Int 轉(zhuǎn)換為字符)
How to create a hex color string UIColor initializer in Swift?(如何在 Swift 中創(chuàng)建十六進(jìn)制顏色字符串 UIColor 初始化程序?)
主站蜘蛛池模板: 国产中文字幕在线 | 免费黄色片视频 | 黄色一级免费视频 | 啪啪综合网 | 一区二区三区在线看 | 一区二区福利 | 亚洲一区二区在线播放 | 久草视频免费在线 | 香蕉视频在线免费看 | 久久精品欧美一区二区 | 免费网站观看www在线观看 | 欧美福利在线 | 亚洲精品视频免费在线观看 | 手机看片福利视频 | 国产在线一区二区 | 久久精品欧美一区二区 | 色综合一区 | 一本到av | 国产午夜精品一区二区三区四区 | 日韩二区三区 | 在线免费观看黄色片 | 亚洲www啪成人一区二区麻豆 | 国产福利视频在线 | 99在线免费视频 | 亚洲成人精品 | 免费成人黄色 | 狠狠网 | 五月婷婷色综合 | 91亚洲国产成人久久精品网站 | 国产精品手机在线 | 国产精品国产三级国产 | 97国产精品| 日韩在线一区二区 | 超碰国产在线 | 亚洲成人国产 | 五月天婷婷影院 | 成人精品免费视频 | 四虎成人影视 | 高hnp失禁3p小公主 | 青草av在线 | 亚洲精品www久久久久久广东 |