<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luís Fernando Vendrame</title>
    <description>The latest articles on DEV Community by Luís Fernando Vendrame (@lvendrame).</description>
    <link>https://dev.to/lvendrame</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F736632%2Fb6a01ac6-278f-48d7-b2a0-426aa33c68bd.jpeg</url>
      <title>DEV Community: Luís Fernando Vendrame</title>
      <link>https://dev.to/lvendrame</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lvendrame"/>
    <language>en</language>
    <item>
      <title>Building a File-Based Mock Server in Rust: A Learning Journey</title>
      <dc:creator>Luís Fernando Vendrame</dc:creator>
      <pubDate>Thu, 24 Jul 2025 23:32:55 +0000</pubDate>
      <link>https://dev.to/lvendrame/building-a-file-based-mock-server-in-rust-a-learning-journey-4lc3</link>
      <guid>https://dev.to/lvendrame/building-a-file-based-mock-server-in-rust-a-learning-journey-4lc3</guid>
      <description>&lt;p&gt;Every developer has a folder of "side projects." Some are ambitious, world-changing ideas, while others are born from a simple, immediate need. My latest project, &lt;strong&gt;rs-mock-server&lt;/strong&gt;, falls firmly into the second category. I needed a simple mock server for another project, one that didn't require complex configuration files or a steep learning curve. I also wanted to sharpen my Rust skills. Why not kill two birds with one stone?&lt;/p&gt;

&lt;p&gt;This journey turned out to be more than just building a tool; it was a deep dive into the heart of Rust's most powerful (and sometimes feared) features.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem: Simple Mocks, No Fuss
&lt;/h3&gt;

&lt;p&gt;For front-end development or testing microservices, you often need a fake API that returns predictable data. I wanted a server that worked directly from the file system. If I need a &lt;code&gt;/users/1&lt;/code&gt; endpoint, I should just have to create a &lt;code&gt;/users/1&lt;/code&gt; file. The folder structure itself should be the API contract.&lt;/p&gt;

&lt;p&gt;This idea became the core concept for &lt;strong&gt;rs-mock-server&lt;/strong&gt;: a zero-configuration mock server that maps directories and files directly to API routes.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Learning Curve: More Than Just a Project
&lt;/h3&gt;

&lt;p&gt;Building this server was the perfect excuse to get my hands dirty and truly grapple with Rust's core concepts. I'll be the first to admit it's a first version with plenty of "horrible" code, but that's a natural part of the learning process! The initial goal was to make it work; now, the journey of improving and refactoring it begins.&lt;/p&gt;

&lt;h4&gt;
  
  
  🤝 Befriending the Borrow Checker
&lt;/h4&gt;

&lt;p&gt;Ah, the infamous Borrow Checker. Like many, my initial experience with it felt like wrestling with a stubborn compiler. But this project forced me to move beyond just making the errors disappear. I started to think in terms of ownership, lifetimes, and borrowing. Instead of passing values around and hoping for the best, I began to design how data flowed through the application. It was a mindset shift from "fighting the compiler" to "working with the language," and it was gratifying.&lt;/p&gt;

&lt;h4&gt;
  
  
  🌐 Web Services with Axum
&lt;/h4&gt;

&lt;p&gt;On top of Tokio, I used &lt;strong&gt;Axum&lt;/strong&gt;, a modern and ergonomic web framework. Axum’s extractor pattern is brilliant. It makes pulling data from requests—whether it's a path parameter, a query, or a header—spotless and boilerplate-free. Building the routing logic, which dynamically maps file paths like &lt;code&gt;get{id}.json&lt;/code&gt; to routes like &lt;code&gt;/api/users/:id&lt;/code&gt;, was a fascinating challenge that Axum made manageable.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Result: &lt;code&gt;rs-mock-server&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;After navigating these challenges, &lt;code&gt;rs-mock-server&lt;/code&gt; was born. It's a lightweight, fast, and incredibly simple tool that does exactly what I set out to create.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A folder at &lt;code&gt;./mocks/api/users&lt;/code&gt; becomes the &lt;code&gt;/api/users&lt;/code&gt; route.&lt;/li&gt;
&lt;li&gt;  A file named &lt;code&gt;get.json&lt;/code&gt; inside it responds to &lt;code&gt;GET /api/users&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  A file named &lt;code&gt;get{id}.json&lt;/code&gt; handles dynamic requests like &lt;code&gt;GET /api/users/123&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It was the perfect project—small enough to finish, but complex enough to teach me valuable lessons.&lt;/p&gt;

&lt;h3&gt;
  
  
  From Private Tool to Public Crate
&lt;/h3&gt;

&lt;p&gt;Beyond the code itself, a huge desire of mine was to learn the full lifecycle of a public project. It's one thing to build a tool for yourself, but it's another to package and publish it so everyone can use it easily.&lt;/p&gt;

