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

  • Home
  • SEARCH
  • 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 44239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:37:16+00:00 2026-05-10T15:37:16+00:00

I have two points (a line segment) and a rectangle. I would like to

  • 0

I have two points (a line segment) and a rectangle. I would like to know how to calculate if the line segment intersects the rectangle.

  • 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. 2026-05-10T15:37:16+00:00Added an answer on May 10, 2026 at 3:37 pm

    From my ‘Geometry’ class:

    public struct Line {     public static Line Empty;      private PointF p1;     private PointF p2;      public Line(PointF p1, PointF p2)     {         this.p1 = p1;         this.p2 = p2;     }      public PointF P1     {         get { return p1; }         set { p1 = value; }     }      public PointF P2     {         get { return p2; }         set { p2 = value; }     }      public float X1     {         get { return p1.X; }         set { p1.X = value; }     }      public float X2     {         get { return p2.X; }         set { p2.X = value; }     }      public float Y1     {         get { return p1.Y; }         set { p1.Y = value; }     }      public float Y2     {         get { return p2.Y; }         set { p2.Y = value; }     } }  public struct Polygon: IEnumerable<PointF> {     private PointF[] points;      public Polygon(PointF[] points)     {         this.points = points;     }      public PointF[] Points     {         get { return points; }         set { points = value; }     }      public int Length     {         get { return points.Length; }     }      public PointF this[int index]     {         get { return points[index]; }         set { points[index] = value; }     }      public static implicit operator PointF[](Polygon polygon)     {         return polygon.points;     }      public static implicit operator Polygon(PointF[] points)     {         return new Polygon(points);     }      IEnumerator<PointF> IEnumerable<PointF>.GetEnumerator()     {         return (IEnumerator<PointF>)points.GetEnumerator();     }      public IEnumerator GetEnumerator()     {         return points.GetEnumerator();     } }  public enum Intersection {     None,     Tangent,     Intersection,     Containment }  public static class Geometry {      public static Intersection IntersectionOf(Line line, Polygon polygon)     {         if (polygon.Length == 0)         {             return Intersection.None;         }         if (polygon.Length == 1)         {             return IntersectionOf(polygon[0], line);         }         bool tangent = false;         for (int index = 0; index < polygon.Length; index++)         {             int index2 = (index + 1)%polygon.Length;             Intersection intersection = IntersectionOf(line, new Line(polygon[index], polygon[index2]));             if (intersection == Intersection.Intersection)             {                 return intersection;             }             if (intersection == Intersection.Tangent)             {                 tangent = true;             }         }         return tangent ? Intersection.Tangent : IntersectionOf(line.P1, polygon);     }      public static Intersection IntersectionOf(PointF point, Polygon polygon)     {         switch (polygon.Length)         {             case 0:                 return Intersection.None;             case 1:                 if (polygon[0].X == point.X && polygon[0].Y == point.Y)                 {                     return Intersection.Tangent;                 }                 else                 {                     return Intersection.None;                 }             case 2:                 return IntersectionOf(point, new Line(polygon[0], polygon[1]));         }          int counter = 0;         int i;         PointF p1;         int n = polygon.Length;         p1 = polygon[0];         if (point == p1)         {             return Intersection.Tangent;         }          for (i = 1; i <= n; i++)         {             PointF p2 = polygon[i % n];             if (point == p2)             {                 return Intersection.Tangent;             }             if (point.Y > Math.Min(p1.Y, p2.Y))             {                 if (point.Y <= Math.Max(p1.Y, p2.Y))                 {                     if (point.X <= Math.Max(p1.X, p2.X))                     {                         if (p1.Y != p2.Y)                         {                             double xinters = (point.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;                             if (p1.X == p2.X || point.X <= xinters)                                 counter++;                         }                     }                 }             }             p1 = p2;         }          return (counter % 2 == 1) ? Intersection.Containment : Intersection.None;     }      public static Intersection IntersectionOf(PointF point, Line line)     {         float bottomY = Math.Min(line.Y1, line.Y2);         float topY = Math.Max(line.Y1, line.Y2);         bool heightIsRight = point.Y >= bottomY &&                              point.Y <= topY;         //Vertical line, slope is divideByZero error!         if (line.X1 == line.X2)         {             if (point.X == line.X1 && heightIsRight)             {                 return Intersection.Tangent;             }             else             {                 return Intersection.None;             }         }         float slope = (line.X2 - line.X1)/(line.Y2 - line.Y1);         bool onLine = (line.Y1 - point.Y) == (slope*(line.X1 - point.X));         if (onLine && heightIsRight)         {             return Intersection.Tangent;         }         else         {             return Intersection.None;         }     }  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 51k
  • Answers 51k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The first plan doesn't use index on shows. If you… May 11, 2026 at 6:26 am
  • added an answer You can create a subclass of ViewController and add three… May 11, 2026 at 6:26 am
  • added an answer HttpContext.Application['MyValue'] = test May 11, 2026 at 6:26 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.