sahaun's blog

By sahaun, history, 2 months ago, In English

Hello Codeforces!

As promised almost three years ago, I have finally found the time and scope to write about using CP techniques outside CP itself.

My first post is about a use case of binary search in video games. Unfortunately, the post is written in MDX (with videos and custom elements that Codeforces doesn't support), so I have to post a link if you want to read the whole post.

You can find it here: Continuous Collision Detection in Video Games using Binary Search

Here is a summary.


The Problem

In a standard game loop, objects "teleport" from position $$$(x, y)$$$ to $$$(x + \Delta x, y + \Delta y)$$$ in a single frame.

If a wall is thin enough, a fast-moving bullet can start on one side and end on the other in a single frame, completely skipping the collision check. This well-known bug is called tunneling.

w w2

To fix this, we need to check the swept volume — the continuous volume the object occupies during its movement from time $$$t_0$$$ to $$$t_1$$$.

e2

The problem is that we need to find the exact time of impact $$$t \in [t_0, t_1]$$$ where the object first touches the wall.

The Solution: Binary Search on Time

Let's define a function $$$f(t)$$$ such that:

  • $$$f(t) = 1$$$ if the object's swept volume from $$$t_0$$$ to $$$t$$$ intersects the obstacle.
  • $$$f(t) = 0$$$ otherwise.

Claim: If there is a collision in the swept volume during $$$[t_0, t]$$$, there will also be a collision in any later time interval $$$[t_0, t']$$$ where $$$t' \geq t$$$. This can be proven formally, but the intuition is simple.

Proof

Since the function is monotonic, we can Binary Search on the time $$$t$$$ to find the first moment of collision.

double find_collision_time(double t0, double t1, Object obstacle) {
  // Handle t0 separately - in case we are already colliding
  if (check_collision(t0, t0, obstacle)) return t0;

  // Handle (t0, t1]
  const double eps = clamp((t1 - t0) / this->speed, 1e-6, 1e-3);
  const int maxIterations = 1 + ceil(log2((t1 - t0) / eps));
  for (int i = 0; (i < maxIterations) && ((t1 - t0) > eps); ++i) {
    double mid = (t0 + t1) / 2.;
    if (check_collision(t0, mid, obstacle)) {
      t1 = mid;
    } else {
      t0 = mid;
    }
  }
  return t1;
}

The time complexity is $$$O(\log ((t_1 - t_0) / \epsilon))$$$ which becomes $$$O(\log S)$$$ when $$$\epsilon \approx 1 / S$$$.


Thanks for reading! Let me know if you use binary search for anything else.

Full text and comments »

  • Vote: I like it
  • +8
  • Vote: I do not like it

By sahaun, history, 5 months ago, In English

Advent of Code!

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university coursework, practice problems, a speed contest, or to challenge each other.

The first puzzles will unlock on December 1st at midnight EST (UTC-5).

Some changes starting this year from the previous year(s):

Full text and comments »

  • Vote: I like it
  • +31
  • Vote: I do not like it

By sahaun, history, 17 months ago, In English

Advent of Code 2024!

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university coursework, practice problems, a speed contest, or to challenge each other.

The first puzzles will unlock on December 1st at midnight EST (UTC-5).

Full text and comments »

Tags aoc
  • Vote: I like it
  • +21
  • Vote: I do not like it

By sahaun, history, 23 months ago, In English

This is not a problem from any online judge, but for a project I am working on.

There is an ellipse and a list of points. The task is to score the list with a number between $$$0$$$ and $$$1$$$. A score of $$$1$$$ means that the list of points forms a perfect ellipse. A list forms a perfect ellipse when:

  1. All the points in the list lie on the circumference of the ellipse.

  2. The points are evenly spaced (distributed evenly) across the circumference.

Formal-ish statement

The expected time complexity is less than $$$O(N^3)$$$, not strictly necessary. $$$N$$$ being the number of points in the list.

If you want some inspiration, I have attempted the same problem but checked matching with circles and polygons. I am explaining how I did it for the circle.

Circle

How it works
Code

You can see how the same method will work for regular polygons. For irregular (closed) polygons, I have a more sophisticated but similar way. The closed polygon method can work for ellipses (since an ellipse is actually a closed polygon with many small lines), but I am looking for a more natural solution for ellipses.

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it

By sahaun, history, 2 years ago, In English

Advent of Code 2023!

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university coursework, practice problems, a speed contest, or to challenge each other.

The first puzzles will unlock on December 1st at midnight EST (UTC-5).

Full text and comments »

Tags aoc
  • Vote: I like it
  • +39
  • Vote: I do not like it

By sahaun, history, 3 years ago, In English

Hello!

We would like to invite you to participate in the Replay of AUST CSE Carnival 1.0 Programming Contest. It will be held on February 2, 2023 at 7:00 PM GMT+6. You will be given 11 problems and 4 hours to solve them.

AUST CSE Carnival 1.0 was a week-long event held at Ahsanullah University of Science and Technology (AUST). The event was organized by AUST CSE Society.

Participants with rating less than 1900 might find the problems interesting and/or educational.

Good luck!

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it

By sahaun, history, 4 years ago, In English

I submitted exactly the same code for 86D - Powerful array on different platforms. Here are the results:

There is a small increase in memory when using 64-bit, but the time difference is pretty huge!

Can anybody explain why?

Full text and comments »

  • Vote: I like it
  • +24
  • Vote: I do not like it

By sahaun, history, 4 years ago, In English

The time limit per test 840D - Destiny is 2.5 seconds. But 157972351 passed in 3525 ms.

What's going on? ಠ_ಠ

Full text and comments »

  • Vote: I like it
  • +10
  • Vote: I do not like it