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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:30:52+00:00 2026-05-26T20:30:52+00:00

I am making program where circle is flying and bouncing of screen borders. I

  • 0

I am making program where circle is flying and bouncing of screen borders.

I made four lines for each border of screen

Circle is moving through invisible line which intersects with screen border line. The problem is that in this example i will always get t=1 , because it will intersect with last line it had intersected. So question is, how do i make “if” so that it will only intersect with one border line, not with two of them.

Here are the ifs i have written, but they don’t work as intended.

if (intersect(l2,n1)!=0){
   tk=intersect(l2,n1);
  }
  else if (intersect(l2,n2)!=0){
    tk=intersect(l2,n2);
  }
  else if (intersect(l2,n3)!=0){
    tk=intersect(l2,n3);
  }
  else if (intersect(l2,n4)!=0){
    tk=intersect(l2,n4);
  }

Here is the intersect function:

double intersect(Line l, Line n){
  double r1x=l.a.x, v1x=l.b.x-l.a.x;
  double r1y=l.a.y, v1y=l.b.y-l.a.y;
  double r2x=n.a.x, v2x=n.b.x-n.a.x;
  double r2y=n.a.y, v2y=n.b.y-n.a.y;
  double a1=v1x, a2=v1y, b1=-v2x, b2=-v2y;
  double c1=r2x-r1x, c2=r2y-r1y;
  double tl=(c1*b2-b1*c2)/(a1*b2-b1*a2);
  return tl;
}

Here is the function where it happens:

void draw_picture(Canvas & canvas) {

SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
  SDL_HWSURFACE | SDL_DOUBLEBUF );

PairXY a(200,400);
PairXY b(400, 0);
int o=20;
Line l(a,b);


PairXY c(0,0);
PairXY d(640, 0);
Line n1 (c,d);
draw_line(n1, canvas);

PairXY e(640, 0);
PairXY f(640 , 480);
Line n2 (e,f);
draw_line(n2, canvas);

PairXY g(0,0);
PairXY h(0, 480);
Line n3 (g,h);
draw_line(n3, canvas);

PairXY j(0,480);
PairXY k(640, 480);
Line n4 (j,k);
draw_line(n4, canvas);



Circle cir(a,o);
draw_circle(cir, canvas);


double tk;

for (int i=3;i--;i>0){
Line l2=l;
double t=0;


if (intersect(l2,n1)!=0){
 tk=intersect(l2,n1);
}
else if (intersect(l2,n2)!=0){
  tk=intersect(l2,n2);
}
else if (intersect(l2,n3)!=0){
  tk=intersect(l2,n3);
}
else if (intersect(l2,n4)!=0){
  tk=intersect(l2,n4);
}



 while (t<tk){

    l.a = l.a + (l.b - l.a) * t;
    Circle cir1(l.a,o);
    draw_circle(cir1, canvas);
    SDL_Flip(screen);
    SDL_Delay(2);
    draw_bcircle(cir1, canvas);
    t=t+0.0001;

  }

l2=orto_line(l2,l.a);

l=l2;
}
  • 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-26T20:30:52+00:00Added an answer on May 26, 2026 at 8:30 pm

    I guess you should find all the times your line intersects with borders like:

    tk1 = intersect(l2,n1);
    tk2 = intersect(l2,n2);
    tk3 = intersect(l2,n3);
    tk4 = intersect(l2,n4);
    

    And then find the minimum of tk’s which is greater than t:

    double min = (very big number);
    if (tk1 >= t && tk1 < min)
      min = tk1;
    if (tk2 >= t && tk2 < min)
      min = tk2;
    if (tk3 >= t && tk3 < min)
      min = tk3;
    if (tk4 >= t && tk4 < min)
      min = tk4;
    
    while (t < min)
    {
       ...
    }
    

    So you’ll find the time at which your circle will hit a border next.

    I strongly suggest to use arrays for n’s and tk’s.

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

Sidebar

Related Questions

I'm making a program that retrieves decently large amounts of data through a python
I'm making a program that takes a snapshot of the screen, and saves it
I'm making a program in vb6. i;d like to connect using a proxy through
I'm making a program which sends invoices to registered users as PDF attachment. Each
I'm making a program that creates a grid with a number of lines and
I'm making a program that will output lines to a text file. I don't
I am using the cool map making program DIY map and i want to
I'm making a program which the user build directories (not in windows, in my
I'm making a program that fits the wizard concept ideally; the user is walked
I noticed while making a program that a lot of my int type variables

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.