&lt;p&gt;Figuring out how to document the project, configure the Cargo.toml metadata, and publish it to crates.io was the final, crucial step. Seeing my project available to anyone with a simple cargo install command was an incredibly rewarding conclusion to this learning journey.&lt;/p&gt;

&lt;h3&gt;
  
  
  Try It Out!
&lt;/h3&gt;

&lt;p&gt;This journey was a powerful reminder that the best way to learn is by building. I not only solved a practical problem but also came away with a much deeper understanding of the Rust ecosystem.&lt;/p&gt;

&lt;p&gt;If you have a similar need or are just curious, I’d love for you to check it out.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Install the crate from crates.io:&lt;/strong&gt; &lt;a href="https://crates.io/crates/rs-mock-server" rel="noopener noreferrer"&gt;&lt;strong&gt;https://crates.io/crates/rs-mock-server&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explore the code on GitHub:&lt;/strong&gt; &lt;a href="https://github.com/lvendrame/rs-mock-server" rel="noopener noreferrer"&gt;&lt;strong&gt;https://github.com/lvendrame/rs-mock-server&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Of course, I'm not so good with words, so I used some LLM to improve this text, but it kept the soul.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>webdev</category>
      <category>axum</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to compare the intersection of two intervals</title>
      <dc:creator>Luís Fernando Vendrame</dc:creator>
      <pubDate>Wed, 28 Feb 2024 12:50:16 +0000</pubDate>
      <link>https://dev.to/lvendrame/how-to-compare-the-intersection-of-two-intervals-1e43</link>
      <guid>https://dev.to/lvendrame/how-to-compare-the-intersection-of-two-intervals-1e43</guid>
      <description>&lt;p&gt;One of the problems I have often faced is comparing two intervals, whether they are dates or points in space, which, by the way, is what we refer to as a collision in games.&lt;/p&gt;

&lt;p&gt;When we stop to analyze the problem at first, it may seem complex, as the natural approach would be to perform 8 comparisons to ensure that a collision occurs.&lt;/p&gt;

&lt;p&gt;Let's look at the example below for a better understanding: Suppose meeting A starts at 13:30 and ends at 15:30. Another meeting, B, is being scheduled from 15:00 to 16:00. How do we computationally verify that they do not intersect with each other? In the illustration below, we have all possible intersections that we should check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;A1 &lt;span class="o"&gt;=&lt;/span&gt; Start of A, A2 &lt;span class="o"&gt;=&lt;/span&gt; End of A, B1 &lt;span class="o"&gt;=&lt;/span&gt; Start of B, B2 &lt;span class="o"&gt;=&lt;/span&gt; End of B
        A1            A2
        |             |
    B1-----B2         |                B1 &amp;lt;&lt;span class="o"&gt;=&lt;/span&gt; A1 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; B2 &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; A1
        |             |
    B1-------------------B2            B1 &amp;lt;&lt;span class="o"&gt;=&lt;/span&gt; A1 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; B2 &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; A2
        |             |
        |         B1-----B2            B1 &amp;lt;&lt;span class="o"&gt;=&lt;/span&gt; A2 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; B2 &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; A2
        |             |
        |  B1-----B2  |                B1 &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; A1 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; B2 &amp;lt;&lt;span class="o"&gt;=&lt;/span&gt; A2
        |             |
        |             |

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Given the conditions above, our algorithm would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;verifyIntersection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;a1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;a1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;a1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;a1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doesIntersectionExist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;verifyIntersection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// true        &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, instead of analyzing cases where there is an intersection, we can analyze cases where there is no intersection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;A1 &lt;span class="o"&gt;=&lt;/span&gt; Start of A, A2 &lt;span class="o"&gt;=&lt;/span&gt; End of A, B1 &lt;span class="o"&gt;=&lt;/span&gt; Start of B, B2 &lt;span class="o"&gt;=&lt;/span&gt; End of B
        A1            A2
        |             |
B1---B2 |             |                B2 &amp;lt; A1
        |             |
        |             | B1---B2        B1 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; A2
        |             |

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this new analysis, we can rewrite the algorithm as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;verifyIntersection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;a1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doesIntersectionExist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;verifyIntersection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// true        &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that with this new perspective on the problem, we've reduced from 8 comparisons to just 2. Also, note that now the return of false for comparisons will occur earlier, unlike the previous algorithm that returned true. Another important point is that each period should be ordered from the lowest to the highest value at the time of comparison.&lt;/p&gt;

&lt;p&gt;Although in most applications, this case just simplifies the problem, as this comparison does not occur all the time, in the case of games, it is a significant performance gain, as these comparisons occur in large numbers at each iteration of the &lt;code&gt;main loop&lt;/code&gt;, comparing the intervals of objects on the X, Y axes, and in the case of 3D, the Z axis.&lt;/p&gt;

&lt;p&gt;Therefore, when faced with such problems, try drawing a diagram with possibilities for both truth and falsehood and see what is most worthwhile to be used in algorithm construction.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>algorithms</category>
      <category>gamedev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
