Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7599893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:46:21+00:00 2026-05-30T22:46:21+00:00

In iOS, I’m trying to determine the point on a rectangle intersected by an

  • 0

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);    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:46:23+00:00Added an answer on May 30, 2026 at 10:46 pm

    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:

    diagram of rectangle with overlaid line

    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:

    edgepoint screen shot

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an iOS application, and trying to zip the file I have
I'm trying to drag a CALayer in an iOS app. As soon as I
How do I create a png image from text in iOS?
In iOS,I would like to include both a delegate for the flipsideView (from a
iOS beginner here. I'm trying to build the Facebook Demo App but I keep
The iOS GUI guidelines say of a UITabBar that ... and should be accessible
An iOS app which consumes content from back end server. The content actually does
I'm writing an iOS app with a table view inside a tab view. In
Possible Duplicate: iOS - Detecting whether or not device support phone calls? I'm writing
In Objective-C for iOS, how would I remove the last character of a string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.