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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:55:43+00:00 2026-06-06T09:55:43+00:00

I have line that is defined as two points. start = (xs,ys) end =

  • 0

I have line that is defined as two points.
start = (xs,ys)
end = (xe, ye)

Drawing function that I’m using Only accepts lines that are fully in screen coordinates.
Screen size is (xSize, ySize).

Top left corner is (0,0). Bottom right corner is (xSize, ySize).

Some other funcions gives me line that that is defined for example as start(-50, -15) end(5000, 200). So it’s ends are outside of screen size.

In C++

struct Vec2
{
 int x, y
};

Vec2 start, end //This is all little bit pseudo code
Vec2 screenSize;//You can access coordinates like start.x end.y

How can I calculate new start and endt that is at the screen edge, not outside screen.
I know how to do it on paper. But I can’t transfer it to c++.
On paper I’m sershing for point that belongs to edge and line. But it is to much calculations for c++.

Can you help?

  • 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-06-06T09:55:45+00:00Added an answer on June 6, 2026 at 9:55 am

    There are many line clipping algorithms like:

    • Cohen–Sutherland wikipedia page with implementation
    • Liang–Barsky wikipedia page
    • Nicholl–Lee–Nicholl (NLN)
      and many more. see Line Clipping on wikipedia

    [EDIT1]
    See below figure:
    enter image description here

    there are 3 kinds of start point:

    1. sx > 0 and sy < 0 (red line)
    2. sx < 0 and sy > 0 (yellow line)
    3. sx < 0 and sy < 0 (green and violet lines)

    In situations 1 and 2 simply find Xintersect and Yintersect respectively and choose them as new start point.
    As you can see, there are 2 kinds of lines in situation 3. In this situation find Xintersect and Yintersect and choose the intersect point near the end point which is the point that has minimum distance to endPoint.

    min(distance(Xintersect, endPoint), distance(Yintersect, endPoint))

    [EDIT2]

    // Liang-Barsky function by Daniel White @ http://www.skytopia.com/project/articles/compsci/clipping.html
    // This function inputs 8 numbers, and outputs 4 new numbers (plus a boolean value to say whether the clipped line is drawn at all).
    //
    bool LiangBarsky (double edgeLeft, double edgeRight, double edgeBottom, double edgeTop,   // Define the x/y clipping values for the border.
                      double x0src, double y0src, double x1src, double y1src,                 // Define the start and end points of the line.
                      double &x0clip, double &y0clip, double &x1clip, double &y1clip)         // The output values, so declare these outside.
    {
    
        double t0 = 0.0;    double t1 = 1.0;
        double xdelta = x1src-x0src;
        double ydelta = y1src-y0src;
        double p,q,r;
    
        for(int edge=0; edge<4; edge++) {   // Traverse through left, right, bottom, top edges.
            if (edge==0) {  p = -xdelta;    q = -(edgeLeft-x0src);  }
            if (edge==1) {  p = xdelta;     q =  (edgeRight-x0src); }
            if (edge==2) {  p = -ydelta;    q = -(edgeBottom-y0src);}
            if (edge==3) {  p = ydelta;     q =  (edgeTop-y0src);   }   
            r = q/p;
            if(p==0 && q<0) return false;   // Don't draw line at all. (parallel line outside)
    
            if(p<0) {
                if(r>t1) return false;         // Don't draw line at all.
                else if(r>t0) t0=r;            // Line is clipped!
            } else if(p>0) {
                if(r<t0) return false;      // Don't draw line at all.
                else if(r<t1) t1=r;         // Line is clipped!
            }
        }
    
        x0clip = x0src + t0*xdelta;
        y0clip = y0src + t0*ydelta;
        x1clip = x0src + t1*xdelta;
        y1clip = y0src + t1*ydelta;
    
        return true;        // (clipped) line is drawn
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lines: Line1 and Line2. Each line is defined by two points
I have a set of lines that define a W shape. On each line
I have a line of code that counts all attribute names that end in
In my Rails App, I have a line that says: <% if user_signed_in? &&
I have a line chart that is updated every so and so seconds, similar
I have this line below that shows a link to go the next page
I have a command line that I'm trying to modify to remove some of
i have a jQuery line that execute 2 animations, what i want is to
I have a command line program that prints out a report such as: I
I have a line of code that gets the following error when run through

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.