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 7700627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:45:10+00:00 2026-05-31T22:45:10+00:00

I have a vector represented by the slope m. Then there is rectangle (assume

  • 0

I have a vector represented by the slope m. Then there is rectangle (assume axis aligned), which is represented by top-left and bottom-right corner.
Of course, there may be many lines with slope m and intersecting the given rectangle. The problem is to find out the line whose length of line intercept inside the rectangle is maximum among all such lines. i.e., if the line intersects rectangle at P1 and P2, then the problem is to find the equation of line for which length of P1P2 is maximum.

I proceeded like this. Let the line is: y = m*x + c. Then find out the intersection with each side of rectangle and finding out the maxima for distance function between each pair of points. But it will only give me the length of line segment and there seem to be many corner cases to handle.

Could anyone please suggest a better way to do this.

Thanks in advance.

  • 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-31T22:45:11+00:00Added an answer on May 31, 2026 at 10:45 pm

    Think about it like you want to scale a triangle to fit inside the rectangle. Consider a triangle with base width 1.

    we know that dy/dx = m, so the height of the triangle with the same slope is m.

    Now take your rectangle and work out the maximum scale that fits the triangle inside the rectangle. For positive m:

    min(rectWidth, rectHeight/m)
    

    This is the scale we have to use to fit the triangle inside the rectangle. Now it’s easy with pytagoras’ theorem to get the length of the intersection

    scale = min(rectWidth, abs(m/rectHeight)) // m could be negative so we take abs
    length = sqrt(scale*scale + scale*m*scale*m)
    

    Note that there are potentially many possible solutions for a line of this length, be we are certain that for positive m (triangle pointing up) it will fit in the bottom left of the rectangle, and for negative m (triangle pointing down) it will fit in the top right.

    So lets say your rectangle is made up of 4 values minX, minY, maxX, maxY

    if m is positive, the line intersects at minX minY, and exits the rectangle at

    [minX + scale, minY + (m * scale)]
    

    and if m is negative the line intersects at maxX maxY and exits the rectangle at

    [maX - scale, maxY + (m * scale)] (noting that m is negative)
    

    All you need to handle now is slope of 0, which is trivial.

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

Sidebar

Related Questions

Right now I have vector3 values represented as lists. is there a way to
I have a table represented by a std::vector where every element is a boost::fusion::vector
I have an elevation map represented by a 2D array of floats. There are
Assume I have two vectors represented by two arrays of type double , each
I have a Vector class which represents a 2D vector. It is templated to
I have implemented printf for vector type. The values in vector type are represented
in clojure i have vector [myfn1 myfn2 myfn3] how can i call functions named
I have a vector that I want to insert into a set . This
I have a vector-like class that contains an array of objects of type T
I have a vector with 1000 nodes if(count + 1 > m_listItems.capacity()) m_listItems.reserve(count +

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.