{"id":33394,"date":"2024-11-01T09:16:07","date_gmt":"2024-11-01T09:16:07","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33394"},"modified":"2024-11-01T11:38:53","modified_gmt":"2024-11-01T11:38:53","slug":"java-coding-test-course-determining-if-line-segments-intersect","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33394\/","title":{"rendered":"Java Coding Test Course, Determining if Line Segments Intersect"},"content":{"rendered":"<p><body><\/p>\n<article>\n<section>\n<h2>Problem Description<\/h2>\n<p>\n            Given two line segments, implement an algorithm to determine whether these two segments intersect.<br \/>\n            Segment A is given by points A1(x1, y1) and A2(x2, y2), while segment B is given by points B1(x3, y3) and B2(x4, y4).<br \/>\n            The intersection status is determined considering cases where the segments intersect, the endpoints of the segments are located inside the other segment, or the segments lie on a straight line.\n        <\/p>\n<\/section>\n<section>\n<h2>Example Input<\/h2>\n<pre>\n            A1: (1, 1)\n            A2: (4, 4)\n            B1: (1, 4)\n            B2: (4, 1)\n        <\/pre>\n<h2>Example Output<\/h2>\n<pre>Intersect<\/pre>\n<\/section>\n<section>\n<h2>Problem Solving Process<\/h2>\n<p>\n            To solve this problem, we will utilize several geometric concepts and mathematical calculations.<br \/>\n            To determine whether the segments intersect, we first identify the direction of both segments and then use that to decide the intersection status.\n        <\/p>\n<h3>Step 1: Direction Calculation<\/h3>\n<p>\n            Using the endpoints of the given two segments, calculate the direction in which each segment lies.<br \/>\n            When we have two segments A(A1, A2) and B(B1, B2), we can calculate the direction as follows:\n        <\/p>\n<pre>\n            def direction(a1, a2, b1, b2):\n                # Function to determine direction\n                diff = (a2[0] - a1[0]) * (b1[1] - a1[1]) - (a2[1] - a1[1]) * (b1[0] - a1[0])\n                if diff == 0:\n                    return 0  # Same direction\n                return 1 if diff > 0 else -1  # Left (+) or Right (-)\n        <\/pre>\n<h3>Step 2: Intersection Conditions<\/h3>\n<p>\n            Whether the two segments intersect can be determined by the following conditions:\n        <\/p>\n<ol>\n<li>The direction of segment A does not match at points B1 and B2, and the direction of segment B does not match at points A1 and A2, then the two segments intersect.<\/li>\n<li>If the points of segment A exist on the extension of segment B, meaning one of the endpoints of the line is included inside the other line, it is also considered an intersection.<\/li>\n<li>If all points lie on the same straight line and the endpoints overlap, that is considered an intersection.<\/li>\n<\/ol>\n<h3>Step 3: Implementation<\/h3>\n<p>\n            Now, let&#8217;s implement this condition in Java.\n        <\/p>\n<pre>\n            public class LineIntersection {\n                static class Point {\n                    int x, y;\n                    Point(int x, int y) { this.x = x; this.y = y; }\n                }\n\n                static int direction(Point a1, Point a2, Point b) {\n                    int diff = (a2.x - a1.x) * (b.y - a1.y) - (a2.y - a1.y) * (b.x - a1.x);\n                    return (diff == 0) ? 0 : (diff > 0) ? 1 : -1; \n                }\n\n                static boolean isIntersect(Point a1, Point a2, Point b1, Point b2) {\n                    int d1 = direction(a1, a2, b1);\n                    int d2 = direction(a1, a2, b2);\n                    int d3 = direction(b1, b2, a1);\n                    int d4 = direction(b1, b2, a2);\n\n                    \/\/ Cross case\n                    if(d1 != d2 && d3 != d4) return true;\n\n                    \/\/ Collinear case\n                    if(d1 == 0 && onSegment(a1, a2, b1)) return true;\n                    if(d2 == 0 && onSegment(a1, a2, b2)) return true;\n                    if(d3 == 0 && onSegment(b1, b2, a1)) return true;\n                    if(d4 == 0 && onSegment(b1, b2, a2)) return true;\n\n                    return false; \n                }\n\n                static boolean onSegment(Point a, Point b, Point p) {\n                    return (p.x <= Math.max(a.x, b.x) &#038;&#038; p.x >= Math.min(a.x, b.x) &&\n                            p.y <= Math.max(a.y, b.y) &#038;&#038; p.y >= Math.min(a.y, b.y));\n                }\n\n                public static void main(String[] args) {\n                    Point A1 = new Point(1, 1);\n                    Point A2 = new Point(4, 4);\n                    Point B1 = new Point(1, 4);\n                    Point B2 = new Point(4, 1);\n\n                    if (isIntersect(A1, A2, B1, B2)) {\n                        System.out.println(\"Intersect\");\n                    } else {\n                        System.out.println(\"Do not intersect\");\n                    }\n                }\n            }\n        <\/pre>\n<p>Running the code above will allow you to determine whether the two segments intersect.<\/p>\n<h3>Step 4: Testing<\/h3>\n<p>\n            Try various test cases to check whether the code functions correctly.\n        <\/p>\n<pre>\n            \/\/ Test Case 1\n            A1: (1, 1)\n            A2: (4, 4)\n            B1: (1, 4)\n            B2: (4, 1)  \/\/ Output: Intersect\n\n            \/\/ Test Case 2\n            A1: (1, 1)\n            A2: (1, 3)\n            B1: (1, 2)\n            B2: (1, 4)  \/\/ Output: Intersect\n\n            \/\/ Test Case 3\n            A1: (1, 1)\n            A2: (2, 2)\n            B1: (3, 3)\n            B2: (4, 4)  \/\/ Output: Do not intersect\n        <\/pre>\n<p>This way, you can verify whether the algorithm works correctly through various cases.<\/p>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>\n            Through this course, we learned how to implement an algorithm for determining the intersection of line segments.<br \/>\n            It was a process of understanding and applying intersection conditions through geometric thinking and code implementation.<br \/>\n            This knowledge can be used in various applications, and it will greatly assist in improving understanding of algorithms and data structures.<br \/>\n            I hope this course helps all of you to solve various problems and further solidify your fundamentals.\n        <\/p>\n<\/section>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description Given two line segments, implement an algorithm to determine whether these two segments intersect. Segment A is given by points A1(x1, y1) and A2(x2, y2), while segment B is given by points B1(x3, y3) and B2(x4, y4). The intersection status is determined considering cases where the segments intersect, the endpoints of the segments &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33394\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, Determining if Line Segments Intersect&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[139],"tags":[],"class_list":["post-33394","post","type-post","status-publish","format-standard","hentry","category-java-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Coding Test Course, Determining if Line Segments Intersect - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/33394\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Coding Test Course, Determining if Line Segments Intersect - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description Given two line segments, implement an algorithm to determine whether these two segments intersect. Segment A is given by points A1(x1, y1) and A2(x2, y2), while segment B is given by points B1(x3, y3) and B2(x4, y4). The intersection status is determined considering cases where the segments intersect, the endpoints of the segments &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, Determining if Line Segments Intersect&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33394\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:16:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:38:53+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33394\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33394\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, Determining if Line Segments Intersect\",\"datePublished\":\"2024-11-01T09:16:07+00:00\",\"dateModified\":\"2024-11-01T11:38:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33394\/\"},\"wordCount\":383,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33394\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33394\/\",\"name\":\"Java Coding Test Course, Determining if Line Segments Intersect - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:16:07+00:00\",\"dateModified\":\"2024-11-01T11:38:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33394\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33394\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33394\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, Determining if Line Segments Intersect\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Coding Test Course, Determining if Line Segments Intersect - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/33394\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, Determining if Line Segments Intersect - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description Given two line segments, implement an algorithm to determine whether these two segments intersect. Segment A is given by points A1(x1, y1) and A2(x2, y2), while segment B is given by points B1(x3, y3) and B2(x4, y4). The intersection status is determined considering cases where the segments intersect, the endpoints of the segments &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, Determining if Line Segments Intersect\"","og_url":"https:\/\/atmokpo.com\/w\/33394\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:16:07+00:00","article_modified_time":"2024-11-01T11:38:53+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33394\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33394\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, Determining if Line Segments Intersect","datePublished":"2024-11-01T09:16:07+00:00","dateModified":"2024-11-01T11:38:53+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33394\/"},"wordCount":383,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33394\/","url":"https:\/\/atmokpo.com\/w\/33394\/","name":"Java Coding Test Course, Determining if Line Segments Intersect - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:16:07+00:00","dateModified":"2024-11-01T11:38:53+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33394\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33394\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33394\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, Determining if Line Segments Intersect"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=33394"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33394\/revisions"}],"predecessor-version":[{"id":33395,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33394\/revisions\/33395"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}