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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:22:09+00:00 2026-05-30T10:22:09+00:00

I’m using Python 2.7 and Pygame but any language answer will do. I’m trying

  • 0

I’m using Python 2.7 and Pygame but any language answer will do.

I’m trying to find a given point along the vector in the perimeter of a rectangle. I know the vector, and the center, width and height of the rectangle, but the center can be (0, 0) for the sake of simplicity

So for example I want to find the point in the perimeter of the rectangle following the vector of roughly (0.7, 0.7) of a rectangle that is 2 wide by 6 height.

What I’ve got working now does the work; but there’s has got to be a better, more elegant way: I’m taking the length of the wider value between height and width of the rectangle and using every number in between that and 0 and testing that against each of the 4 sides of the rectangle and see if it’s in it.

This is the code I use specific to my game, not generalized at all: http://pastebin.com/8Ai1iQeL

  • 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-30T10:22:11+00:00Added an answer on May 30, 2026 at 10:22 am

    I’d go about this by comparing the ratio of the components of your vector, which is also the slope of the line parallel to the vector, to the same quantity for the vector pointing from the rectangle’s center to its corner. That tells you whether the vector hits a horizontal or vertical side. After that, you can use a simple proportionality to find the intersection point.

    Suppose your vector is (x,y), and for the moment assume that both coordinates are positive. The slope is y/x and the equivalent quantity for the rectangle is h/w, using a coordinate system in which the rectangle’s center is at (0,0). Now, if y/x > h/w, your intersection point will be on the top edge, so you know its height is h/2. You can then compute the coordinates as (0.5*h*x/y,0.5*h). If y/x < h/w, the intersection point is on the right edge, and the coordinates are (0.5*w,0.5*w*y/x).

    To use this in practice, you’d want to actually do the comparison between y*w and x*h, both to avoid problems with division by zero and to avoid the relatively expensive division operator (not that that really makes much of a difference). Also, you can find the correct signs for the components of the intersection point by just using the signs of x and y. So in code, it’d look something like this:

    def intersect_perimeter(x, y, w, h):
        if abs(y*w) > abs(x*h):
            return (0.5*h*x/abs(y), 0.5*h*sign(y))
        else:
            return (0.5*w*sign(x), 0.5*w*y/abs(x))
    

    (untested). This will fail if x is zero and either y or w is zero, but in that case you have either a zero vector (and the problem is undefined) or a zero-width rectangle (again, the problem is undefined). So I wouldn’t bother with error checking for that case.

    If your rectangle is centered at a point other than (0,0), you just need to add the position vector representing the rectangle’s center to the result from that function.

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.